minor code review of lz4-related code

This commit is contained in:
Raymond Hill 2018-08-21 11:07:34 -04:00
parent 3d867baf0b
commit b3dec3825c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 12 additions and 13 deletions

View File

@ -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) {

View File

@ -20,8 +20,9 @@ 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`

View File

@ -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

View File

@ -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);
};