Coverage Report

Created: 2026-09-14 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/compression_brotli.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2024 Brad Hards <bradh@frogmouth.net>
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 "compression.h"
22
23
#if HAVE_BROTLI
24
25
const size_t BUF_SIZE = (1 << 18);
26
#include <brotli/decode.h>
27
#include <brotli/encode.h>
28
#include <cstring>
29
#include <cstdio>
30
#include <string>
31
#include <vector>
32
33
#include "error.h"
34
#include "security_limits.h"
35
36
37
Result<std::vector<uint8_t>> decompress_brotli(const std::vector<uint8_t> &compressed_input,
38
                                               const heif_security_limits* limits)
39
1.19k
{
40
1.19k
    BrotliDecoderResult result = BROTLI_DECODER_RESULT_ERROR;
41
1.19k
    std::vector<uint8_t> buffer(BUF_SIZE, 0);
42
1.19k
    size_t available_in = compressed_input.size();
43
1.19k
    const std::uint8_t *next_in = reinterpret_cast<const std::uint8_t *>(compressed_input.data());
44
1.19k
    size_t available_out = buffer.size();
45
1.19k
    std::uint8_t *next_output = buffer.data();
46
47
1.19k
    std::unique_ptr<BrotliDecoderState, void(*)(BrotliDecoderState*)> state(BrotliDecoderCreateInstance(0, 0, 0), BrotliDecoderDestroyInstance);
48
49
1.19k
    std::vector<uint8_t> output;
50
51
    // Track the growth of `output` against the security limits. Without this, a
52
    // high-ratio "decompression bomb" would expand a few KB of input into GBs of
53
    // output, bypassing max_memory_block_size / max_total_memory (GHSA-24wx-9w62-c96w).
54
1.19k
    MemoryHandle output_memory_handle;
55
56
1.57k
    while (true)
57
1.57k
    {
58
1.57k
        result = BrotliDecoderDecompressStream(state.get(), &available_in, &next_in, &available_out, &next_output, 0);
59
60
1.57k
        if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT)
61
381
        {
62
381
            size_t n_new_bytes = static_cast<size_t>(std::distance(buffer.data(), next_output));
63
381
            if (Error memErr = output_memory_handle.alloc(n_new_bytes, limits, "brotli decompression output")) {
64
0
                return memErr;
65
0
            }
66
381
            output.insert(output.end(), buffer.data(), buffer.data() + n_new_bytes);
67
381
            available_out = buffer.size();
68
381
            next_output = buffer.data();
69
381
        }
70
1.19k
        else if (result == BROTLI_DECODER_RESULT_SUCCESS)
71
63
        {
72
63
            size_t n_new_bytes = static_cast<size_t>(std::distance(buffer.data(), next_output));
73
63
            if (Error memErr = output_memory_handle.alloc(n_new_bytes, limits, "brotli decompression output")) {
74
0
                return memErr;
75
0
            }
76
63
            output.insert(output.end(), buffer.data(), buffer.data() + n_new_bytes);
77
63
            break;
78
63
        }
79
1.12k
        else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT)
80
644
        {
81
644
            std::stringstream sstr;
82
644
            sstr << "Error performing brotli inflate - insufficient data.\n";
83
644
            return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str());
84
644
        }
85
484
        else if (result == BROTLI_DECODER_RESULT_ERROR)
86
484
        {
87
484
            const char* errorMessage = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(state.get()));
88
484
            std::stringstream sstr;
89
484
            sstr << "Error performing brotli inflate - " << errorMessage << "\n";
90
484
            return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str());
91
484
        }
92
0
        else
93
0
        {
94
0
            const char* errorMessage = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(state.get()));
95
0
            std::stringstream sstr;
96
0
            sstr << "Unknown error performing brotli inflate - " << errorMessage << "\n";
97
0
            return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str());
98
0
        }
99
1.57k
    }
100
101
63
    return output;
102
1.19k
}
103
104
105
std::vector<uint8_t> compress_brotli(const uint8_t* input, size_t size)
106
0
{
107
0
  std::unique_ptr<BrotliEncoderState, void(*)(BrotliEncoderState*)> state(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr), BrotliEncoderDestroyInstance);
108
109
0
  size_t available_in = size;
110
0
  const uint8_t* next_in = input;
111
112
0
  std::vector<uint8_t> tmp(BUF_SIZE);
113
0
  size_t available_out = BUF_SIZE;
114
0
  uint8_t* next_out = tmp.data();
115
116
0
  std::vector<uint8_t> result;
117
118
0
  for (;;) {
119
0
    BROTLI_BOOL success = BrotliEncoderCompressStream(state.get(),
120
0
                                                      BROTLI_OPERATION_FINISH,
121
0
                                                      &available_in,
122
0
                                                      &next_in,
123
0
                                                      &available_out,
124
0
                                                      &next_out,
125
0
                                                      nullptr);
126
0
    if (!success) {
127
0
      return {};
128
0
    }
129
130
0
    if (next_out != tmp.data()) {
131
0
      result.insert(result.end(), tmp.data(), tmp.data() + std::distance(tmp.data(), next_out));
132
0
      available_out = BUF_SIZE;
133
0
      next_out = tmp.data();
134
0
    }
135
136
0
    if (BrotliEncoderIsFinished(state.get())) {
137
0
      break;
138
0
    }
139
0
  }
140
141
0
  return result;
142
0
}
143
144
#endif