2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-06-20 11:14:24 -04:00
|
|
|
use brotli::enc::backward_references::BrotliEncoderMode;
|
|
|
|
use brotli::enc::encode::BrotliEncoderCompress;
|
|
|
|
use brotli::enc::encode::BrotliEncoderOperation;
|
2023-06-24 10:12:08 -04:00
|
|
|
use brotli::enc::encode::BrotliEncoderParameter;
|
2024-06-20 11:14:24 -04:00
|
|
|
use brotli::enc::encode::BrotliEncoderStateStruct;
|
|
|
|
use brotli::writer::StandardAlloc;
|
|
|
|
use brotli::BrotliDecompressStream;
|
|
|
|
use brotli::BrotliResult;
|
|
|
|
use brotli::BrotliState;
|
2023-09-07 23:41:33 -04:00
|
|
|
use brotli::Decompressor;
|
2023-06-24 10:12:08 -04:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-09-14 02:29:44 -04:00
|
|
|
use deno_core::op2;
|
2023-06-24 10:12:08 -04:00
|
|
|
use deno_core::JsBuffer;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ToJsBuffer;
|
2024-06-20 11:14:24 -04:00
|
|
|
use std::cell::RefCell;
|
2023-09-07 23:41:33 -04:00
|
|
|
use std::io::Read;
|
2023-06-24 10:12:08 -04:00
|
|
|
|
|
|
|
fn encoder_mode(mode: u32) -> Result<BrotliEncoderMode, AnyError> {
|
2024-06-20 11:14:24 -04:00
|
|
|
Ok(match mode {
|
|
|
|
0 => BrotliEncoderMode::BROTLI_MODE_GENERIC,
|
|
|
|
1 => BrotliEncoderMode::BROTLI_MODE_TEXT,
|
|
|
|
2 => BrotliEncoderMode::BROTLI_MODE_FONT,
|
|
|
|
3 => BrotliEncoderMode::BROTLI_FORCE_LSB_PRIOR,
|
|
|
|
4 => BrotliEncoderMode::BROTLI_FORCE_MSB_PRIOR,
|
|
|
|
5 => BrotliEncoderMode::BROTLI_FORCE_UTF8_PRIOR,
|
|
|
|
6 => BrotliEncoderMode::BROTLI_FORCE_SIGNED_PRIOR,
|
|
|
|
_ => return Err(type_error("Invalid encoder mode")),
|
|
|
|
})
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_brotli_compress(
|
2023-09-23 08:04:47 -04:00
|
|
|
#[buffer] buffer: &[u8],
|
|
|
|
#[buffer] out: &mut [u8],
|
|
|
|
#[smi] quality: i32,
|
|
|
|
#[smi] lgwin: i32,
|
|
|
|
#[smi] mode: u32,
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
2024-06-20 11:14:24 -04:00
|
|
|
let mode = encoder_mode(mode)?;
|
2023-06-24 10:12:08 -04:00
|
|
|
let mut out_size = out.len();
|
|
|
|
|
2024-06-20 11:14:24 -04:00
|
|
|
let result = BrotliEncoderCompress(
|
|
|
|
StandardAlloc::default(),
|
|
|
|
&mut StandardAlloc::default(),
|
|
|
|
quality,
|
|
|
|
lgwin,
|
|
|
|
mode,
|
|
|
|
buffer.len(),
|
|
|
|
buffer,
|
|
|
|
&mut out_size,
|
|
|
|
out,
|
|
|
|
&mut |_, _, _, _| (),
|
|
|
|
);
|
|
|
|
if result != 1 {
|
2023-06-24 10:12:08 -04:00
|
|
|
return Err(type_error("Failed to compress"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(out_size)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_compressed_size(input_size: usize) -> usize {
|
|
|
|
if input_size == 0 {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [window bits / empty metadata] + N * [uncompressed] + [last empty]
|
|
|
|
let num_large_blocks = input_size >> 14;
|
|
|
|
let overhead = 2 + (4 * num_large_blocks) + 3 + 1;
|
|
|
|
let result = input_size + overhead;
|
|
|
|
|
|
|
|
if result < input_size {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub async fn op_brotli_compress_async(
|
2023-09-14 02:29:44 -04:00
|
|
|
#[buffer] input: JsBuffer,
|
|
|
|
#[smi] quality: i32,
|
|
|
|
#[smi] lgwin: i32,
|
|
|
|
#[smi] mode: u32,
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2024-06-20 11:14:24 -04:00
|
|
|
let mode = encoder_mode(mode)?;
|
2023-06-24 10:12:08 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2024-06-20 11:14:24 -04:00
|
|
|
let input = &*input;
|
|
|
|
let mut out = vec![0u8; max_compressed_size(input.len())];
|
2023-06-24 10:12:08 -04:00
|
|
|
let mut out_size = out.len();
|
|
|
|
|
2024-06-20 11:14:24 -04:00
|
|
|
let result = BrotliEncoderCompress(
|
|
|
|
StandardAlloc::default(),
|
|
|
|
&mut StandardAlloc::default(),
|
|
|
|
quality,
|
|
|
|
lgwin,
|
|
|
|
mode,
|
|
|
|
input.len(),
|
|
|
|
input,
|
|
|
|
&mut out_size,
|
|
|
|
&mut out,
|
|
|
|
&mut |_, _, _, _| (),
|
|
|
|
);
|
|
|
|
if result != 1 {
|
2023-06-24 10:12:08 -04:00
|
|
|
return Err(type_error("Failed to compress"));
|
|
|
|
}
|
|
|
|
|
|
|
|
out.truncate(out_size);
|
|
|
|
Ok(out.into())
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BrotliCompressCtx {
|
2024-06-20 11:14:24 -04:00
|
|
|
inst: RefCell<BrotliEncoderStateStruct<StandardAlloc>>,
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for BrotliCompressCtx {}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_create_brotli_compress(
|
|
|
|
state: &mut OpState,
|
2023-09-14 02:29:44 -04:00
|
|
|
#[serde] params: Vec<(u8, i32)>,
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> u32 {
|
2024-06-20 11:14:24 -04:00
|
|
|
let mut inst = BrotliEncoderStateStruct::new(StandardAlloc::default());
|
2023-06-24 10:12:08 -04:00
|
|
|
|
|
|
|
for (key, value) in params {
|
2024-06-20 11:14:24 -04:00
|
|
|
inst.set_parameter(encoder_param(key), value as u32);
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-20 11:14:24 -04:00
|
|
|
state.resource_table.add(BrotliCompressCtx {
|
|
|
|
inst: RefCell::new(inst),
|
|
|
|
})
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encoder_param(param: u8) -> BrotliEncoderParameter {
|
|
|
|
// SAFETY: BrotliEncoderParam is valid for 0-255
|
|
|
|
unsafe { std::mem::transmute(param as u32) }
|
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_brotli_compress_stream(
|
|
|
|
state: &mut OpState,
|
2023-09-23 08:04:47 -04:00
|
|
|
#[smi] rid: u32,
|
|
|
|
#[buffer] input: &[u8],
|
|
|
|
#[buffer] output: &mut [u8],
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
|
|
|
let ctx = state.resource_table.get::<BrotliCompressCtx>(rid)?;
|
2024-06-20 11:14:24 -04:00
|
|
|
let mut inst = ctx.inst.borrow_mut();
|
|
|
|
let mut output_offset = 0;
|
|
|
|
|
|
|
|
let result = inst.compress_stream(
|
|
|
|
BrotliEncoderOperation::BROTLI_OPERATION_PROCESS,
|
|
|
|
&mut input.len(),
|
|
|
|
input,
|
|
|
|
&mut 0,
|
|
|
|
&mut output.len(),
|
|
|
|
output,
|
|
|
|
&mut output_offset,
|
|
|
|
&mut None,
|
|
|
|
&mut |_, _, _, _| (),
|
|
|
|
);
|
|
|
|
if !result {
|
|
|
|
return Err(type_error("Failed to compress"));
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
2024-06-20 11:14:24 -04:00
|
|
|
|
|
|
|
Ok(output_offset)
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_brotli_compress_stream_end(
|
|
|
|
state: &mut OpState,
|
2023-09-23 08:04:47 -04:00
|
|
|
#[smi] rid: u32,
|
|
|
|
#[buffer] output: &mut [u8],
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
2024-02-07 11:23:32 -05:00
|
|
|
let ctx = state.resource_table.get::<BrotliCompressCtx>(rid)?;
|
2024-06-20 11:14:24 -04:00
|
|
|
let mut inst = ctx.inst.borrow_mut();
|
|
|
|
let mut output_offset = 0;
|
|
|
|
|
|
|
|
let result = inst.compress_stream(
|
|
|
|
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
|
|
|
|
&mut 0,
|
|
|
|
&[],
|
|
|
|
&mut 0,
|
|
|
|
&mut output.len(),
|
|
|
|
output,
|
|
|
|
&mut output_offset,
|
|
|
|
&mut None,
|
|
|
|
&mut |_, _, _, _| (),
|
|
|
|
);
|
|
|
|
if !result {
|
|
|
|
return Err(type_error("Failed to compress"));
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
2024-06-20 11:14:24 -04:00
|
|
|
|
|
|
|
Ok(output_offset)
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn brotli_decompress(buffer: &[u8]) -> Result<ToJsBuffer, AnyError> {
|
2023-09-07 23:41:33 -04:00
|
|
|
let mut output = Vec::with_capacity(4096);
|
|
|
|
let mut decompressor = Decompressor::new(buffer, buffer.len());
|
|
|
|
decompressor.read_to_end(&mut output)?;
|
|
|
|
Ok(output.into())
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
pub fn op_brotli_decompress(
|
|
|
|
#[buffer] buffer: &[u8],
|
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2023-06-24 10:12:08 -04:00
|
|
|
brotli_decompress(buffer)
|
|
|
|
}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub async fn op_brotli_decompress_async(
|
2023-09-14 02:29:44 -04:00
|
|
|
#[buffer] buffer: JsBuffer,
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
|
|
|
tokio::task::spawn_blocking(move || brotli_decompress(&buffer)).await?
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BrotliDecompressCtx {
|
2024-06-20 11:14:24 -04:00
|
|
|
inst: RefCell<BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>>,
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for BrotliDecompressCtx {}
|
|
|
|
|
2023-09-14 02:29:44 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_create_brotli_decompress(state: &mut OpState) -> u32 {
|
2024-06-20 11:14:24 -04:00
|
|
|
let inst = BrotliState::new(
|
|
|
|
StandardAlloc::default(),
|
|
|
|
StandardAlloc::default(),
|
|
|
|
StandardAlloc::default(),
|
|
|
|
);
|
|
|
|
state.resource_table.add(BrotliDecompressCtx {
|
|
|
|
inst: RefCell::new(inst),
|
|
|
|
})
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_brotli_decompress_stream(
|
|
|
|
state: &mut OpState,
|
2023-09-23 08:04:47 -04:00
|
|
|
#[smi] rid: u32,
|
|
|
|
#[buffer] input: &[u8],
|
|
|
|
#[buffer] output: &mut [u8],
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
|
|
|
let ctx = state.resource_table.get::<BrotliDecompressCtx>(rid)?;
|
2024-06-20 11:14:24 -04:00
|
|
|
let mut inst = ctx.inst.borrow_mut();
|
|
|
|
let mut output_offset = 0;
|
|
|
|
|
|
|
|
let result = BrotliDecompressStream(
|
|
|
|
&mut input.len(),
|
|
|
|
&mut 0,
|
|
|
|
input,
|
|
|
|
&mut output.len(),
|
|
|
|
&mut output_offset,
|
|
|
|
output,
|
|
|
|
&mut 0,
|
|
|
|
&mut inst,
|
|
|
|
);
|
|
|
|
if matches!(result, BrotliResult::ResultFailure) {
|
|
|
|
return Err(type_error("Failed to decompress"));
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
2024-06-20 11:14:24 -04:00
|
|
|
|
|
|
|
Ok(output_offset)
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
2023-06-24 10:12:08 -04:00
|
|
|
pub fn op_brotli_decompress_stream_end(
|
|
|
|
state: &mut OpState,
|
2023-09-23 08:04:47 -04:00
|
|
|
#[smi] rid: u32,
|
|
|
|
#[buffer] output: &mut [u8],
|
2023-06-24 10:12:08 -04:00
|
|
|
) -> Result<usize, AnyError> {
|
|
|
|
let ctx = state.resource_table.get::<BrotliDecompressCtx>(rid)?;
|
2024-06-20 11:14:24 -04:00
|
|
|
let mut inst = ctx.inst.borrow_mut();
|
|
|
|
let mut output_offset = 0;
|
|
|
|
|
|
|
|
let result = BrotliDecompressStream(
|
|
|
|
&mut 0,
|
|
|
|
&mut 0,
|
|
|
|
&[],
|
|
|
|
&mut output.len(),
|
|
|
|
&mut output_offset,
|
|
|
|
output,
|
|
|
|
&mut 0,
|
|
|
|
&mut inst,
|
|
|
|
);
|
|
|
|
if matches!(result, BrotliResult::ResultFailure) {
|
|
|
|
return Err(type_error("Failed to decompress"));
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|
2024-06-20 11:14:24 -04:00
|
|
|
|
|
|
|
Ok(output_offset)
|
2023-06-24 10:12:08 -04:00
|
|
|
}
|