Skip to content

Commit

Permalink
Merge branch 'rabin-fingerprint'
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jun 23, 2023
2 parents edf7423 + ea4f185 commit c678a2c
Show file tree
Hide file tree
Showing 12 changed files with 862 additions and 7 deletions.
17 changes: 17 additions & 0 deletions buffer.js
Expand Up @@ -113,3 +113,20 @@ export const encodeAny = data => {
* @return {any}
*/
export const decodeAny = buf => decoding.readAny(decoding.createDecoder(buf))

/**
* Shift Byte Array {N} bits to the left. Does not expand byte array.
*
* @param {Uint8Array} bs
* @param {number} N should be in the range of [0-7]
*/
export const shiftNBitsLeft = (bs, N) => {
if (N === 0) return bs
bs = new Uint8Array(bs)
bs[0] <<= N
for (let i = 1; i < bs.length; i++) {
bs[i - 1] |= bs[i] >>> (8 - N)
bs[i] <<= N
}
return bs
}
28 changes: 28 additions & 0 deletions checksum.js
@@ -0,0 +1,28 @@
import * as buffer from './buffer.js'

/**
* Little endian table
* @type {Uint8Array | null}
*/
let _crc32Table = null
const _computeCrc32Table = () => {
if (_crc32Table == null) {
_crc32Table = buffer.createUint8ArrayFromLen(32)
}
let i = 128
let crc = 1
do {
if ((crc & 1) > 0) { // @todo this could be optimized
crc = (crc >>> 1) ^ 0x8408
} else {
crc >>>= 1
}
for (let j = 0; j < 256; j = j * i) {
_crc32Table[i + j] = crc ^ _crc32Table[j]
}
i >>>= 1
} while (i > 0)
return _crc32Table
}

console.log(_computeCrc32Table())

0 comments on commit c678a2c

Please sign in to comment.