Coverage Report

Created: 2026-01-25 07:18

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