···11+import { crc32 } from '@mary/crc32';
22+13import {
24 ChecksumMismatchError,
35 InvalidSignatureError,
···79 UnsupportedFeatureError,
810} from '../errors.ts';
911import { concat, decodeUtf8From, read } from '../utils/buffer.ts';
1010-import { crc32 } from '../utils/crc32.ts';
1112import {
1213 crc32Verifier,
1314 type DataSegment,
-36
lib/utils/crc32.ts
···11-const TABLE = /*#__PURE__*/ (() => {
22- const table = new Uint32Array(256);
33-44- for (let idx = 0; idx < 256; idx++) {
55- let c = idx;
66-77- for (let bit = 0; bit < 8; bit++) {
88- c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
99- }
1010-1111- table[idx] = c;
1212- }
1313-1414- return table;
1515-})();
1616-1717-/**
1818- * computes the CRC-32 checksum of a byte sequence, matching the algorithm RAR
1919- * uses for header and file data checksums.
2020- *
2121- * the function can be called incrementally by feeding the previous result back
2222- * as `seed`, allowing a checksum to be built up across streamed chunks.
2323- *
2424- * @param data bytes to checksum
2525- * @param seed running checksum from a previous chunk, or 0 to start fresh
2626- * @returns the unsigned 32-bit checksum
2727- */
2828-export const crc32 = (data: Uint8Array, seed: number = 0): number => {
2929- let crc = (seed ^ 0xffffffff) >>> 0;
3030-3131- for (let idx = 0, len = data.length; idx < len; idx++) {
3232- crc = TABLE[(crc ^ data[idx]) & 0xff] ^ (crc >>> 8);
3333- }
3434-3535- return (crc ^ 0xffffffff) >>> 0;
3636-};