@taichunmin/crc
Version:
A cross platform alternative for calculating Cyclic Redundancy Checks (CRC) values.
57 lines (52 loc) • 5.38 kB
JavaScript
function l(c){let r=typeof c;return c!=null&&(r==="object"||r==="function")}function f(c,r,t){if(!l(c)||!Array.isArray(r)||r.length<1)return c;let e=c;for(let o of r.slice(0,-1))l(e[o])||(e[o]={}),e=e[o];return e[r.at(-1)]=t,c}var h=new Uint8Array(1),m=new Uint16Array(1),p=new Uint32Array(1);var a=class{#r=new Uint8Array(256);constructor(){let r=this.#r;for(let t=0;t<256;t++){let e=t>>>1&85|(t&85)<<1;e=e>>>2&51|(e&51)<<2,r[t]=e>>>4&15|(e&15)<<4}}u8(r){return this.#r[r&255]}u16(r){return this.u8(r)<<8|this.u8(r>>>8)}u32(r){return(this.u16(r)<<16|this.u16(r>>>16))>>>0}},x=new a;function s(c){return"0x"+`0000000${(c>>>0).toString(16).toUpperCase()}`.slice(-8)}var i=class{name;initial;poly;refin;refout;tbl=new Uint32Array(256);u32=new Uint32Array(1);xorout;constructor(r){this.name=r.name,this.poly=r.poly,this.initial=r.initial,this.xorout=r.xorout,this.refin=r.refin,this.refout=r.refout,this.buildPoly(r.poly)}buildPoly(r){let[t,e,o]=[this.u32,this.refin,this.tbl];for(let n=0;n<256;n++){t[0]=(e?x.u8(n):n)<<24,o[n]=0;for(let u=0;u<8;u++)o[n]=(((o[n]^t[0])&2147483648)!==0?r:0)^o[n]<<1,t[0]<<=1;e&&(o[n]=x.u32(o[n]))}}getCrc(r){let[t,e,o,n]=[this.u32,this.refout,this.tbl,this.xorout];if(e){t[0]=x.u32(this.initial);for(let u of r)t[0]=o[(t[0]^u)&255]^t[0]>>>8}else{t[0]=this.initial;for(let u of r)t[0]=o[t[0]>>>24^u]^t[0]<<8}return(t[0]^n)>>>0}dumpPoly(r=0){let[t,e]=[this.u32,this.tbl],o=[];for(let n=0;n<32;n++){let u=[];for(let F=0;F<8;F++)t[0]=e[n*8+F],u.push(s(t[0]));o.push("".padStart(r)+u.join(", ")+`,
`)}return o.join("")}exportCrcFn(){let r=s((this.refout?x.u32(this.initial):this.initial)^this.xorout),t=this.xorout===0?"":`
const xorout = ${s(this.xorout)}`,e=this.xorout===0?"":" ^ xorout // revert xorout",o=this.xorout===0?"u32[0]":"(u32[0] ^ xorout) >>> 0",n=this.refin?"POLY_TABLE[(u32[0] ^ b) & 0xFF] ^ (u32[0] >>> 8)":"POLY_TABLE[(u32[0] >>> 24) ^ b] ^ (u32[0] << 8)";return`import { setObject, u32 } from './common2'
const POLY_TABLE = new Uint32Array([
${this.dumpPoly(2).trim()}
])
/**
* - poly: ${s(this.poly)}
* - initial: ${s(this.initial)}
* - xorout: ${s(this.xorout)}
* - refin: ${this.refin}
* - refout: ${this.refout}
*/${t}
export default function ${this.name} (buf: Uint8Array = new Uint8Array(), prev: number = ${r}): number {
u32[0] = prev${e}
for (const b of buf) u32[0] = ${n}
return ${o}
}
setObject(globalThis, ['taichunmin', 'crc', '${this.name}'], ${this.name})
`}exportTest1(){let r=t=>s(this.getCrc(Buffer.from(t,"hex"))).slice(2);return`
describe('${this.name}', () => {
test.each([
{ crc: '${r("")}', hex: '' },
{ crc: '${r("31")}', hex: '31' },
{ crc: '${r("48656C6C6F20576F726C6421")}', hex: '48656C6C6F20576F726C6421' },
{ crc: '${r("313233343536373839")}', hex: '313233343536373839' },
])('#getCrc(Buffer.from("$hex", "hex")) = 0x$crc', ({ hex, crc }) => {
const u8arr = hexToU8Arr(hex)
expect(sut.${this.name}.getCrc(u8arr)).toBe(parseInt(crc, 16))
})
})
`}exportTest2(){let r=t=>s(this.getCrc(Buffer.from(t,"hex"))).slice(2);return`
describe('${this.name}', () => {
test.each([
{ crc: '${r("")}', hex: '' },
{ crc: '${r("31")}', hex: '31' },
{ crc: '${r("48656C6C6F20576F726C6421")}', hex: '48656C6C6F20576F726C6421' },
{ crc: '${r("313233343536373839")}', hex: '313233343536373839' },
])('${this.name}(Buffer.from("$hex", "hex")) = 0x$crc', ({ hex, crc }) => {
const u8arr = hex === '' ? undefined : hexToU8Arr(hex)
expect(${this.name}(u8arr)).toBe(parseInt(crc, 16))
})
test.each([
{ crc: '${r("48656C6C6F20576F726C6421")}', hex: '48656C6C6F20576F726C6421' },
{ crc: '${r("313233343536373839")}', hex: '313233343536373839' },
])('${this.name}(Buffer.from("$hex", "hex")) = 0x$crc', ({ hex, crc }) => {
const u8arr = hexToU8Arr(hex)
const prev = ${this.name}(u8arr.subarray(0, 1))
expect(${this.name}(u8arr.subarray(1), prev)).toBe(parseInt(crc, 16))
})
})
`}};f(globalThis,["taichunmin","crc","GenericCrc32"],i);var g=new i({name:"crc32",poly:79764919,initial:4294967295,xorout:4294967295,refin:!0,refout:!0}),d=new i({name:"crc32autosar",poly:4104977171,initial:4294967295,xorout:4294967295,refin:!0,refout:!0}),w=new i({name:"crc32bzip2",poly:79764919,initial:4294967295,xorout:4294967295,refin:!1,refout:!1}),A=new i({name:"crc32c",poly:517762881,initial:4294967295,xorout:4294967295,refin:!0,refout:!0}),B=new i({name:"crc32cdromedc",poly:2147581979,initial:0,xorout:0,refin:!0,refout:!0}),T=new i({name:"crc32d",poly:2821953579,initial:4294967295,xorout:4294967295,refin:!0,refout:!0}),U=new i({name:"crc32jamcrc",poly:79764919,initial:4294967295,xorout:0,refin:!0,refout:!0}),j=new i({name:"crc32mef",poly:1947962583,initial:4294967295,xorout:0,refin:!0,refout:!0}),D=new i({name:"crc32mpeg2",poly:79764919,initial:4294967295,xorout:0,refin:!1,refout:!1}),O=new i({name:"crc32posix",poly:79764919,initial:0,xorout:4294967295,refin:!1,refout:!1}),v=new i({name:"crc32q",poly:2168537515,initial:0,xorout:0,refin:!1,refout:!1}),P=new i({name:"crc32sata",poly:79764919,initial:1379029042,xorout:0,refin:!1,refout:!1}),L=new i({name:"crc32xfer",poly:175,initial:0,xorout:0,refin:!1,refout:!1});export{i as GenericCrc32,g as crc32,d as crc32autosar,w as crc32bzip2,A as crc32c,B as crc32cdromedc,T as crc32d,U as crc32jamcrc,j as crc32mef,D as crc32mpeg2,O as crc32posix,v as crc32q,P as crc32sata,L as crc32xfer};