Coverage Report

Created: 2026-10-27 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/file_layout.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif 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
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "file_layout.h"
22
#include "sequences/seq_boxes.h"
23
#include <limits>
24
25
26
FileLayout::FileLayout()
27
45.4k
{
28
45.4k
  auto ftyp = std::make_shared<Box_ftyp>();
29
45.4k
  ftyp->set_output_position(0);
30
45.4k
  m_boxes.push_back(ftyp);
31
32
  // TODO: these variables are not used yet
33
45.4k
  (void)m_writeMode;
34
45.4k
  (void)m_file_size;
35
45.4k
}
36
37
38
Error FileLayout::read(const std::shared_ptr<StreamReader>& stream, const heif_security_limits* limits)
39
22.7k
{
40
22.7k
  m_boxes.clear();
41
42
22.7k
  m_stream_reader = stream;
43
44
  // --- read initial range, large enough to cover 'ftyp' box
45
46
22.7k
  m_max_length = stream->request_range(0, INITIAL_FTYP_REQUEST);
47
48
22.7k
  if (m_max_length < MAXIMUM_BOX_HEADER_SIZE) {
49
16
    return {heif_error_Invalid_input,
50
16
            heif_suberror_Unspecified,
51
16
            "File size too small."};
52
16
  }
53
54
  // --- read 'ftyp' box header
55
56
22.7k
  BitstreamRange ftyp_hdr_range(m_stream_reader, m_max_length);
57
22.7k
  BoxHeader ftyp_header;
58
22.7k
  Error err;
59
22.7k
  err = ftyp_header.parse_header(ftyp_hdr_range);
60
22.7k
  if (err) {
61
35
    return err;
62
35
  }
63
64
  // --- check whether it is a valid 'ftyp' box header
65
66
22.6k
  if (ftyp_header.get_short_type() != fourcc("ftyp")) {
67
0
    return {heif_error_Invalid_input,
68
0
            heif_suberror_No_ftyp_box,
69
0
            "File does not start with 'ftyp' box."};
70
0
  }
71
72
22.6k
  uint64_t ftyp_size = ftyp_header.get_box_size();
73
74
22.6k
  if (ftyp_size == 0) {
75
1
    return {heif_error_Invalid_input,
76
1
            heif_suberror_No_ftyp_box,
77
1
            "ftyp box shall not be the only box in the file"};
78
1
  }
79
80
22.6k
  if (ftyp_size > m_max_length) {
81
51
    return {heif_error_Invalid_input,
82
51
            heif_suberror_No_ftyp_box,
83
51
            "ftyp box larger than initial read range"};
84
51
  }
85
86
  // --- read the 'ftyp' box
87
88
22.6k
  BitstreamRange ftyp_range(m_stream_reader, 0, ftyp_size);
89
22.6k
  std::shared_ptr<Box> ftyp_box;
90
22.6k
  err = Box::read(ftyp_range, &ftyp_box, limits);
91
22.6k
  if (err) {
92
7
    return err;
93
7
  }
94
95
22.6k
  m_boxes.push_back(ftyp_box);
96
22.6k
  m_ftyp_box = std::dynamic_pointer_cast<Box_ftyp>(ftyp_box);
97
98
99
  // --- skip through box headers until we find the 'meta' box
100
101
22.6k
  uint64_t next_box_start = ftyp_size;
102
103
22.6k
  bool meta_found = false;
104
22.6k
  bool mini_found = false;
105
22.6k
  bool moov_found = false;
106
107
67.6k
  for (;;) {
108
    // TODO: overflow
109
67.6k
    uint64_t next_box_header_end = next_box_start + MAXIMUM_BOX_HEADER_SIZE;
110
67.6k
    if (next_box_header_end > m_max_length) {
111
26.2k
      m_max_length = stream->request_range(next_box_start, next_box_header_end);
112
26.2k
    }
113
114
67.6k
    if (next_box_header_end > m_max_length) {
115
19.3k
      if (meta_found || mini_found || moov_found) {
116
19.1k
        return Error::Ok;
117
19.1k
      }
118
202
      else {
119
202
        return {heif_error_Invalid_input,
120
202
                heif_suberror_Unspecified,
121
202
                "Insufficient input data"};
122
202
      }
123
19.3k
    }
124
125
48.3k
    BitstreamRange box_range(m_stream_reader, next_box_start, m_max_length);
126
48.3k
    BoxHeader box_header;
127
48.3k
    err = box_header.parse_header(box_range);
128
48.3k
    if (err) {
129
1
      return err;
130
1
    }
131
132
48.3k
    if (box_header.get_short_type() == fourcc("meta")) {
133
23.7k
      const uint64_t meta_box_start = next_box_start;
134
23.7k
      uint64_t end_of_meta_box;
135
23.7k
      if (box_header.get_box_size() == BoxHeader::size_until_end_of_file) {
136
        // A box size of 0 means the box extends to the end of the file
137
        // (ISO/IEC 14496-12 clause 4.2), which is legal for the last box.
138
        // Resolve it to the file size.
139
126
        end_of_meta_box = m_stream_reader->request_range(meta_box_start,
140
126
                                                         std::numeric_limits<uint64_t>::max());
141
126
        m_max_length = end_of_meta_box;
142
126
        if (end_of_meta_box <= meta_box_start) {
143
0
          return {heif_error_Invalid_input,
144
0
                  heif_suberror_No_meta_box,
145
0
                  "Cannot read meta box with unspecified size"};
146
0
        }
147
126
      }
148
23.6k
      else {
149
23.6k
        end_of_meta_box = box_header.get_box_size();
150
23.6k
        if (end_of_meta_box > std::numeric_limits<uint64_t>::max() - meta_box_start) {
151
0
          return {heif_error_Invalid_input,
152
0
                  heif_suberror_No_meta_box,
153
0
                  "Cannot read meta box with invalid size"};
154
0
        }
155
23.6k
        end_of_meta_box += meta_box_start;
156
23.6k
      }
157
23.7k
      if (m_max_length < end_of_meta_box) {
158
2.82k
        m_max_length = m_stream_reader->request_range(meta_box_start, end_of_meta_box);
159
2.82k
      }
160
161
23.7k
      if (m_max_length < end_of_meta_box) {
162
91
        return {heif_error_Invalid_input,
163
91
                heif_suberror_No_meta_box,
164
91
                "Cannot read full meta box"};
165
91
      }
166
167
23.6k
      BitstreamRange meta_box_range(m_stream_reader, meta_box_start, end_of_meta_box);
168
23.6k
      std::shared_ptr<Box> meta_box;
169
23.6k
      err = Box::read(meta_box_range, &meta_box, limits);
170
23.6k
      if (err) {
171
2.34k
        return err;
172
2.34k
      }
173
174
21.2k
      m_boxes.push_back(meta_box);
175
21.2k
      m_meta_box = std::dynamic_pointer_cast<Box_meta>(meta_box);
176
21.2k
      meta_found = true;
177
21.2k
    }
178
179
    // TODO: this is basically the same as the meta box case above, with different error handling.
180
45.8k
    if (box_header.get_short_type() == fourcc("mini")) {
181
1.82k
      const uint64_t mini_box_start = next_box_start;
182
1.82k
      uint64_t end_of_mini_box;
183
1.82k
      if (box_header.get_box_size() == BoxHeader::size_until_end_of_file) {
184
        // A box size of 0 means the box extends to the end of the file
185
        // (ISO/IEC 14496-12 clause 4.2), which is legal for the last box.
186
        // Resolve it to the file size.
187
56
        end_of_mini_box = m_stream_reader->request_range(mini_box_start,
188
56
                                                         std::numeric_limits<uint64_t>::max());
189
56
        m_max_length = end_of_mini_box;
190
56
        if (end_of_mini_box <= mini_box_start) {
191
0
          return {heif_error_Invalid_input,
192
0
                  heif_suberror_Invalid_mini_box,
193
0
                  "Cannot read mini box with unspecified size"};
194
0
        }
195
56
      }
196
1.77k
      else {
197
1.77k
        end_of_mini_box = box_header.get_box_size();
198
1.77k
        if (end_of_mini_box > std::numeric_limits<uint64_t>::max() - mini_box_start) {
199
0
          return {heif_error_Invalid_input,
200
0
                  heif_suberror_Invalid_mini_box,
201
0
                  "Cannot read mini box with invalid size"};
202
0
        }
203
1.77k
        end_of_mini_box += mini_box_start;
204
1.77k
      }
205
1.82k
      if (m_max_length < end_of_mini_box) {
206
880
        m_max_length = m_stream_reader->request_range(mini_box_start, end_of_mini_box);
207
880
      }
208
209
1.82k
      if (m_max_length < end_of_mini_box) {
210
98
        return {heif_error_Invalid_input,
211
98
                heif_suberror_Invalid_mini_box,
212
98
                "Cannot read full mini box"};
213
98
      }
214
1.73k
      BitstreamRange mini_box_range(m_stream_reader, mini_box_start, end_of_mini_box);
215
1.73k
      std::shared_ptr<Box> mini_box;
216
1.73k
      err = Box::read(mini_box_range, &mini_box, limits);
217
1.73k
      if (err) {
218
203
        return err;
219
203
      }
220
221
1.52k
      m_boxes.push_back(mini_box);
222
1.52k
      m_mini_box = std::dynamic_pointer_cast<Box_mini>(mini_box);
223
224
1.52k
      mini_found = true;
225
1.52k
    }
226
227
45.5k
    if (box_header.get_short_type() == fourcc("moov")) {
228
905
      const uint64_t moov_box_start = next_box_start;
229
905
      uint64_t end_of_moov_box;
230
905
      if (box_header.get_box_size() == BoxHeader::size_until_end_of_file) {
231
        // A box size of 0 means the box extends to the end of the file
232
        // (ISO/IEC 14496-12 clause 4.2), which is legal for the last box.
233
        // Resolve it to the file size.
234
65
        end_of_moov_box = m_stream_reader->request_range(moov_box_start,
235
65
                                                         std::numeric_limits<uint64_t>::max());
236
65
        m_max_length = end_of_moov_box;
237
65
        if (end_of_moov_box <= moov_box_start) {
238
0
          return {heif_error_Invalid_input,
239
0
                  heif_suberror_No_moov_box,
240
0
                  "Cannot read moov box with unspecified size"};
241
0
        }
242
65
      }
243
840
      else {
244
840
        end_of_moov_box = box_header.get_box_size();
245
840
        if (end_of_moov_box > std::numeric_limits<uint64_t>::max() - moov_box_start) {
246
0
          return {heif_error_Invalid_input,
247
0
                  heif_suberror_No_moov_box,
248
0
                  "Cannot read moov box with invalid size"};
249
0
        }
250
840
        end_of_moov_box += moov_box_start;
251
840
      }
252
905
      if (m_max_length < end_of_moov_box) {
253
302
        m_max_length = m_stream_reader->request_range(moov_box_start, end_of_moov_box);
254
302
      }
255
256
905
      if (m_max_length < end_of_moov_box) {
257
105
        return {heif_error_Invalid_input,
258
105
                heif_suberror_No_moov_box,
259
105
                "Cannot read full moov box"};
260
105
      }
261
262
800
      BitstreamRange moov_box_range(m_stream_reader, moov_box_start, end_of_moov_box);
263
800
      std::shared_ptr<Box> moov_box;
264
800
      err = Box::read(moov_box_range, &moov_box, limits);
265
800
      if (err) {
266
186
        return err;
267
186
      }
268
269
614
      m_boxes.push_back(moov_box);
270
614
      m_moov_box = std::dynamic_pointer_cast<Box_moov>(moov_box);
271
272
614
      moov_found = true;
273
614
    }
274
275
45.2k
    uint64_t boxSize = box_header.get_box_size();
276
45.2k
    if (boxSize == Box::size_until_end_of_file) {
277
239
      if (meta_found || mini_found || moov_found) {
278
238
        return Error::Ok;
279
238
      }
280
1
      else {
281
1
        return {heif_error_Invalid_input,
282
1
                heif_suberror_Unspecified,
283
1
                "No meta box found"};
284
1
      }
285
239
    }
286
287
45.0k
    if (std::numeric_limits<uint64_t>::max() - boxSize < next_box_start) {
288
0
      return {heif_error_Invalid_input,
289
0
              heif_suberror_Unspecified,
290
0
              "Box size too large, integer overflow"};
291
0
    }
292
293
45.0k
    next_box_start = next_box_start + boxSize;
294
45.0k
  }
295
296
0
  return Error::Ok;
297
22.6k
}
298
299
300
void FileLayout::set_write_mode(WriteMode writeMode, const std::shared_ptr<StreamWriter>& writer)
301
0
{
302
303
0
}
304
305
306
Error FileLayout::write(std::shared_ptr<StreamWriter>& stream)
307
0
{
308
0
  return Error::Ok;
309
0
}