Mercurial > public > ostc4
comparison ostc4pack/src/checksum_final_add_fletcher.cpp @ 49:1e707b34667e
add o4pack files
author | heinrichsweikamp |
---|---|
date | Mon, 30 Jul 2018 11:31:20 +0200 |
parents | |
children | 01f40cb1057e |
comparison
equal
deleted
inserted
replaced
48:bf43851a678b | 49:1e707b34667e |
---|---|
1 #include <iostream> | |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 #include <string.h> | |
5 #include <time.h> | |
6 | |
7 void fletcher16(unsigned char *result1, unsigned char *result2, unsigned char const *data, size_t bytes ) | |
8 { | |
9 unsigned short sum1 = 0xff, sum2 = 0xff; | |
10 size_t tlen; | |
11 | |
12 while (bytes) { | |
13 tlen = bytes >= 20 ? 20 : bytes; | |
14 bytes -= tlen; | |
15 do { | |
16 sum2 += sum1 += *data++; | |
17 } while (--tlen); | |
18 sum1 = (sum1 & 0xff) + (sum1 >> 8); | |
19 sum2 = (sum2 & 0xff) + (sum2 >> 8); | |
20 } | |
21 /* Second reduction step to reduce sums to 8 bits */ | |
22 sum1 = (sum1 & 0xff) + (sum1 >> 8); | |
23 sum2 = (sum2 & 0xff) + (sum2 >> 8); | |
24 *result2 = (sum2 & 0xff); | |
25 *result1 = (sum1 & 0xff); | |
26 // return sum2 << 8 | sum1; | |
27 } | |
28 | |
29 | |
30 // This is a variant of fletcher16 with a 16 bit sum instead of an 8 bit sum, | |
31 // and modulo 2^16 instead of 2^16-1 | |
32 void | |
33 hw_ostc3_firmware_checksum (unsigned char *result1, unsigned char *result2, unsigned char *result3, unsigned char *result4, unsigned char const *data, size_t bytes ) | |
34 { | |
35 unsigned short low = 0; | |
36 unsigned short high = 0; | |
37 for (unsigned int i = 0; i < bytes; i++) { | |
38 low += data[i]; | |
39 high += low; | |
40 } | |
41 *result1 = (low & 0xff); | |
42 *result2 = (low/256 & 0xff); | |
43 *result3 = (unsigned char)(high & 0xff); | |
44 *result4 = (unsigned char)((high/256) & 0xff); | |
45 // return (((unsigned int)high) << 16) + low; | |
46 } | |
47 | |
48 | |
49 int main(int argc, char** argv) { | |
50 | |
51 | |
52 FILE *fp, * fpout; | |
53 size_t lenTotal,lenTemp; | |
54 unsigned char buf[2000000]; | |
55 char *file = argv[1]; | |
56 char *file2 = argv[2]; | |
57 char *file3 = argv[3]; | |
58 unsigned int pruefsumme; | |
59 unsigned char buf2[4]; | |
60 | |
61 printf("1: %s\n", file); | |
62 printf("2: %s\n", file2); | |
63 printf("3: %s\n", file3); | |
64 printf("\n"); | |
65 | |
66 | |
67 //write File with length and cheksum | |
68 char filename[500], filenameout[510] ; | |
69 sprintf(filename,"%s",file); | |
70 int filelength = strlen(filename); | |
71 filename[filelength -4] = 0; | |
72 | |
73 lenTotal = 0; | |
74 if (NULL == (fp = fopen(file, "rb"))) | |
75 { | |
76 printf("Unable to open %s for reading\n", file); | |
77 return -1; | |
78 } | |
79 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf), fp); | |
80 // lenTemp = fread(buf, sizeof(char), sizeof(buf), fp); | |
81 lenTotal = lenTemp; | |
82 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp); | |
83 fclose(fp); | |
84 | |
85 if(file2) | |
86 { | |
87 if (NULL == (fp = fopen(file2, "rb"))) | |
88 { | |
89 printf("Unable to open %s for reading\n", file2); | |
90 return -1; | |
91 } | |
92 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp); | |
93 lenTotal += lenTemp; | |
94 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp); | |
95 fclose(fp); | |
96 } | |
97 if(file3) | |
98 { | |
99 if (NULL == (fp = fopen(file3, "rb"))) | |
100 { | |
101 printf("Unable to open %s for reading\n", file3); | |
102 return -1; | |
103 } | |
104 lenTemp = fread(&buf[lenTotal], sizeof(char), sizeof(buf)-lenTotal, fp); | |
105 lenTotal += lenTemp; | |
106 printf("%d bytes read (hex: %#x )\n", lenTemp,lenTemp); | |
107 fclose(fp); | |
108 } | |
109 | |
110 printf("\n"); | |
111 printf("%d bytes read (hex: %#x ) total \n", lenTotal,lenTotal); | |
112 | |
113 time_t rawtime; | |
114 time (&rawtime); | |
115 struct tm *timeinfo; | |
116 timeinfo = localtime(&rawtime); | |
117 | |
118 // sprintf(filenameout,"fwupdate_%s.bin",ctime(&rawtime)); | |
119 sprintf(filenameout,"OSTC4update_%02u%02u%02u.bin",timeinfo->tm_year-100,timeinfo->tm_mon+1,timeinfo->tm_mday); | |
120 | |
121 fpout = fopen(filenameout, "wb"); | |
122 for(int i = 0;i <lenTotal;i++) | |
123 { | |
124 if(fwrite(&buf[i],1,1,fpout) != 1) | |
125 printf("error writing\n"); | |
126 } | |
127 | |
128 hw_ostc3_firmware_checksum(&buf2[0],&buf2[1],&buf2[2],&buf2[3],buf,lenTotal); | |
129 printf("checksum %#x %#x %#x %#x\n", buf2[0],buf2[1], buf2[2], buf2[3]); | |
130 if(fwrite(&buf2[0],1,4,fpout) != 4) | |
131 printf("error writing checksum\n"); | |
132 ; | |
133 fclose(fpout); | |
134 } |