Coverage Report

Created: 2026-04-29 07:00

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