Fixed linting issues and added CRC-8 tests

This commit is contained in:
mshwed 2019-07-02 15:30:59 -04:00
parent bc1bd2427d
commit 44643c151a
2 changed files with 164 additions and 44 deletions

View file

@ -43,7 +43,7 @@ class CRC8Checksum extends Operation {
"CRC-8/WCDMA" "CRC-8/WCDMA"
] ]
} }
] ];
} }
/** /**
@ -58,7 +58,7 @@ class CRC8Checksum extends Operation {
for (let i = 0; i < 256; i++) { for (let i = 0; i < 256; i++) {
currentByte = i; currentByte = i;
for (let bit = 0; bit < 8; bit++) { for (let bit = 0; bit < 8; bit++) {
if ((currentByte & 0x80) != 0) { if ((currentByte & 0x80) !== 0) {
currentByte <<= 1; currentByte <<= 1;
currentByte ^= polynomial; currentByte ^= polynomial;
} else { } else {
@ -81,26 +81,25 @@ class CRC8Checksum extends Operation {
* @param {boolean} inputReflection * @param {boolean} inputReflection
* @param {boolean} outputReflection * @param {boolean} outputReflection
* @param {number} xorOut * @param {number} xorOut
* @param {number} check
*/ */
calculateCRC8(input, polynomial, initializationValue, inputReflection, outputReflection, xorOut, check) { calculateCRC8(input, polynomial, initializationValue, inputReflection, outputReflection, xorOut) {
const crcSize = 8; const crcSize = 8;
const crcTable = this.calculateCRC8LookupTable(polynomial); const crcTable = this.calculateCRC8LookupTable(polynomial);
let crc = initializationValue != 0 ? initializationValue : 0; let crc = initializationValue !== 0 ? initializationValue : 0;
let currentByte, position; let currentByte, position;
input = new Uint8Array(input); input = new Uint8Array(input);
for (let inputByte of input) { for (const inputByte of input) {
currentByte = inputReflection ? this.reverseBits(inputByte, crcSize) : inputByte; currentByte = inputReflection ? this.reverseBits(inputByte, crcSize) : inputByte;
position = (currentByte ^ crc ) & 255; position = (currentByte ^ crc) & 255;
crc = crcTable[position]; crc = crcTable[position];
} }
crc = outputReflection ? this.reverseBits(crc, crcSize) : crc; crc = outputReflection ? this.reverseBits(crc, crcSize) : crc;
if (xorOut != 0) crc = crc ^ xorOut; if (xorOut !== 0) crc = crc ^ xorOut;
return toHex(new Uint8Array([crc])); return toHex(new Uint8Array([crc]));
} }
@ -129,25 +128,25 @@ class CRC8Checksum extends Operation {
const algorithm = args[0]; const algorithm = args[0];
if (algorithm === "CRC-8") { if (algorithm === "CRC-8") {
return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x0, 0xF4); return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x0);
} else if (algorithm === "CRC-8/CDMA2000") { } else if (algorithm === "CRC-8/CDMA2000") {
return this.calculateCRC8(input, 0x9B, 0xFF, false, false, 0x0, 0xDA); return this.calculateCRC8(input, 0x9B, 0xFF, false, false, 0x0);
} else if (algorithm === "CRC-8/DARC") { } else if (algorithm === "CRC-8/DARC") {
return this.calculateCRC8(input, 0x39, 0x0, true, true, 0x0, 0x15); return this.calculateCRC8(input, 0x39, 0x0, true, true, 0x0);
} else if (algorithm === "CRC-8/DVB-S2") { } else if (algorithm === "CRC-8/DVB-S2") {
return this.calculateCRC8(input, 0xD5, 0x0, false, false, 0x0, 0xBC); return this.calculateCRC8(input, 0xD5, 0x0, false, false, 0x0);
} else if (algorithm === "CRC-8/EBU") { } else if (algorithm === "CRC-8/EBU") {
return this.calculateCRC8(input, 0x1D, 0xFF, true, true, 0x0, 0x97); return this.calculateCRC8(input, 0x1D, 0xFF, true, true, 0x0);
} else if (algorithm === "CRC-8/I-CODE") { } else if (algorithm === "CRC-8/I-CODE") {
return this.calculateCRC8(input, 0x1D, 0xFD, false, false, 0x0, 0x7E); return this.calculateCRC8(input, 0x1D, 0xFD, false, false, 0x0);
} else if (algorithm === "CRC-8/ITU") { } else if (algorithm === "CRC-8/ITU") {
return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x55, 0xA1); return this.calculateCRC8(input, 0x7, 0x0, false, false, 0x55);
} else if (algorithm === "CRC-8/MAXIM") { } else if (algorithm === "CRC-8/MAXIM") {
return this.calculateCRC8(input, 0x31, 0x0, true, true, 0x0, 0xA1); return this.calculateCRC8(input, 0x31, 0x0, true, true, 0x0);
} else if (algorithm === "CRC-8/ROHC") { } else if (algorithm === "CRC-8/ROHC") {
return this.calculateCRC8(input, 0x7, 0xFF, true, true, 0x0, 0xD0); return this.calculateCRC8(input, 0x7, 0xFF, true, true, 0x0);
} else if (algorithm === "CRC-8/WCDMA") { } else if (algorithm === "CRC-8/WCDMA") {
return this.calculateCRC8(input, 0x9B, 0x0, true, true, 0x0, 0x25); return this.calculateCRC8(input, 0x9B, 0x0, true, true, 0x0);
} }
throw new OperationError("Unknown checksum algorithm"); throw new OperationError("Unknown checksum algorithm");

View file

@ -29,6 +29,127 @@ const ALL_BYTES = [
].join(""); ].join("");
TestRegister.addTests([ TestRegister.addTests([
{
name: "CRC-8: nothing",
input: "",
expectedOutput: "00",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8"]
}
]
},
{
name: "CRC-8: default check",
input: "123456789",
expectedOutput: "f4",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8"]
}
]
},
{
name: "CRC-8: CDMA2000",
input: "123456789",
expectedOutput: "da",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/CDMA2000"]
}
]
},
{
name: "CRC-8: DARC",
input: "123456789",
expectedOutput: "15",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/DARC"]
}
]
},
{
name: "CRC-8: DVB-S2",
input: "123456789",
expectedOutput: "bc",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/DVB-S2"]
}
]
},
{
name: "CRC-8: EBU",
input: "123456789",
expectedOutput: "97",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/EBU"]
}
]
},
{
name: "CRC-8: I-CODE",
input: "123456789",
expectedOutput: "7e",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/I-CODE"]
}
]
},
{
name: "CRC-8: ITU",
input: "123456789",
expectedOutput: "a1",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/ITU"]
}
]
},
{
name: "CRC-8: MAXIM",
input: "123456789",
expectedOutput: "a1",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/MAXIM"]
}
]
},
{
name: "CRC-8: ROHC",
input: "123456789",
expectedOutput: "d0",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/ROHC"]
}
]
},
{
name: "CRC-8: WCDMA",
input: "123456789",
expectedOutput: "25",
recipeConfig: [
{
"op": "CRC-8 Checksum",
"args": ["CRC-8/WCDMA"]
}
]
},
{ {
name: "CRC-16: nothing", name: "CRC-16: nothing",
input: "", input: "",
@ -116,5 +237,5 @@ TestRegister.addTests([
"args": [] "args": []
} }
] ]
}, }
]); ]);