FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/apetag.c
Date: 2022-12-05 03:11:11
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 "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
38 33 static int ape_tag_read_field(AVFormatContext *s)
39 {
40 33 AVIOContext *pb = s->pb;
41 uint8_t key[1024], *value;
42 int64_t size, flags;
43 int i, c;
44
45 33 size = avio_rl32(pb); /* field size */
46 33 flags = avio_rl32(pb); /* field flags */
47
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
232 for (i = 0; i < sizeof(key) - 1; i++) {
48 232 c = avio_r8(pb);
49
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)
50 break;
51 else
52 199 key[i] = c;
53 }
54 33 key[i] = 0;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (c != 0) {
56 av_log(s, AV_LOG_WARNING, "Invalid APE tag key '%s'.\n", key);
57 return -1;
58 }
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (flags & APE_TAG_FLAG_IS_BINARY) {
64 uint8_t filename[1024];
65 enum AVCodecID id;
66 int ret;
67 2 AVStream *st = avformat_new_stream(s, NULL);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
69 return AVERROR(ENOMEM);
70
71 2 ret = avio_get_str(pb, size, filename, sizeof(filename));
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
73 return ret;
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size <= ret) {
75 av_log(s, AV_LOG_WARNING, "Skipping binary tag '%s'.\n", key);
76 return 0;
77 }
78 2 size -= ret;
79
80 2 av_dict_set(&st->metadata, key, filename, 0);
81
82
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if ((id = ff_guess_image2_codec(filename)) != AV_CODEC_ID_NONE) {
83 2 int ret = ff_add_attached_pic(s, st, s->pb, NULL, size);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
85 av_log(s, AV_LOG_ERROR, "Error reading cover art.\n");
86 return ret;
87 }
88 2 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 31 value = av_malloc(size+1);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!value)
97 return AVERROR(ENOMEM);
98 31 c = avio_read(pb, value, size);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (c < 0) {
100 av_free(value);
101 return c;
102 }
103 31 value[c] = 0;
104 31 av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
105 }
106 33 return 0;
107 }
108
109 87 int64_t ff_ape_parse_tag(AVFormatContext *s)
110 {
111 87 AVIOContext *pb = s->pb;
112 87 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (file_size < APE_TAG_FOOTER_BYTES)
119 return 0;
120
121 87 avio_seek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
122
123 87 avio_read(pb, buf, 8); /* APETAGEX */
124
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 9 times.
87 if (strncmp(buf, APE_TAG_PREAMBLE, 8)) {
125 78 return 0;
126 }
127
128 9 val = avio_rl32(pb); /* APE tag version */
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 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 9 tag_bytes = avio_rl32(pb); /* tag size */
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 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 9 fields = avio_rl32(pb); /* number of fields */
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (fields > 65536) {
147 av_log(s, AV_LOG_ERROR, "Too many tag fields (%"PRIu32")\n", fields);
148 return 0;
149 }
150
151 9 val = avio_rl32(pb); /* flags */
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 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 9 avio_seek(pb, file_size - tag_bytes, SEEK_SET);
158
159
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
160 9 tag_bytes += APE_TAG_HEADER_BYTES;
161
162 9 tag_start = file_size - tag_bytes;
163
164
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9 times.
42 for (i=0; i<fields; i++)
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if (ape_tag_read_field(s) < 0) break;
166
167 9 return tag_start;
168 }
169
170 2 static int string_is_ascii(const uint8_t *str)
171 {
172
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++;
173 2 return !*str;
174 }
175
176 4 int ff_ape_write_tag(AVFormatContext *s)
177 {
178 4 const AVDictionaryEntry *e = NULL;
179 4 int size, ret, count = 0;
180 AVIOContext *dyn_bc;
181 uint8_t *dyn_buf;
182
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
184 return ret;
185
186 4 ff_standardize_creation_time(s);
187
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 while ((e = av_dict_iterate(s->metadata, e))) {
188 int val_len;
189
190
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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 2 val_len = strlen(e->value);
196 2 avio_wl32(dyn_bc, val_len); // value length
197 2 avio_wl32(dyn_bc, 0); // item flags
198 2 avio_put_str(dyn_bc, e->key); // key
199 2 avio_write(dyn_bc, e->value, val_len); // value
200 2 count++;
201 }
202
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!count)
203 2 goto end;
204
205 2 size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (size <= 0)
207 goto end;
208 2 size += APE_TAG_FOOTER_BYTES;
209
210 // header
211 2 avio_write(s->pb, "APETAGEX", 8); // id
212 2 avio_wl32(s->pb, APE_TAG_VERSION); // version
213 2 avio_wl32(s->pb, size);
214 2 avio_wl32(s->pb, count);
215
216 // flags
217 2 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_IS_HEADER);
218 2 ffio_fill(s->pb, 0, 8); // reserved
219
220 2 avio_write(s->pb, dyn_buf, size - APE_TAG_FOOTER_BYTES);
221
222 // footer
223 2 avio_write(s->pb, "APETAGEX", 8); // id
224 2 avio_wl32(s->pb, APE_TAG_VERSION); // version
225 2 avio_wl32(s->pb, size); // size
226 2 avio_wl32(s->pb, count); // tag count
227
228 // flags
229 2 avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER);
230 2 ffio_fill(s->pb, 0, 8); // reserved
231
232 4 end:
233 4 ffio_free_dyn_buf(&dyn_bc);
234
235 4 return ret;
236 }
237