Skip to content

Commit

Permalink
Use a crc library
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Dec 28, 2024
1 parent 7ee8043 commit 222b219
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"build": "node ./bin/build.mjs"
},
"dependencies": {
"@hqtsm/crc": "^1.0.3",
"@xmldom/xmldom": "^0.8.10"
},
"devDependencies": {
Expand Down
33 changes: 5 additions & 28 deletions util/crc64xz.mjs
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
/* eslint-disable no-bitwise */

import {Writable} from 'node:stream';

function createTable() {
const table = new BigUint64Array(256);
let crc;
for (let i = 0n; i < 256n; i++) {
crc = i;
for (let j = 0; j < 8; j++) {
crc = crc & 1n ? 0xc96c5795d7870f42n ^ (crc >> 1n) : crc >> 1n;
}
table[i] = crc;
}
return table;
}

let TABLE;
import CRC64 from '@hqtsm/crc/crc-64/xz';

export class Crc64xz extends Writable {
constructor() {
super();

TABLE = TABLE || createTable();
this._state = 0xffffffffffffffffn;
this._state = CRC64.init();
}

update(chunk, encoding) {
let state = this._state;
const b = Buffer.from(chunk, encoding);
const {length} = b;
for (let i = 0; i < length; i++) {
state = TABLE[Number(state & 0xffn) ^ b[i]] ^ (state >> 8n);
}
this._state = state;
this._state = CRC64.update(this._state, b);
}

digest(encoding = null) {
let state = this._state;
state = state ^ 0xffffffffffffffffn;
const value = CRC64.finalize(this._state);
this._state = null;
const e = Buffer.alloc(8);
e.writeBigUint64BE(state);
e.writeBigUint64BE(value);
return encoding ? e.toString(encoding) : e;
}

Expand Down

0 comments on commit 222b219

Please sign in to comment.