@taichunmin/crc
Version:
A cross platform alternative for calculating Cyclic Redundancy Checks (CRC) values.
56 lines (51 loc) • 6.61 kB
JavaScript
"use strict";(()=>{function l(c){let r=typeof c;return c!=null&&(r==="object"||r==="function")}function p(c,r,t){if(!l(c)||!Array.isArray(r)||r.length<1)return c;let o=c;for(let n of r.slice(0,-1))l(o[n])||(o[n]={}),o=o[n];return o[r.at(-1)]=t,c}var m=new Uint8Array(1),F=new Uint16Array(1),h=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}},u=new f;function s(c){return"0x"+`000${(c&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 i=0;i<256;i++){t[0]=(o?u.u8(i):i)<<8,n[i]=0;for(let x=0;x<8;x++)n[i]=(((n[i]^t[0])&32768)!==0?r:0)^n[i]<<1,t[0]<<=1;o&&(n[i]=u.u16(n[i]))}}getCrc(r){let[t,o,n,i]=[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]^i}dumpPoly(r=0){let[t,o]=[this.u16,this.tbl],n=[];for(let i=0;i<32;i++){let x=[];for(let a=0;a<8;a++)t[0]=o[i*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?"":` ^ ${s(this.xorout)}`,o=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}
*/
export default function ${this.name} (buf: Uint8Array = new Uint8Array(), prev: number = ${r}): number {
u16[0] = prev${t} // revert of refout and xorout
for (const b of buf) u16[0] = ${o}
return u16[0]${t}
}
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))
})
})
`}};p(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});})();