Coverage Report

Created: 2026-02-14 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/box_content_decoder.cc
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/jxl/box_content_decoder.h"
7
8
#include <brotli/decode.h>
9
#include <jxl/decode.h>
10
11
#include <algorithm>
12
#include <cstddef>
13
#include <cstdint>
14
#include <cstring>
15
16
#include "lib/jxl/base/sanitizers.h"
17
18
namespace jxl {
19
20
28.5k
JxlBoxContentDecoder::JxlBoxContentDecoder() = default;
21
22
28.5k
JxlBoxContentDecoder::~JxlBoxContentDecoder() {
23
28.5k
  if (brotli_dec) {
24
0
    BrotliDecoderDestroyInstance(brotli_dec);
25
0
  }
26
28.5k
}
27
28
void JxlBoxContentDecoder::StartBox(bool brob_decode, bool box_until_eof,
29
70.5k
                                    size_t contents_size) {
30
70.5k
  if (brotli_dec) {
31
0
    BrotliDecoderDestroyInstance(brotli_dec);
32
0
    brotli_dec = nullptr;
33
0
  }
34
70.5k
  header_done_ = false;
35
70.5k
  brob_decode_ = brob_decode;
36
70.5k
  box_until_eof_ = box_until_eof;
37
70.5k
  remaining_ = box_until_eof ? 0 : contents_size;
38
70.5k
  pos_ = 0;
39
70.5k
}
40
41
JxlDecoderStatus JxlBoxContentDecoder::Process(const uint8_t* next_in,
42
                                               size_t avail_in, size_t box_pos,
43
                                               uint8_t** next_out,
44
148
                                               size_t* avail_out) {
45
  // The caller provides `box_pos` as the current position (in bytes) within
46
  // the box contents corresponding to `next_in`. Our internal `pos_` tracks
47
  // how many bytes of box contents have been consumed/processed so far.
48
  // If `box_pos` ever exceeds `pos_`, adjusting by `pos_ - box_pos` would
49
  // underflow and cause out-of-bounds reads.
50
148
  if (box_pos > pos_) return JXL_DEC_ERROR;
51
148
  const size_t offset = pos_ - box_pos;
52
148
  if (offset > avail_in) return JXL_DEC_ERROR;
53
148
  next_in += offset;
54
148
  avail_in -= offset;
55
56
148
  if (brob_decode_) {
57
0
    if (!header_done_) {
58
0
      if (avail_in < 4) return JXL_DEC_NEED_MORE_INPUT;
59
0
      if (!box_until_eof_) {
60
0
        if (remaining_ < 4) return JXL_DEC_ERROR;
61
0
        remaining_ -= 4;
62
0
      }
63
0
      next_in += 4;
64
0
      avail_in -= 4;
65
0
      pos_ += 4;
66
0
      header_done_ = true;
67
0
    }
68
69
0
    if (!brotli_dec) {
70
0
      brotli_dec = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
71
0
    }
72
73
0
    const uint8_t* next_in_before = next_in;
74
0
    uint8_t* next_out_before = *next_out;
75
0
    msan::MemoryIsInitialized(next_in, avail_in);
76
0
    BrotliDecoderResult res = BrotliDecoderDecompressStream(
77
0
        brotli_dec, &avail_in, &next_in, avail_out, next_out, nullptr);
78
0
    size_t consumed = next_in - next_in_before;
79
0
    size_t produced = *next_out - next_out_before;
80
0
    if (res == BROTLI_DECODER_RESULT_ERROR) {
81
0
      return JXL_DEC_ERROR;
82
0
    }
83
0
    msan::UnpoisonMemory(next_out_before, produced);
84
0
    pos_ += consumed;
85
0
    if (!box_until_eof_) remaining_ -= consumed;
86
0
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
87
0
      return JXL_DEC_NEED_MORE_INPUT;
88
0
    }
89
0
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
90
0
      return JXL_DEC_BOX_NEED_MORE_OUTPUT;
91
0
    }
92
0
    if (res == BROTLI_DECODER_RESULT_SUCCESS) {
93
0
      return JXL_DEC_BOX_COMPLETE;
94
0
    }
95
    // unknown Brotli result
96
0
    return JXL_DEC_ERROR;
97
148
  } else {
98
    // remaining box bytes as seen from dec->file_pos
99
148
    size_t can_read = avail_in;
100
148
    if (!box_until_eof_) can_read = std::min<size_t>(can_read, remaining_);
101
148
    size_t to_write = std::min<size_t>(can_read, *avail_out);
102
148
    memcpy(*next_out, next_in, to_write);
103
104
148
    *next_out += to_write;
105
148
    *avail_out -= to_write;
106
148
    if (!box_until_eof_) remaining_ -= to_write;
107
148
    pos_ += to_write;
108
109
148
    if (to_write < can_read) return JXL_DEC_BOX_NEED_MORE_OUTPUT;
110
111
148
    if (!box_until_eof_ && remaining_ > 0) return JXL_DEC_NEED_MORE_INPUT;
112
113
102
    return JXL_DEC_BOX_COMPLETE;
114
148
  }
115
148
}
116
117
}  // namespace jxl