+int CmdAnalyseCRC(const char *Cmd) {
+
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_analyse_crc();
+
+ int len = strlen(Cmd);
+ if ( len & 1 ) return usage_analyse_crc();
+
+ // add 1 for null terminator.
+ uint8_t *data = malloc(len+1);
+ if ( data == NULL ) return 1;
+
+ if ( param_gethex(Cmd, 0, data, len)) {
+ free(data);
+ return usage_analyse_crc();
+ }
+ len >>= 1;
+
+ //PrintAndLog("\nTests with '%s' hex bytes", sprint_hex(data, len));
+
+ PrintAndLog("\nTests of reflection. Two current methods in source code");
+ PrintAndLog(" reflect(0x3e23L,3) is %04X == 0x3e26", reflect(0x3e23L,3) );
+ PrintAndLog(" SwapBits(0x3e23L,3) is %04X == 0x3e26", SwapBits(0x3e23L,3) );
+ PrintAndLog(" 0xB400 == %04X", reflect( (1 << 16 | 0xb400),16) );
+
+ //
+ // Test of CRC16, '123456789' string.
+ //
+ PrintAndLog("\nTests with '123456789' string");
+ uint8_t dataStr[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
+ uint8_t legic8 = CRC8Legic(dataStr, sizeof(dataStr));
+
+ PrintAndLog("LEGIC: CRC16: %X", CRC16Legic(dataStr, sizeof(dataStr), legic8));
+
+ //these below has been tested OK.
+ PrintAndLog("Confirmed CRC Implementations");
+ PrintAndLog("LEGIC: CRC8 : %X (0xC6 expected)", legic8);
+ PrintAndLog("MAXIM: CRC8 : %X (0xA1 expected)", CRC8Maxim(dataStr, sizeof(dataStr)));
+ PrintAndLog("DNP : CRC16: %X (0x82EA expected)", CRC16_DNP(dataStr, sizeof(dataStr)));
+ PrintAndLog("CCITT: CRC16: %X (0xE5CC expected)", CRC16_CCITT(dataStr, sizeof(dataStr)));
+
+ free(data);
+ return 0;
+}
+int CmdAnalyseCHKSUM(const char *Cmd){
+
+ uint8_t data[50];
+ uint8_t cmdp = 0;
+ uint32_t mask = 0xFF;
+ bool errors = false;
+ int len = 0;
+ memset(data, 0x0, sizeof(data));
+
+ while(param_getchar(Cmd, cmdp) != 0x00) {
+ switch(param_getchar(Cmd, cmdp)) {
+ case 'b':
+ case 'B':
+ param_gethex_ex(Cmd, cmdp+1, data, &len);
+ if ( len%2 ) errors = true;
+ len >>= 1;
+ cmdp += 2;
+ break;
+ case 'm':
+ case 'M':
+ mask = param_get32ex(Cmd, cmdp+1, 0, 16);
+ cmdp += 2;
+ break;
+ case 'h':
+ case 'H':
+ return usage_analyse_checksum();
+ default:
+ PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ if(errors) break;
+ }
+ //Validations
+ if(errors) return usage_analyse_checksum();
+
+ PrintAndLog("\nByte Add | 0x%X", calcSumByteAdd(data, len, mask));
+ PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAdd(data, len, mask));
+ PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAdd(data, len, mask));
+
+ PrintAndLog("\nByte Subtract | 0x%X", calcSumByteSub(data, len, mask));
+ PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSub(data, len, mask));
+
+ PrintAndLog("\nCHECKSUM - One's complement");
+ PrintAndLog("Byte Add | 0x%X", calcSumByteAddOnes(data, len, mask));
+ PrintAndLog("Nibble Add | 0x%X", calcSumNibbleAddOnes(data, len, mask));
+ PrintAndLog("Crumb Add | 0x%X", calcSumCrumbAddOnes(data, len, mask));
+
+ PrintAndLog("Byte Subtract | 0x%X", calcSumByteSubOnes(data, len, mask));
+ PrintAndLog("Nibble Subtract | 0x%X", calcSumNibbleSubOnes(data, len, mask));
+
+ return 0;
+}