Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libcc2rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ libc = "0.2"
nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }
jiff = "0.2"
sprintf = "0.4"
brotli = "8"
brotli-decompressor = "5"
2 changes: 2 additions & 0 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ impl<T: CChar> Ptr<T> {
pub fn with_slice_mut<R>(&self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
let off = self.offset;
match &self.kind {
PtrKind::Null if len == 0 => f(&mut []),
PtrKind::Null => panic!("ub: null pointer"),
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
assert!(off == 0 && len <= 1, "ub: with_slice_mut out of bounds");
Expand Down Expand Up @@ -1036,6 +1037,7 @@ impl<T: CChar> Ptr<T> {
pub fn with_slice<R>(&self, len: usize, f: impl FnOnce(&[T]) -> R) -> R {
let off = self.offset;
match &self.kind {
PtrKind::Null if len == 0 => f(&[]),
PtrKind::Null => panic!("ub: null pointer"),
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
assert!(off == 0 && len <= 1, "ub: with_slice out of bounds");
Expand Down
180 changes: 180 additions & 0 deletions rules/brotli/shim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#![allow(non_snake_case)]

use crate::{ByteRepr, Ptr};
use brotli::enc::backward_references::BrotliEncoderMode;
use brotli::enc::{BrotliCompress, BrotliEncoderParams, StandardAlloc};
use brotli_decompressor::{BrotliDecompress, BrotliDecompressStream, BrotliResult};

const BROTLI_DECODER_RESULT_ERROR: u32 = 0;
const BROTLI_DECODER_RESULT_SUCCESS: u32 = 1;
const BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: u32 = 2;
const BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: u32 = 3;

pub struct BrotliDecoderState {
pub state: brotli_decompressor::BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>,
taken: Ptr<u8>,
}

impl ByteRepr for BrotliDecoderState {}

impl Default for BrotliDecoderState {
fn default() -> Self {
Self {
state: brotli_decompressor::BrotliState::new_strict(
StandardAlloc::default(),
StandardAlloc::default(),
StandardAlloc::default(),
),
taken: Ptr::null(),
}
}
}

impl BrotliDecoderState {
fn release_taken(&mut self) {
self.taken.delete();
self.taken = Ptr::null();
}
}

pub fn BrotliEncoderCompress(
quality: i32,
lgwin: i32,
mode: u32,
input_size: usize,
input_buffer: Ptr<u8>,
encoded_size: Ptr<usize>,
encoded_buffer: Ptr<u8>,
) -> i32 {
let params = BrotliEncoderParams {
quality,
lgwin,
mode: match mode {
1 => BrotliEncoderMode::BROTLI_MODE_TEXT,
2 => BrotliEncoderMode::BROTLI_MODE_FONT,
_ => BrotliEncoderMode::BROTLI_MODE_GENERIC,
},
size_hint: input_size,
large_window: lgwin > 24,
..Default::default()
};
let input = input_buffer.with_slice(input_size, |s| s.to_vec());
let mut out = Vec::new();
match BrotliCompress(&mut input.as_slice(), &mut out, &params) {
Ok(_) if out.len() <= encoded_size.read() => {
encoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out));
encoded_size.write(out.len());
1
}
_ => {
encoded_size.write(0);
0
}
}
}

pub fn BrotliDecoderDecompress(
encoded_size: usize,
encoded_buffer: Ptr<u8>,
decoded_size: Ptr<usize>,
decoded_buffer: Ptr<u8>,
) -> u32 {
let input = encoded_buffer.with_slice(encoded_size, |s| s.to_vec());
let mut out = Vec::new();
match BrotliDecompress(&mut input.as_slice(), &mut out) {
Ok(()) if out.len() <= decoded_size.read() => {
decoded_buffer.with_slice_mut(out.len(), |d| d.copy_from_slice(&out));
decoded_size.write(out.len());
BROTLI_DECODER_RESULT_SUCCESS
}
_ => BROTLI_DECODER_RESULT_ERROR,
}
}

pub fn BrotliDecoderDestroyInstance(state: Ptr<BrotliDecoderState>) {
if state.is_null() {
return;
}
state.with_mut(|s| s.release_taken());
state.delete();
}

pub fn BrotliDecoderDecompressStream(
state: Ptr<BrotliDecoderState>,
available_in: Ptr<usize>,
next_in: Ptr<Ptr<u8>>,
available_out: Ptr<usize>,
next_out: Ptr<Ptr<u8>>,
total_out: Ptr<usize>,
) -> u32 {
let in_ptr = next_in.read();
let input = in_ptr.with_slice(available_in.read(), |s| s.to_vec());
let out_ptr = if next_out.is_null() {
Ptr::null()
} else {
next_out.read()
};
let mut out = vec![
0u8;
if out_ptr.is_null() {
0
} else {
available_out.read()
}
];
let mut avail_in = input.len();
let mut avail_out = available_out.read();
let mut input_offset = 0;
let mut output_offset = 0;
let mut total = 0;
let result = state.with_mut(|s| {
s.release_taken();
BrotliDecompressStream(
&mut avail_in,
&mut input_offset,
&input,
&mut avail_out,
&mut output_offset,
&mut out,
&mut total,
&mut s.state,
)
});
if output_offset > 0 {
out_ptr.with_slice_mut(output_offset, |d| d.copy_from_slice(&out[..output_offset]));
next_out.write(out_ptr.offset(output_offset));
}
if input_offset > 0 {
next_in.write(in_ptr.offset(input_offset));
}
available_in.write(avail_in);
available_out.write(avail_out);
if !total_out.is_null() {
total_out.write(total);
}
match result {
BrotliResult::ResultSuccess => BROTLI_DECODER_RESULT_SUCCESS,
BrotliResult::NeedsMoreInput => BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT,
BrotliResult::NeedsMoreOutput => BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT,
BrotliResult::ResultFailure => BROTLI_DECODER_RESULT_ERROR,
}
}

pub fn BrotliDecoderTakeOutput(state: Ptr<BrotliDecoderState>, size: Ptr<usize>) -> Ptr<u8> {
let mut n = size.read();
let taken = state.with_mut(|s| {
s.release_taken();
let out = brotli_decompressor::BrotliDecoderTakeOutput(&mut s.state, &mut n);
s.taken = Ptr::alloc_array(out.to_vec().into_boxed_slice());
s.taken.clone()
});
size.write(n);
taken
}

pub fn BrotliDecoderVersion() -> u32 {
brotli::enc::encode::BrotliEncoderVersion()
}
105 changes: 35 additions & 70 deletions rules/brotli/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,70 @@
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;
use std::cell::RefCell;
use std::rc::Rc;

fn t3() -> Ptr<BrotliDecoderState> {
Ptr::null()
}

fn t4() -> Ptr<BrotliDecoderState> {
Ptr::null()
}

fn t6() -> BrotliDecoderState {
Default::default()
}

fn t7() -> BrotliDecoderState {
Default::default()
}

fn f2(
a0: libc::c_int,
a1: libc::c_int,
a2: brotli_sys::BrotliEncoderMode,
a2: ::brotli_sys::BrotliEncoderMode,
a3: usize,
a4: Ptr<u8>,
a5: Ptr<usize>,
a6: Ptr<u8>,
) -> libc::c_int {
// Compress into a buffer bounded by the input size, as the output buffer
// may be much larger and is expensive to borrow if reinterpreted.
let mut __out_len = a5.read();
let __max = unsafe { ::brotli_sys::BrotliEncoderMaxCompressedSize(a3) };
let mut __out = vec![0u8; if __max == 0 { __out_len } else { __out_len.min(__max) }];
__out_len = __out.len();
let __ok = a4.with_slice(a3, |__in| unsafe {
::brotli_sys::BrotliEncoderCompress(
a0,
a1,
a2,
a3,
__in.as_ptr(),
&mut __out_len,
__out.as_mut_ptr(),
)
});
if __ok != 0 {
a6.with_slice_mut(__out_len, |__s| __s.copy_from_slice(&__out[..__out_len]));
a5.write(__out_len);
}
__ok
BrotliEncoderCompress(a0, a1, a2, a3, a4, a5, a6)
}

fn f5(a0: usize, a1: Ptr<u8>, a2: Ptr<usize>, a3: Ptr<u8>) -> ::brotli_sys::BrotliDecoderResult {
let __out_len = a2.read();
a1.with_slice(a0, |__in| {
a2.with_mut(|_v2| {
a3.with_slice_mut(__out_len, |__out| unsafe {
::brotli_sys::BrotliDecoderDecompress(
a0,
__in.as_ptr(),
_v2 as *mut usize,
__out.as_mut_ptr(),
)
})
})
})
BrotliDecoderDecompress(a0, a1, a2, a3)
}

fn f6(
a0: ::brotli_sys::brotli_alloc_func,
a1: ::brotli_sys::brotli_free_func,
a2: *mut std::ffi::c_void,
) -> *mut ::brotli_sys::BrotliDecoderState {
unsafe { ::brotli_sys::BrotliDecoderCreateInstance(None, None, std::ptr::null_mut()) }
) -> Ptr<BrotliDecoderState> {
Ptr::alloc(BrotliDecoderState::default())
}

fn f7(a0: *mut ::brotli_sys::BrotliDecoderState) {
unsafe { ::brotli_sys::BrotliDecoderDestroyInstance(a0) }
fn f7(a0: Ptr<BrotliDecoderState>) {
BrotliDecoderDestroyInstance(a0)
}

fn f8(
a0: *mut ::brotli_sys::BrotliDecoderState,
a0: Ptr<BrotliDecoderState>,
a1: Ptr<usize>,
a2: Ptr<Ptr<u8>>,
a3: Ptr<usize>,
a4: Ptr<Ptr<u8>>,
a5: Ptr<usize>,
) -> ::brotli_sys::BrotliDecoderResult {
let __in = a2.read();
let __in_len = a1.read();
let __r = __in.with_slice(__in_len, |__s| {
a1.with_mut(|_v1| {
a3.with_mut(|_v3| unsafe {
::brotli_sys::BrotliDecoderDecompressStream(
a0,
_v1,
&mut __s.as_ptr(),
_v3,
std::ptr::null_mut(),
std::ptr::null_mut(),
)
})
})
});
a2.write(__in.offset(__in_len - a1.read()));
__r
BrotliDecoderDecompressStream(a0, a1, a2, a3, a4, a5)
}

fn f9(a0: Ptr<BrotliDecoderState>, a1: Ptr<usize>) -> Ptr<u8> {
BrotliDecoderTakeOutput(a0, a1)
}

fn f35(a0: Ptr<BrotliDecoderState>) -> ::brotli_sys::BrotliDecoderErrorCode {
a0.with(|__s| __s.state.error_code as i32)
}

fn f9(a0: *mut ::brotli_sys::BrotliDecoderState, a1: Ptr<usize>) -> Ptr<u8> {
unsafe {
a1.with_mut(|_v1| {
let output: *const u8 = ::brotli_sys::BrotliDecoderTakeOutput(a0, _v1 as *mut usize);
let slice = std::slice::from_raw_parts(output, *_v1);
let result: Ptr<Vec<u8>> = Ptr::alloc(slice.to_vec());
result.decay()
})
}
fn f36() -> u32 {
BrotliDecoderVersion()
}
Loading