@taichunmin/crc
Version:
A cross platform alternative for calculating Cyclic Redundancy Checks (CRC) values.
56 lines (51 loc) • 5.96 kB
JavaScript
;var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var y=(n,r)=>{for(var t in r)a(n,t,{get:r[t],enumerable:!0})},$=(n,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let e of p(r))!b.call(n,e)&&e!==t&&a(n,e,{get:()=>r[e],enumerable:!(o=m(r,e))||o.enumerable});return n};var C=n=>$(a({},"__esModule",{value:!0}),n);var E={};y(E,{GenericCrc32:()=>i,crc32:()=>g,crc32autosar:()=>d,crc32bzip2:()=>w,crc32c:()=>A,crc32cdromedc:()=>B,crc32d:()=>T,crc32jamcrc:()=>U,crc32mef:()=>j,crc32mpeg2:()=>D,crc32posix:()=>O,crc32q:()=>v,crc32sata:()=>P,crc32xfer:()=>L});module.exports=C(E);function l(n){let r=typeof n;return n!=null&&(r==="object"||r==="function")}function h(n,r,t){if(!l(n)||!Array.isArray(r)||r.length<1)return n;let o=n;for(let e of r.slice(0,-1))l(o[e])||(o[e]={}),o=o[e];return o[r.at(-1)]=t,n}var S=new Uint8Array(1),I=new Uint16Array(1),Y=new Uint32Array(1);var f=class{#r=new Uint8Array(256);constructor(){let r=this.#r;for(let t=0;t<256;t++){let o=t>>>1&85|(t&85)<<1;o=o>>>2&51|(o&51)<<2,r[t]=o>>>4&15|(o&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 f;function s(n){return"0x"+`0000000${(n>>>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,o,e]=[this.u32,this.refin,this.tbl];for(let c=0;c<256;c++){t[0]=(o?x.u8(c):c)<<24,e[c]=0;for(let u=0;u<8;u++)e[c]=(((e[c]^t[0])&2147483648)!==0?r:0)^e[c]<<1,t[0]<<=1;o&&(e[c]=x.u32(e[c]))}}getCrc(r){let[t,o,e,c]=[this.u32,this.refout,this.tbl,this.xorout];if(o){t[0]=x.u32(this.initial);for(let u of r)t[0]=e[(t[0]^u)&255]^t[0]>>>8}else{t[0]=this.initial;for(let u of r)t[0]=e[t[0]>>>24^u]^t[0]<<8}return(t[0]^c)>>>0}dumpPoly(r=0){let[t,o]=[this.u32,this.tbl],e=[];for(let c=0;c<32;c++){let u=[];for(let F=0;F<8;F++)t[0]=o[c*8+F],u.push(s(t[0]));e.push("".padStart(r)+u.join(", ")+`,
`)}return e.join("")}exportCrcFn(){let r=s((this.refout?x.u32(this.initial):this.initial)^this.xorout),t=this.xorout===0?"":` ^ ${s(this.xorout)}`,o=this.refin?"POLY_TABLE[(u32[0] ^ b) & 0xFF] ^ (u32[0] >>> 8)":"POLY_TABLE[(u32[0] >>> 24) ^ b] ^ (u32[0] << 8)",e=t===""?"u32[0]":`(u32[0]${t}) >>> 0`;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}
*/
export default function ${this.name} (buf: Uint8Array = new Uint8Array(), prev: number = ${r}): number {
u32[0] = prev${t} // revert of refout and xorout
for (const b of buf) u32[0] = ${o}
return ${e}
}
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))
})
})
`}};h(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});0&&(module.exports={GenericCrc32,crc32,crc32autosar,crc32bzip2,crc32c,crc32cdromedc,crc32d,crc32jamcrc,crc32mef,crc32mpeg2,crc32posix,crc32q,crc32sata,crc32xfer});