Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/tiff_common.c |
Date: | 2022-07-04 00:18:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 84 | 115 | 73.0% |
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 "tiff_common.h" | ||
30 | |||
31 | |||
32 | 795 | int ff_tis_ifd(unsigned tag) | |
33 | { | ||
34 | int i; | ||
35 |
2/2✓ Branch 0 taken 2337 times.
✓ Branch 1 taken 763 times.
|
3100 | for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) { |
36 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2305 times.
|
2337 | if (ifd_tags[i] == tag) { |
37 | 32 | return i + 1; | |
38 | } | ||
39 | } | ||
40 | 763 | return 0; | |
41 | } | ||
42 | |||
43 | |||
44 | 1443 | unsigned ff_tget_short(GetByteContext *gb, int le) | |
45 | { | ||
46 |
2/2✓ Branch 0 taken 1127 times.
✓ Branch 1 taken 316 times.
|
1443 | return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb); |
47 | } | ||
48 | |||
49 | |||
50 | 2503 | unsigned ff_tget_long(GetByteContext *gb, int le) | |
51 | { | ||
52 |
2/2✓ Branch 0 taken 2199 times.
✓ Branch 1 taken 304 times.
|
2503 | return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb); |
53 | } | ||
54 | |||
55 | |||
56 | ✗ | double ff_tget_double(GetByteContext *gb, int le) | |
57 | { | ||
58 | ✗ | av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)}; | |
59 | ✗ | return i.f64; | |
60 | } | ||
61 | |||
62 | |||
63 | 1689 | unsigned ff_tget(GetByteContext *gb, int type, int le) | |
64 | { | ||
65 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
✓ Branch 2 taken 1516 times.
✗ Branch 3 not taken.
|
1689 | switch (type) { |
66 | ✗ | case TIFF_BYTE: return bytestream2_get_byte(gb); | |
67 | 173 | case TIFF_SHORT: return ff_tget_short(gb, le); | |
68 | 1516 | case TIFF_LONG: return ff_tget_long(gb, le); | |
69 | ✗ | default: return UINT_MAX; | |
70 | } | ||
71 | } | ||
72 | |||
73 | 10302 | static const char *auto_sep(int count, const char *sep, int i, int columns) | |
74 | { | ||
75 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10298 times.
|
10302 | if (sep) |
76 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | return i ? sep : ""; |
77 |
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) { |
78 | 9488 | return ", "; | |
79 | } else | ||
80 |
2/2✓ Branch 0 taken 632 times.
✓ Branch 1 taken 178 times.
|
810 | return columns < count ? "\n" : ""; |
81 | } | ||
82 | |||
83 | 188 | static int bprint_to_avdict(AVBPrint *bp, const char *name, | |
84 | AVDictionary **metadata) | ||
85 | { | ||
86 | char *ap; | ||
87 | int ret; | ||
88 | |||
89 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
|
188 | if (!av_bprint_is_complete(bp)) { |
90 | ✗ | av_bprint_finalize(bp, NULL); | |
91 | ✗ | return AVERROR(ENOMEM); | |
92 | } | ||
93 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
|
188 | if ((ret = av_bprint_finalize(bp, &ap)) < 0) |
94 | ✗ | return ret; | |
95 | |||
96 | 188 | return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL); | |
97 | } | ||
98 | |||
99 | 56 | int ff_tadd_rational_metadata(int count, const char *name, const char *sep, | |
100 | GetByteContext *gb, int le, AVDictionary **metadata) | ||
101 | { | ||
102 | AVBPrint bp; | ||
103 | int32_t nom, denom; | ||
104 | int i; | ||
105 | |||
106 |
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) |
107 | ✗ | return AVERROR_INVALIDDATA; | |
108 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
|
56 | if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t)) |
109 | ✗ | return AVERROR_INVALIDDATA; | |
110 | |||
111 | 56 | av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED); | |
112 | |||
113 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
|
112 | for (i = 0; i < count; i++) { |
114 | 56 | nom = ff_tget_long(gb, le); | |
115 | 56 | denom = ff_tget_long(gb, le); | |
116 | 56 | av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom); | |
117 | } | ||
118 | |||
119 | 56 | return bprint_to_avdict(&bp, name, metadata); | |
120 | } | ||
121 | |||
122 | |||
123 | 20 | int ff_tadd_long_metadata(int count, const char *name, const char *sep, | |
124 | GetByteContext *gb, int le, AVDictionary **metadata) | ||
125 | { | ||
126 | AVBPrint bp; | ||
127 | int i; | ||
128 | |||
129 |
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) |
130 | ✗ | return AVERROR_INVALIDDATA; | |
131 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
|
20 | if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t)) |
132 | ✗ | return AVERROR_INVALIDDATA; | |
133 | |||
134 | 20 | av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED); | |
135 | |||
136 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
40 | for (i = 0; i < count; i++) { |
137 | 20 | av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le)); | |
138 | } | ||
139 | |||
140 | 20 | return bprint_to_avdict(&bp, name, metadata); | |
141 | } | ||
142 | |||
143 | |||
144 | ✗ | int ff_tadd_doubles_metadata(int count, const char *name, const char *sep, | |
145 | GetByteContext *gb, int le, AVDictionary **metadata) | ||
146 | { | ||
147 | AVBPrint bp; | ||
148 | int i; | ||
149 | |||
150 | ✗ | if (count >= INT_MAX / sizeof(int64_t) || count <= 0) | |
151 | ✗ | return AVERROR_INVALIDDATA; | |
152 | ✗ | if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t)) | |
153 | ✗ | return AVERROR_INVALIDDATA; | |
154 | |||
155 | ✗ | av_bprint_init(&bp, 10 * count, 100 * count); | |
156 | |||
157 | ✗ | for (i = 0; i < count; i++) { | |
158 | ✗ | av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le)); | |
159 | } | ||
160 | |||
161 | ✗ | return bprint_to_avdict(&bp, name, metadata); | |
162 | } | ||
163 | |||
164 | |||
165 | 76 | int ff_tadd_shorts_metadata(int count, const char *name, const char *sep, | |
166 | GetByteContext *gb, int le, int is_signed, AVDictionary **metadata) | ||
167 | { | ||
168 | AVBPrint bp; | ||
169 | int i; | ||
170 | |||
171 |
2/4✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
|
76 | if (count >= INT_MAX / sizeof(int16_t) || count <= 0) |
172 | ✗ | return AVERROR_INVALIDDATA; | |
173 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
|
76 | if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t)) |
174 | ✗ | return AVERROR_INVALIDDATA; | |
175 | |||
176 | 76 | av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED); | |
177 | |||
178 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 76 times.
|
154 | for (i = 0; i < count; i++) { |
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
78 | int v = is_signed ? (int16_t)ff_tget_short(gb, le) : ff_tget_short(gb, le); |
180 | 78 | av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v); | |
181 | } | ||
182 | |||
183 | 76 | return bprint_to_avdict(&bp, name, metadata); | |
184 | } | ||
185 | |||
186 | |||
187 | 36 | int ff_tadd_bytes_metadata(int count, const char *name, const char *sep, | |
188 | GetByteContext *gb, int le, int is_signed, AVDictionary **metadata) | ||
189 | { | ||
190 | AVBPrint bp; | ||
191 | int i; | ||
192 | |||
193 |
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) |
194 | ✗ | return AVERROR_INVALIDDATA; | |
195 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t)) |
196 | ✗ | return AVERROR_INVALIDDATA; | |
197 | |||
198 | 36 | av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED); | |
199 | |||
200 |
2/2✓ Branch 0 taken 10148 times.
✓ Branch 1 taken 36 times.
|
10184 | for (i = 0; i < count; i++) { |
201 |
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); |
202 | 10148 | av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v); | |
203 | } | ||
204 | |||
205 | 36 | return bprint_to_avdict(&bp, name, metadata); | |
206 | } | ||
207 | |||
208 | 58 | int ff_tadd_string_metadata(int count, const char *name, | |
209 | GetByteContext *gb, int le, AVDictionary **metadata) | ||
210 | { | ||
211 | char *value; | ||
212 | |||
213 |
2/4✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 58 times.
|
58 | if (bytestream2_get_bytes_left(gb) < count || count < 0) |
214 | ✗ | return AVERROR_INVALIDDATA; | |
215 | |||
216 | 58 | value = av_malloc(count + 1); | |
217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (!value) |
218 | ✗ | return AVERROR(ENOMEM); | |
219 | |||
220 | 58 | bytestream2_get_bufferu(gb, value, count); | |
221 | 58 | value[count] = 0; | |
222 | |||
223 | 58 | av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL); | |
224 | 58 | return 0; | |
225 | } | ||
226 | |||
227 | |||
228 | 41 | int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset) | |
229 | { | ||
230 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
|
41 | if (bytestream2_get_bytes_left(gb) < 8) { |
231 | ✗ | return AVERROR_INVALIDDATA; | |
232 | } | ||
233 | |||
234 | 41 | *le = bytestream2_get_le16u(gb); | |
235 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 14 times.
|
41 | if (*le == AV_RB16("II")) { |
236 | 27 | *le = 1; | |
237 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | } else if (*le == AV_RB16("MM")) { |
238 | 14 | *le = 0; | |
239 | } else { | ||
240 | ✗ | return AVERROR_INVALIDDATA; | |
241 | } | ||
242 | |||
243 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
|
41 | if (ff_tget_short(gb, *le) != 42) { |
244 | ✗ | return AVERROR_INVALIDDATA; | |
245 | } | ||
246 | |||
247 | 41 | *ifd_offset = ff_tget_long(gb, *le); | |
248 | |||
249 | 41 | return 0; | |
250 | } | ||
251 | |||
252 | |||
253 | 547 | int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type, | |
254 | unsigned *count, int *next) | ||
255 | { | ||
256 | int ifd_tag; | ||
257 | int valid_type; | ||
258 | |||
259 | 547 | *tag = ff_tget_short(gb, le); | |
260 | 547 | *type = ff_tget_short(gb, le); | |
261 | 547 | *count = ff_tget_long (gb, le); | |
262 | |||
263 | 547 | ifd_tag = ff_tis_ifd(*tag); | |
264 |
2/4✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 547 times.
✗ Branch 3 not taken.
|
547 | valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes); |
265 | |||
266 | 547 | *next = bytestream2_tell(gb) + 4; | |
267 | |||
268 | // check for valid type | ||
269 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
|
547 | if (!valid_type) { |
270 | ✗ | return AVERROR_INVALIDDATA; | |
271 | } | ||
272 | |||
273 | // seek to offset if this is an IFD-tag or | ||
274 | // if count values do not fit into the offset value | ||
275 |
8/8✓ Branch 0 taken 531 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 431 times.
✓ Branch 3 taken 100 times.
✓ Branch 4 taken 119 times.
✓ Branch 5 taken 312 times.
✓ Branch 6 taken 115 times.
✓ Branch 7 taken 4 times.
|
547 | if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == TIFF_STRING))) { |
276 | 231 | bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET); | |
277 | } | ||
278 | |||
279 | 547 | return 0; | |
280 | } | ||
281 |