Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/tiff_common.c
Line
Count
Source (jump to first uncovered line)
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
int ff_tis_ifd(unsigned tag)
34
777k
{
35
777k
    int i;
36
3.09M
    for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
37
2.32M
        if (ifd_tags[i] == tag) {
38
5.02k
            return i + 1;
39
5.02k
        }
40
2.32M
    }
41
772k
    return 0;
42
777k
}
43
44
45
unsigned ff_tget_short(GetByteContext *gb, int le)
46
6.58G
{
47
6.58G
    return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
48
6.58G
}
49
50
51
unsigned ff_tget_long(GetByteContext *gb, int le)
52
5.63G
{
53
5.63G
    return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
54
5.63G
}
55
56
57
double ff_tget_double(GetByteContext *gb, int le)
58
1.56G
{
59
1.56G
    av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
60
1.56G
    return i.f64;
61
1.56G
}
62
63
64
unsigned ff_tget(GetByteContext *gb, int type, int le)
65
7.13M
{
66
7.13M
    switch (type) {
67
297k
    case AV_TIFF_BYTE:  return bytestream2_get_byte(gb);
68
6.72M
    case AV_TIFF_SHORT: return ff_tget_short(gb, le);
69
117k
    case AV_TIFF_LONG:  return ff_tget_long(gb, le);
70
1.48k
    default:         return UINT_MAX;
71
7.13M
    }
72
7.13M
}
73
74
static const char *auto_sep(int count, const char *sep, int i, int columns)
75
510k
{
76
510k
    if (sep)
77
62.0k
        return i ? sep : "";
78
448k
    if (i && i%columns) {
79
370k
        return ", ";
80
370k
    } else
81
78.4k
        return columns < count ? "\n" : "";
82
448k
}
83
84
static int bprint_to_avdict(AVBPrint *bp, const char *name,
85
                            AVDictionary **metadata)
86
14.6k
{
87
14.6k
    char *ap;
88
14.6k
    int ret;
89
90
14.6k
    if (!av_bprint_is_complete(bp)) {
91
0
        av_bprint_finalize(bp, NULL);
92
0
        return AVERROR(ENOMEM);
93
0
    }
94
14.6k
    if ((ret = av_bprint_finalize(bp, &ap)) < 0)
95
0
        return ret;
96
97
14.6k
    return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
98
14.6k
}
99
100
int ff_tadd_rational_metadata(int count, const char *name, const char *sep,
101
                              GetByteContext *gb, int le, AVDictionary **metadata)
102
0
{
103
0
    AVBPrint bp;
104
0
    int32_t nom, denom;
105
0
    int i;
106
107
0
    if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
108
0
        return AVERROR_INVALIDDATA;
109
0
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
110
0
        return AVERROR_INVALIDDATA;
111
112
0
    av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
113
114
0
    for (i = 0; i < count; i++) {
115
0
        nom   = ff_tget_long(gb, le);
116
0
        denom = ff_tget_long(gb, le);
117
0
        av_bprintf(&bp, "%s%7"PRId32":%-7"PRId32, auto_sep(count, sep, i, 4), nom, denom);
118
0
    }
119
120
0
    return bprint_to_avdict(&bp, name, metadata);
121
0
}
122
123
124
int ff_tadd_long_metadata(int count, const char *name, const char *sep,
125
                          GetByteContext *gb, int le, AVDictionary **metadata)
126
0
{
127
0
    AVBPrint bp;
128
0
    int i;
129
130
0
    if (count >= INT_MAX / sizeof(int32_t) || count <= 0)
131
0
        return AVERROR_INVALIDDATA;
132
0
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int32_t))
133
0
        return AVERROR_INVALIDDATA;
134
135
0
    av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
136
137
0
    for (i = 0; i < count; i++) {
138
0
        av_bprintf(&bp, "%s%7i", auto_sep(count, sep, i, 8), ff_tget_long(gb, le));
139
0
    }
140
141
0
    return bprint_to_avdict(&bp, name, metadata);
142
0
}
143
144
145
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
146
                             GetByteContext *gb, int le, AVDictionary **metadata)
147
5.51k
{
148
5.51k
    AVBPrint bp;
149
5.51k
    int i;
150
151
5.51k
    if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
152
515
        return AVERROR_INVALIDDATA;
153
4.99k
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
154
1.08k
        return AVERROR_INVALIDDATA;
155
156
3.91k
    av_bprint_init(&bp, 10 * count, 100 * count);
157
158
114k
    for (i = 0; i < count; i++) {
159
110k
        av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
160
110k
    }
161
162
3.91k
    return bprint_to_avdict(&bp, name, metadata);
163
4.99k
}
164
165
166
int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
167
                            GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
168
19.4k
{
169
19.4k
    AVBPrint bp;
170
19.4k
    int i;
171
172
19.4k
    if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
173
6.28k
        return AVERROR_INVALIDDATA;
174
13.1k
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
175
2.39k
        return AVERROR_INVALIDDATA;
176
177
10.7k
    av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
178
179
411k
    for (i = 0; i < count; i++) {
180
400k
        int v = is_signed ? (int16_t)ff_tget_short(gb, le) :  ff_tget_short(gb, le);
181
400k
        av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
182
400k
    }
183
184
10.7k
    return bprint_to_avdict(&bp, name, metadata);
185
13.1k
}
186
187
188
int ff_tadd_bytes_metadata(int count, const char *name, const char *sep,
189
                           GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
190
0
{
191
0
    AVBPrint bp;
192
0
    int i;
193
194
0
    if (count >= INT_MAX / sizeof(int8_t) || count < 0)
195
0
        return AVERROR_INVALIDDATA;
196
0
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int8_t))
197
0
        return AVERROR_INVALIDDATA;
198
199
0
    av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
200
201
0
    for (i = 0; i < count; i++) {
202
0
        int v = is_signed ? (int8_t)bytestream2_get_byte(gb) :  bytestream2_get_byte(gb);
203
0
        av_bprintf(&bp, "%s%3i", auto_sep(count, sep, i, 16), v);
204
0
    }
205
206
0
    return bprint_to_avdict(&bp, name, metadata);
207
0
}
208
209
int ff_tadd_string_metadata(int count, const char *name,
210
                            GetByteContext *gb, int le, AVDictionary **metadata)
211
19.8k
{
212
19.8k
    char *value;
213
214
19.8k
    if (bytestream2_get_bytes_left(gb) < count || count < 0)
215
4.03k
        return AVERROR_INVALIDDATA;
216
217
15.7k
    value = av_malloc(count + 1);
218
15.7k
    if (!value)
219
0
        return AVERROR(ENOMEM);
220
221
15.7k
    bytestream2_get_bufferu(gb, value, count);
222
15.7k
    value[count] = 0;
223
224
15.7k
    av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
225
15.7k
    return 0;
226
15.7k
}
227
228
229
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
230
712k
{
231
712k
    if (bytestream2_get_bytes_left(gb) < 8) {
232
196k
        return AVERROR_INVALIDDATA;
233
196k
    }
234
235
515k
    *le = bytestream2_get_le16u(gb);
236
515k
    if (*le == AV_RB16("II")) {
237
249k
        *le = 1;
238
266k
    } else if (*le == AV_RB16("MM")) {
239
130k
        *le = 0;
240
135k
    } else {
241
135k
        return AVERROR_INVALIDDATA;
242
135k
    }
243
244
379k
    if (ff_tget_short(gb, *le) != 42) {
245
26.8k
        return AVERROR_INVALIDDATA;
246
26.8k
    }
247
248
352k
    *ifd_offset = ff_tget_long(gb, *le);
249
250
352k
    return 0;
251
379k
}
252
253
254
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
255
                 unsigned *count, int *next)
256
657k
{
257
657k
    int ifd_tag;
258
657k
    int valid_type;
259
260
657k
    *tag    = ff_tget_short(gb, le);
261
657k
    *type   = ff_tget_short(gb, le);
262
657k
    *count  = ff_tget_long (gb, le);
263
264
657k
    ifd_tag    = ff_tis_ifd(*tag);
265
657k
    valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
266
267
657k
    *next = bytestream2_tell(gb) + 4;
268
269
    // check for valid type
270
657k
    if (!valid_type) {
271
475k
        return AVERROR_INVALIDDATA;
272
475k
    }
273
274
    // seek to offset if this is an IFD-tag or
275
    // if count values do not fit into the offset value
276
182k
    if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == AV_TIFF_STRING))) {
277
95.5k
        bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
278
95.5k
    }
279
280
182k
    return 0;
281
657k
}