FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/id3v2.c
Date: 2024-10-17 22:24:08
Exec Total Coverage
Lines: 471 662 71.1%
Functions: 32 33 97.0%
Branches: 265 455 58.2%

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