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