Skip to content

Commit ea3a221

Browse files
committed
Add safe shim for brotli
1 parent 26dcab2 commit ea3a221

3 files changed

Lines changed: 217 additions & 70 deletions

File tree

‎libcc2rs/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ libc = "0.2"
99
nix = { version = "0.30", features = ["socket", "net", "fs", "poll", "time", "user", "dir", "term", "event", "hostname"] }
1010
jiff = "0.2"
1111
sprintf = "0.4"
12+
brotli = "8"
13+
brotli-decompressor = "5"

‎rules/brotli/shim.rs‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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, &params) {
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+
}

‎rules/brotli/tgt_refcount.rs‎

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,105 +2,70 @@
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

44
use libcc2rs::*;
5-
use std::cell::RefCell;
6-
use std::rc::Rc;
5+
6+
fn t3() -> Ptr<BrotliDecoderState> {
7+
Ptr::null()
8+
}
9+
10+
fn t4() -> Ptr<BrotliDecoderState> {
11+
Ptr::null()
12+
}
13+
14+
fn t6() -> BrotliDecoderState {
15+
Default::default()
16+
}
17+
18+
fn t7() -> BrotliDecoderState {
19+
Default::default()
20+
}
721

822
fn f2(
923
a0: libc::c_int,
1024
a1: libc::c_int,
11-
a2: brotli_sys::BrotliEncoderMode,
25+
a2: ::brotli_sys::BrotliEncoderMode,
1226
a3: usize,
1327
a4: Ptr<u8>,
1428
a5: Ptr<usize>,
1529
a6: Ptr<u8>,
1630
) -> libc::c_int {
17-
// Compress into a buffer bounded by the input size, as the output buffer
18-
// may be much larger and is expensive to borrow if reinterpreted.
19-
let mut __out_len = a5.read();
20-
let __max = unsafe { ::brotli_sys::BrotliEncoderMaxCompressedSize(a3) };
21-
let mut __out = vec![0u8; if __max == 0 { __out_len } else { __out_len.min(__max) }];
22-
__out_len = __out.len();
23-
let __ok = a4.with_slice(a3, |__in| unsafe {
24-
::brotli_sys::BrotliEncoderCompress(
25-
a0,
26-
a1,
27-
a2,
28-
a3,
29-
__in.as_ptr(),
30-
&mut __out_len,
31-
__out.as_mut_ptr(),
32-
)
33-
});
34-
if __ok != 0 {
35-
a6.with_slice_mut(__out_len, |__s| __s.copy_from_slice(&__out[..__out_len]));
36-
a5.write(__out_len);
37-
}
38-
__ok
31+
BrotliEncoderCompress(a0, a1, a2, a3, a4, a5, a6)
3932
}
4033

4134
fn f5(a0: usize, a1: Ptr<u8>, a2: Ptr<usize>, a3: Ptr<u8>) -> ::brotli_sys::BrotliDecoderResult {
42-
let __out_len = a2.read();
43-
a1.with_slice(a0, |__in| {
44-
a2.with_mut(|_v2| {
45-
a3.with_slice_mut(__out_len, |__out| unsafe {
46-
::brotli_sys::BrotliDecoderDecompress(
47-
a0,
48-
__in.as_ptr(),
49-
_v2 as *mut usize,
50-
__out.as_mut_ptr(),
51-
)
52-
})
53-
})
54-
})
35+
BrotliDecoderDecompress(a0, a1, a2, a3)
5536
}
5637

5738
fn f6(
5839
a0: ::brotli_sys::brotli_alloc_func,
5940
a1: ::brotli_sys::brotli_free_func,
6041
a2: *mut std::ffi::c_void,
61-
) -> *mut ::brotli_sys::BrotliDecoderState {
62-
unsafe { ::brotli_sys::BrotliDecoderCreateInstance(None, None, std::ptr::null_mut()) }
42+
) -> Ptr<BrotliDecoderState> {
43+
Ptr::alloc(BrotliDecoderState::default())
6344
}
6445

65-
fn f7(a0: *mut ::brotli_sys::BrotliDecoderState) {
66-
unsafe { ::brotli_sys::BrotliDecoderDestroyInstance(a0) }
46+
fn f7(a0: Ptr<BrotliDecoderState>) {
47+
BrotliDecoderDestroyInstance(a0)
6748
}
6849

6950
fn f8(
70-
a0: *mut ::brotli_sys::BrotliDecoderState,
51+
a0: Ptr<BrotliDecoderState>,
7152
a1: Ptr<usize>,
7253
a2: Ptr<Ptr<u8>>,
7354
a3: Ptr<usize>,
7455
a4: Ptr<Ptr<u8>>,
7556
a5: Ptr<usize>,
7657
) -> ::brotli_sys::BrotliDecoderResult {
77-
let __in = a2.read();
78-
let __in_len = a1.read();
79-
let __r = __in.with_slice(__in_len, |__s| {
80-
a1.with_mut(|_v1| {
81-
a3.with_mut(|_v3| unsafe {
82-
::brotli_sys::BrotliDecoderDecompressStream(
83-
a0,
84-
_v1,
85-
&mut __s.as_ptr(),
86-
_v3,
87-
std::ptr::null_mut(),
88-
std::ptr::null_mut(),
89-
)
90-
})
91-
})
92-
});
93-
a2.write(__in.offset(__in_len - a1.read()));
94-
__r
58+
BrotliDecoderDecompressStream(a0, a1, a2, a3, a4, a5)
59+
}
60+
61+
fn f9(a0: Ptr<BrotliDecoderState>, a1: Ptr<usize>) -> Ptr<u8> {
62+
BrotliDecoderTakeOutput(a0, a1)
63+
}
64+
65+
fn f35(a0: Ptr<BrotliDecoderState>) -> ::brotli_sys::BrotliDecoderErrorCode {
66+
a0.with(|__s| __s.state.error_code as i32)
9567
}
9668

97-
fn f9(a0: *mut ::brotli_sys::BrotliDecoderState, a1: Ptr<usize>) -> Ptr<u8> {
98-
unsafe {
99-
a1.with_mut(|_v1| {
100-
let output: *const u8 = ::brotli_sys::BrotliDecoderTakeOutput(a0, _v1 as *mut usize);
101-
let slice = std::slice::from_raw_parts(output, *_v1);
102-
let result: Ptr<Vec<u8>> = Ptr::alloc(slice.to_vec());
103-
result.decay()
104-
})
105-
}
69+
fn f36() -> u32 {
70+
BrotliDecoderVersion()
10671
}

0 commit comments

Comments
 (0)