diff --git a/src/js/lz4.js b/src/js/lz4.js index 5ee2e71fc..d6d7dfe6a 100644 --- a/src/js/lz4.js +++ b/src/js/lz4.js @@ -114,6 +114,7 @@ let encodeValue = function(key, value) { let inputArray = textEncoder.encode(value); let inputSize = inputArray.byteLength; let outputArray = lz4CodecInstance.encodeBlock(inputArray, 8); + if ( outputArray instanceof Uint8Array === false ) { return; } outputArray[0] = 0x18; outputArray[1] = 0x4D; outputArray[2] = 0x22; @@ -146,6 +147,7 @@ let decodeValue = function(key, inputArray) { (inputArray[4] << 0) | (inputArray[5] << 8) | (inputArray[6] << 16) | (inputArray[7] << 24); let outputArray = lz4CodecInstance.decodeBlock(inputArray, 8, outputSize); + if ( outputArray instanceof Uint8Array === false ) { return; } if ( textDecoder === undefined ) { textDecoder = new TextDecoder(); } @@ -173,7 +175,7 @@ return { if ( dataOut instanceof Uint8Array ) { dataOut = new Blob([ dataOut ]); } - return { key, data: dataOut }; + return { key, data: dataOut || dataIn }; }); }, decode: function(key, dataIn) { diff --git a/src/lib/lz4/README.md b/src/lib/lz4/README.md index c1176f08a..ecd76e9cb 100644 --- a/src/lib/lz4/README.md +++ b/src/lib/lz4/README.md @@ -20,9 +20,10 @@ If the choosen implementation is not specified, there will be an attempt to create a WebAssembly-based instance. If for whatever reason this fails, a pure javascript-based instance will be created. -The script for either instance are dynamically loaded, such that no resources -are wasted by keeping in memory code which won't be used. - +The script for either instance are dynamically loaded and only when needed, +such that no resources are wasted by keeping in memory code which won't be +used. + ### `lz4-block-codec-wasm.js` This contains the code to instanciate WebAssembly-based LZ4 block codec. Note diff --git a/src/lib/lz4/lz4-block-codec-js.js b/src/lib/lz4/lz4-block-codec-js.js index 0fa36b7fe..ac0ebb639 100644 --- a/src/lib/lz4/lz4-block-codec-js.js +++ b/src/lib/lz4/lz4-block-codec-js.js @@ -64,7 +64,7 @@ let encodeBound = function(size) { let encodeBlock = function(instance, iBuf, oOffset) { let iLen = iBuf.byteLength; - if ( iLen >= 0x7E000000 ) { throw new TypeError(); } + if ( iLen >= 0x7E000000 ) { throw new RangeError(); } // "The last match must start at least 12 bytes before end of block" let lastMatchPos = iLen - 12; @@ -216,7 +216,7 @@ let decodeBlock = function(instance, iBuf, iOffset, oLen) { // match let mOffset = iBuf[iPos+0] | (iBuf[iPos+1] << 8); - if ( mOffset === 0 || mOffset > oPos ) { return 0; } + if ( mOffset === 0 || mOffset > oPos ) { return; } iPos += 2; // length of match diff --git a/src/lib/lz4/lz4-block-codec-wasm.js b/src/lib/lz4/lz4-block-codec-wasm.js index a13e7e6aa..52e3a0948 100644 --- a/src/lib/lz4/lz4-block-codec-wasm.js +++ b/src/lib/lz4/lz4-block-codec-wasm.js @@ -72,7 +72,7 @@ let encodeBlock = function(wasmInstance, inputArray, outputOffset) { let mem0 = lz4api.getLinearMemoryOffset(); let hashTableSize = 65536 * 4; let inputSize = inputArray.byteLength; - if ( inputSize >= 0x7E000000 ) { throw new TypeError(); } + if ( inputSize >= 0x7E000000 ) { throw new RangeError(); } let memSize = hashTableSize + inputSize + @@ -87,9 +87,7 @@ let encodeBlock = function(wasmInstance, inputArray, outputOffset) { inputSize, mem0 + hashTableSize + inputSize + outputOffset ); - if ( outputSize === 0 ) { - inputSize = 0 - inputSize; - } + if ( outputSize === 0 ) { return; } let outputArray = new Uint8Array( memBuffer, mem0 + hashTableSize + inputSize, @@ -111,9 +109,7 @@ let decodeBlock = function(wasmInstance, inputArray, inputOffset, outputSize) { inputSize - inputOffset, mem0 + inputSize ); - if ( outputSize === 0 ) { - throw new Error('LZ4BlockWASM: block-level compression failed'); - } + if ( outputSize === 0 ) { return; } return new Uint8Array(memBuffer, mem0 + inputSize, outputSize); };