Skip to content

Commit

Permalink
Fix issue incorrect output size when writing archives
Browse files Browse the repository at this point in the history
  • Loading branch information
nika-begiashvili committed Jan 27, 2024
1 parent c90e0be commit 711347d
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 55 deletions.
Binary file modified dist/libarchive.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/worker-bundle-node.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/worker-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/build/libarchive.js

Large diffs are not rendered by default.

Binary file modified lib/build/libarchive.wasm
Binary file not shown.
Binary file modified lib/build/main.o
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ emcc ../wrapper/main.c -c -I /usr/local/include/ -o ../build/main.o
emcc ../build/main.o /usr/local/lib/libarchive.a /usr/local/lib/liblzma.a /usr/local/lib/libssl.a /usr/local/lib/libcrypto.a \
-o ../build/libarchive.js \
-s USE_ZLIB=1 -s USE_BZIP2=1 -s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME=libarchive -s WASM=1 -O3 -s ALLOW_MEMORY_GROWTH=1 \
-s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap","allocate"]' \
-s EXPORTED_RUNTIME_METHODS='["cwrap","allocate"]' \
-s EXPORTED_FUNCTIONS=@$PWD/lib.exports -s ERROR_ON_UNDEFINED_SYMBOLS=0

cp ../build/libarchive.js ../../src/webworker/wasm-gen/
Expand Down
3 changes: 1 addition & 2 deletions lib/tools/lib.exports
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
"_size_of_size_t",
"_start_archive_write",
"_write_archive_file",
"_archive_write_close",
"_archive_write_free"
"_finish_archive_write"
]
17 changes: 16 additions & 1 deletion lib/wrapper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,27 @@ void write_archive_file( void *a, char *pathname, size_t filesize , void *fileda
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, 0644);
archive_write_header(a, entry);
archive_write_data(a, filedata, filesize);
int write_output = archive_write_data(a, filedata, filesize);
if( write_output != filesize ) {
fprintf(stderr, "Error occured while writing file, written bytes don't match file size\n");
}

archive_entry_free(entry);
}

EMSCRIPTEN_KEEPALIVE
int size_of_size_t(){
return sizeof(size_t);
}

EMSCRIPTEN_KEEPALIVE
int finish_archive_write(void *a, size_t *outputsize){
if( archive_write_close(a) != ARCHIVE_OK ){
return -1;
}

if( archive_write_free(a) != ARCHIVE_OK ){
return -1;
}
return *outputsize;
}
24 changes: 5 additions & 19 deletions src/webworker/archive-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export class ArchiveWriter {
this._runCode.free(fileData.ptr);
}

const closeStatus = this._runCode.closeArchiveWrite(newArchive);
const freeStatus = this._runCode.freeArchiveWrite(newArchive);
const outputSize = this._runCode.finishArchiveWrite(
newArchive,
outputSizePtr,
);

if (closeStatus !== 0 || freeStatus !== 0) {
if (outputSize < 0) {
throw new Error(this._runCode.getError(newArchive));
}

const outputSize = this.readNumberFromPointer(outputSizePtr);

return this._wasmModule.HEAPU8.slice(bufferPtr, bufferPtr + outputSize);
}

Expand All @@ -60,18 +60,4 @@ export class ArchiveWriter {
length: array.length,
};
}

readNumberFromPointer(ptr) {
const ptrSize = this._runCode.sizeOfSizeT();
const outputSizeBytes = this._wasmModule.HEAPU8.slice(ptr, ptr + ptrSize);

let output = null;
if (ptrSize == 4) {
output = new Uint32Array(outputSizeBytes)[0];
} else if (ptrSize == 8) {
output = new BigUint64Array(outputSizeBytes)[0];
} else throw Error("Unexpected size of size_t: " + ptrSize);

return output;
}
}
42 changes: 18 additions & 24 deletions src/webworker/wasm-gen/libarchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3982,64 +3982,58 @@ var libarchive = (() => {
));
var _size_of_size_t = (Module["_size_of_size_t"] = () =>
(_size_of_size_t = Module["_size_of_size_t"] = wasmExports["O"])());
var _finish_archive_write = (Module["_finish_archive_write"] = (a0, a1) =>
(_finish_archive_write = Module["_finish_archive_write"] =
wasmExports["P"])(a0, a1));
var _free = (Module["_free"] = (a0) =>
(_free = Module["_free"] = wasmExports["P"])(a0));
var ___errno_location = () => (___errno_location = wasmExports["Q"])();
(_free = Module["_free"] = wasmExports["Q"])(a0));
var ___errno_location = () => (___errno_location = wasmExports["R"])();
var _archive_entry_birthtime_nsec = (Module[
"_archive_entry_birthtime_nsec"
] = (a0) =>
(_archive_entry_birthtime_nsec = Module["_archive_entry_birthtime_nsec"] =
wasmExports["R"])(a0));
wasmExports["S"])(a0));
var _archive_entry_filetype = (Module["_archive_entry_filetype"] = (a0) =>
(_archive_entry_filetype = Module["_archive_entry_filetype"] =
wasmExports["S"])(a0));
wasmExports["T"])(a0));
var _archive_entry_mtime_nsec = (Module["_archive_entry_mtime_nsec"] = (
a0,
) =>
(_archive_entry_mtime_nsec = Module["_archive_entry_mtime_nsec"] =
wasmExports["T"])(a0));
wasmExports["U"])(a0));
var _archive_entry_pathname = (Module["_archive_entry_pathname"] = (a0) =>
(_archive_entry_pathname = Module["_archive_entry_pathname"] =
wasmExports["U"])(a0));
wasmExports["V"])(a0));
var _archive_entry_pathname_utf8 = (Module["_archive_entry_pathname_utf8"] =
(a0) =>
(_archive_entry_pathname_utf8 = Module["_archive_entry_pathname_utf8"] =
wasmExports["V"])(a0));
wasmExports["W"])(a0));
var _archive_entry_size = (Module["_archive_entry_size"] = (a0) =>
(_archive_entry_size = Module["_archive_entry_size"] = wasmExports["W"])(
(_archive_entry_size = Module["_archive_entry_size"] = wasmExports["X"])(
a0,
));
var _archive_entry_is_encrypted = (Module["_archive_entry_is_encrypted"] = (
a0,
) =>
(_archive_entry_is_encrypted = Module["_archive_entry_is_encrypted"] =
wasmExports["X"])(a0));
wasmExports["Y"])(a0));
var _archive_read_has_encrypted_entries = (Module[
"_archive_read_has_encrypted_entries"
] = (a0) =>
(_archive_read_has_encrypted_entries = Module[
"_archive_read_has_encrypted_entries"
] =
wasmExports["Y"])(a0));
wasmExports["Z"])(a0));
var _archive_read_data_skip = (Module["_archive_read_data_skip"] = (a0) =>
(_archive_read_data_skip = Module["_archive_read_data_skip"] =
wasmExports["Z"])(a0));
var _archive_write_close = (Module["_archive_write_close"] = (a0) =>
(_archive_write_close = Module["_archive_write_close"] =
wasmExports["$"])(a0));
var _archive_write_free = (Module["_archive_write_free"] = (a0) =>
(_archive_write_free = Module["_archive_write_free"] = wasmExports["aa"])(
a0,
));
wasmExports["_"])(a0));
var _htons = (a0) => (_htons = wasmExports["htons"])(a0);
var _ntohs = (a0) => (_ntohs = wasmExports["ntohs"])(a0);
var setTempRet0 = (a0) => (setTempRet0 = wasmExports["ba"])(a0);
var stackSave = () => (stackSave = wasmExports["ca"])();
var stackRestore = (a0) => (stackRestore = wasmExports["da"])(a0);
var stackAlloc = (a0) => (stackAlloc = wasmExports["ea"])(a0);
var setTempRet0 = (a0) => (setTempRet0 = wasmExports["aa"])(a0);
var stackSave = () => (stackSave = wasmExports["ba"])();
var stackRestore = (a0) => (stackRestore = wasmExports["ca"])(a0);
var stackAlloc = (a0) => (stackAlloc = wasmExports["da"])(a0);
Module["cwrap"] = cwrap;
Module["UTF8ToString"] = UTF8ToString;
Module["intArrayFromString"] = intArrayFromString;
Module["allocate"] = allocate;
var calledRun;
dependenciesFulfilled = function runCaller() {
Expand Down
Binary file modified src/webworker/wasm-gen/libarchive.wasm
Binary file not shown.
8 changes: 3 additions & 5 deletions src/webworker/wasm-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ export class WasmModule {
"number",
]),

// int archive_write_close(struct archive *);
closeArchiveWrite: this.cwrap("archive_write_close", "number", [
// int finish_archive_write(void *a, size_t *outputsize)
finishArchiveWrite: this.cwrap("finish_archive_write", "number", [
"number",
"number",
]),

// int archive_write_free(struct archive *);
freeArchiveWrite: this.cwrap("archive_write_free", "number", ["number"]),

/*
* Returns 1 if the archive contains at least one encrypted entry.
* If the archive format not support encryption at all
Expand Down

0 comments on commit 711347d

Please sign in to comment.