Coverage Report

Created: 2025-07-16 07:53

/src/libjxl/lib/jxl/box_content_decoder.cc
Line
Count
Source (jump to first uncovered line)
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 <algorithm>
9
#include <cstddef>
10
#include <cstdint>
11
#include <cstring>
12
13
#include "lib/jxl/base/sanitizers.h"
14
15
namespace jxl {
16
17
1.96k
JxlBoxContentDecoder::JxlBoxContentDecoder() = default;
18
19
1.96k
JxlBoxContentDecoder::~JxlBoxContentDecoder() {
20
1.96k
  if (brotli_dec) {
21
49
    BrotliDecoderDestroyInstance(brotli_dec);
22
49
  }
23
1.96k
}
24
25
void JxlBoxContentDecoder::StartBox(bool brob_decode, bool box_until_eof,
26
6.76k
                                    size_t contents_size) {
27
6.76k
  if (brotli_dec) {
28
65
    BrotliDecoderDestroyInstance(brotli_dec);
29
65
    brotli_dec = nullptr;
30
65
  }
31
6.76k
  header_done_ = false;
32
6.76k
  brob_decode_ = brob_decode;
33
6.76k
  box_until_eof_ = box_until_eof;
34
6.76k
  remaining_ = box_until_eof ? 0 : contents_size;
35
6.76k
  pos_ = 0;
36
6.76k
}
37
38
JxlDecoderStatus JxlBoxContentDecoder::Process(const uint8_t* next_in,
39
                                               size_t avail_in, size_t box_pos,
40
                                               uint8_t** next_out,
41
5.33k
                                               size_t* avail_out) {
42
5.33k
  next_in += pos_ - box_pos;
43
5.33k
  avail_in -= pos_ - box_pos;
44
45
5.33k
  if (brob_decode_) {
46
3.54k
    if (!header_done_) {
47
114
      if (avail_in < 4) return JXL_DEC_NEED_MORE_INPUT;
48
114
      if (!box_until_eof_) {
49
98
        if (remaining_ < 4) return JXL_DEC_ERROR;
50
98
        remaining_ -= 4;
51
98
      }
52
114
      next_in += 4;
53
114
      avail_in -= 4;
54
114
      pos_ += 4;
55
114
      header_done_ = true;
56
114
    }
57
58
3.54k
    if (!brotli_dec) {
59
114
      brotli_dec = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
60
114
    }
61
62
3.54k
    const uint8_t* next_in_before = next_in;
63
3.54k
    uint8_t* next_out_before = *next_out;
64
3.54k
    msan::MemoryIsInitialized(next_in, avail_in);
65
3.54k
    BrotliDecoderResult res = BrotliDecoderDecompressStream(
66
3.54k
        brotli_dec, &avail_in, &next_in, avail_out, next_out, nullptr);
67
3.54k
    size_t consumed = next_in - next_in_before;
68
3.54k
    size_t produced = *next_out - next_out_before;
69
3.54k
    if (res == BROTLI_DECODER_RESULT_ERROR) {
70
24
      return JXL_DEC_ERROR;
71
24
    }
72
3.52k
    msan::UnpoisonMemory(next_out_before, produced);
73
3.52k
    pos_ += consumed;
74
3.52k
    if (!box_until_eof_) remaining_ -= consumed;
75
3.52k
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
76
52
      return JXL_DEC_NEED_MORE_INPUT;
77
52
    }
78
3.47k
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
79
3.44k
      return JXL_DEC_BOX_NEED_MORE_OUTPUT;
80
3.44k
    }
81
30
    if (res == BROTLI_DECODER_RESULT_SUCCESS) {
82
30
      return JXL_DEC_BOX_COMPLETE;
83
30
    }
84
    // unknown Brotli result
85
0
    return JXL_DEC_ERROR;
86
1.79k
  } else {
87
    // remaining box bytes as seen from dec->file_pos
88
1.79k
    size_t can_read = avail_in;
89
1.79k
    if (!box_until_eof_) can_read = std::min<size_t>(can_read, remaining_);
90
1.79k
    size_t to_write = std::min<size_t>(can_read, *avail_out);
91
1.79k
    memcpy(*next_out, next_in, to_write);
92
93
1.79k
    *next_out += to_write;
94
1.79k
    *avail_out -= to_write;
95
1.79k
    if (!box_until_eof_) remaining_ -= to_write;
96
1.79k
    pos_ += to_write;
97
98
1.79k
    if (to_write < can_read) return JXL_DEC_BOX_NEED_MORE_OUTPUT;
99
100
860
    if (!box_until_eof_ && remaining_ > 0) return JXL_DEC_NEED_MORE_INPUT;
101
102
857
    return JXL_DEC_BOX_COMPLETE;
103
860
  }
104
5.33k
}
105
106
}  // namespace jxl