forked from RyanHope/HondaECU
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecksumHonda.cpp
95 lines (84 loc) · 2.21 KB
/
checksumHonda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdio>
using namespace std;
#include <argagg.hpp>
#define MAX_BYTES 262144
int main(int argc, char **argv)
{
using argagg::parser_results;
using argagg::parser;
using std::cerr;
using std::cout;
using std::endl;
using std::ostringstream;
using std::string;
parser argparser {{
{
"help", {"-h", "--help"},
"Print help and exit", 0},
{
"fix", {"--fix"},
"Fix checksum (default: false)", 0},
}};
// Define our usage text.
ostringstream usage;
usage
<< "Usage: " << argv[0] << " [OPTIONS]... [FILE]" << endl
<< endl;
ostringstream header;
header
<< "Honda Checksum Utility v1.0" << endl;
argagg::parser_results args;
try {
args = argparser.parse(argc, argv);
} catch (const std::exception& e) {
argagg::fmt_ostream fmt(cerr);
fmt << usage.str() << argparser << endl
<< "Encountered exception while parsing arguments: " << e.what()
<< endl;
return EXIT_FAILURE;
}
// If the help flag was specified then spit out the usage and help text and
// exit.
if (args["help"]) {
cerr << header.str();
argagg::fmt_ostream fmt(cerr);
fmt << usage.str() << argparser;
return EXIT_SUCCESS;
}
if (args.pos.size()==1) {
FILE *bin;
int csum_old = 0;
int csum_new = 0;
string status;
bin = fopen(args.pos[0], "rb");
if (!bin)
return EXIT_FAILURE;
unsigned char buffer[MAX_BYTES];
fread(buffer, sizeof(unsigned char), MAX_BYTES, bin);
fclose(bin);
csum_old = buffer[MAX_BYTES-8];
buffer[MAX_BYTES-8] = 0;
for (int i=0; i<MAX_BYTES; i++) {
csum_new += buffer[i];
}
csum_new = ((csum_new ^ 0xff) + 1) & 0xff;
cout << "file checksum: " << csum_old << endl;
cout << "computed checksum: " << csum_new << endl;
if (csum_old == csum_new) {
status = "good";
} else {
if (args["fix"]) {
buffer[MAX_BYTES-8] = csum_new;
bin = fopen(args.pos[0], "wb");
fwrite(buffer, sizeof(unsigned char), MAX_BYTES, bin);
fclose(bin);
status = "fixed";
} else {
status = "bad";
}
}
cout << "status: " << status << endl;
}
return EXIT_SUCCESS;
}