]> git.sesse.net Git - ffmpeg/blob - libavformat/apetag.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / apetag.c
1 /*
2  * APE tag handling
3  * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4  *  based upon libdemac from Dave Chapman.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "apetag.h"
26
27 #define APE_TAG_VERSION               2000
28 #define APE_TAG_FOOTER_BYTES          32
29 #define APE_TAG_FLAG_CONTAINS_HEADER  (1 << 31)
30 #define APE_TAG_FLAG_IS_HEADER        (1 << 29)
31
32 static int ape_tag_read_field(AVFormatContext *s)
33 {
34     AVIOContext *pb = s->pb;
35     uint8_t key[1024], *value;
36     uint32_t size;
37     int i, c;
38
39     size = avio_rl32(pb);  /* field size */
40     avio_skip(pb, 4);      /* field flags */
41     for (i = 0; i < sizeof(key) - 1; i++) {
42         c = avio_r8(pb);
43         if (c < 0x20 || c > 0x7E)
44             break;
45         else
46             key[i] = c;
47     }
48     key[i] = 0;
49     if (c != 0) {
50         av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
51         return -1;
52     }
53     if (size >= UINT_MAX)
54         return -1;
55     value = av_malloc(size+1);
56     if (!value)
57         return AVERROR(ENOMEM);
58     avio_read(pb, value, size);
59     value[size] = 0;
60     av_metadata_set2(&s->metadata, key, value, AV_METADATA_DONT_STRDUP_VAL);
61     return 0;
62 }
63
64 void ff_ape_parse_tag(AVFormatContext *s)
65 {
66     AVIOContext *pb = s->pb;
67     int file_size = avio_size(pb);
68     uint32_t val, fields, tag_bytes;
69     uint8_t buf[8];
70     int i;
71
72     if (file_size < APE_TAG_FOOTER_BYTES)
73         return;
74
75     avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
76
77     avio_read(pb, buf, 8);     /* APETAGEX */
78     if (strncmp(buf, "APETAGEX", 8)) {
79         return;
80     }
81
82     val = avio_rl32(pb);       /* APE tag version */
83     if (val > APE_TAG_VERSION) {
84         av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
85         return;
86     }
87
88     tag_bytes = avio_rl32(pb); /* tag size */
89     if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
90         av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
91         return;
92     }
93
94     fields = avio_rl32(pb);    /* number of fields */
95     if (fields > 65536) {
96         av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
97         return;
98     }
99
100     val = avio_rl32(pb);       /* flags */
101     if (val & APE_TAG_FLAG_IS_HEADER) {
102         av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
103         return;
104     }
105
106     avio_seek(pb, file_size - tag_bytes, SEEK_SET);
107
108     for (i=0; i<fields; i++)
109         if (ape_tag_read_field(s) < 0) break;
110 }