1 /*
2  * Copyright (c) 2003 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * ID3v2 header parser
24  *
25  * Specifications available at:
26  * http://id3.org/Developer_Information
27  */
28 
29 #include "config.h"
30 
31 #if CONFIG_ZLIB
32 #include <zlib.h>
33 #endif
34 
35 #include "libavutil/avstring.h"
36 #include "libavutil/bprint.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavcodec/png.h"
40 #include "avio_internal.h"
41 #include "demux.h"
42 #include "id3v1.h"
43 #include "id3v2.h"
44 
45 const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
46     { "TALB", "album"        },
47     { "TCOM", "composer"     },
48     { "TCON", "genre"        },
49     { "TCOP", "copyright"    },
50     { "TENC", "encoded_by"   },
51     { "TIT2", "title"        },
52     { "TLAN", "language"     },
53     { "TPE1", "artist"       },
54     { "TPE2", "album_artist" },
55     { "TPE3", "performer"    },
56     { "TPOS", "disc"         },
57     { "TPUB", "publisher"    },
58     { "TRCK", "track"        },
59     { "TSSE", "encoder"      },
60     { "USLT", "lyrics"       },
61     { 0 }
62 };
63 
64 const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
65     { "TCMP", "compilation"   },
66     { "TDRC", "date"          },
67     { "TDRL", "date"          },
68     { "TDEN", "creation_time" },
69     { "TSOA", "album-sort"    },
70     { "TSOP", "artist-sort"   },
71     { "TSOT", "title-sort"    },
72     { "TIT1", "grouping"      },
73     { 0 }
74 };
75 
76 static const AVMetadataConv id3v2_2_metadata_conv[] = {
77     { "TAL", "album"        },
78     { "TCO", "genre"        },
79     { "TCP", "compilation"  },
80     { "TT2", "title"        },
81     { "TEN", "encoded_by"   },
82     { "TP1", "artist"       },
83     { "TP2", "album_artist" },
84     { "TP3", "performer"    },
85     { "TRK", "track"        },
86     { 0 }
87 };
88 
89 const char ff_id3v2_tags[][4] = {
90     "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
91     "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
92     "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
93     "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
94     { 0 },
95 };
96 
97 const char ff_id3v2_4_tags[][4] = {
98     "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
99     "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
100     { 0 },
101 };
102 
103 const char ff_id3v2_3_tags[][4] = {
104     "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
105     { 0 },
106 };
107 
108 const char * const ff_id3v2_picture_types[21] = {
109     "Other",
110     "32x32 pixels 'file icon'",
111     "Other file icon",
112     "Cover (front)",
113     "Cover (back)",
114     "Leaflet page",
115     "Media (e.g. label side of CD)",
116     "Lead artist/lead performer/soloist",
117     "Artist/performer",
118     "Conductor",
119     "Band/Orchestra",
120     "Composer",
121     "Lyricist/text writer",
122     "Recording Location",
123     "During recording",
124     "During performance",
125     "Movie/video screen capture",
126     "A bright coloured fish",
127     "Illustration",
128     "Band/artist logotype",
129     "Publisher/Studio logotype",
130 };
131 
132 const CodecMime ff_id3v2_mime_tags[] = {
133     { "image/gif",  AV_CODEC_ID_GIF   },
134     { "image/jpeg", AV_CODEC_ID_MJPEG },
135     { "image/jpg",  AV_CODEC_ID_MJPEG },
136     { "image/png",  AV_CODEC_ID_PNG   },
137     { "image/tiff", AV_CODEC_ID_TIFF  },
138     { "image/bmp",  AV_CODEC_ID_BMP   },
139     { "JPG",        AV_CODEC_ID_MJPEG }, /* ID3v2.2  */
140     { "PNG",        AV_CODEC_ID_PNG   }, /* ID3v2.2  */
141     { "",           AV_CODEC_ID_NONE  },
142 };
143 
ff_id3v2_match(const uint8_t *buf, const char *magic)144 int ff_id3v2_match(const uint8_t *buf, const char *magic)
145 {
146     return  buf[0]         == magic[0] &&
147             buf[1]         == magic[1] &&
148             buf[2]         == magic[2] &&
149             buf[3]         != 0xff     &&
150             buf[4]         != 0xff     &&
151            (buf[6] & 0x80) == 0        &&
152            (buf[7] & 0x80) == 0        &&
153            (buf[8] & 0x80) == 0        &&
154            (buf[9] & 0x80) == 0;
155 }
156 
ff_id3v2_tag_len(const uint8_t *buf)157 int ff_id3v2_tag_len(const uint8_t *buf)
158 {
159     int len = ((buf[6] & 0x7f) << 21) +
160               ((buf[7] & 0x7f) << 14) +
161               ((buf[8] & 0x7f) << 7) +
162               (buf[9] & 0x7f) +
163               ID3v2_HEADER_SIZE;
164     if (buf[5] & 0x10)
165         len += ID3v2_HEADER_SIZE;
166     return len;
167 }
168 
get_size(AVIOContext *s, int len)169 static unsigned int get_size(AVIOContext *s, int len)
170 {
171     int v = 0;
172     while (len--)
173         v = (v << 7) + (avio_r8(s) & 0x7F);
174     return v;
175 }
176 
size_to_syncsafe(unsigned int size)177 static unsigned int size_to_syncsafe(unsigned int size)
178 {
179     return (((size) & (0x7f <<  0)) >> 0) +
180            (((size) & (0x7f <<  8)) >> 1) +
181            (((size) & (0x7f << 16)) >> 2) +
182            (((size) & (0x7f << 24)) >> 3);
183 }
184 
185 /* No real verification, only check that the tag consists of
186  * a combination of capital alpha-numerical characters */
is_tag(const char *buf, unsigned int len)187 static int is_tag(const char *buf, unsigned int len)
188 {
189     if (!len)
190         return 0;
191 
192     while (len--)
193         if ((buf[len] < 'A' ||
194              buf[len] > 'Z') &&
195             (buf[len] < '0' ||
196              buf[len] > '9'))
197             return 0;
198 
199     return 1;
200 }
201 
202 /**
203  * Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error
204  */
check_tag(AVIOContext *s, int offset, unsigned int len)205 static int check_tag(AVIOContext *s, int offset, unsigned int len)
206 {
207     char tag[4];
208 
209     if (len > 4 ||
210         avio_seek(s, offset, SEEK_SET) < 0 ||
211         avio_read(s, tag, len) < (int)len)
212         return -1;
213     else if (!AV_RB32(tag) || is_tag(tag, len))
214         return 1;
215 
216     return 0;
217 }
218 
219 /**
220  * Free GEOB type extra metadata.
221  */
free_geobtag(void *obj)222 static void free_geobtag(void *obj)
223 {
224     ID3v2ExtraMetaGEOB *geob = obj;
225     av_freep(&geob->mime_type);
226     av_freep(&geob->file_name);
227     av_freep(&geob->description);
228     av_freep(&geob->data);
229 }
230 
231 /**
232  * Decode characters to UTF-8 according to encoding type. The decoded buffer is
233  * always null terminated. Stop reading when either *maxread bytes are read from
234  * pb or U+0000 character is found.
235  *
236  * @param dst Pointer where the address of the buffer with the decoded bytes is
237  * stored. Buffer must be freed by caller.
238  * @param maxread Pointer to maximum number of characters to read from the
239  * AVIOContext. After execution the value is decremented by the number of bytes
240  * actually read.
241  * @returns 0 if no error occurred, dst is uninitialized on error
242  */
decode_str(AVFormatContext *s, AVIOContext *pb, int encoding, uint8_t **dst, int *maxread)243 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
244                       uint8_t **dst, int *maxread)
245 {
246     int ret;
247     uint8_t tmp;
248     uint32_t ch = 1;
249     int left = *maxread;
250     unsigned int (*get)(AVIOContext*) = avio_rb16;
251     AVIOContext *dynbuf;
252 
253     if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
254         av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
255         return ret;
256     }
257 
258     switch (encoding) {
259     case ID3v2_ENCODING_ISO8859:
260         while (left && ch) {
261             ch = avio_r8(pb);
262             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
263             left--;
264         }
265         break;
266 
267     case ID3v2_ENCODING_UTF16BOM:
268         if ((left -= 2) < 0) {
269             av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
270             ffio_free_dyn_buf(&dynbuf);
271             *dst = NULL;
272             return AVERROR_INVALIDDATA;
273         }
274 
275 #ifdef OHOS_NONSTANDARD_BOM
276         char buffer[2];
277         avio_read_partial(pb, buffer, 2);
278 
279         unsigned int value = (buffer[0] << 8) + buffer[1];
280         avio_seek(pb, -2, SEEK_CUR);
281 
282         switch (value) {
283         case 0xfffe:
284             av_log(s, AV_LOG_INFO, "BOM 0xfffe\n");
285             avio_rb16(pb);
286             get = avio_rl16;
287             break;
288         case 0xfeff:
289             av_log(s, AV_LOG_INFO, "BOM 0xfeff\n");
290             avio_rb16(pb);
291             break;
292         default:
293             av_log(s, AV_LOG_ERROR, "Notstandard BOM value\n");
294             left += 2;
295             get = avio_rl16;
296         }
297 #else
298         switch (avio_rb16(pb)) {
299         case 0xfffe:
300             get = avio_rl16;
301         case 0xfeff:
302             break;
303         default:
304             av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
305             ffio_free_dyn_buf(&dynbuf);
306             *dst = NULL;
307             *maxread = left;
308             return AVERROR_INVALIDDATA;
309         }
310 #endif
311         // fall-through
312 
313     case ID3v2_ENCODING_UTF16BE:
314         while ((left > 1) && ch) {
315             GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
316             PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
317         }
318         if (left < 0)
319             left += 2;  /* did not read last char from pb */
320         break;
321 
322     case ID3v2_ENCODING_UTF8:
323         while (left && ch) {
324             ch = avio_r8(pb);
325             avio_w8(dynbuf, ch);
326             left--;
327         }
328         break;
329     default:
330         av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
331     }
332 
333     if (ch)
334         avio_w8(dynbuf, 0);
335 
336     avio_close_dyn_buf(dynbuf, dst);
337     *maxread = left;
338 
339     return 0;
340 }
341 
342 #ifdef OHOS_OPT_COMPAT
343 #include <iconv.h>
iso8859_convert_utf8(char *input, size_t inputlen, char *output, size_t outputlen)344 static int iso8859_convert_utf8(char *input, size_t inputlen, char *output, size_t outputlen)
345 {
346     int resultLen = -1;
347     size_t inbuferlen = inputlen;
348     size_t outbuferlen = outputlen;
349     iconv_t cd = iconv_open("ISO-8859-1", "UTF-8");
350     if (cd != (iconv_t)-1) {
351         size_t ret = iconv(cd, &input, (size_t *)&inbuferlen, &output, (size_t *)&outbuferlen);
352         if (ret != -1) {
353             resultLen = outputlen - outbuferlen;
354         }
355         iconv_close(cd);
356     }
357     return resultLen;
358 }
359 #endif
360 
361 /**
362  * Parse a text tag.
363  */
read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata, const char *key)364 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen,
365                       AVDictionary **metadata, const char *key)
366 {
367     uint8_t *dst;
368     int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
369     unsigned genre;
370 
371     if (taglen < 1)
372         return;
373 
374     encoding = avio_r8(pb);
375     taglen--; /* account for encoding type byte */
376 
377     if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
378         av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
379         return;
380     }
381 
382     if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))                         &&
383         (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
384         genre <= ID3v1_GENRE_MAX) {
385         av_freep(&dst);
386         dst = av_strdup(ff_id3v1_genre_str[genre]);
387     } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
388         /* dst now contains the key, need to get value */
389         key = dst;
390         if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
391             av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
392             av_freep(&key);
393             return;
394         }
395         dict_flags |= AV_DICT_DONT_STRDUP_KEY;
396     } else if (!*dst)
397         av_freep(&dst);
398 
399 #ifdef OHOS_OPT_COMPAT
400     if (dst) {
401         if (encoding == ID3v2_ENCODING_ISO8859) {
402             const int utf8len = 256;
403             char *utf8 = av_malloc(utf8len + 1);
404             utf8[utf8len] = '\0';
405             int resultLen = iso8859_convert_utf8(dst, strlen(dst), utf8, utf8len);
406             if (resultLen >= 0) {
407                 char *utf8Valid = av_malloc(resultLen + 1);
408                 av_strlcpy(utf8Valid, utf8, resultLen + 1);
409                 av_dict_set(metadata, key, utf8Valid, dict_flags);
410                 av_freep(&utf8);
411             } else {
412                 av_dict_set(metadata, key, utf8, dict_flags);
413             }
414             av_freep(&dst);
415         } else {
416             av_dict_set(metadata, key, dst, dict_flags);
417         }
418     }
419 #else
420     if (dst)
421         av_dict_set(metadata, key, dst, dict_flags);
422 #endif
423 }
424 
read_uslt(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata)425 static void read_uslt(AVFormatContext *s, AVIOContext *pb, int taglen,
426                       AVDictionary **metadata)
427 {
428     uint8_t lang[4];
429     uint8_t *descriptor = NULL; // 'Content descriptor'
430     uint8_t *text;
431     char *key;
432     int encoding;
433     int ok = 0;
434 
435     if (taglen < 1)
436         goto error;
437 
438     encoding = avio_r8(pb);
439     taglen--;
440 
441     if (avio_read(pb, lang, 3) < 3)
442         goto error;
443     lang[3] = '\0';
444     taglen -= 3;
445 
446     if (decode_str(s, pb, encoding, &descriptor, &taglen) < 0 || taglen < 0)
447         goto error;
448 
449     if (decode_str(s, pb, encoding, &text, &taglen) < 0 || taglen < 0)
450         goto error;
451 
452     // FFmpeg does not support hierarchical metadata, so concatenate the keys.
453     key = av_asprintf("lyrics-%s%s%s", descriptor[0] ? (char *)descriptor : "",
454                                        descriptor[0] ? "-" : "",
455                                        lang);
456     if (!key) {
457         av_free(text);
458         goto error;
459     }
460 
461     av_dict_set(metadata, key, text,
462                 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
463 
464     ok = 1;
465 error:
466     if (!ok)
467         av_log(s, AV_LOG_ERROR, "Error reading lyrics, skipped\n");
468     av_free(descriptor);
469 }
470 
471 /**
472  * Parse a comment tag.
473  */
read_comment(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata)474 static void read_comment(AVFormatContext *s, AVIOContext *pb, int taglen,
475                       AVDictionary **metadata)
476 {
477     const char *key = "comment";
478     uint8_t *dst;
479     int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
480     av_unused int language;
481 
482     if (taglen < 4)
483         return;
484 
485     encoding = avio_r8(pb);
486     language = avio_rl24(pb);
487     taglen -= 4;
488 
489     if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
490         av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
491         return;
492     }
493 
494     if (dst && !*dst)
495         av_freep(&dst);
496 
497     if (dst) {
498         key = (const char *) dst;
499         dict_flags |= AV_DICT_DONT_STRDUP_KEY;
500     }
501 
502     if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
503         av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
504         if (dict_flags & AV_DICT_DONT_STRDUP_KEY)
505             av_freep((void*)&key);
506         return;
507     }
508 
509     if (dst)
510         av_dict_set(metadata, key, (const char *) dst, dict_flags);
511 }
512 
513 typedef struct ExtraMetaList {
514     ID3v2ExtraMeta *head, *tail;
515 } ExtraMetaList;
516 
list_append(ID3v2ExtraMeta *new_elem, ExtraMetaList *list)517 static void list_append(ID3v2ExtraMeta *new_elem, ExtraMetaList *list)
518 {
519     if (list->tail)
520         list->tail->next = new_elem;
521     else
522         list->head = new_elem;
523     list->tail = new_elem;
524 }
525 
526 /**
527  * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
528  */
read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)529 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
530                          const char *tag, ExtraMetaList *extra_meta, int isv34)
531 {
532     ID3v2ExtraMetaGEOB *geob_data = NULL;
533     ID3v2ExtraMeta *new_extra     = NULL;
534     char encoding;
535     unsigned int len;
536 
537     if (taglen < 1)
538         return;
539 
540     new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
541     if (!new_extra) {
542         av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
543                sizeof(ID3v2ExtraMeta));
544         return;
545     }
546 
547     geob_data = &new_extra->data.geob;
548 
549     /* read encoding type byte */
550     encoding = avio_r8(pb);
551     taglen--;
552 
553     /* read MIME type (always ISO-8859) */
554     if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
555                    &taglen) < 0 ||
556         taglen <= 0)
557         goto fail;
558 
559     /* read file name */
560     if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
561         taglen <= 0)
562         goto fail;
563 
564     /* read content description */
565     if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
566         taglen < 0)
567         goto fail;
568 
569     if (taglen) {
570         /* save encapsulated binary data */
571         geob_data->data = av_malloc(taglen);
572         if (!geob_data->data) {
573             av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
574             goto fail;
575         }
576         if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
577             av_log(s, AV_LOG_WARNING,
578                    "Error reading GEOB frame, data truncated.\n");
579         geob_data->datasize = len;
580     } else {
581         geob_data->data     = NULL;
582         geob_data->datasize = 0;
583     }
584 
585     /* add data to the list */
586     new_extra->tag  = "GEOB";
587     list_append(new_extra, extra_meta);
588 
589     return;
590 
591 fail:
592     av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
593     free_geobtag(geob_data);
594     av_free(new_extra);
595     return;
596 }
597 
is_number(const char *str)598 static int is_number(const char *str)
599 {
600     while (*str >= '0' && *str <= '9')
601         str++;
602     return !*str;
603 }
604 
get_date_tag(AVDictionary *m, const char *tag)605 static AVDictionaryEntry *get_date_tag(AVDictionary *m, const char *tag)
606 {
607     AVDictionaryEntry *t;
608     if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
609         strlen(t->value) == 4 && is_number(t->value))
610         return t;
611     return NULL;
612 }
613 
merge_date(AVDictionary **m)614 static void merge_date(AVDictionary **m)
615 {
616     AVDictionaryEntry *t;
617     char date[17] = { 0 };      // YYYY-MM-DD hh:mm
618 
619     if (!(t = get_date_tag(*m, "TYER")) &&
620         !(t = get_date_tag(*m, "TYE")))
621         return;
622     av_strlcpy(date, t->value, 5);
623     av_dict_set(m, "TYER", NULL, 0);
624     av_dict_set(m, "TYE", NULL, 0);
625 
626     if (!(t = get_date_tag(*m, "TDAT")) &&
627         !(t = get_date_tag(*m, "TDA")))
628         goto finish;
629     snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
630     av_dict_set(m, "TDAT", NULL, 0);
631     av_dict_set(m, "TDA", NULL, 0);
632 
633     if (!(t = get_date_tag(*m, "TIME")) &&
634         !(t = get_date_tag(*m, "TIM")))
635         goto finish;
636     snprintf(date + 10, sizeof(date) - 10,
637              " %.2s:%.2s", t->value, t->value + 2);
638     av_dict_set(m, "TIME", NULL, 0);
639     av_dict_set(m, "TIM", NULL, 0);
640 
641 finish:
642     if (date[0])
643         av_dict_set(m, "date", date, 0);
644 }
645 
free_apic(void *obj)646 static void free_apic(void *obj)
647 {
648     ID3v2ExtraMetaAPIC *apic = obj;
649     av_buffer_unref(&apic->buf);
650     av_freep(&apic->description);
651 }
652 
rstrip_spaces(char *buf)653 static void rstrip_spaces(char *buf)
654 {
655     size_t len = strlen(buf);
656     while (len > 0 && buf[len - 1] == ' ')
657         buf[--len] = 0;
658 }
659 
read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)660 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
661                       const char *tag, ExtraMetaList *extra_meta, int isv34)
662 {
663     int enc, pic_type;
664     char mimetype[64] = {0};
665     const CodecMime *mime     = ff_id3v2_mime_tags;
666     enum AVCodecID id         = AV_CODEC_ID_NONE;
667     ID3v2ExtraMetaAPIC *apic  = NULL;
668     ID3v2ExtraMeta *new_extra = NULL;
669     int64_t end               = avio_tell(pb) + taglen;
670 
671     if (taglen <= 4 || (!isv34 && taglen <= 6))
672         goto fail;
673 
674     new_extra = av_mallocz(sizeof(*new_extra));
675     if (!new_extra)
676         goto fail;
677 
678     apic = &new_extra->data.apic;
679 
680     enc = avio_r8(pb);
681     taglen--;
682 
683     /* mimetype */
684     if (isv34) {
685         int ret = avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
686         if (ret < 0 || ret >= taglen)
687             goto fail;
688         taglen -= ret;
689     } else {
690         if (avio_read(pb, mimetype, 3) < 0)
691             goto fail;
692 
693         mimetype[3] = 0;
694         taglen    -= 3;
695     }
696 
697     while (mime->id != AV_CODEC_ID_NONE) {
698         if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
699             id = mime->id;
700             break;
701         }
702         mime++;
703     }
704     if (id == AV_CODEC_ID_NONE) {
705         av_log(s, AV_LOG_WARNING,
706                "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
707         goto fail;
708     }
709     apic->id = id;
710 
711     /* picture type */
712     pic_type = avio_r8(pb);
713     taglen--;
714     if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
715         av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
716                pic_type);
717         pic_type = 0;
718     }
719     apic->type = ff_id3v2_picture_types[pic_type];
720 
721     /* description and picture data */
722     if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
723         av_log(s, AV_LOG_ERROR,
724                "Error decoding attached picture description.\n");
725         goto fail;
726     }
727 
728     apic->buf = av_buffer_alloc(taglen + AV_INPUT_BUFFER_PADDING_SIZE);
729     if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
730         goto fail;
731     memset(apic->buf->data + taglen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
732 
733     new_extra->tag  = "APIC";
734 
735     // The description must be unique, and some ID3v2 tag writers add spaces
736     // to write several APIC entries with the same description.
737     rstrip_spaces(apic->description);
738     list_append(new_extra, extra_meta);
739 
740     return;
741 
742 fail:
743     if (apic)
744         free_apic(apic);
745     av_freep(&new_extra);
746     avio_seek(pb, end, SEEK_SET);
747 }
748 
free_chapter(void *obj)749 static void free_chapter(void *obj)
750 {
751     ID3v2ExtraMetaCHAP *chap = obj;
752     av_freep(&chap->element_id);
753     av_dict_free(&chap->meta);
754 }
755 
read_chapter(AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ExtraMetaList *extra_meta, int isv34)756 static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len,
757                          const char *ttag, ExtraMetaList *extra_meta, int isv34)
758 {
759     int taglen;
760     char tag[5];
761     ID3v2ExtraMeta *new_extra = NULL;
762     ID3v2ExtraMetaCHAP *chap  = NULL;
763 
764     new_extra = av_mallocz(sizeof(*new_extra));
765     if (!new_extra)
766         return;
767 
768     chap = &new_extra->data.chap;
769 
770     if (decode_str(s, pb, 0, &chap->element_id, &len) < 0)
771         goto fail;
772 
773     if (len < 16)
774         goto fail;
775 
776     chap->start = avio_rb32(pb);
777     chap->end   = avio_rb32(pb);
778     avio_skip(pb, 8);
779 
780     len -= 16;
781     while (len > 10) {
782         if (avio_read(pb, tag, 4) < 4)
783             goto fail;
784         tag[4] = 0;
785         taglen = avio_rb32(pb);
786         avio_skip(pb, 2);
787         len -= 10;
788         if (taglen < 0 || taglen > len)
789             goto fail;
790         if (tag[0] == 'T')
791             read_ttag(s, pb, taglen, &chap->meta, tag);
792         else
793             avio_skip(pb, taglen);
794         len -= taglen;
795     }
796 
797     ff_metadata_conv(&chap->meta, NULL, ff_id3v2_34_metadata_conv);
798     ff_metadata_conv(&chap->meta, NULL, ff_id3v2_4_metadata_conv);
799 
800     new_extra->tag  = "CHAP";
801     list_append(new_extra, extra_meta);
802 
803     return;
804 
805 fail:
806     free_chapter(chap);
807     av_freep(&new_extra);
808 }
809 
free_priv(void *obj)810 static void free_priv(void *obj)
811 {
812     ID3v2ExtraMetaPRIV *priv = obj;
813     av_freep(&priv->owner);
814     av_freep(&priv->data);
815 }
816 
read_priv(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ExtraMetaList *extra_meta, int isv34)817 static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
818                       const char *tag, ExtraMetaList *extra_meta, int isv34)
819 {
820     ID3v2ExtraMeta *meta;
821     ID3v2ExtraMetaPRIV *priv;
822 
823     meta = av_mallocz(sizeof(*meta));
824     if (!meta)
825         return;
826 
827     priv = &meta->data.priv;
828 
829     if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
830         goto fail;
831 
832     priv->data = av_malloc(taglen);
833     if (!priv->data)
834         goto fail;
835 
836     priv->datasize = taglen;
837 
838     if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
839         goto fail;
840 
841     meta->tag   = "PRIV";
842     list_append(meta, extra_meta);
843 
844     return;
845 
846 fail:
847     free_priv(priv);
848     av_freep(&meta);
849 }
850 
851 typedef struct ID3v2EMFunc {
852     const char *tag3;
853     const char *tag4;
854     void (*read)(AVFormatContext *s, AVIOContext *pb, int taglen,
855                  const char *tag, ExtraMetaList *extra_meta,
856                  int isv34);
857     void (*free)(void *obj);
858 } ID3v2EMFunc;
859 
860 static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
861     { "GEO", "GEOB", read_geobtag, free_geobtag },
862     { "PIC", "APIC", read_apic,    free_apic    },
863     { "CHAP","CHAP", read_chapter, free_chapter },
864     { "PRIV","PRIV", read_priv,    free_priv    },
865     { NULL }
866 };
867 
868 /**
869  * Get the corresponding ID3v2EMFunc struct for a tag.
870  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
871  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
872  */
get_extra_meta_func(const char *tag, int isv34)873 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
874 {
875     int i = 0;
876     while (id3v2_extra_meta_funcs[i].tag3) {
877         if (tag && !memcmp(tag,
878                     (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
879                              id3v2_extra_meta_funcs[i].tag3),
880                     (isv34 ? 4 : 3)))
881             return &id3v2_extra_meta_funcs[i];
882         i++;
883     }
884     return NULL;
885 }
886 
id3v2_parse(AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, int len, uint8_t version, uint8_t flags, ExtraMetaList *extra_meta)887 static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
888                         AVFormatContext *s, int len, uint8_t version,
889                         uint8_t flags, ExtraMetaList *extra_meta)
890 {
891     int isv34, unsync;
892     unsigned tlen;
893     char tag[5];
894     int64_t next, end = avio_tell(pb);
895     int taghdrlen;
896     const char *reason = NULL;
897     FFIOContext pb_local;
898     AVIOContext *pbx;
899     unsigned char *buffer = NULL;
900     int buffer_size       = 0;
901     const ID3v2EMFunc *extra_func = NULL;
902     unsigned char *uncompressed_buffer = NULL;
903     av_unused int uncompressed_buffer_size = 0;
904     const char *comm_frame;
905 
906     if (end > INT64_MAX - len - 10)
907         return;
908     end += len;
909 
910     av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
911 
912     switch (version) {
913     case 2:
914         if (flags & 0x40) {
915             reason = "compression";
916             goto error;
917         }
918         isv34     = 0;
919         taghdrlen = 6;
920         comm_frame = "COM";
921         break;
922 
923     case 3:
924     case 4:
925         isv34     = 1;
926         taghdrlen = 10;
927         comm_frame = "COMM";
928         break;
929 
930     default:
931         reason = "version";
932         goto error;
933     }
934 
935     unsync = flags & 0x80;
936 
937     if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
938         int extlen = get_size(pb, 4);
939         if (version == 4)
940             /* In v2.4 the length includes the length field we just read. */
941             extlen -= 4;
942 
943         if (extlen < 0) {
944             reason = "invalid extended header length";
945             goto error;
946         }
947         avio_skip(pb, extlen);
948         len -= extlen + 4;
949         if (len < 0) {
950             reason = "extended header too long.";
951             goto error;
952         }
953     }
954 
955     while (len >= taghdrlen) {
956         unsigned int tflags = 0;
957         int tunsync         = 0;
958         int tcomp           = 0;
959         int tencr           = 0;
960         unsigned long av_unused dlen;
961 
962         if (isv34) {
963             if (avio_read(pb, tag, 4) < 4)
964                 break;
965             tag[4] = 0;
966             if (version == 3) {
967                 tlen = avio_rb32(pb);
968             } else {
969                 /* some encoders incorrectly uses v3 sizes instead of syncsafe ones
970                  * so check the next tag to see which one to use */
971                 tlen = avio_rb32(pb);
972                 if (tlen > 0x7f) {
973                     if (tlen < len) {
974                         int64_t cur = avio_tell(pb);
975 
976                         if (ffio_ensure_seekback(pb, 2 /* tflags */ + tlen + 4 /* next tag */))
977                             break;
978 
979                         if (check_tag(pb, cur + 2 + size_to_syncsafe(tlen), 4) == 1)
980                             tlen = size_to_syncsafe(tlen);
981                         else if (check_tag(pb, cur + 2 + tlen, 4) != 1)
982                             break;
983                         avio_seek(pb, cur, SEEK_SET);
984                     } else
985                         tlen = size_to_syncsafe(tlen);
986                 }
987             }
988             tflags  = avio_rb16(pb);
989             tunsync = tflags & ID3v2_FLAG_UNSYNCH;
990         } else {
991             if (avio_read(pb, tag, 3) < 3)
992                 break;
993             tag[3] = 0;
994             tlen   = avio_rb24(pb);
995         }
996         if (tlen > (1<<28))
997             break;
998         len -= taghdrlen + tlen;
999 
1000         if (len < 0)
1001             break;
1002 
1003         next = avio_tell(pb) + tlen;
1004 
1005         if (!tlen) {
1006             if (tag[0])
1007                 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
1008                        tag);
1009             continue;
1010         }
1011 
1012         if (tflags & ID3v2_FLAG_DATALEN) {
1013             if (tlen < 4)
1014                 break;
1015             dlen = avio_rb32(pb);
1016             tlen -= 4;
1017         } else
1018             dlen = tlen;
1019 
1020         tcomp = tflags & ID3v2_FLAG_COMPRESSION;
1021         tencr = tflags & ID3v2_FLAG_ENCRYPTION;
1022 
1023         /* skip encrypted tags and, if no zlib, compressed tags */
1024         if (tencr || (!CONFIG_ZLIB && tcomp)) {
1025             const char *type;
1026             if (!tcomp)
1027                 type = "encrypted";
1028             else if (!tencr)
1029                 type = "compressed";
1030             else
1031                 type = "encrypted and compressed";
1032 
1033             av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
1034             avio_skip(pb, tlen);
1035         /* check for text tag or supported special meta tag */
1036         } else if (tag[0] == 'T' ||
1037                    !memcmp(tag, "USLT", 4) ||
1038                    !strcmp(tag, comm_frame) ||
1039                    (extra_meta &&
1040                     (extra_func = get_extra_meta_func(tag, isv34)))) {
1041             pbx = pb;
1042 
1043             if (unsync || tunsync || tcomp) {
1044                 av_fast_malloc(&buffer, &buffer_size, tlen);
1045                 if (!buffer) {
1046                     av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
1047                     goto seek;
1048                 }
1049             }
1050             if (unsync || tunsync) {
1051                 uint8_t *b = buffer;
1052                 uint8_t *t = buffer;
1053                 uint8_t *end = t + tlen;
1054 
1055                 if (avio_read(pb, buffer, tlen) != tlen) {
1056                     av_log(s, AV_LOG_ERROR, "Failed to read tag data\n");
1057                     goto seek;
1058                 }
1059 
1060                 while (t != end) {
1061                     *b++ = *t++;
1062                     if (t != end && t[-1] == 0xff && !t[0])
1063                         t++;
1064                 }
1065 
1066                 ffio_init_context(&pb_local, buffer, b - buffer, 0, NULL, NULL, NULL,
1067                                   NULL);
1068                 tlen = b - buffer;
1069                 pbx  = &pb_local.pub; // read from sync buffer
1070             }
1071 
1072 #if CONFIG_ZLIB
1073                 if (tcomp) {
1074                     int err;
1075 
1076                     av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
1077 
1078                     if (tlen <= 0)
1079                         goto seek;
1080                     if (dlen / 32768 > tlen)
1081                         goto seek;
1082 
1083                     av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
1084                     if (!uncompressed_buffer) {
1085                         av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
1086                         goto seek;
1087                     }
1088 
1089                     if (!(unsync || tunsync)) {
1090                         err = avio_read(pb, buffer, tlen);
1091                         if (err < 0) {
1092                             av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
1093                             goto seek;
1094                         }
1095                         tlen = err;
1096                     }
1097 
1098                     err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
1099                     if (err != Z_OK) {
1100                         av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
1101                         goto seek;
1102                     }
1103                     ffio_init_context(&pb_local, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
1104                     tlen = dlen;
1105                     pbx = &pb_local.pub; // read from sync buffer
1106                 }
1107 #endif
1108             if (tag[0] == 'T')
1109                 /* parse text tag */
1110                 read_ttag(s, pbx, tlen, metadata, tag);
1111             else if (!memcmp(tag, "USLT", 4))
1112                 read_uslt(s, pbx, tlen, metadata);
1113             else if (!strcmp(tag, comm_frame))
1114                 read_comment(s, pbx, tlen, metadata);
1115             else
1116                 /* parse special meta tag */
1117                 extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
1118         } else if (!tag[0]) {
1119             if (tag[1])
1120                 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
1121             avio_skip(pb, tlen);
1122             break;
1123         }
1124         /* Skip to end of tag */
1125 seek:
1126         avio_seek(pb, next, SEEK_SET);
1127     }
1128 
1129     /* Footer preset, always 10 bytes, skip over it */
1130     if (version == 4 && flags & 0x10)
1131         end += 10;
1132 
1133 error:
1134     if (reason)
1135         av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
1136                version, reason);
1137     avio_seek(pb, end, SEEK_SET);
1138     av_free(buffer);
1139     av_free(uncompressed_buffer);
1140     return;
1141 }
1142 
id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_metap, int64_t max_search_size)1143 static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
1144                                 AVFormatContext *s, const char *magic,
1145                                 ID3v2ExtraMeta **extra_metap, int64_t max_search_size)
1146 {
1147     int len, ret;
1148     uint8_t buf[ID3v2_HEADER_SIZE];
1149     ExtraMetaList extra_meta = { NULL };
1150     int found_header;
1151     int64_t start, off;
1152 
1153     if (extra_metap)
1154         *extra_metap = NULL;
1155 
1156     if (max_search_size && max_search_size < ID3v2_HEADER_SIZE)
1157         return;
1158 
1159     start = avio_tell(pb);
1160     do {
1161         /* save the current offset in case there's nothing to read/skip */
1162         off = avio_tell(pb);
1163         if (max_search_size && off - start >= max_search_size - ID3v2_HEADER_SIZE) {
1164             avio_seek(pb, off, SEEK_SET);
1165             break;
1166         }
1167 
1168         ret = ffio_ensure_seekback(pb, ID3v2_HEADER_SIZE);
1169         if (ret >= 0)
1170             ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
1171         if (ret != ID3v2_HEADER_SIZE) {
1172             avio_seek(pb, off, SEEK_SET);
1173             break;
1174         }
1175         found_header = ff_id3v2_match(buf, magic);
1176         if (found_header) {
1177             /* parse ID3v2 header */
1178             len = ((buf[6] & 0x7f) << 21) |
1179                   ((buf[7] & 0x7f) << 14) |
1180                   ((buf[8] & 0x7f) << 7) |
1181                    (buf[9] & 0x7f);
1182             id3v2_parse(pb, metadata, s, len, buf[3], buf[5],
1183                         extra_metap ? &extra_meta : NULL);
1184         } else {
1185             avio_seek(pb, off, SEEK_SET);
1186         }
1187     } while (found_header);
1188     ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
1189     ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
1190     ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
1191     merge_date(metadata);
1192     if (extra_metap)
1193         *extra_metap = extra_meta.head;
1194 }
1195 
ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)1196 void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata,
1197                         const char *magic, ID3v2ExtraMeta **extra_meta)
1198 {
1199     id3v2_read_internal(pb, metadata, NULL, magic, extra_meta, 0);
1200 }
1201 
ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)1202 void ff_id3v2_read(AVFormatContext *s, const char *magic,
1203                    ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
1204 {
1205     id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta, max_search_size);
1206 }
1207 
ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)1208 void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
1209 {
1210     ID3v2ExtraMeta *current = *extra_meta, *next;
1211     const ID3v2EMFunc *extra_func;
1212 
1213     while (current) {
1214         if ((extra_func = get_extra_meta_func(current->tag, 1)))
1215             extra_func->free(&current->data);
1216         next = current->next;
1217         av_freep(&current);
1218         current = next;
1219     }
1220 
1221     *extra_meta = NULL;
1222 }
1223 
ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)1224 int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
1225 {
1226     ID3v2ExtraMeta *cur;
1227 
1228     for (cur = extra_meta; cur; cur = cur->next) {
1229         ID3v2ExtraMetaAPIC *apic;
1230         AVStream *st;
1231         int ret;
1232 
1233         if (strcmp(cur->tag, "APIC"))
1234             continue;
1235         apic = &cur->data.apic;
1236 
1237         ret = ff_add_attached_pic(s, NULL, NULL, &apic->buf, 0);
1238         if (ret < 0)
1239             return ret;
1240         st  = s->streams[s->nb_streams - 1];
1241         st->codecpar->codec_id   = apic->id;
1242 
1243         if (AV_RB64(st->attached_pic.data) == PNGSIG)
1244             st->codecpar->codec_id = AV_CODEC_ID_PNG;
1245 
1246         if (apic->description[0])
1247             av_dict_set(&st->metadata, "title", apic->description, 0);
1248 
1249         av_dict_set(&st->metadata, "comment", apic->type, 0);
1250     }
1251 
1252     return 0;
1253 }
1254 
ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)1255 int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
1256 {
1257     AVRational time_base = {1, 1000};
1258     int ret;
1259 
1260     for (unsigned i = 0; cur; cur = cur->next) {
1261         ID3v2ExtraMetaCHAP *chap;
1262         AVChapter *chapter;
1263 
1264         if (strcmp(cur->tag, "CHAP"))
1265             continue;
1266 
1267         chap = &cur->data.chap;
1268         chapter = avpriv_new_chapter(s, i++, time_base, chap->start,
1269                                      chap->end, chap->element_id);
1270         if (!chapter)
1271             continue;
1272 
1273         if ((ret = av_dict_copy(&chapter->metadata, chap->meta, 0)) < 0)
1274             return ret;
1275     }
1276 
1277     return 0;
1278 }
1279 
ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)1280 int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
1281 {
1282     ID3v2ExtraMeta *cur;
1283     int dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL;
1284 
1285     for (cur = extra_meta; cur; cur = cur->next) {
1286         if (!strcmp(cur->tag, "PRIV")) {
1287             ID3v2ExtraMetaPRIV *priv = &cur->data.priv;
1288             AVBPrint bprint;
1289             char *escaped, *key;
1290             int i, ret;
1291 
1292             if ((key = av_asprintf(ID3v2_PRIV_METADATA_PREFIX "%s", priv->owner)) == NULL) {
1293                 return AVERROR(ENOMEM);
1294             }
1295 
1296             av_bprint_init(&bprint, priv->datasize + 1, AV_BPRINT_SIZE_UNLIMITED);
1297 
1298             for (i = 0; i < priv->datasize; i++) {
1299                 if (priv->data[i] < 32 || priv->data[i] > 126 || priv->data[i] == '\\') {
1300                     av_bprintf(&bprint, "\\x%02x", priv->data[i]);
1301                 } else {
1302                     av_bprint_chars(&bprint, priv->data[i], 1);
1303                 }
1304             }
1305 
1306             if ((ret = av_bprint_finalize(&bprint, &escaped)) < 0) {
1307                 av_free(key);
1308                 return ret;
1309             }
1310 
1311             if ((ret = av_dict_set(metadata, key, escaped, dict_flags)) < 0) {
1312                 return ret;
1313             }
1314         }
1315     }
1316 
1317     return 0;
1318 }
1319 
ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)1320 int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
1321 {
1322     return ff_id3v2_parse_priv_dict(&s->metadata, extra_meta);
1323 }
1324