FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/tiff_common.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 84 115 73.0%
Functions: 13 15 86.7%
Branches: 63 100 63.0%

Line Branch Exec Source
1 /*
2 * TIFF Common Routines
3 * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * TIFF Common Routines
25 * @author Thilo Borgmann <thilo.borgmann _at_ mail.de>
26 */
27
28 #include "libavutil/bprint.h"
29 #include "libavutil/mem.h"
30 #include "tiff_common.h"
31
32
33 1015 int ff_tis_ifd(unsigned tag)
34 {
35 int i;
36
2/2
✓ Branch 0 taken 2997 times.
✓ Branch 1 taken 983 times.
3980 for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
37
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2965 times.
2997 if (ifd_tags[i] == tag) {
38 32 return i + 1;
39 }
40 }
41 983 return 0;
42 }
43
44
45 2099 unsigned ff_tget_short(GetByteContext *gb, int le)
46 {
47
2/2
✓ Branch 0 taken 1783 times.
✓ Branch 1 taken 316 times.
2099 return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
48 }
49
50
51 2605 unsigned ff_tget_long(GetByteContext *gb, int le)
52 {
53
2/2
✓ Branch 0 taken 2335 times.
✓ Branch 1 taken 270 times.
2605 return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
54 }
55
56
57 double ff_tget_double(GetByteContext *gb, int le)
58 {
59 av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
60 return i.f64;
61 }
62
63
64 1569 unsigned ff_tget(GetByteContext *gb, int type, int le)
65 {
66
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 333 times.
✓ Branch 2 taken 1236 times.
✗ Branch 3 not taken.
1569 switch (type) {
67 case TIFF_BYTE: return bytestream2_get_byte(gb);
68 333 case TIFF_SHORT: return ff_tget_short(gb, le);
69 1236 case TIFF_LONG: return ff_tget_long(gb, le);
70 default: return UINT_MAX;
71 }
72 }
73
74 10322 static const char *auto_sep(int count, const char *sep, int i, int columns)
75 {
76
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10298 times.
10322 if (sep)
77
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 return i ? sep : "";
78
4/4
✓ Branch 0 taken 10112 times.
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 9488 times.
✓ Branch 3 taken 624 times.
10298 if (i && i%columns) {
79 9488 return ", ";
80 } else
81
2/2
✓ Branch 0 taken 632 times.
✓ Branch 1 taken 178 times.
810 return columns < count ? "\n" : "";
82 }
83
84 198 static int bprint_to_avdict(AVBPrint *bp, const char *name,
85 AVDictionary **metadata)
86 {
87 char *ap;
88 int ret;
89
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
198 if (!av_bprint_is_complete(bp)) {
91 av_bprint_finalize(bp, NULL);
92 return AVERROR(ENOMEM);
93 }
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
198 if ((ret = av_bprint_finalize(bp, &ap)) < 0)
95 return ret;
96
97 198 return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
98 }
99
100 56 int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
101 GetByteContext *gb, int le, AVDictionary **metadata)
102 {
103 AVBPrint bp;
104 int32_t nom, denom;
105 int i;
106
107
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
56 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
108 return AVERROR_INVALIDDATA;
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
110 return AVERROR_INVALIDDATA;
111
112 56 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
113
114
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
112 for (i = 0; i < count; i++) {
115 56 nom = ff_tget_long(gb, le);
116 56 denom = ff_tget_long(gb, le);
117 56 av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
118 }
119
120 56 return bprint_to_avdict(&bp, name, metadata);
121 }
122
123
124 20 int ff_tadd_long_metadata(int count, const char *name, const char *sep,
125 GetByteContext *gb, int le, AVDictionary **metadata)
126 {
127 AVBPrint bp;
128 int i;
129
130
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
131 return AVERROR_INVALIDDATA;
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
133 return AVERROR_INVALIDDATA;
134
135 20 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
136
137
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 for (i = 0; i < count; i++) {
138 20 av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
139 }
140
141 20 return bprint_to_avdict(&bp, name, metadata);
142 }
143
144
145 int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
146 GetByteContext *gb, int le, AVDictionary **metadata)
147 {
148 AVBPrint bp;
149 int i;
150
151 if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
152 return AVERROR_INVALIDDATA;
153 if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
154 return AVERROR_INVALIDDATA;
155
156 av_bprint_init(&bp, 10 * count, 100 * count);
157
158 for (i = 0; i < count; i++) {
159 av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
160 }
161
162 return bprint_to_avdict(&bp, name, metadata);
163 }
164
165
166 86 int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
167 GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
168 {
169 AVBPrint bp;
170 int i;
171
172
2/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 86 times.
86 if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
173 return AVERROR_INVALIDDATA;
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
175 return AVERROR_INVALIDDATA;
176
177 86 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
178
179
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 86 times.
184 for (i = 0; i < count; i++) {
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le);
181 98 av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
182 }
183
184 86 return bprint_to_avdict(&bp, name, metadata);
185 }
186
187
188 36 int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
189 GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
190 {
191 AVBPrint bp;
192 int i;
193
194
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if (count >= INT_MAX / sizeof(int8_t) || count < 0)
195 return AVERROR_INVALIDDATA;
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
36 if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
197 return AVERROR_INVALIDDATA;
198
199 36 av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
200
201
2/2
✓ Branch 0 taken 10148 times.
✓ Branch 1 taken 36 times.
10184 for (i = 0; i < count; i++) {
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10148 times.
10148 int v = is_signed ? (int8_t)bytestream2_get_byte(gb) : bytestream2_get_byte(gb);
203 10148 av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
204 }
205
206 36 return bprint_to_avdict(&bp, name, metadata);
207 }
208
209 60 int ff_tadd_string_metadata(int count, const char *name,
210 GetByteContext *gb, int le, AVDictionary **metadata)
211 {
212 char *value;
213
214
2/4
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
60 if (bytestream2_get_bytes_left(gb) < count || count < 0)
215 return AVERROR_INVALIDDATA;
216
217 60 value = av_malloc(count + 1);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!value)
219 return AVERROR(ENOMEM);
220
221 60 bytestream2_get_bufferu(gb, value, count);
222 60 value[count] = 0;
223
224 60 av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
225 60 return 0;
226 }
227
228
229 53 int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
230 {
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
53 if (bytestream2_get_bytes_left(gb) < 8) {
232 return AVERROR_INVALIDDATA;
233 }
234
235 53 *le = bytestream2_get_le16u(gb);
236
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 14 times.
53 if (*le == AV_RB16("II")) {
237 39 *le = 1;
238
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 } else if (*le == AV_RB16("MM")) {
239 14 *le = 0;
240 } else {
241 return AVERROR_INVALIDDATA;
242 }
243
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
53 if (ff_tget_short(gb, *le) != 42) {
245 return AVERROR_INVALIDDATA;
246 }
247
248 53 *ifd_offset = ff_tget_long(gb, *le);
249
250 53 return 0;
251 }
252
253
254 767 int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
255 unsigned *count, int *next)
256 {
257 int ifd_tag;
258 int valid_type;
259
260 767 *tag = ff_tget_short(gb, le);
261 767 *type = ff_tget_short(gb, le);
262 767 *count = ff_tget_long (gb, le);
263
264 767 ifd_tag = ff_tis_ifd(*tag);
265
2/4
✓ Branch 0 taken 767 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 767 times.
✗ Branch 3 not taken.
767 valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
266
267 767 *next = bytestream2_tell(gb) + 4;
268
269 // check for valid type
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 767 times.
767 if (!valid_type) {
271 return AVERROR_INVALIDDATA;
272 }
273
274 // seek to offset if this is an IFD-tag or
275 // if count values do not fit into the offset value
276
8/8
✓ Branch 0 taken 751 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 639 times.
✓ Branch 3 taken 112 times.
✓ Branch 4 taken 173 times.
✓ Branch 5 taken 466 times.
✓ Branch 6 taken 169 times.
✓ Branch 7 taken 4 times.
767 if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) {
277 297 bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
278 }
279
280 767 return 0;
281 }
282