UNPKG

@taichunmin/crc

Version:

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

56 lines (51 loc) 6.3 kB
"use strict";var l=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var F=(i,r)=>{for(var e in r)l(i,e,{get:r[e],enumerable:!0})},g=(i,r,e,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of y(r))!b.call(i,n)&&n!==e&&l(i,n,{get:()=>r[n],enumerable:!(o=p(r,n))||o.enumerable});return i};var $=i=>g(l({},"__esModule",{value:!0}),i);var z={};F(z,{GenericCrc8:()=>t,crc8:()=>d,crc8autosar:()=>C,crc8bluetooth:()=>w,crc8cardx:()=>A,crc8cdma2000:()=>T,crc8darc:()=>U,crc8dvbs2:()=>j,crc8ebu:()=>B,crc8gsma:()=>v,crc8gsmb:()=>D,crc8hitag:()=>O,crc8icode:()=>P,crc8itu:()=>H,crc8legic:()=>L,crc8mad:()=>S,crc8maxim:()=>I,crc8nrsc5:()=>E,crc8opensafety:()=>Y,crc8rohc:()=>_,crc8saej1850:()=>k,crc8wcdma:()=>q});module.exports=$(z);function h(i){let r=typeof i;return i!=null&&(r==="object"||r==="function")}function m(i,r,e){if(!h(i)||!Array.isArray(r)||r.length<1)return i;let o=i;for(let n of r.slice(0,-1))h(o[n])||(o[n]={}),o=o[n];return o[r.at(-1)]=e,i}var J=new Uint8Array(1),K=new Uint16Array(1),M=new Uint32Array(1);var f=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}},u=new f;function x(i){return"0x"+`0${(i&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,n]=[this.u8,this.refin,this.tbl];for(let c=0;c<256;c++){e[0]=o?u.u8(c):c,n[c]=0;for(let s=0;s<8;s++)n[c]=(((n[c]^e[0])&128)!==0?r:0)^n[c]<<1,e[0]<<=1;o&&(n[c]=u.u8(n[c]))}}getCrc(r){let[e,o,n,c]=[this.u8,this.refout,this.tbl,this.xorout];e[0]=o?u.u8(this.initial):this.initial;for(let s of r)e[0]=n[e[0]^s];return e[0]^c}dumpPoly(r=0){let[e,o]=[this.u8,this.tbl],n=[];for(let c=0;c<32;c++){let s=[];for(let a=0;a<8;a++)e[0]=o[c*8+a],s.push(x(e[0]));n.push("".padStart(r)+s.join(", ")+`, `)}return n.join("")}exportCrcFn(){let r=x((this.refout?u.u8(this.initial):this.initial)^this.xorout),e=this.xorout===0?"":` ^ ${x(this.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} */ export default function ${this.name} (buf: Uint8Array = new Uint8Array(), prev: number = ${r}): number { u8[0] = prev${e} for (const b of buf) u8[0] = POLY_TABLE[u8[0] ^ b] return u8[0]${e} } 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)) }) }) `}};m(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});0&&(module.exports={GenericCrc8,crc8,crc8autosar,crc8bluetooth,crc8cardx,crc8cdma2000,crc8darc,crc8dvbs2,crc8ebu,crc8gsma,crc8gsmb,crc8hitag,crc8icode,crc8itu,crc8legic,crc8mad,crc8maxim,crc8nrsc5,crc8opensafety,crc8rohc,crc8saej1850,crc8wcdma});