Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/tiff_common.c
Line
Count
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
int ff_tis_ifd(unsigned tag)
34
4.54M
{
35
4.54M
    int i;
36
18.1M
    for (i = 0; i < FF_ARRAY_ELEMS(ifd_tags); i++) {
37
13.5M
        if (ifd_tags[i] == tag) {
38
19.3k
            return i + 1;
39
19.3k
        }
40
13.5M
    }
41
4.52M
    return 0;
42
4.54M
}
43
44
45
unsigned ff_tget_short(GetByteContext *gb, int le)
46
20.7M
{
47
20.7M
    return le ? bytestream2_get_le16(gb) : bytestream2_get_be16(gb);
48
20.7M
}
49
50
51
unsigned ff_tget_long(GetByteContext *gb, int le)
52
10.4M
{
53
10.4M
    return le ? bytestream2_get_le32(gb) : bytestream2_get_be32(gb);
54
10.4M
}
55
56
57
double ff_tget_double(GetByteContext *gb, int le)
58
321k
{
59
321k
    av_alias64 i = { .u64 = le ? bytestream2_get_le64(gb) : bytestream2_get_be64(gb)};
60
321k
    return i.f64;
61
321k
}
62
63
64
unsigned ff_tget(GetByteContext *gb, int type, int le)
65
5.55M
{
66
5.55M
    switch (type) {
67
661k
    case AV_TIFF_BYTE:  return bytestream2_get_byte(gb);
68
4.39M
    case AV_TIFF_SHORT: return ff_tget_short(gb, le);
69
298k
    case AV_TIFF_LONG:  return ff_tget_long(gb, le);
70
200k
    default:         return UINT_MAX;
71
5.55M
    }
72
5.55M
}
73
74
static const char *auto_sep(int count, const char *sep, int i, int columns)
75
340k
{
76
340k
    if (sep)
77
58.4k
        return i ? sep : "";
78
282k
    if (i && i%columns) {
79
228k
        return ", ";
80
228k
    } else
81
53.8k
        return columns < count ? "\n" : "";
82
282k
}
83
84
static int bprint_to_avdict(AVBPrint *bp, const char *name,
85
                            AVDictionary **metadata)
86
16.3k
{
87
16.3k
    char *ap;
88
16.3k
    int ret;
89
90
16.3k
    if (!av_bprint_is_complete(bp)) {
91
0
        av_bprint_finalize(bp, NULL);
92
0
        return AVERROR(ENOMEM);
93
0
    }
94
16.3k
    if ((ret = av_bprint_finalize(bp, &ap)) < 0)
95
0
        return ret;
96
97
16.3k
    return av_dict_set(metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
98
16.3k
}
99
100
int ff_tadd_doubles_metadata(int count, const char *name, const char *sep,
101
                             GetByteContext *gb, int le, AVDictionary **metadata)
102
6.62k
{
103
6.62k
    AVBPrint bp;
104
6.62k
    int i;
105
106
6.62k
    if (count >= INT_MAX / sizeof(int64_t) || count <= 0)
107
1.19k
        return AVERROR_INVALIDDATA;
108
5.43k
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int64_t))
109
2.13k
        return AVERROR_INVALIDDATA;
110
111
3.30k
    av_bprint_init(&bp, 10 * count, 100 * count);
112
113
87.7k
    for (i = 0; i < count; i++) {
114
84.4k
        av_bprintf(&bp, "%s%.15g", auto_sep(count, sep, i, 4), ff_tget_double(gb, le));
115
84.4k
    }
116
117
3.30k
    return bprint_to_avdict(&bp, name, metadata);
118
5.43k
}
119
120
121
int ff_tadd_shorts_metadata(int count, const char *name, const char *sep,
122
                            GetByteContext *gb, int le, int is_signed, AVDictionary **metadata)
123
28.9k
{
124
28.9k
    AVBPrint bp;
125
28.9k
    int i;
126
127
28.9k
    if (count >= INT_MAX / sizeof(int16_t) || count <= 0)
128
13.8k
        return AVERROR_INVALIDDATA;
129
15.1k
    if (bytestream2_get_bytes_left(gb) < count * sizeof(int16_t))
130
2.07k
        return AVERROR_INVALIDDATA;
131
132
13.0k
    av_bprint_init(&bp, 10 * count, AV_BPRINT_SIZE_UNLIMITED);
133
134
269k
    for (i = 0; i < count; i++) {
135
256k
        int v = is_signed ? (int16_t)ff_tget_short(gb, le) :  ff_tget_short(gb, le);
136
256k
        av_bprintf(&bp, "%s%5i", auto_sep(count, sep, i, 8), v);
137
256k
    }
138
139
13.0k
    return bprint_to_avdict(&bp, name, metadata);
140
15.1k
}
141
142
int ff_tadd_string_metadata(int count, const char *name,
143
                            GetByteContext *gb, int le, AVDictionary **metadata)
144
49.3k
{
145
49.3k
    char *value;
146
147
49.3k
    if (bytestream2_get_bytes_left(gb) < count || count < 0)
148
7.43k
        return AVERROR_INVALIDDATA;
149
150
41.9k
    value = av_malloc(count + 1);
151
41.9k
    if (!value)
152
0
        return AVERROR(ENOMEM);
153
154
41.9k
    bytestream2_get_bufferu(gb, value, count);
155
41.9k
    value[count] = 0;
156
157
41.9k
    av_dict_set(metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
158
41.9k
    return 0;
159
41.9k
}
160
161
162
int ff_tdecode_header(GetByteContext *gb, int *le, int *ifd_offset)
163
1.62M
{
164
1.62M
    if (bytestream2_get_bytes_left(gb) < 8) {
165
342k
        return AVERROR_INVALIDDATA;
166
342k
    }
167
168
1.28M
    *le = bytestream2_get_le16u(gb);
169
1.28M
    if (*le == AV_RB16("II")) {
170
822k
        *le = 1;
171
822k
    } else if (*le == AV_RB16("MM")) {
172
258k
        *le = 0;
173
258k
    } else {
174
201k
        return AVERROR_INVALIDDATA;
175
201k
    }
176
177
1.08M
    if (ff_tget_short(gb, *le) != 42) {
178
47.7k
        return AVERROR_INVALIDDATA;
179
47.7k
    }
180
181
1.03M
    *ifd_offset = ff_tget_long(gb, *le);
182
183
1.03M
    return 0;
184
1.08M
}
185
186
187
int ff_tread_tag(GetByteContext *gb, int le, unsigned *tag, unsigned *type,
188
                 unsigned *count, int *next)
189
3.92M
{
190
3.92M
    int ifd_tag;
191
3.92M
    int valid_type;
192
193
3.92M
    *tag    = ff_tget_short(gb, le);
194
3.92M
    *type   = ff_tget_short(gb, le);
195
3.92M
    *count  = ff_tget_long (gb, le);
196
197
3.92M
    ifd_tag    = ff_tis_ifd(*tag);
198
3.92M
    valid_type = *type != 0 && *type < FF_ARRAY_ELEMS(type_sizes);
199
200
3.92M
    *next = bytestream2_tell(gb) + 4;
201
202
    // check for valid type
203
3.92M
    if (!valid_type) {
204
2.90M
        return AVERROR_INVALIDDATA;
205
2.90M
    }
206
207
    // seek to offset if this is an IFD-tag or
208
    // if count values do not fit into the offset value
209
1.02M
    if (ifd_tag || (*count > 4 || !(type_sizes[*type] * (*count) <= 4 || *type == AV_TIFF_STRING))) {
210
280k
        bytestream2_seek(gb, ff_tget_long (gb, le), SEEK_SET);
211
280k
    }
212
213
1.02M
    return 0;
214
3.92M
}