Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd-codec support for decompressing files #382

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
decompress works
  • Loading branch information
Sarah Wang committed Aug 10, 2023
commit 9fd7296b6ec0102f2f697105e976e21ec2e41f58
21 changes: 11 additions & 10 deletions webusb/src/logic/useDecompress2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const useDecompress2 = (flashFile) => {
const total = useRef(0);
const stream = useRef();
const data = useRef();
const i = useRef(0);
const chunk_i = useRef(0);
const i = useRef(0); // for indexing through flashfile
const chunk_i = useRef(0); // for building chunk headers


const [USBDevice, setUSBDevice] = useState();
Expand All @@ -42,14 +42,14 @@ const useDecompress2 = (flashFile) => {
// things to test: do you have to await on
const processChunk = async(chunk) => {
console.log('processing...');
let res = await p2();
console.log(res);
res = await p3();
console.log(res);
await p3();
// let res = await p2();
// console.log(res);
// res = await p3();
// console.log(res);
// await p3();

await process_chunk(USBDevice, chunk);
// chunk_i.current += 1; TODO: CHANGE THIS FOR ME
await process_chunk(USBDevice, chunk, chunk_i.current);
chunk_i.current += 1; //TODO: CHANGE THIS FOR ME

console.log("COMPLETED ONE CHUNK");
console.log(chunk);
Expand Down Expand Up @@ -157,10 +157,11 @@ const useDecompress2 = (flashFile) => {
console.log("stream.end() fail");
return;
}
chunk_processor.current.flush(processChunk);
return;
}

let chunk = data.current.slice(size*i.current, Math.min(size*(i.current+1), data.current.length));
let chunk = data.current.slice(size*i.current, Math.min(size*(i.current+1), data.current.length)); //TODO: maybe don't need min thigs, handled automatically
i.current += 1;

if (!stream.current.load(chunk)) {
Expand Down
14 changes: 11 additions & 3 deletions webusb/src/logic/useFlash.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function get_reply(USBDevice, success_str) {
if (success_str !== ab_to_str(result.data.buffer)) {
throw new Error ("failed to send data:", ab_to_str(result.data.buffer))
}
console.log(result.data.buffer);
}

async function send_output(USBDevice, data) {
Expand Down Expand Up @@ -67,30 +68,37 @@ async function send_chunk_begin (USBDevice, chunk_len, i) {

async function flash_all (USBDevice) {
await send_data(USBDevice, str_to_arr("flash:all"), "OKAY");
console.log("flash all");
}

export async function process_chunk (USBDevice, chunk, i) {
console.log("process_chunk");
console.log(USBDevice, chunk, i);
// pad chunk with zeros
let pad_count = (chunk.length/BLK_SZ)*BLK_SZ - chunk.length;

let pad_count = Math.ceil(chunk.length/BLK_SZ)*BLK_SZ - chunk.length

if (pad_count) {
let pad = new Uint8Array(pad_count);
let new_chunk = new Uint8Array(chunk.length + pad_count);
new_chunk.set(chunk, 0);
new_chunk.set(pad, chunk.length);
chunk = new_chunk;
console.log("new chunk", chunk)
}

await send_chunk_begin(USBDevice, chunk.length, i);
console.log("sent chunk begin")

let offset = 0;
let packet;
while (offset < chunk.length) {
packet = chunk.slice(offset, offset + PACKET_SZ);
await send_output(USBDevice, packet);
console.log("sent output")
offset += PACKET_SZ;
}
console.log("finished loop")
await get_reply(USBDevice, "OKAY");
await flash_all();

await flash_all(USBDevice);
}