-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as pako from '../node_modules/pako/dist/pako.esm.mjs'; | ||
import { struct } from './core.js'; | ||
|
||
const zlib_decompress = function (buf, itemsize) { | ||
let input_array = new Uint8Array(buf); | ||
return pako.inflate(input_array).buffer; | ||
} | ||
|
||
const unshuffle = function (buf, itemsize) { | ||
let buffer_size = buf.byteLength; | ||
let unshuffled_view = new Uint8Array(buffer_size); | ||
let step = Math.floor(buffer_size / itemsize); | ||
let shuffled_view = new DataView(buf); | ||
for (var j = 0; j < itemsize; j++) { | ||
for (var i = 0; i < step; i++) { | ||
unshuffled_view[j + i * itemsize] = shuffled_view.getUint8(j * step + i); | ||
} | ||
} | ||
return unshuffled_view.buffer; | ||
} | ||
|
||
const fletch32 = function (buf, itemsize) { | ||
_verify_fletcher32(buf); | ||
//# strip off 4-byte checksum from end of buffer | ||
return buf.slice(0, -4); | ||
} | ||
|
||
function _verify_fletcher32(chunk_buffer) { | ||
//""" Verify a chunk with a fletcher32 checksum. """ | ||
//# calculate checksums | ||
var odd_chunk_buffer = ((chunk_buffer.byteLength % 2) != 0); | ||
var data_length = chunk_buffer.byteLength - 4; | ||
var view = new DataView(chunk_buffer); | ||
|
||
var sum1 = 0; | ||
var sum2 = 0; | ||
for (var offset=0; offset<(data_length-1); offset+=2) { | ||
let datum = view.getUint16(offset, true); // little-endian | ||
sum1 = (sum1 + datum) % 65535 | ||
sum2 = (sum2 + sum1) % 65535 | ||
} | ||
if (odd_chunk_buffer) { | ||
// process the last item: | ||
let datum = view.getUint8(data_length-1); | ||
sum1 = (sum1 + datum) % 65535 | ||
sum2 = (sum2 + sum1) % 65535 | ||
} | ||
|
||
//# extract stored checksums | ||
var [ref_sum1, ref_sum2] = struct.unpack_from('>HH', chunk_buffer, data_length); // .fromstring(chunk_buffer[-4:], '>u2') | ||
ref_sum1 = ref_sum1 % 65535 | ||
ref_sum2 = ref_sum2 % 65535 | ||
|
||
//# compare | ||
if (sum1 != ref_sum1 || sum2 != ref_sum2) { | ||
throw 'ValueError("fletcher32 checksum invalid")'; | ||
} | ||
return true | ||
} | ||
|
||
//# IV.A.2.l The Data Storage - Filter Pipeline message | ||
var RESERVED_FILTER = 0; | ||
const GZIP_DEFLATE_FILTER = 1; | ||
const SHUFFLE_FILTER = 2; | ||
const FLETCH32_FILTER = 3; | ||
var SZIP_FILTER = 4; | ||
var NBIT_FILTER = 5; | ||
var SCALEOFFSET_FILTER = 6; | ||
|
||
// To register a new filter, add a function (ArrayBuffer) => ArrayBuffer | ||
// the the following map, using a key that corresponds to filter_id (int) | ||
export const Filters = new Map([ | ||
[GZIP_DEFLATE_FILTER, zlib_decompress], | ||
[SHUFFLE_FILTER, unshuffle], | ||
[FLETCH32_FILTER, fletch32] | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters