Josh Gao | 50ab0a6 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | enum class BrotliDecodeResult { |
| 27 | Error, |
| 28 | Done, |
| 29 | NeedInput, |
| 30 | MoreOutput, |
| 31 | }; |
| 32 | |
| 33 | struct BrotliDecoder { |
| 34 | explicit BrotliDecoder(std::span<char> output_buffer) |
| 35 | : output_buffer_(output_buffer), |
| 36 | decoder_(BrotliDecoderCreateInstance(nullptr, nullptr, nullptr), |
| 37 | BrotliDecoderDestroyInstance) {} |
| 38 | |
| 39 | void Append(Block&& block) { input_buffer_.append(std::move(block)); } |
| 40 | |
| 41 | BrotliDecodeResult Decode(std::span<char>* output) { |
| 42 | size_t available_in = input_buffer_.front_size(); |
| 43 | const uint8_t* next_in = reinterpret_cast<const uint8_t*>(input_buffer_.front_data()); |
| 44 | |
| 45 | size_t available_out = output_buffer_.size(); |
| 46 | uint8_t* next_out = reinterpret_cast<uint8_t*>(output_buffer_.data()); |
| 47 | |
| 48 | BrotliDecoderResult r = BrotliDecoderDecompressStream( |
| 49 | decoder_.get(), &available_in, &next_in, &available_out, &next_out, nullptr); |
| 50 | |
| 51 | size_t bytes_consumed = input_buffer_.front_size() - available_in; |
| 52 | input_buffer_.drop_front(bytes_consumed); |
| 53 | |
| 54 | size_t bytes_emitted = output_buffer_.size() - available_out; |
| 55 | *output = std::span<char>(output_buffer_.data(), bytes_emitted); |
| 56 | |
| 57 | switch (r) { |
| 58 | case BROTLI_DECODER_RESULT_SUCCESS: |
| 59 | return BrotliDecodeResult::Done; |
| 60 | case BROTLI_DECODER_RESULT_ERROR: |
| 61 | return BrotliDecodeResult::Error; |
| 62 | case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: |
| 63 | // Brotli guarantees as one of its invariants that if it returns NEEDS_MORE_INPUT, |
| 64 | // it will consume the entire input buffer passed in, so we don't have to worry |
| 65 | // about bytes left over in the front block with more input remaining. |
| 66 | return BrotliDecodeResult::NeedInput; |
| 67 | case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: |
| 68 | return BrotliDecodeResult::MoreOutput; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | IOVector input_buffer_; |
| 74 | std::span<char> output_buffer_; |
| 75 | std::unique_ptr<BrotliDecoderState, void (*)(BrotliDecoderState*)> decoder_; |
| 76 | }; |
| 77 | |
| 78 | enum class BrotliEncodeResult { |
| 79 | Error, |
| 80 | Done, |
| 81 | NeedInput, |
| 82 | MoreOutput, |
| 83 | }; |
| 84 | |
| 85 | template <size_t OutputBlockSize> |
| 86 | struct 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 | |
| 98 | BrotliEncodeResult Encode(Block* output) { |
| 99 | 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)) { |
| 115 | return BrotliEncodeResult::Error; |
| 116 | } |
| 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_); |
| 126 | return BrotliEncodeResult::Done; |
| 127 | } else if (output_bytes_left_ == 0) { |
| 128 | *output = std::move(output_block_); |
| 129 | output_block_.resize(OutputBlockSize); |
| 130 | output_bytes_left_ = OutputBlockSize; |
| 131 | return BrotliEncodeResult::MoreOutput; |
| 132 | } else if (input_buffer_.empty()) { |
| 133 | return BrotliEncodeResult::NeedInput; |
| 134 | } |
| 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 | }; |