UNPKG

@taichunmin/crc

Version:

A cross platform alternative for calculating Cyclic Redundancy Checks (CRC) values.

57 lines (52 loc) 5.33 kB
"use strict";(()=>{function f(c){let r=typeof c;return c!=null&&(r==="object"||r==="function")}function h(c,r,e){if(!f(c)||!Array.isArray(r)||r.length<1)return c;let o=c;for(let i of r.slice(0,-1))f(o[i])||(o[i]={}),o=o[i];return o[r.at(-1)]=e,c}var m=new Uint8Array(1),p=new Uint16Array(1),y=new Uint32Array(1);var l=class{#r=new Uint8Array(256);constructor(){let r=this.#r;for(let e=0;e<256;e++){let o=e>>>1&85|(e&85)<<1;o=o>>>2&51|(o&51)<<2,r[e]=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}},s=new l;function x(c){return"0x"+`0${(c&255).toString(16).toUpperCase()}`.slice(-2)}var t=class{name;initial;poly;refin;refout;tbl=new Uint8Array(256);u8=new Uint8Array(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[e,o,i]=[this.u8,this.refin,this.tbl];for(let n=0;n<256;n++){e[0]=o?s.u8(n):n,i[n]=0;for(let u=0;u<8;u++)i[n]=(((i[n]^e[0])&128)!==0?r:0)^i[n]<<1,e[0]<<=1;o&&(i[n]=s.u8(i[n]))}}getCrc(r){let[e,o,i,n]=[this.u8,this.refout,this.tbl,this.xorout];e[0]=o?s.u8(this.initial):this.initial;for(let u of r)e[0]=i[e[0]^u];return e[0]^n}dumpPoly(r=0){let[e,o]=[this.u8,this.tbl],i=[];for(let n=0;n<32;n++){let u=[];for(let a=0;a<8;a++)e[0]=o[n*8+a],u.push(x(e[0]));i.push("".padStart(r)+u.join(", ")+`, `)}return i.join("")}exportCrcFn(){let r=x((this.refout?s.u8(this.initial):this.initial)^this.xorout),e=this.xorout===0?"":` const xorout = ${x(this.xorout)}`,o=this.xorout===0?"":" ^ xorout // revert xorout";return`import { setObject, u8 } from './common2' const POLY_TABLE = new Uint8Array([ ${this.dumpPoly(2).trim()} ]) /** * - poly: ${x(this.poly)} * - initial: ${x(this.initial)} * - xorout: ${x(this.xorout)} * - refin: ${this.refin} * - refout: ${this.refout} */${e} export default function ${this.name} (buf: Uint8Array = new Uint8Array(), prev: number = ${r}): number { u8[0] = prev${o} for (const b of buf) u8[0] = POLY_TABLE[u8[0] ^ b] return u8[0]${o} } setObject(globalThis, ['taichunmin', 'crc', '${this.name}'], ${this.name}) `}exportTest1(){let r=e=>x(this.getCrc(Buffer.from(e,"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=e=>x(this.getCrc(Buffer.from(e,"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 = hex === '' ? undefined : hexToU8Arr(hex) expect(${this.name}(u8arr)).toBe(parseInt(crc, 16)) }) test.each([ { crc: '${r("48656C6C6F20576F726C6421")}', hex: '48656C6C6F20576F726C6421' }, { crc: '${r("313233343536373839")}', hex: '313233343536373839' }, ])('#getCrc(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","GenericCrc8"],t);var d=new t({name:"crc8",poly:7,initial:0,xorout:0,refin:!1,refout:!1}),C=new t({name:"crc8autosar",poly:47,initial:255,xorout:255,refin:!1,refout:!1}),w=new t({name:"crc8bluetooth",poly:167,initial:0,xorout:0,refin:!0,refout:!0}),A=new t({name:"crc8cardx",poly:7,initial:44,xorout:0,refin:!1,refout:!1}),T=new t({name:"crc8cdma2000",poly:155,initial:255,xorout:0,refin:!1,refout:!1}),U=new t({name:"crc8darc",poly:57,initial:0,xorout:0,refin:!0,refout:!0}),j=new t({name:"crc8dvbs2",poly:213,initial:0,xorout:0,refin:!1,refout:!1}),B=new t({name:"crc8ebu",poly:29,initial:255,xorout:0,refin:!0,refout:!0}),v=new t({name:"crc8gsma",poly:29,initial:0,xorout:0,refin:!1,refout:!1}),D=new t({name:"crc8gsmb",poly:73,initial:0,xorout:255,refin:!1,refout:!1}),O=new t({name:"crc8hitag",poly:29,initial:255,xorout:0,refin:!1,refout:!1}),P=new t({name:"crc8icode",poly:29,initial:253,xorout:0,refin:!1,refout:!1}),H=new t({name:"crc8itu",poly:7,initial:0,xorout:85,refin:!1,refout:!1}),L=new t({name:"crc8legic",poly:99,initial:85,xorout:0,refin:!0,refout:!0}),S=new t({name:"crc8mad",poly:29,initial:199,xorout:0,refin:!1,refout:!1}),I=new t({name:"crc8maxim",poly:49,initial:0,xorout:0,refin:!0,refout:!0}),E=new t({name:"crc8nrsc5",poly:49,initial:255,xorout:0,refin:!1,refout:!1}),Y=new t({name:"crc8opensafety",poly:47,initial:0,xorout:0,refin:!1,refout:!1}),_=new t({name:"crc8rohc",poly:7,initial:255,xorout:0,refin:!0,refout:!0}),k=new t({name:"crc8saej1850",poly:29,initial:255,xorout:255,refin:!1,refout:!1}),q=new t({name:"crc8wcdma",poly:155,initial:0,xorout:0,refin:!0,refout:!0});})();