FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/apetag.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 91 122 74.6%
Functions: 4 4 100.0%
Branches: 38 64 59.4%

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 90 avio_read(pb, buf, 8); /* APETAGEX */
125
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 9 times.
90 if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
126 81 return 0;
127 }
128
129 9 val = avio_rl32(pb); /* APE tag version */
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (val > APE_TAG_VERSION) {
131 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
132 return 0;
133 }
134
135 9 tag_bytes = avio_rl32(pb); /* tag size */
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
137 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n");
138 return 0;
139 }
140
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (tag_bytes > file_size - APE_TAG_FOOTER_BYTES) {
142 av_log(s, AV_LOG_ERROR, "Invalid tag size %"PRIu32".\n", tag_bytes);
143 return 0;
144 }
145
146 9 fields = avio_rl32(pb); /* number of fields */
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (fields > 65536) {
148 av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
149 return 0;
150 }
151
152 9 val = avio_rl32(pb); /* flags */
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (val & APE_TAG_FLAG_IS_HEADER) {
154 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n");
155 return 0;
156 }
157
158 9 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
159
160
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
161 9 tag_bytes += APE_TAG_HEADER_BYTES;
162
163 9 tag_start = file_size - tag_bytes;
164
165
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9 times.
42 for (i=0; i<fields; i++)
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (ape_tag_read_field(s) < 0) break;
167
168 9 return tag_start;
169 }
170
171 2 static int string_is_ascii(const uint8_t *str)
172 {
173
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++;
174 2 return !*str;
175 }
176
177 4 int ff_ape_write_tag(AVFormatContext *s)
178 {
179 4 const AVDictionaryEntry *e = NULL;
180 4 int size, ret, count = 0;
181 AVIOContext *dyn_bc;
182 uint8_t *dyn_buf;
183
184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
185 return ret;
186
187 4 ff_standardize_creation_time(s);
188
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 while ((e = av_dict_iterate(s->metadata, e))) {
189 int val_len;
190
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!string_is_ascii(e->key)) {
192 av_log(s, AV_LOG_WARNING, "Non ASCII keys are not allowed\n");
193 continue;
194 }
195
196 2 val_len = strlen(e->value);
197 2 avio_wl32(dyn_bc, val_len); // value length
198 2 avio_wl32(dyn_bc, 0); // item flags
199 2 avio_put_str(dyn_bc, e->key); // key
200 2 avio_write(dyn_bc, e->value, val_len); // value
201 2 count++;
202 }
203
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!count)
204 2 goto end;
205
206 2 size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size <= 0)
208 goto end;
209 2 size += APE_TAG_FOOTER_BYTES;
210
211 // header
212 2 avio_write(s->pb, "APETAGEX", 8); // id
213 2 avio_wl32(s->pb, APE_TAG_VERSION); // version
214 2 avio_wl32(s->pb, size);
215 2 avio_wl32(s->pb, count);
216
217 // flags
218 2 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_IS_HEADER);
219 2 ffio_fill(s->pb, 0, 8); // reserved
220
221 2 avio_write(s->pb, dyn_buf, size - APE_TAG_FOOTER_BYTES);
222
223 // footer
224 2 avio_write(s->pb, "APETAGEX", 8); // id
225 2 avio_wl32(s->pb, APE_TAG_VERSION); // version
226 2 avio_wl32(s->pb, size); // size
227 2 avio_wl32(s->pb, count); // tag count
228
229 // flags
230 2 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER);
231 2 ffio_fill(s->pb, 0, 8); // reserved
232
233 4 end:
234 4 ffio_free_dyn_buf(&dyn_bc);
235
236 4 return ret;
237 }
238