mirror of https://github.com/gorhill/uBlock.git
Better handle unexpected conditions when deserializing
For example, when deserialzing from corrupted storage.
This commit is contained in:
parent
ff5fc61753
commit
4c299bfca9
|
@ -1097,32 +1097,36 @@ export const serialize = (data, options = {}) => {
|
||||||
return ratio <= 0.85 ? t : s;
|
return ratio <= 0.85 ? t : s;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deserialize = s => {
|
const deserializeById = (blockid, s) => {
|
||||||
if ( s.startsWith(MAGICLZ4PREFIX) ) {
|
|
||||||
refCounter = 1;
|
|
||||||
readStr = s;
|
|
||||||
readEnd = s.length;
|
|
||||||
readPtr = MAGICLZ4PREFIX.length;
|
|
||||||
const lz4 = _deserialize();
|
|
||||||
readRefs.clear();
|
|
||||||
readStr = '';
|
|
||||||
const lz4Util = new LZ4BlockJS();
|
|
||||||
const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size);
|
|
||||||
s = textCodec.decode(new Uint8Array(uint8ArrayAfter));
|
|
||||||
}
|
|
||||||
if ( s.startsWith(MAGICPREFIX) === false ) { return; }
|
|
||||||
refCounter = 1;
|
refCounter = 1;
|
||||||
readStr = s;
|
readStr = s;
|
||||||
readEnd = s.length;
|
readEnd = s.length;
|
||||||
readPtr = MAGICPREFIX.length;
|
readPtr = blockid.length;
|
||||||
const data = _deserialize();
|
const data = _deserialize();
|
||||||
readRefs.clear();
|
readRefs.clear();
|
||||||
readStr = '';
|
readStr = '';
|
||||||
uint8Input = null;
|
|
||||||
if ( readPtr === FAILMARK ) { return; }
|
if ( readPtr === FAILMARK ) { return; }
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deserialize = s => {
|
||||||
|
if ( s.startsWith(MAGICLZ4PREFIX) ) {
|
||||||
|
const lz4 = deserializeById(MAGICLZ4PREFIX, s);
|
||||||
|
if ( lz4 ) {
|
||||||
|
const lz4Util = new LZ4BlockJS();
|
||||||
|
const uint8ArrayAfter = lz4Util.decode(lz4.data, 0, lz4.size);
|
||||||
|
if ( uint8ArrayAfter ) {
|
||||||
|
s = textCodec.decode(new Uint8Array(uint8ArrayAfter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const data = s.startsWith(MAGICPREFIX)
|
||||||
|
? deserializeById(MAGICPREFIX, s)
|
||||||
|
: undefined;
|
||||||
|
uint8Input = null;
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
export const isSerialized = s =>
|
export const isSerialized = s =>
|
||||||
typeof s === 'string' &&
|
typeof s === 'string' &&
|
||||||
(s.startsWith(MAGICLZ4PREFIX) || s.startsWith(MAGICPREFIX));
|
(s.startsWith(MAGICLZ4PREFIX) || s.startsWith(MAGICPREFIX));
|
||||||
|
|
Loading…
Reference in New Issue