]> git.sesse.net Git - ffmpeg/blob - libavformat/apetag.c
apetag: make sure avio_get_str() doesn't read more than it should.
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "libavutil/dict.h"
25 #include "avformat.h"
26 #include "apetag.h"
27
28 #define APE_TAG_VERSION               2000
29 #define APE_TAG_FOOTER_BYTES          32
30 #define APE_TAG_FLAG_CONTAINS_HEADER  (1 << 31)
31 #define APE_TAG_FLAG_IS_HEADER        (1 << 29)
32 #define APE_TAG_FLAG_IS_BINARY        (1 << 1)
33
34 static int ape_tag_read_field(AVFormatContext *s)
35 {
36     AVIOContext *pb = s->pb;
37     uint8_t key[1024], *value;
38     uint32_t size, flags;
39     int i, c;
40
41     size = avio_rl32(pb);  /* field size */
42     flags = avio_rl32(pb); /* field flags */
43     for (i = 0; i < sizeof(key) - 1; i++) {
44         c = avio_r8(pb);
45         if (c < 0x20 || c > 0x7E)
46             break;
47         else
48             key[i] = c;
49     }
50     key[i] = 0;
51     if (c != 0) {
52         av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
53         return -1;
54     }
55     if (size >= UINT_MAX)
56         return -1;
57     if (flags & APE_TAG_FLAG_IS_BINARY) {
58         uint8_t filename[1024];
59         AVStream *st = avformat_new_stream(s, NULL);
60         if (!st)
61             return AVERROR(ENOMEM);
62         avio_get_str(pb, size, filename, sizeof(filename));
63         st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
64         if (!st->codec->extradata)
65             return AVERROR(ENOMEM);
66         if (avio_read(pb, st->codec->extradata, size) != size) {
67             av_freep(&st->codec->extradata);
68             return AVERROR(EIO);
69         }
70         st->codec->extradata_size = size;
71         av_dict_set(&st->metadata, key, filename, 0);
72         st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT;
73     } else {
74         value = av_malloc(size+1);
75         if (!value)
76             return AVERROR(ENOMEM);
77         c = avio_read(pb, value, size);
78         if (c < 0) {
79             av_free(value);
80             return c;
81         }
82         value[c] = 0;
83         av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
84     }
85     return 0;
86 }
87
88 void ff_ape_parse_tag(AVFormatContext *s)
89 {
90     AVIOContext *pb = s->pb;
91     int file_size = avio_size(pb);
92     uint32_t val, fields, tag_bytes;
93     uint8_t buf[8];
94     int i;
95
96     if (file_size < APE_TAG_FOOTER_BYTES)
97         return;
98
99     avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
100
101     avio_read(pb, buf, 8);     /* APETAGEX */
102     if (strncmp(buf, "APETAGEX", 8)) {
103         return;
104     }
105
106     val = avio_rl32(pb);       /* APE tag version */
107     if (val > APE_TAG_VERSION) {
108         av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
109         return;
110     }
111
112     tag_bytes = avio_rl32(pb); /* tag size */
113     if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
114         av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
115         return;
116     }
117
118     fields = avio_rl32(pb);    /* number of fields */
119     if (fields > 65536) {
120         av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
121         return;
122     }
123
124     val = avio_rl32(pb);       /* flags */
125     if (val & APE_TAG_FLAG_IS_HEADER) {
126         av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
127         return;
128     }
129
130     avio_seek(pb, file_size - tag_bytes, SEEK_SET);
131
132     for (i=0; i<fields; i++)
133         if (ape_tag_read_field(s) < 0) break;
134 }