... | ... |
@@ -5,6 +5,8 @@ |
5 | 5 |
*/ |
6 | 6 |
|
7 | 7 |
#include <stdio.h> |
8 |
+#include "str.h" |
|
9 |
+#include "ut.h" |
|
8 | 10 |
#include "crc.h" |
9 | 11 |
|
10 | 12 |
#define OK 0 |
... | ... |
@@ -179,6 +181,46 @@ unsigned short int crc_16_tab[] = { /* CRC polynomial 0xA001 */ |
179 | 181 |
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040, |
180 | 182 |
}; |
181 | 183 |
|
184 |
+unsigned short crcitt_string( char *s, int len ) |
|
185 |
+{ |
|
186 |
+ register unsigned short ccitt; |
|
187 |
+ |
|
188 |
+ ccitt = 0xFFFF; |
|
189 |
+ |
|
190 |
+ while( len ) { |
|
191 |
+ ccitt = UPDCIT(*s, ccitt); |
|
192 |
+ s++; len--; |
|
193 |
+ } |
|
194 |
+ return ~ccitt; |
|
195 |
+} |
|
196 |
+ |
|
197 |
+void crcitt_string_array( char *dst, str src[], int size ) |
|
198 |
+{ |
|
199 |
+ register int i; |
|
200 |
+ register unsigned short ccitt; |
|
201 |
+ register char *c; |
|
202 |
+ register int len; |
|
203 |
+ int str_len; |
|
204 |
+ |
|
205 |
+ ccitt = 0xFFFF; |
|
206 |
+ str_len=CRC16_LEN; |
|
207 |
+ for (i=0; i<size; i++ ) { |
|
208 |
+ c=src[i].s; |
|
209 |
+ len=src[i].len; |
|
210 |
+ while(len) { |
|
211 |
+ ccitt = UPDCIT( *c, ccitt ); |
|
212 |
+ c++;len--; |
|
213 |
+ } |
|
214 |
+ } |
|
215 |
+ ccitt = ~ccitt; |
|
216 |
+ if (int2reverse_hex( &dst, &str_len, ccitt )==-1) { |
|
217 |
+ /* bug ... printed ccitt value longer than CRC32_LEN */ |
|
218 |
+ LOG(L_CRIT, "ERROR: crcitt_string_array: string conversion incomplete\n"); |
|
219 |
+ } |
|
220 |
+} |
|
221 |
+ |
|
222 |
+ |
|
223 |
+ |
|
182 | 224 |
int crc32file (char *name) |
183 | 225 |
{ |
184 | 226 |
register FILE *fin; |
... | ... |
@@ -3,9 +3,14 @@ |
3 | 3 |
#ifndef _CRC_H_ |
4 | 4 |
#define _CRC_H_ |
5 | 5 |
|
6 |
+#define CRC16_LEN 4 |
|
7 |
+ |
|
6 | 8 |
extern unsigned long int crc_32_tab[]; |
7 | 9 |
extern unsigned short int ccitt_tab[]; |
8 | 10 |
extern unsigned short int crc_16_tab[]; |
9 | 11 |
|
12 |
+unsigned short crcitt_string( char *s, int len ); |
|
13 |
+void crcitt_string_array( char *dst, str src[], int size ); |
|
14 |
+ |
|
10 | 15 |
#endif |
11 | 16 |
|
... | ... |
@@ -60,44 +60,6 @@ |
60 | 60 |
)==0 ) |
61 | 61 |
|
62 | 62 |
|
63 |
-static int reverse_hex2int( char *c, int len ) |
|
64 |
-{ |
|
65 |
- char *pc; |
|
66 |
- int r; |
|
67 |
- char mychar; |
|
68 |
- |
|
69 |
- r=0; |
|
70 |
- for (pc=c+len-1; len>0; pc--, len--) { |
|
71 |
- r <<= 4 ; |
|
72 |
- mychar=*pc; |
|
73 |
- if ( mychar >='0' && mychar <='9') r+=mychar -'0'; |
|
74 |
- else if (mychar >='a' && mychar <='f') r+=mychar -'a'+10; |
|
75 |
- else if (mychar >='A' && mychar <='F') r+=mychar -'A'+10; |
|
76 |
- else return -1; |
|
77 |
- } |
|
78 |
- return r; |
|
79 |
-} |
|
80 |
- |
|
81 |
-inline static int int2reverse_hex( char **c, int *size, int nr ) |
|
82 |
-{ |
|
83 |
- unsigned short digit; |
|
84 |
- |
|
85 |
- if (*size && nr==0) { |
|
86 |
- **c = '0'; |
|
87 |
- (*c)++; |
|
88 |
- (*size)--; |
|
89 |
- return 1; |
|
90 |
- } |
|
91 |
- |
|
92 |
- while (*size && nr ) { |
|
93 |
- digit = nr & 0xf ; |
|
94 |
- **c= digit >= 10 ? digit + 'a' - 10 : digit + '0'; |
|
95 |
- nr >>= 4; |
|
96 |
- (*c)++; |
|
97 |
- (*size)--; |
|
98 |
- } |
|
99 |
- return nr ? -1 /* number not processed; too little space */ : 1; |
|
100 |
-} |
|
101 | 63 |
|
102 | 64 |
/* function returns: |
103 | 65 |
* -1 - transaction wasn't found |
... | ... |
@@ -156,5 +156,44 @@ static inline char* q_memchr(char* p, int c, unsigned int size) |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
|
159 |
+static int reverse_hex2int( char *c, int len ) |
|
160 |
+{ |
|
161 |
+ char *pc; |
|
162 |
+ int r; |
|
163 |
+ char mychar; |
|
164 |
+ |
|
165 |
+ r=0; |
|
166 |
+ for (pc=c+len-1; len>0; pc--, len--) { |
|
167 |
+ r <<= 4 ; |
|
168 |
+ mychar=*pc; |
|
169 |
+ if ( mychar >='0' && mychar <='9') r+=mychar -'0'; |
|
170 |
+ else if (mychar >='a' && mychar <='f') r+=mychar -'a'+10; |
|
171 |
+ else if (mychar >='A' && mychar <='F') r+=mychar -'A'+10; |
|
172 |
+ else return -1; |
|
173 |
+ } |
|
174 |
+ return r; |
|
175 |
+} |
|
176 |
+ |
|
177 |
+inline static int int2reverse_hex( char **c, int *size, int nr ) |
|
178 |
+{ |
|
179 |
+ unsigned short digit; |
|
180 |
+ |
|
181 |
+ if (*size && nr==0) { |
|
182 |
+ **c = '0'; |
|
183 |
+ (*c)++; |
|
184 |
+ (*size)--; |
|
185 |
+ return 1; |
|
186 |
+ } |
|
187 |
+ |
|
188 |
+ while (*size && nr ) { |
|
189 |
+ digit = nr & 0xf ; |
|
190 |
+ **c= digit >= 10 ? digit + 'a' - 10 : digit + '0'; |
|
191 |
+ nr >>= 4; |
|
192 |
+ (*c)++; |
|
193 |
+ (*size)--; |
|
194 |
+ } |
|
195 |
+ return nr ? -1 /* number not processed; too little space */ : 1; |
|
196 |
+} |
|
197 |
+ |
|
159 | 198 |
|
160 | 199 |
#endif |