@taichunmin/crc
Version:
A cross platform alternative for calculating Cyclic Redundancy Checks (CRC) values.
57 lines (52 loc) • 8.13 kB
JavaScript
;var f=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var b=(i,r)=>{for(var t in r)f(i,t,{get:r[t],enumerable:!0})},d=(i,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of h(r))!y.call(i,n)&&n!==t&&f(i,n,{get:()=>r[n],enumerable:!(o=F(r,n))||o.enumerable});return i};var w=i=>d(f({},"__esModule",{value:!0}),i);var rr={};b(rr,{GenericCrc16:()=>e,crc16a:()=>$,crc16arc:()=>g,crc16augccitt:()=>C,crc16buypass:()=>A,crc16ccittfalse:()=>T,crc16cdma2000:()=>U,crc16cms:()=>j,crc16dds110:()=>B,crc16dectr:()=>O,crc16dectx:()=>v,crc16dnp:()=>P,crc16en13757:()=>L,crc16genibus:()=>k,crc16gsm:()=>D,crc16iclass:()=>E,crc16kermit:()=>H,crc16lj1200:()=>S,crc16m17:()=>I,crc16maxim:()=>Y,crc16mcrf4xx:()=>_,crc16modbus:()=>q,crc16nrsc5:()=>z,crc16opensafetya:()=>G,crc16opensafetyb:()=>J,crc16philips:()=>K,crc16profibus:()=>M,crc16riello:()=>N,crc16t10dif:()=>Q,crc16teledisk:()=>R,crc16tms37157:()=>V,crc16usb:()=>W,crc16x25:()=>X,crc16xmodem:()=>Z});module.exports=w(rr);function p(i){let r=typeof i;return i!=null&&(r==="object"||r==="function")}function m(i,r,t){if(!p(i)||!Array.isArray(r)||r.length<1)return i;let o=i;for(let n of r.slice(0,-1))p(o[n])||(o[n]={}),o=o[n];return o[r.at(-1)]=t,i}var tr=new Uint8Array(1),or=new Uint16Array(1),nr=new Uint32Array(1);var l=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}},u=new l;function s(i){return"0x"+`000${(i&65535).toString(16).toUpperCase()}`.slice(-4)}var e=class{name;initial;poly;refin;refout;tbl=new Uint16Array(256);u16=new Uint16Array(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,n]=[this.u16,this.refin,this.tbl];for(let c=0;c<256;c++){t[0]=(o?u.u8(c):c)<<8,n[c]=0;for(let x=0;x<8;x++)n[c]=(((n[c]^t[0])&32768)!==0?r:0)^n[c]<<1,t[0]<<=1;o&&(n[c]=u.u16(n[c]))}}getCrc(r){let[t,o,n,c]=[this.u16,this.refout,this.tbl,this.xorout];if(o){t[0]=u.u16(this.initial);for(let x of r)t[0]=n[(t[0]^x)&255]^t[0]>>>8}else{t[0]=this.initial;for(let x of r)t[0]=n[t[0]>>>8^x]^t[0]<<8}return t[0]^c}dumpPoly(r=0){let[t,o]=[this.u16,this.tbl],n=[];for(let c=0;c<32;c++){let x=[];for(let a=0;a<8;a++)t[0]=o[c*8+a],x.push(s(t[0]));n.push("".padStart(r)+x.join(", ")+`,
`)}return n.join("")}exportCrcFn(){let r=s((this.refout?u.u16(this.initial):this.initial)^this.xorout),t=this.xorout===0?"":`
const xorout = ${s(this.xorout)}`,o=this.xorout===0?"":" ^ xorout // revert xorout",n=this.refin?"POLY_TABLE[(u16[0] ^ b) & 0xFF] ^ (u16[0] >>> 8)":"POLY_TABLE[(u16[0] >>> 8) ^ b] ^ (u16[0] << 8)";return`import { setObject, u16 } from './common2'
const POLY_TABLE = new Uint16Array([
${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 {
u16[0] = prev${o}
for (const b of buf) u16[0] = ${n}
return u16[0]${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))
})
})
`}};m(globalThis,["taichunmin","crc","GenericCrc16"],e);var $=new e({name:"crc16a",poly:4129,initial:50886,xorout:0,refin:!0,refout:!0}),g=new e({name:"crc16arc",poly:32773,initial:0,xorout:0,refin:!0,refout:!0}),C=new e({name:"crc16augccitt",poly:4129,initial:7439,xorout:0,refin:!1,refout:!1}),A=new e({name:"crc16buypass",poly:32773,initial:0,xorout:0,refin:!1,refout:!1}),T=new e({name:"crc16ccittfalse",poly:4129,initial:65535,xorout:0,refin:!1,refout:!1}),U=new e({name:"crc16cdma2000",poly:51303,initial:65535,xorout:0,refin:!1,refout:!1}),j=new e({name:"crc16cms",poly:32773,initial:65535,xorout:0,refin:!1,refout:!1}),B=new e({name:"crc16dds110",poly:32773,initial:32781,xorout:0,refin:!1,refout:!1}),O=new e({name:"crc16dectr",poly:1417,initial:0,xorout:1,refin:!1,refout:!1}),v=new e({name:"crc16dectx",poly:1417,initial:0,xorout:0,refin:!1,refout:!1}),P=new e({name:"crc16dnp",poly:15717,initial:0,xorout:65535,refin:!0,refout:!0}),L=new e({name:"crc16en13757",poly:15717,initial:0,xorout:65535,refin:!1,refout:!1}),k=new e({name:"crc16genibus",poly:4129,initial:65535,xorout:65535,refin:!1,refout:!1}),D=new e({name:"crc16gsm",poly:4129,initial:0,xorout:65535,refin:!1,refout:!1}),E=new e({name:"crc16iclass",poly:4129,initial:18439,xorout:3011,refin:!0,refout:!0}),H=new e({name:"crc16kermit",poly:4129,initial:0,xorout:0,refin:!0,refout:!0}),S=new e({name:"crc16lj1200",poly:28515,initial:0,xorout:0,refin:!1,refout:!1}),I=new e({name:"crc16m17",poly:22837,initial:65535,xorout:0,refin:!1,refout:!1}),Y=new e({name:"crc16maxim",poly:32773,initial:0,xorout:65535,refin:!0,refout:!0}),_=new e({name:"crc16mcrf4xx",poly:4129,initial:65535,xorout:0,refin:!0,refout:!0}),q=new e({name:"crc16modbus",poly:32773,initial:65535,xorout:0,refin:!0,refout:!0}),z=new e({name:"crc16nrsc5",poly:2059,initial:65535,xorout:0,refin:!0,refout:!0}),G=new e({name:"crc16opensafetya",poly:22837,initial:0,xorout:0,refin:!1,refout:!1}),J=new e({name:"crc16opensafetyb",poly:30043,initial:0,xorout:0,refin:!1,refout:!1}),K=new e({name:"crc16philips",poly:4129,initial:18851,xorout:0,refin:!0,refout:!0}),M=new e({name:"crc16profibus",poly:7631,initial:65535,xorout:65535,refin:!1,refout:!1}),N=new e({name:"crc16riello",poly:4129,initial:45738,xorout:0,refin:!0,refout:!0}),Q=new e({name:"crc16t10dif",poly:35767,initial:0,xorout:0,refin:!1,refout:!1}),R=new e({name:"crc16teledisk",poly:41111,initial:0,xorout:0,refin:!1,refout:!1}),V=new e({name:"crc16tms37157",poly:4129,initial:35308,xorout:0,refin:!0,refout:!0}),W=new e({name:"crc16usb",poly:32773,initial:65535,xorout:65535,refin:!0,refout:!0}),X=new e({name:"crc16x25",poly:4129,initial:65535,xorout:65535,refin:!0,refout:!0}),Z=new e({name:"crc16xmodem",poly:4129,initial:0,xorout:0,refin:!1,refout:!1});0&&(module.exports={GenericCrc16,crc16a,crc16arc,crc16augccitt,crc16buypass,crc16ccittfalse,crc16cdma2000,crc16cms,crc16dds110,crc16dectr,crc16dectx,crc16dnp,crc16en13757,crc16genibus,crc16gsm,crc16iclass,crc16kermit,crc16lj1200,crc16m17,crc16maxim,crc16mcrf4xx,crc16modbus,crc16nrsc5,crc16opensafetya,crc16opensafetyb,crc16philips,crc16profibus,crc16riello,crc16t10dif,crc16teledisk,crc16tms37157,crc16usb,crc16x25,crc16xmodem});