Skip to content

Instantly share code, notes, and snippets.

@parthpower
Last active January 15, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save parthpower/fa8d6d6003b2ddc9d43712ca8bacc7f4 to your computer and use it in GitHub Desktop.
Save parthpower/fa8d6d6003b2ddc9d43712ca8bacc7f4 to your computer and use it in GitHub Desktop.
/*
* crcheck.cpp
*
* Created on: Apr 1, 2017
* Author: Parth Parikh
*/
/*
MIT License
Copyright (c) 2017 Parth Parikh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#define POLY 0xA053
typedef unsigned char uint8_t;
void *handle(int crc, int i, uint8_t data, int add_at_end = 1);
int crc16(uint8_t* data, int length = 504, int add_at_end = 1,
void *(handler(int crc, int i, uint8_t data,
int add_at_end)) = handle) {int current_crc = 0xffff;
int i,j;
for (i = 0; i < length-8; ++i) {
current_crc ^= data[i]&0xff;
for (j = 0; j < 8; ++j) {
if((current_crc & 1) != 0 ) {
current_crc = (current_crc>>1)^POLY;
}
else {
current_crc = current_crc >>1;
}
}
handler(~current_crc & 0xffff,i,data[i],add_at_end);
}
current_crc = ~current_crc & 0xffff;
handler(current_crc,-1,data[i],add_at_end);
return current_crc;
}
int readFile(const char* filename, uint8_t* buffer) {
std::ifstream ins;
ins.open(filename, std::ios::in);
if (!ins.is_open() || ins.fail()) {
std::cout << "Cannot open file " << filename << std::endl;
return -1;
}
ins.seekg(0, ins.end);
int fileLength = ins.tellg();
ins.seekg(0, ins.beg);
if (fileLength > 512) {
std::cout << "File length invalid" << std::endl;
return -1;
}
ins.read((char*) buffer, fileLength);
ins.close();
return fileLength;
}
void bufferFill(uint8_t* buffer, int size) {
int i;
for (i = size; i < 512; i++)
buffer[i] = '.';
}
void *handle(int crc, int i, uint8_t data, int add_at_end) {
if (add_at_end == 1 && i == -1)
std::cout << "0000" << std::setw(4) << std::setfill('0') << crc;
else
std::cout << (char) data;
if ((i + 1) % 64 == 0 || i == -1) {
std::cout << std::right << " - " << std::hex << "0000" << std::setw(4)
<< std::setfill('0') << crc << std::endl;
}
}
int getDataCrc(const uint8_t* buffer, int length, uint8_t* crc, uint8_t* data) {
int i;
for (i = 0; i < length - 8; ++i) {
data[i] = buffer[i];
}
for (i = length - 8; i < length; i++) {
crc[i - length + 8] = buffer[i];
}
for (i = 0; i < 4; i++) {
if (crc[i] != '0') {
std::cout << std::endl << "Invalid File" << std::endl;
return -1;
}
}
return 0;
}
uint8_t char2hex(const char hex) {
if (hex >= '0' && hex <= '9')
return hex - '0';
else
return 10 + (hex - 'a');
}
int hexStr2int(const char* hexStr, int length = 8) {
int i;
int val = 0;
for (i = 0; i < length; i++) {
val += char2hex(hexStr[i]) * pow(16, length - i - 1);
}
return val;
}
int verifyCrc(const uint8_t* buffer, int size) {
int crcVal;
uint8_t data[508], crc[8];
if (getDataCrc(buffer, size, crc, data) != 0) {
return -1;
}
int crcValFromCalc = crc16(data, size - 8, 1);
crcVal = hexStr2int((char*) crc, 8);
if (crcVal == crcValFromCalc) {
return 1;
} else {
return 0;
}
}
int doCalculate(const char* filename) {
uint8_t buffer[513];
int i;
int fileLen = readFile(filename, buffer);
if (fileLen < 0) {
return 1;
}
//Show buffer
std::cout << "Calculate CRC" << std::endl << std::endl
<< "CRC Input Text File" << std::endl;
for (i = 0; i < fileLen; i++) {
std::cout << buffer[i];
}
std::cout << '\0' << std::endl << std::endl;
bufferFill(buffer, fileLen);
std::cout << "Calculating CRC..." << std::endl << std::endl;
crc16(buffer);
return 0;
}
int doVerify(const char* filename) {
uint8_t buffer[513];
int i;
int fileLen = readFile(filename, buffer);
if (fileLen < 0) {
return 1;
}
//Show buffer
std::cout << "Verify CRC" << std::endl << std::endl << "CRC Input Text File"
<< std::endl;
for (i = 0; i < fileLen; i++) {
std::cout << buffer[i];
}
std::cout << '\0' << std::endl << std::endl;
std::cout << "Verifying CRC..." << std::endl << std::endl;
if (verifyCrc(buffer, fileLen) == 1) {
std::cout << std::endl << "CRC Verified" << std::endl;
} else {
std::cout << std::endl << "CRC Not Verified" << std::endl;
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " <option> filename" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "c : Calculate CRC" << std::endl;
std::cout << "v : Verify CRC of file" << std::endl;
return 1;
}
std::cout << std::endl;
int retValue = 0;
if (strcmp(argv[1], "c") == 0)
retValue = doCalculate(argv[2]);
else if (strcmp(argv[1], "v") == 0)
retValue = doVerify(argv[2]);
else {
std::cout << "Invalid argument " << argv[1] << std::endl;
retValue = 1;
}
return retValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment