1/*
2 * ID3v2 header parser
3 * Copyright (c) 2003 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "id3v2.h"
23#include "id3v1.h"
24#include "libavutil/avstring.h"
25#include "libavutil/intreadwrite.h"
26#include "libavutil/dict.h"
27#include "avio_internal.h"
28
29const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
30    { "TALB", "album"},
31    { "TCOM", "composer"},
32    { "TCON", "genre"},
33    { "TCOP", "copyright"},
34    { "TENC", "encoded_by"},
35    { "TIT2", "title"},
36    { "TLAN", "language"},
37    { "TPE1", "artist"},
38    { "TPE2", "album_artist"},
39    { "TPE3", "performer"},
40    { "TPOS", "disc"},
41    { "TPUB", "publisher"},
42    { "TRCK", "track"},
43    { "TSSE", "encoder"},
44    { 0 }
45};
46
47const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
48    { "TDRL", "date"},
49    { "TDRC", "date"},
50    { "TDEN", "creation_time"},
51    { "TSOA", "album-sort"},
52    { "TSOP", "artist-sort"},
53    { "TSOT", "title-sort"},
54    { 0 }
55};
56
57static const AVMetadataConv id3v2_2_metadata_conv[] = {
58    { "TAL",  "album"},
59    { "TCO",  "genre"},
60    { "TT2",  "title"},
61    { "TEN",  "encoded_by"},
62    { "TP1",  "artist"},
63    { "TP2",  "album_artist"},
64    { "TP3",  "performer"},
65    { "TRK",  "track"},
66    { 0 }
67};
68
69
70const char ff_id3v2_tags[][4] = {
71   "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
72   "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
73   "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
74   "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
75   { 0 },
76};
77
78const char ff_id3v2_4_tags[][4] = {
79   "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
80   "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
81   { 0 },
82};
83
84const char ff_id3v2_3_tags[][4] = {
85   "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
86   { 0 },
87};
88
89int ff_id3v2_match(const uint8_t *buf, const char * magic)
90{
91    return  buf[0]         == magic[0] &&
92            buf[1]         == magic[1] &&
93            buf[2]         == magic[2] &&
94            buf[3]         != 0xff &&
95            buf[4]         != 0xff &&
96           (buf[6] & 0x80) ==    0 &&
97           (buf[7] & 0x80) ==    0 &&
98           (buf[8] & 0x80) ==    0 &&
99           (buf[9] & 0x80) ==    0;
100}
101
102int ff_id3v2_tag_len(const uint8_t * buf)
103{
104    int len = ((buf[6] & 0x7f) << 21) +
105              ((buf[7] & 0x7f) << 14) +
106              ((buf[8] & 0x7f) << 7) +
107               (buf[9] & 0x7f) +
108              ID3v2_HEADER_SIZE;
109    if (buf[5] & 0x10)
110        len += ID3v2_HEADER_SIZE;
111    return len;
112}
113
114static unsigned int get_size(AVIOContext *s, int len)
115{
116    int v = 0;
117    while (len--)
118        v = (v << 7) + (avio_r8(s) & 0x7F);
119    return v;
120}
121
122/**
123 * Free GEOB type extra metadata.
124 */
125static void free_geobtag(void *obj)
126{
127    ID3v2ExtraMetaGEOB *geob = obj;
128    av_free(geob->mime_type);
129    av_free(geob->file_name);
130    av_free(geob->description);
131    av_free(geob->data);
132    av_free(geob);
133}
134
135/**
136 * Decode characters to UTF-8 according to encoding type. The decoded buffer is
137 * always null terminated. Stop reading when either *maxread bytes are read from
138 * pb or U+0000 character is found.
139 *
140 * @param dst Pointer where the address of the buffer with the decoded bytes is
141 * stored. Buffer must be freed by caller.
142 * @param maxread Pointer to maximum number of characters to read from the
143 * AVIOContext. After execution the value is decremented by the number of bytes
144 * actually read.
145 * @returns 0 if no error occurred, dst is uninitialized on error
146 */
147static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
148                      uint8_t **dst, int *maxread)
149{
150    int ret;
151    uint8_t tmp;
152    uint32_t ch = 1;
153    int left = *maxread;
154    unsigned int (*get)(AVIOContext*) = avio_rb16;
155    AVIOContext *dynbuf;
156
157    if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
158        av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
159        return ret;
160    }
161
162    switch (encoding) {
163
164    case ID3v2_ENCODING_ISO8859:
165        while (left && ch) {
166            ch = avio_r8(pb);
167            PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
168            left--;
169        }
170        break;
171
172    case ID3v2_ENCODING_UTF16BOM:
173        if ((left -= 2) < 0) {
174            av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
175            avio_close_dyn_buf(dynbuf, dst);
176            av_freep(dst);
177            return AVERROR_INVALIDDATA;
178        }
179        switch (avio_rb16(pb)) {
180        case 0xfffe:
181            get = avio_rl16;
182        case 0xfeff:
183            break;
184        default:
185            av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
186            avio_close_dyn_buf(dynbuf, dst);
187            av_freep(dst);
188            *maxread = left;
189            return AVERROR_INVALIDDATA;
190        }
191        // fall-through
192
193    case ID3v2_ENCODING_UTF16BE:
194        while ((left > 1) && ch) {
195            GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
196            PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
197        }
198        if (left < 0)
199            left += 2; /* did not read last char from pb */
200        break;
201
202    case ID3v2_ENCODING_UTF8:
203        while (left && ch) {
204            ch = avio_r8(pb);
205            avio_w8(dynbuf, ch);
206            left--;
207        }
208        break;
209    default:
210        av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
211    }
212
213    if (ch)
214        avio_w8(dynbuf, 0);
215
216    avio_close_dyn_buf(dynbuf, dst);
217    *maxread = left;
218
219    return 0;
220}
221
222/**
223 * Parse a text tag.
224 */
225static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
226{
227    uint8_t *dst;
228    int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
229    unsigned genre;
230
231    if (taglen < 1)
232        return;
233
234    encoding = avio_r8(pb);
235    taglen--; /* account for encoding type byte */
236
237    if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
238        av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
239        return;
240    }
241
242    if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
243        && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
244        && genre <= ID3v1_GENRE_MAX) {
245        av_freep(&dst);
246        dst = ff_id3v1_genre_str[genre];
247    } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
248        /* dst now contains the key, need to get value */
249        key = dst;
250        if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
251            av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
252            av_freep(&key);
253            return;
254        }
255        dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
256    }
257    else if (*dst)
258        dict_flags |= AV_DICT_DONT_STRDUP_VAL;
259
260    if (dst)
261        av_dict_set(&s->metadata, key, dst, dict_flags);
262}
263
264/**
265 * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
266 */
267static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
268{
269    ID3v2ExtraMetaGEOB *geob_data = NULL;
270    ID3v2ExtraMeta *new_extra = NULL;
271    char encoding;
272    unsigned int len;
273
274    if (taglen < 1)
275        return;
276
277    geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
278    if (!geob_data) {
279        av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
280        return;
281    }
282
283    new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
284    if (!new_extra) {
285        av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
286        goto fail;
287    }
288
289    /* read encoding type byte */
290    encoding = avio_r8(pb);
291    taglen--;
292
293    /* read MIME type (always ISO-8859) */
294    if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
295        || taglen <= 0)
296        goto fail;
297
298    /* read file name */
299    if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
300        || taglen <= 0)
301        goto fail;
302
303    /* read content description */
304    if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
305        || taglen < 0)
306        goto fail;
307
308    if (taglen) {
309        /* save encapsulated binary data */
310        geob_data->data = av_malloc(taglen);
311        if (!geob_data->data) {
312            av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
313            goto fail;
314        }
315        if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
316            av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
317        geob_data->datasize = len;
318    } else {
319        geob_data->data = NULL;
320        geob_data->datasize = 0;
321    }
322
323    /* add data to the list */
324    new_extra->tag = "GEOB";
325    new_extra->data = geob_data;
326    new_extra->next = *extra_meta;
327    *extra_meta = new_extra;
328
329    return;
330
331fail:
332    av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
333    free_geobtag(geob_data);
334    av_free(new_extra);
335    return;
336}
337
338static int is_number(const char *str)
339{
340    while (*str >= '0' && *str <= '9') str++;
341    return !*str;
342}
343
344static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
345{
346    AVDictionaryEntry *t;
347    if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
348        strlen(t->value) == 4 && is_number(t->value))
349        return t;
350    return NULL;
351}
352
353static void merge_date(AVDictionary **m)
354{
355    AVDictionaryEntry *t;
356    char date[17] = {0};      // YYYY-MM-DD hh:mm
357
358    if (!(t = get_date_tag(*m, "TYER")) &&
359        !(t = get_date_tag(*m, "TYE")))
360        return;
361    av_strlcpy(date, t->value, 5);
362    av_dict_set(m, "TYER", NULL, 0);
363    av_dict_set(m, "TYE",  NULL, 0);
364
365    if (!(t = get_date_tag(*m, "TDAT")) &&
366        !(t = get_date_tag(*m, "TDA")))
367        goto finish;
368    snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
369    av_dict_set(m, "TDAT", NULL, 0);
370    av_dict_set(m, "TDA",  NULL, 0);
371
372    if (!(t = get_date_tag(*m, "TIME")) &&
373        !(t = get_date_tag(*m, "TIM")))
374        goto finish;
375    snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
376    av_dict_set(m, "TIME", NULL, 0);
377    av_dict_set(m, "TIM",  NULL, 0);
378
379finish:
380    if (date[0])
381        av_dict_set(m, "date", date, 0);
382}
383
384typedef struct ID3v2EMFunc {
385    const char *tag3;
386    const char *tag4;
387    void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
388    void (*free)(void *obj);
389} ID3v2EMFunc;
390
391static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
392    { "GEO", "GEOB", read_geobtag, free_geobtag },
393    { NULL }
394};
395
396/**
397 * Get the corresponding ID3v2EMFunc struct for a tag.
398 * @param isv34 Determines if v2.2 or v2.3/4 strings are used
399 * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
400 */
401static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
402{
403    int i = 0;
404    while (id3v2_extra_meta_funcs[i].tag3) {
405        if (!memcmp(tag,
406                    (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
407                             id3v2_extra_meta_funcs[i].tag3),
408                    (isv34 ? 4 : 3)))
409            return &id3v2_extra_meta_funcs[i];
410        i++;
411    }
412    return NULL;
413}
414
415static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
416{
417    int isv34, tlen, unsync;
418    char tag[5];
419    int64_t next, end = avio_tell(s->pb) + len;
420    int taghdrlen;
421    const char *reason = NULL;
422    AVIOContext pb;
423    AVIOContext *pbx;
424    unsigned char *buffer = NULL;
425    int buffer_size = 0;
426    const ID3v2EMFunc *extra_func;
427
428    switch (version) {
429    case 2:
430        if (flags & 0x40) {
431            reason = "compression";
432            goto error;
433        }
434        isv34 = 0;
435        taghdrlen = 6;
436        break;
437
438    case 3:
439    case 4:
440        isv34 = 1;
441        taghdrlen = 10;
442        break;
443
444    default:
445        reason = "version";
446        goto error;
447    }
448
449    unsync = flags & 0x80;
450
451    if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
452        int extlen = get_size(s->pb, 4);
453        if (version == 4)
454            extlen -= 4;     // in v2.4 the length includes the length field we just read
455
456        if (extlen < 0) {
457            reason = "invalid extended header length";
458            goto error;
459        }
460        avio_skip(s->pb, extlen);
461    }
462
463    while (len >= taghdrlen) {
464        unsigned int tflags = 0;
465        int tunsync = 0;
466
467        if (isv34) {
468            avio_read(s->pb, tag, 4);
469            tag[4] = 0;
470            if(version==3){
471                tlen = avio_rb32(s->pb);
472            }else
473                tlen = get_size(s->pb, 4);
474            tflags = avio_rb16(s->pb);
475            tunsync = tflags & ID3v2_FLAG_UNSYNCH;
476        } else {
477            avio_read(s->pb, tag, 3);
478            tag[3] = 0;
479            tlen = avio_rb24(s->pb);
480        }
481        if (tlen < 0 || tlen > len - taghdrlen) {
482            av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
483            break;
484        }
485        len -= taghdrlen + tlen;
486        next = avio_tell(s->pb) + tlen;
487
488        if (!tlen) {
489            if (tag[0])
490                av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
491            continue;
492        }
493
494        if (tflags & ID3v2_FLAG_DATALEN) {
495            avio_rb32(s->pb);
496            tlen -= 4;
497        }
498
499        if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
500            av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
501            avio_skip(s->pb, tlen);
502        /* check for text tag or supported special meta tag */
503        } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
504            if (unsync || tunsync) {
505                int64_t end = avio_tell(s->pb) + tlen;
506                uint8_t *b;
507                av_fast_malloc(&buffer, &buffer_size, tlen);
508                if (!buffer) {
509                    av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
510                    goto seek;
511                }
512                b = buffer;
513                while (avio_tell(s->pb) < end && !s->pb->eof_reached) {
514                    *b++ = avio_r8(s->pb);
515                    if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1 &&
516                        !s->pb->eof_reached ) {
517                        uint8_t val = avio_r8(s->pb);
518                        *b++ = val ? val : avio_r8(s->pb);
519                    }
520                }
521                ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
522                tlen = b - buffer;
523                pbx = &pb; // read from sync buffer
524            } else {
525                pbx = s->pb; // read straight from input
526            }
527            if (tag[0] == 'T')
528                /* parse text tag */
529                read_ttag(s, pbx, tlen, tag);
530            else
531                /* parse special meta tag */
532                extra_func->read(s, pbx, tlen, tag, extra_meta);
533        }
534        else if (!tag[0]) {
535            if (tag[1])
536                av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
537            avio_skip(s->pb, tlen);
538            break;
539        }
540        /* Skip to end of tag */
541seek:
542        avio_seek(s->pb, next, SEEK_SET);
543    }
544
545    if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
546        end += 10;
547
548  error:
549    if (reason)
550        av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
551    avio_seek(s->pb, end, SEEK_SET);
552    av_free(buffer);
553    return;
554}
555
556void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
557{
558    int len, ret;
559    uint8_t buf[ID3v2_HEADER_SIZE];
560    int     found_header;
561    int64_t off;
562
563    do {
564        /* save the current offset in case there's nothing to read/skip */
565        off = avio_tell(s->pb);
566        ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
567        if (ret != ID3v2_HEADER_SIZE)
568            break;
569            found_header = ff_id3v2_match(buf, magic);
570            if (found_header) {
571            /* parse ID3v2 header */
572            len = ((buf[6] & 0x7f) << 21) |
573                  ((buf[7] & 0x7f) << 14) |
574                  ((buf[8] & 0x7f) << 7) |
575                   (buf[9] & 0x7f);
576            ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
577        } else {
578            avio_seek(s->pb, off, SEEK_SET);
579        }
580    } while (found_header);
581    ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
582    ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
583    ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
584    merge_date(&s->metadata);
585}
586
587void ff_id3v2_read(AVFormatContext *s, const char *magic)
588{
589    ff_id3v2_read_all(s, magic, NULL);
590}
591
592void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
593{
594    ID3v2ExtraMeta *current = *extra_meta, *next;
595    const ID3v2EMFunc *extra_func;
596
597    while (current) {
598        if ((extra_func = get_extra_meta_func(current->tag, 1)))
599            extra_func->free(current->data);
600        next = current->next;
601        av_freep(&current);
602        current = next;
603    }
604}
605