| Josh Gao | 939fc19 | 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 |  | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 26 | enum class DecodeResult { | 
|  | 27 | Error, | 
|  | 28 | Done, | 
|  | 29 | NeedInput, | 
|  | 30 | MoreOutput, | 
|  | 31 | }; | 
|  | 32 |  | 
|  | 33 | enum class EncodeResult { | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 34 | Error, | 
|  | 35 | Done, | 
|  | 36 | NeedInput, | 
|  | 37 | MoreOutput, | 
|  | 38 | }; | 
|  | 39 |  | 
|  | 40 | struct 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 Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 48 | DecodeResult Decode(std::span<char>* output) { | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 49 | 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 Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 66 | return DecodeResult::Done; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 67 | case BROTLI_DECODER_RESULT_ERROR: | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 68 | return DecodeResult::Error; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 69 | 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 Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 73 | return DecodeResult::NeedInput; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 74 | case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 75 | return DecodeResult::MoreOutput; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 76 | } | 
|  | 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 Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 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 |  | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 98 | EncodeResult Encode(Block* output) { | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 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)) { | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 115 | return EncodeResult::Error; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 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_); | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 126 | return EncodeResult::Done; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 127 | } else if (output_bytes_left_ == 0) { | 
|  | 128 | *output = std::move(output_block_); | 
|  | 129 | output_block_.resize(OutputBlockSize); | 
|  | 130 | output_bytes_left_ = OutputBlockSize; | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 131 | return EncodeResult::MoreOutput; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 132 | } else if (input_buffer_.empty()) { | 
| Josh Gao | 6e697f2 | 2020-03-26 14:06:55 -0700 | [diff] [blame] | 133 | return EncodeResult::NeedInput; | 
| Josh Gao | 939fc19 | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 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 | }; |