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 <inttypes.h>
24
25 #include "libavutil/dict.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "apetag.h"
29 #include "demux.h"
30 #include "internal.h"
31 #include "mux.h"
32
33 #define APE_TAG_FLAG_CONTAINS_HEADER (1U << 31)
34 #define APE_TAG_FLAG_LACKS_FOOTER (1 << 30)
35 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
36 #define APE_TAG_FLAG_IS_BINARY (1 << 1)
37
ape_tag_read_field(AVFormatContext * s)38 static int ape_tag_read_field(AVFormatContext *s)
39 {
40 AVIOContext *pb = s->pb;
41 uint8_t key[1024], *value;
42 int64_t size, flags;
43 int i, c;
44
45 size = avio_rl32(pb); /* field size */
46 flags = avio_rl32(pb); /* field flags */
47 for (i = 0; i < sizeof(key) - 1; i++) {
48 c = avio_r8(pb);
49 if (c < 0x20 || c > 0x7E)
50 break;
51 else
52 key[i] = c;
53 }
54 key[i] = 0;
55 if (c != 0) {
56 av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
57 return -1;
58 }
59 if (size > INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
60 av_log(s, AV_LOG_ERROR, "APE tag size too large.\n");
61 return AVERROR_INVALIDDATA;
62 }
63 if (flags & APE_TAG_FLAG_IS_BINARY) {
64 uint8_t filename[1024];
65 enum AVCodecID id;
66 int ret;
67 AVStream *st = avformat_new_stream(s, NULL);
68 if (!st)
69 return AVERROR(ENOMEM);
70
71 ret = avio_get_str(pb, size, filename, sizeof(filename));
72 if (ret < 0)
73 return ret;
74 if (size <= ret) {
75 av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
76 return 0;
77 }
78 size -= ret;
79
80 av_dict_set(&st->metadata, key, filename, 0);
81
82 if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
83 int ret = ff_add_attached_pic(s, st, s->pb, NULL, size);
84 if (ret < 0) {
85 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
86 return ret;
87 }
88 st->codecpar->codec_id = id;
89 } else {
90 if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
91 return ret;
92 st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT;
93 }
94 } else {
95 value = av_malloc(size+1);
96 if (!value)
97 return AVERROR(ENOMEM);
98 c = avio_read(pb, value, size);
99 if (c < 0) {
100 av_free(value);
101 return c;
102 }
103 value[c] = 0;
104 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
105 }
106 return 0;
107 }
108
ff_ape_parse_tag(AVFormatContext * s)109 int64_t ff_ape_parse_tag(AVFormatContext *s)
110 {
111 AVIOContext *pb = s->pb;
112 int64_t file_size = avio_size(pb);
113 uint32_t val, fields, tag_bytes;
114 uint8_t buf[8];
115 int64_t tag_start;
116 int i;
117
118 if (file_size < APE_TAG_FOOTER_BYTES)
119 return 0;
120
121 avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
122
123 avio_read(pb, buf, 8); /* APETAGEX */
124 if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
125 return 0;
126 }
127
128 val = avio_rl32(pb); /* APE tag version */
129 if (val > APE_TAG_VERSION) {
130 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
131 return 0;
132 }
133
134 tag_bytes = avio_rl32(pb); /* tag size */
135 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
136 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
137 return 0;
138 }
139
140 if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
141 av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
142 return 0;
143 }
144
145 fields = avio_rl32(pb); /* number of fields */
146 if (fields > 65536) {
147 av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
148 return 0;
149 }
150
151 val = avio_rl32(pb); /* flags */
152 if (val & APE_TAG_FLAG_IS_HEADER) {
153 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
154 return 0;
155 }
156
157 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
158
159 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
160 tag_bytes += APE_TAG_HEADER_BYTES;
161
162 tag_start = file_size - tag_bytes;
163
164 for (i=0; i<fields; i++)
165 if (ape_tag_read_field(s) < 0) break;
166
167 return tag_start;
168 }
169
string_is_ascii(const uint8_t * str)170 static int string_is_ascii(const uint8_t *str)
171 {
172 while (*str && *str >= 0x20 && *str <= 0x7e ) str++;
173 return !*str;
174 }
175
ff_ape_write_tag(AVFormatContext * s)176 int ff_ape_write_tag(AVFormatContext *s)
177 {
178 AVDictionaryEntry *e = NULL;
179 int size, ret, count = 0;
180 AVIOContext *dyn_bc;
181 uint8_t *dyn_buf;
182
183 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
184 return ret;
185
186 ff_standardize_creation_time(s);
187 while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
188 int val_len;
189
190 if (!string_is_ascii(e->key)) {
191 av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
192 continue;
193 }
194
195 val_len = strlen(e->value);
196 avio_wl32(dyn_bc, val_len); // value length
197 avio_wl32(dyn_bc, 0); // item flags
198 avio_put_str(dyn_bc, e->key); // key
199 avio_write(dyn_bc, e->value, val_len); // value
200 count++;
201 }
202 if (!count)
203 goto end;
204
205 size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
206 if (size <= 0)
207 goto end;
208 size += APE_TAG_FOOTER_BYTES;
209
210 // header
211 avio_write(s->pb, "APETAGEX", 8); // id
212 avio_wl32(s->pb, APE_TAG_VERSION); // version
213 avio_wl32(s->pb, size);
214 avio_wl32(s->pb, count);
215
216 // flags
217 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_IS_HEADER);
218 ffio_fill(s->pb, 0, 8); // reserved
219
220 avio_write(s->pb, dyn_buf, size - APE_TAG_FOOTER_BYTES);
221
222 // footer
223 avio_write(s->pb, "APETAGEX", 8); // id
224 avio_wl32(s->pb, APE_TAG_VERSION); // version
225 avio_wl32(s->pb, size); // size
226 avio_wl32(s->pb, count); // tag count
227
228 // flags
229 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER);
230 ffio_fill(s->pb, 0, 8); // reserved
231
232 end:
233 ffio_free_dyn_buf(&dyn_bc);
234
235 return ret;
236 }
237