|
| 1 | +// Copyright (c) 2022-present INESC-ID. |
| 2 | +// Distributed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +#![allow(non_snake_case)] |
| 5 | + |
| 6 | +use crate::{ByteRepr, Ptr}; |
| 7 | +use brotli::enc::backward_references::BrotliEncoderMode; |
| 8 | +use brotli::enc::{BrotliCompress, BrotliEncoderParams, StandardAlloc}; |
| 9 | +use brotli_decompressor::{BrotliDecompress, BrotliDecompressStream, BrotliResult}; |
| 10 | + |
| 11 | +const BROTLI_DECODER_RESULT_ERROR: u32 = 0; |
| 12 | +const BROTLI_DECODER_RESULT_SUCCESS: u32 = 1; |
| 13 | +const BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: u32 = 2; |
| 14 | +const BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: u32 = 3; |
| 15 | + |
| 16 | +pub struct BrotliDecoderState { |
| 17 | + pub state: brotli_decompressor::BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>, |
| 18 | + taken: Ptr<u8>, |
| 19 | +} |
| 20 | + |
| 21 | +impl ByteRepr for BrotliDecoderState {} |
| 22 | + |
| 23 | +impl Default for BrotliDecoderState { |
| 24 | + fn default() -> Self { |
| 25 | + Self { |
| 26 | + state: brotli_decompressor::BrotliState::new_strict( |
| 27 | + StandardAlloc::default(), |
| 28 | + StandardAlloc::default(), |
| 29 | + StandardAlloc::default(), |
| 30 | + ), |
| 31 | + taken: Ptr::null(), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl BrotliDecoderState { |
| 37 | + fn release_taken(&mut self) { |
| 38 | + self.taken.delete(); |
| 39 | + self.taken = Ptr::null(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub fn BrotliEncoderCompress( |
| 44 | + quality: i32, |
| 45 | + lgwin: i32, |
| 46 | + mode: u32, |
| 47 | + input_size: usize, |
| 48 | + input_buffer: Ptr<u8>, |
| 49 | + encoded_size: Ptr<usize>, |
| 50 | + encoded_buffer: Ptr<u8>, |
| 51 | +) -> i32 { |
| 52 | + let params = BrotliEncoderParams { |
| 53 | + quality, |
| 54 | + lgwin, |
| 55 | + mode: match mode { |
| 56 | + 1 => BrotliEncoderMode::BROTLI_MODE_TEXT, |
| 57 | + 2 => BrotliEncoderMode::BROTLI_MODE_FONT, |
| 58 | + _ => BrotliEncoderMode::BROTLI_MODE_GENERIC, |
| 59 | + }, |
| 60 | + size_hint: input_size, |
| 61 | + large_window: lgwin > 24, |
| 62 | + ..Default::default() |
| 63 | + }; |
| 64 | + let input = input_buffer.with_slice(input_size, |s| s.to_vec()); |
| 65 | + let mut out = Vec::new(); |
| 66 | + match BrotliCompress(&mut input.as_slice(), &mut out, ¶ms) { |
| 67 | + Ok(_) if out.len() <= encoded_size.read() => { |
| 68 | + encoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out)); |
| 69 | + encoded_size.write(out.len()); |
| 70 | + 1 |
| 71 | + } |
| 72 | + _ => { |
| 73 | + encoded_size.write(0); |
| 74 | + 0 |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +pub fn BrotliDecoderDecompress( |
| 80 | + encoded_size: usize, |
| 81 | + encoded_buffer: Ptr<u8>, |
| 82 | + decoded_size: Ptr<usize>, |
| 83 | + decoded_buffer: Ptr<u8>, |
| 84 | +) -> u32 { |
| 85 | + let input = encoded_buffer.with_slice(encoded_size, |s| s.to_vec()); |
| 86 | + let mut out = Vec::new(); |
| 87 | + match BrotliDecompress(&mut input.as_slice(), &mut out) { |
| 88 | + Ok(()) if out.len() <= decoded_size.read() => { |
| 89 | + decoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out)); |
| 90 | + decoded_size.write(out.len()); |
| 91 | + BROTLI_DECODER_RESULT_SUCCESS |
| 92 | + } |
| 93 | + _ => BROTLI_DECODER_RESULT_ERROR, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub fn BrotliDecoderDestroyInstance(state: Ptr<BrotliDecoderState>) { |
| 98 | + if state.is_null() { |
| 99 | + return; |
| 100 | + } |
| 101 | + state.with_mut(|s| s.release_taken()); |
| 102 | + state.delete(); |
| 103 | +} |
| 104 | + |
| 105 | +pub fn BrotliDecoderDecompressStream( |
| 106 | + state: Ptr<BrotliDecoderState>, |
| 107 | + available_in: Ptr<usize>, |
| 108 | + next_in: Ptr<Ptr<u8>>, |
| 109 | + available_out: Ptr<usize>, |
| 110 | + next_out: Ptr<Ptr<u8>>, |
| 111 | + total_out: Ptr<usize>, |
| 112 | +) -> u32 { |
| 113 | + let in_ptr = next_in.read(); |
| 114 | + let input = in_ptr.with_slice(available_in.read(), |s| s.to_vec()); |
| 115 | + let out_ptr = if next_out.is_null() { |
| 116 | + Ptr::null() |
| 117 | + } else { |
| 118 | + next_out.read() |
| 119 | + }; |
| 120 | + let mut out = vec![ |
| 121 | + 0u8; |
| 122 | + if out_ptr.is_null() { |
| 123 | + 0 |
| 124 | + } else { |
| 125 | + available_out.read() |
| 126 | + } |
| 127 | + ]; |
| 128 | + let mut avail_in = input.len(); |
| 129 | + let mut avail_out = available_out.read(); |
| 130 | + let mut input_offset = 0; |
| 131 | + let mut output_offset = 0; |
| 132 | + let mut total = 0; |
| 133 | + let result = state.with_mut(|s| { |
| 134 | + s.release_taken(); |
| 135 | + BrotliDecompressStream( |
| 136 | + &mut avail_in, |
| 137 | + &mut input_offset, |
| 138 | + &input, |
| 139 | + &mut avail_out, |
| 140 | + &mut output_offset, |
| 141 | + &mut out, |
| 142 | + &mut total, |
| 143 | + &mut s.state, |
| 144 | + ) |
| 145 | + }); |
| 146 | + if output_offset > 0 { |
| 147 | + out_ptr.with_slice_mut(output_offset, |d| d.copy_from_slice(&out[..output_offset])); |
| 148 | + next_out.write(out_ptr.offset(output_offset)); |
| 149 | + } |
| 150 | + if input_offset > 0 { |
| 151 | + next_in.write(in_ptr.offset(input_offset)); |
| 152 | + } |
| 153 | + available_in.write(avail_in); |
| 154 | + available_out.write(avail_out); |
| 155 | + if !total_out.is_null() { |
| 156 | + total_out.write(total); |
| 157 | + } |
| 158 | + match result { |
| 159 | + BrotliResult::ResultSuccess => BROTLI_DECODER_RESULT_SUCCESS, |
| 160 | + BrotliResult::NeedsMoreInput => BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT, |
| 161 | + BrotliResult::NeedsMoreOutput => BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT, |
| 162 | + BrotliResult::ResultFailure => BROTLI_DECODER_RESULT_ERROR, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +pub fn BrotliDecoderTakeOutput(state: Ptr<BrotliDecoderState>, size: Ptr<usize>) -> Ptr<u8> { |
| 167 | + let mut n = size.read(); |
| 168 | + let taken = state.with_mut(|s| { |
| 169 | + s.release_taken(); |
| 170 | + let out = brotli_decompressor::BrotliDecoderTakeOutput(&mut s.state, &mut n); |
| 171 | + s.taken = Ptr::alloc_array(out.to_vec().into_boxed_slice()); |
| 172 | + s.taken.clone() |
| 173 | + }); |
| 174 | + size.write(n); |
| 175 | + taken |
| 176 | +} |
| 177 | + |
| 178 | +pub fn BrotliDecoderVersion() -> u32 { |
| 179 | + brotli::enc::encode::BrotliEncoderVersion() |
| 180 | +} |
0 commit comments