Coverage Report

Created: 2025-09-08 07:52

/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
2.21k
JxlBoxContentDecoder::JxlBoxContentDecoder() = default;
18
19
2.21k
JxlBoxContentDecoder::~JxlBoxContentDecoder() {
20
2.21k
  if (brotli_dec) {
21
39
    BrotliDecoderDestroyInstance(brotli_dec);
22
39
  }
23
2.21k
}
24
25
void JxlBoxContentDecoder::StartBox(bool brob_decode, bool box_until_eof,
26
5.19k
                                    size_t contents_size) {
27
5.19k
  if (brotli_dec) {
28
69
    BrotliDecoderDestroyInstance(brotli_dec);
29
69
    brotli_dec = nullptr;
30
69
  }
31
5.19k
  header_done_ = false;
32
5.19k
  brob_decode_ = brob_decode;
33
5.19k
  box_until_eof_ = box_until_eof;
34
5.19k
  remaining_ = box_until_eof ? 0 : contents_size;
35
5.19k
  pos_ = 0;
36
5.19k
}
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
6.18k
                                               size_t* avail_out) {
42
6.18k
  next_in += pos_ - box_pos;
43
6.18k
  avail_in -= pos_ - box_pos;
44
45
6.18k
  if (brob_decode_) {
46
4.08k
    if (!header_done_) {
47
108
      if (avail_in < 4) return JXL_DEC_NEED_MORE_INPUT;
48
108
      if (!box_until_eof_) {
49
104
        if (remaining_ < 4) return JXL_DEC_ERROR;
50
104
        remaining_ -= 4;
51
104
      }
52
108
      next_in += 4;
53
108
      avail_in -= 4;
54
108
      pos_ += 4;
55
108
      header_done_ = true;
56
108
    }
57
58
4.08k
    if (!brotli_dec) {
59
108
      brotli_dec = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
60
108
    }
61
62
4.08k
    const uint8_t* next_in_before = next_in;
63
4.08k
    uint8_t* next_out_before = *next_out;
64
4.08k
    msan::MemoryIsInitialized(next_in, avail_in);
65
4.08k
    BrotliDecoderResult res = BrotliDecoderDecompressStream(
66
4.08k
        brotli_dec, &avail_in, &next_in, avail_out, next_out, nullptr);
67
4.08k
    size_t consumed = next_in - next_in_before;
68
4.08k
    size_t produced = *next_out - next_out_before;
69
4.08k
    if (res == BROTLI_DECODER_RESULT_ERROR) {
70
25
      return JXL_DEC_ERROR;
71
25
    }
72
4.05k
    msan::UnpoisonMemory(next_out_before, produced);
73
4.05k
    pos_ += consumed;
74
4.05k
    if (!box_until_eof_) remaining_ -= consumed;
75
4.05k
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
76
54
      return JXL_DEC_NEED_MORE_INPUT;
77
54
    }
78
4.00k
    if (res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
79
3.98k
      return JXL_DEC_BOX_NEED_MORE_OUTPUT;
80
3.98k
    }
81
22
    if (res == BROTLI_DECODER_RESULT_SUCCESS) {
82
22
      return JXL_DEC_BOX_COMPLETE;
83
22
    }
84
    // unknown Brotli result
85
0
    return JXL_DEC_ERROR;
86
2.09k
  } else {
87
    // remaining box bytes as seen from dec->file_pos
88
2.09k
    size_t can_read = avail_in;
89
2.09k
    if (!box_until_eof_) can_read = std::min<size_t>(can_read, remaining_);
90
2.09k
    size_t to_write = std::min<size_t>(can_read, *avail_out);
91
2.09k
    memcpy(*next_out, next_in, to_write);
92
93
2.09k
    *next_out += to_write;
94
2.09k
    *avail_out -= to_write;
95
2.09k
    if (!box_until_eof_) remaining_ -= to_write;
96
2.09k
    pos_ += to_write;
97
98
2.09k
    if (to_write < can_read) return JXL_DEC_BOX_NEED_MORE_OUTPUT;
99
100
1.00k
    if (!box_until_eof_ && remaining_ > 0) return JXL_DEC_NEED_MORE_INPUT;
101
102
1.00k
    return JXL_DEC_BOX_COMPLETE;
103
1.00k
  }
104
6.18k
}
105
106
}  // namespace jxl