blob: c445095ca9ebb12d163403d37ccd61c54d46b29f [file] [log] [blame]
Josh Gao939fc192020-03-04 19:34:08 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <span>
20
21#include <brotli/decode.h>
22#include <brotli/encode.h>
23
24#include "types.h"
25
Josh Gao6e697f22020-03-26 14:06:55 -070026enum class DecodeResult {
27 Error,
28 Done,
29 NeedInput,
30 MoreOutput,
31};
32
33enum class EncodeResult {
Josh Gao939fc192020-03-04 19:34:08 -080034 Error,
35 Done,
36 NeedInput,
37 MoreOutput,
38};
39
40struct BrotliDecoder {
41 explicit BrotliDecoder(std::span<char> output_buffer)
42 : output_buffer_(output_buffer),
43 decoder_(BrotliDecoderCreateInstance(nullptr, nullptr, nullptr),
44 BrotliDecoderDestroyInstance) {}
45
46 void Append(Block&& block) { input_buffer_.append(std::move(block)); }
47
Josh Gao6e697f22020-03-26 14:06:55 -070048 DecodeResult Decode(std::span<char>* output) {
Josh Gao939fc192020-03-04 19:34:08 -080049 size_t available_in = input_buffer_.front_size();
50 const uint8_t* next_in = reinterpret_cast<const uint8_t*>(input_buffer_.front_data());
51
52 size_t available_out = output_buffer_.size();
53 uint8_t* next_out = reinterpret_cast<uint8_t*>(output_buffer_.data());
54
55 BrotliDecoderResult r = BrotliDecoderDecompressStream(
56 decoder_.get(), &available_in, &next_in, &available_out, &next_out, nullptr);
57
58 size_t bytes_consumed = input_buffer_.front_size() - available_in;
59 input_buffer_.drop_front(bytes_consumed);
60
61 size_t bytes_emitted = output_buffer_.size() - available_out;
62 *output = std::span<char>(output_buffer_.data(), bytes_emitted);
63
64 switch (r) {
65 case BROTLI_DECODER_RESULT_SUCCESS:
Josh Gao6e697f22020-03-26 14:06:55 -070066 return DecodeResult::Done;
Josh Gao939fc192020-03-04 19:34:08 -080067 case BROTLI_DECODER_RESULT_ERROR:
Josh Gao6e697f22020-03-26 14:06:55 -070068 return DecodeResult::Error;
Josh Gao939fc192020-03-04 19:34:08 -080069 case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
70 // Brotli guarantees as one of its invariants that if it returns NEEDS_MORE_INPUT,
71 // it will consume the entire input buffer passed in, so we don't have to worry
72 // about bytes left over in the front block with more input remaining.
Josh Gao6e697f22020-03-26 14:06:55 -070073 return DecodeResult::NeedInput;
Josh Gao939fc192020-03-04 19:34:08 -080074 case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
Josh Gao6e697f22020-03-26 14:06:55 -070075 return DecodeResult::MoreOutput;
Josh Gao939fc192020-03-04 19:34:08 -080076 }
77 }
78
79 private:
80 IOVector input_buffer_;
81 std::span<char> output_buffer_;
82 std::unique_ptr<BrotliDecoderState, void (*)(BrotliDecoderState*)> decoder_;
83};
84
Josh Gao939fc192020-03-04 19:34:08 -080085template <size_t OutputBlockSize>
86struct BrotliEncoder {
87 explicit BrotliEncoder()
88 : output_block_(OutputBlockSize),
89 output_bytes_left_(OutputBlockSize),
90 encoder_(BrotliEncoderCreateInstance(nullptr, nullptr, nullptr),
91 BrotliEncoderDestroyInstance) {
92 BrotliEncoderSetParameter(encoder_.get(), BROTLI_PARAM_QUALITY, 1);
93 }
94
95 void Append(Block input) { input_buffer_.append(std::move(input)); }
96 void Finish() { finished_ = true; }
97
Josh Gao6e697f22020-03-26 14:06:55 -070098 EncodeResult Encode(Block* output) {
Josh Gao939fc192020-03-04 19:34:08 -080099 output->clear();
100 while (true) {
101 size_t available_in = input_buffer_.front_size();
102 const uint8_t* next_in = reinterpret_cast<const uint8_t*>(input_buffer_.front_data());
103
104 size_t available_out = output_bytes_left_;
105 uint8_t* next_out = reinterpret_cast<uint8_t*>(output_block_.data() +
106 (OutputBlockSize - output_bytes_left_));
107
108 BrotliEncoderOperation op = BROTLI_OPERATION_PROCESS;
109 if (finished_) {
110 op = BROTLI_OPERATION_FINISH;
111 }
112
113 if (!BrotliEncoderCompressStream(encoder_.get(), op, &available_in, &next_in,
114 &available_out, &next_out, nullptr)) {
Josh Gao6e697f22020-03-26 14:06:55 -0700115 return EncodeResult::Error;
Josh Gao939fc192020-03-04 19:34:08 -0800116 }
117
118 size_t bytes_consumed = input_buffer_.front_size() - available_in;
119 input_buffer_.drop_front(bytes_consumed);
120
121 output_bytes_left_ = available_out;
122
123 if (BrotliEncoderIsFinished(encoder_.get())) {
124 output_block_.resize(OutputBlockSize - output_bytes_left_);
125 *output = std::move(output_block_);
Josh Gao6e697f22020-03-26 14:06:55 -0700126 return EncodeResult::Done;
Josh Gao939fc192020-03-04 19:34:08 -0800127 } else if (output_bytes_left_ == 0) {
128 *output = std::move(output_block_);
129 output_block_.resize(OutputBlockSize);
130 output_bytes_left_ = OutputBlockSize;
Josh Gao6e697f22020-03-26 14:06:55 -0700131 return EncodeResult::MoreOutput;
Josh Gao939fc192020-03-04 19:34:08 -0800132 } else if (input_buffer_.empty()) {
Josh Gao6e697f22020-03-26 14:06:55 -0700133 return EncodeResult::NeedInput;
Josh Gao939fc192020-03-04 19:34:08 -0800134 }
135 }
136 }
137
138 private:
139 bool finished_ = false;
140 IOVector input_buffer_;
141 Block output_block_;
142 size_t output_bytes_left_;
143 std::unique_ptr<BrotliEncoderState, void (*)(BrotliEncoderState*)> encoder_;
144};