/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 | | |
35 | | |
36 | | Result<std::vector<uint8_t>> decompress_brotli(const std::vector<uint8_t> &compressed_input) |
37 | 0 | { |
38 | 0 | BrotliDecoderResult result = BROTLI_DECODER_RESULT_ERROR; |
39 | 0 | std::vector<uint8_t> buffer(BUF_SIZE, 0); |
40 | 0 | size_t available_in = compressed_input.size(); |
41 | 0 | const std::uint8_t *next_in = reinterpret_cast<const std::uint8_t *>(compressed_input.data()); |
42 | 0 | size_t available_out = buffer.size(); |
43 | 0 | std::uint8_t *next_output = buffer.data(); |
44 | |
|
45 | 0 | std::unique_ptr<BrotliDecoderState, void(*)(BrotliDecoderState*)> state(BrotliDecoderCreateInstance(0, 0, 0), BrotliDecoderDestroyInstance); |
46 | |
|
47 | 0 | std::vector<uint8_t> output; |
48 | |
|
49 | 0 | while (true) |
50 | 0 | { |
51 | 0 | result = BrotliDecoderDecompressStream(state.get(), &available_in, &next_in, &available_out, &next_output, 0); |
52 | |
|
53 | 0 | if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) |
54 | 0 | { |
55 | 0 | output.insert(output.end(), buffer.data(), buffer.data() + std::distance(buffer.data(), next_output)); |
56 | 0 | available_out = buffer.size(); |
57 | 0 | next_output = buffer.data(); |
58 | 0 | } |
59 | 0 | else if (result == BROTLI_DECODER_RESULT_SUCCESS) |
60 | 0 | { |
61 | 0 | output.insert(output.end(), buffer.data(), buffer.data() + std::distance(buffer.data(), next_output)); |
62 | 0 | break; |
63 | 0 | } |
64 | 0 | else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) |
65 | 0 | { |
66 | 0 | std::stringstream sstr; |
67 | 0 | sstr << "Error performing brotli inflate - insufficient data.\n"; |
68 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str()); |
69 | 0 | } |
70 | 0 | else if (result == BROTLI_DECODER_RESULT_ERROR) |
71 | 0 | { |
72 | 0 | const char* errorMessage = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(state.get())); |
73 | 0 | std::stringstream sstr; |
74 | 0 | sstr << "Error performing brotli inflate - " << errorMessage << "\n"; |
75 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str()); |
76 | 0 | } |
77 | 0 | else |
78 | 0 | { |
79 | 0 | const char* errorMessage = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(state.get())); |
80 | 0 | std::stringstream sstr; |
81 | 0 | sstr << "Unknown error performing brotli inflate - " << errorMessage << "\n"; |
82 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Decompression_invalid_data, sstr.str()); |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | 0 | return output; |
87 | 0 | } |
88 | | |
89 | | |
90 | | std::vector<uint8_t> compress_brotli(const uint8_t* input, size_t size) |
91 | 0 | { |
92 | 0 | std::unique_ptr<BrotliEncoderState, void(*)(BrotliEncoderState*)> state(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr), BrotliEncoderDestroyInstance); |
93 | |
|
94 | 0 | size_t available_in = size; |
95 | 0 | const uint8_t* next_in = input; |
96 | |
|
97 | 0 | std::vector<uint8_t> tmp(BUF_SIZE); |
98 | 0 | size_t available_out = BUF_SIZE; |
99 | 0 | uint8_t* next_out = tmp.data(); |
100 | |
|
101 | 0 | std::vector<uint8_t> result; |
102 | |
|
103 | 0 | for (;;) { |
104 | 0 | BROTLI_BOOL success = BrotliEncoderCompressStream(state.get(), |
105 | 0 | BROTLI_OPERATION_FINISH, |
106 | 0 | &available_in, |
107 | 0 | &next_in, |
108 | 0 | &available_out, |
109 | 0 | &next_out, |
110 | 0 | nullptr); |
111 | 0 | if (!success) { |
112 | 0 | return {}; |
113 | 0 | } |
114 | | |
115 | 0 | if (next_out != tmp.data()) { |
116 | 0 | result.insert(result.end(), tmp.data(), tmp.data() + std::distance(tmp.data(), next_out)); |
117 | 0 | available_out = BUF_SIZE; |
118 | 0 | next_out = tmp.data(); |
119 | 0 | } |
120 | |
|
121 | 0 | if (BrotliEncoderIsFinished(state.get())) { |
122 | 0 | break; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | return result; |
127 | 0 | } |
128 | | |
129 | | #endif |