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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 126 additions & 6 deletions crates/uorc-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::cell::RefCell;
thread_local! {
static INPUT: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
static OUTPUT: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
static ORIGINAL: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}

/// Operation succeeded; the result is in the output buffer.
Expand All @@ -19,16 +20,27 @@ pub const BUDGET: i32 = 1;
pub const OVERFLOW: i32 = 2;
/// See [`uorc::Error::Decode`].
pub const DECODE: i32 = 3;
/// See [`uorc::Error::Mismatch`].
pub const MISMATCH: i32 = 4;

fn resize(buffer: &RefCell<Vec<u8>>, len: usize) -> *mut u8 {
let mut buffer = buffer.borrow_mut();
buffer.clear();
buffer.resize(len, 0);
buffer.as_mut_ptr()
}

/// Resize the input buffer to `len` bytes and return its address in memory.
#[no_mangle]
pub extern "C" fn input_buffer(len: usize) -> *mut u8 {
INPUT.with(|input| {
let mut input = input.borrow_mut();
input.clear();
input.resize(len, 0);
input.as_mut_ptr()
})
INPUT.with(|input| resize(input, len))
}

/// Resize the original buffer (the bytes [`verify`] compares against) to
/// `len` bytes and return its address in memory.
#[no_mangle]
pub extern "C" fn original_buffer(len: usize) -> *mut u8 {
ORIGINAL.with(|original| resize(original, len))
}

/// Address of the last operation's output.
Expand All @@ -49,6 +61,7 @@ fn finish(result: Result<Vec<u8>, uorc::Error>) -> i32 {
Err(uorc::Error::Budget) => (BUDGET, Vec::new()),
Err(uorc::Error::Overflow) => (OVERFLOW, Vec::new()),
Err(uorc::Error::Decode) => (DECODE, Vec::new()),
Err(uorc::Error::Mismatch) => (MISMATCH, Vec::new()),
};
OUTPUT.with(|output| *output.borrow_mut() = bytes);
code
Expand All @@ -65,3 +78,110 @@ pub extern "C" fn compress(steps: u64) -> i32 {
pub extern "C" fn decompress() -> i32 {
finish(INPUT.with(|input| uorc::decompress(&input.borrow(), uorc::DEFAULT_DECODE_WORK)))
}

/// Read the input archive's frame header and integrity seal without decoding;
/// the output is the description as JSON.
#[no_mangle]
pub extern "C" fn inspect() -> i32 {
finish(
INPUT.with(|input| uorc::inspect(&input.borrow()).map(|info| info.to_json().into_bytes())),
)
}

/// Decode the input (one archive or several back to back) in memory and check
/// it; with `compare` nonzero, also check that it reproduces the original
/// buffer. The output is a JSON array describing each archive.
#[no_mangle]
pub extern "C" fn verify(compare: i32) -> i32 {
finish(INPUT.with(|input| {
ORIGINAL.with(|original| {
let original = original.borrow();
uorc::verify(
&input.borrow(),
(compare != 0).then_some(original.as_slice()),
uorc::DEFAULT_DECODE_WORK,
)
.map(|infos| {
let all: Vec<String> = infos.iter().map(uorc::Info::to_json).collect();
format!("[{}]", all.join(",")).into_bytes()
})
})
}))
}

/// Entries cross the wasm boundary as `u32 count` then, per entry,
/// `u8 kind` (0 file, 1 executable, 2 directory), `u32 pathLength`, path,
/// `u32 dataLength`, data; integers little-endian.
fn read_entries(bytes: &[u8]) -> Result<Vec<uorc::tree::Entry>, uorc::Error> {
use uorc::tree::{Entry, Kind};
struct Cursor<'a>(&'a [u8]);
impl<'a> Cursor<'a> {
fn take(&mut self, n: usize) -> Result<&'a [u8], uorc::Error> {
if n > self.0.len() {
return Err(uorc::Error::Decode);
}
let (head, tail) = self.0.split_at(n);
self.0 = tail;
Ok(head)
}
fn word(&mut self) -> Result<usize, uorc::Error> {
Ok(u32::from_le_bytes(self.take(4)?.try_into().expect("4 bytes")) as usize)
}
}
let mut c = Cursor(bytes);
let count = c.word()?;
let mut entries = Vec::new();
for _ in 0..count {
let kind = match c.take(1)?[0] {
0 => Kind::File,
1 => Kind::Executable,
2 => Kind::Directory,
_ => return Err(uorc::Error::Decode),
};
let len = c.word()?;
let path = String::from_utf8(c.take(len)?.to_vec()).map_err(|_| uorc::Error::Decode)?;
let len = c.word()?;
let data = c.take(len)?.to_vec();
entries.push(Entry { path, kind, data });
}
if !c.0.is_empty() {
return Err(uorc::Error::Decode);
}
Ok(entries)
}

fn write_entries(entries: &[uorc::tree::Entry]) -> Vec<u8> {
use uorc::tree::Kind;
let mut out = (entries.len() as u32).to_le_bytes().to_vec();
for entry in entries {
out.push(match entry.kind {
Kind::File => 0,
Kind::Executable => 1,
Kind::Directory => 2,
});
out.extend((entry.path.len() as u32).to_le_bytes());
out.extend(entry.path.as_bytes());
out.extend((entry.data.len() as u32).to_le_bytes());
out.extend(&entry.data);
}
out
}

/// Compress the entries in the input buffer (see [`read_entries`]) into one
/// archive of ordered objects.
#[no_mangle]
pub extern "C" fn compress_tree(steps: u64) -> i32 {
finish(INPUT.with(|input| {
read_entries(&input.borrow()).and_then(|entries| uorc::compress_tree(&entries, steps))
}))
}

/// Restore the entries a tree archive in the input buffer stores; the output
/// is in the [`read_entries`] layout. A one-object archive is not a tree.
#[no_mangle]
pub extern "C" fn decompress_tree() -> i32 {
finish(INPUT.with(|input| {
uorc::decompress_tree(&input.borrow(), uorc::DEFAULT_DECODE_WORK)
.map(|entries| write_entries(&entries))
}))
}
1 change: 1 addition & 0 deletions crates/uorc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ name = "uorc"
path = "src/bin/uorc.rs"

[dependencies]
sha2 = "0.11.1"
uorc_compress = { version = "0.1.0", path = "../../generated/rust/uorc_compress" }
uorc_decompress = { version = "0.1.0", path = "../../generated/rust/uorc_decompress" }

Expand Down
Loading
Loading