1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2015 iceman <iceman at iuse.se>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // CRC Calculations from the software reveng commands
9 //-----------------------------------------------------------------------------
21 # define STDIN_FILENO 0
22 # endif /* STDIN_FILENO */
32 static int split(char *str
, char *arr
[MAX_ARGS
]) {
37 while (isspace(str
[beginIndex
])) {
40 if (str
[beginIndex
] == '\0') {
43 int endIndex
= beginIndex
;
44 while (str
[endIndex
] && !isspace(str
[endIndex
])) {
47 int len
= endIndex
- beginIndex
;
48 char *tmp
= calloc(len
+ 1, sizeof(char));
49 memcpy(tmp
, &str
[beginIndex
], len
);
51 beginIndex
= endIndex
;
52 if (wordCnt
== MAX_ARGS
)
58 //returns array of model names and the count of models returning
59 // as well as a width array for the width of each model
60 int GetModels(char *Models
[], int *count
, uint8_t *width
) {
62 static model_t model
= MZERO
;
64 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
= NULL
, *pptr
= NULL
, *qptr
= NULL
;
67 /* stdin must be binary */
69 _setmode(STDIN_FILENO
, _O_BINARY
);
74 if (width
[0] == 0) { //reveng -D
77 PrintAndLogEx(WARNING
, "no preset models available");
80 for (int mode
= 0; mode
< *count
; ++mode
) {
83 size_t size
= (model
.name
&& *model
.name
) ? strlen(model
.name
) : 7;
84 char *tmp
= calloc(size
+ 1, sizeof(char));
86 PrintAndLogEx(WARNING
, "out of memory?");
89 if (model
.name
!= NULL
) {
90 memcpy(tmp
, model
.name
, size
);
92 width
[mode
] = plen(model
.spoly
);
100 int ibperhx
= 8;//, obperhx = 8;
101 int rflags
= 0, uflags
= 0; /* search and UI flags */
102 if (~model
.flags
& P_MULXN
) {
103 PrintAndLogEx(WARNING
, "cannot search for non-Williams compliant models");
106 praloc(&model
.spoly
, (unsigned long)width
[0]);
107 praloc(&model
.init
, (unsigned long)width
[0]);
108 praloc(&model
.xorout
, (unsigned long)width
[0]);
110 if (!plen(model
.spoly
))
111 palloc(&model
.spoly
, (unsigned long)width
[0]);
113 width
[0] = (uint8_t)plen(model
.spoly
);
115 /* special case if qpoly is zero, search to end of range */
121 /* if endianness not specified, try
122 * little-endian then big-endian.
123 * NB: crossed-endian algorithms will not be
126 /* scan against preset models */
127 if (~uflags
& C_NOPCK
) {
131 int psets
= mcount();
134 mbynum(&pset
, --psets
);
136 /* skip if different width, or refin or refout don't match */
137 if (plen(pset
.spoly
) != width
[0] || (model
.flags
^ pset
.flags
) & (P_REFIN
| P_REFOUT
))
139 /* skip if the preset doesn't match specified parameters */
140 if (rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
142 if (rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
144 if (rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
147 //for additional args (not used yet, maybe future?)
148 apoly
= pclone(pset
.xorout
);
150 if (pset
.flags
& P_REFOUT
)
154 for (qptr
= apolys
; qptr
< pptr
; ++qptr
) {
155 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
166 /* the selected model solved all arguments */
169 size_t size
= (pset
.name
&& *pset
.name
) ? strlen(pset
.name
) : 7;
170 //PrintAndLogEx(NORMAL, "Size: %d, %s, count: %d",size,pset.name, Cnt);
171 char *tmp
= calloc(size
+ 1, sizeof(char));
173 PrintAndLogEx(WARNING
, "out of memory?");
176 width
[Cnt
] = width
[0];
177 memcpy(tmp
, pset
.name
, size
);
185 /* toggle refIn/refOut and reflect arguments */
186 if (~rflags
& R_HAVERI
) {
187 model
.flags
^= P_REFIN
| P_REFOUT
;
188 for (qptr
= apolys
; qptr
< pptr
; ++qptr
) {
189 prevch(qptr
, ibperhx
);
192 } while (~rflags
& R_HAVERI
&& ++pass
< 2);
194 //got everything now free the memory...
196 if (uflags
& C_RESULT
) {
197 for (qptr
= apolys
; qptr
< pptr
; ++qptr
) {
201 if (uflags
& C_NOBFS
&& ~rflags
& R_HAVEP
) {
202 PrintAndLogEx(WARNING
, "no models found");
206 if (!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
)) {
207 PrintAndLogEx(WARNING
, "cannot search for crossed-endian models");
213 model_t
*candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
214 model_t
*mptr
= candmods
;
215 if (mptr
&& plen(mptr
->spoly
)) {
218 while (mptr
&& plen(mptr
->spoly
)) {
222 if (~rflags
& R_HAVERI
) {
223 model
.flags
^= P_REFIN
| P_REFOUT
;
224 for (qptr
= apolys
; qptr
< pptr
; ++qptr
) {
225 prevch(qptr
, ibperhx
);
228 } while (~rflags
& R_HAVERI
&& ++pass
< 2);
230 for (qptr
= apolys
; qptr
< pptr
; ++qptr
) {
236 if (~uflags
& C_RESULT
) {
237 PrintAndLogEx(WARNING
, "no models found");
245 //inModel = valid model name string - CRC-8
246 //inHexStr = input hex string to calculate crc on
247 //reverse = reverse calc option if true
248 //endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
249 // l = little endian input and output, L = little endian output only, t = left justified}
250 //result = calculated crc hex string
251 int RunModel(char *inModel
, char *inHexStr
, bool reverse
, char endian
, char *result
) {
253 static model_t model
= MZERO
;
255 int ibperhx
= 8, obperhx
= 8;
256 // int rflags = 0; // search flags
261 // stdin must be binary
263 _setmode(STDIN_FILENO
, _O_BINARY
);
268 int c
= mbynam(&model
, inModel
);
270 PrintAndLogEx(ERR
, "error: preset model '%s' not found. Use reveng -D to list presets. [%d]", inModel
, c
);
274 PrintAndLogEx(WARNING
, "no preset models available");
277 // rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
281 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
282 model
.flags
&= ~P_REFIN
;
283 //rflags |= R_HAVERI;
285 case 'B': /* B big-endian output (RefOut = false) */
286 model
.flags
&= ~P_REFOUT
;
287 //rflags |= R_HAVERO;
290 case 'r': /* r right-justified */
291 model
.flags
|= P_RTJUST
;
293 case 'l': /* l little-endian input and output */
294 model
.flags
|= P_REFIN
;
295 //rflags |= R_HAVERI;
297 case 'L': /* L little-endian output */
298 model
.flags
|= P_REFOUT
;
299 //rflags |= R_HAVERO;
302 case 't': /* t left-justified */
303 model
.flags
&= ~P_RTJUST
;
306 /* canonicalise the model, so the one we dump is the one we
307 * calculate with (not with -s, spoly may be blank which will
308 * normalise to zero and clear init and xorout.)
314 // v calculate reversed CRC
315 /* Distinct from the -V switch as this causes
316 * the arguments and output to be reversed as well.
322 * if(refout) prev(init); else prev(xorout);
323 * but here the entire argument polynomial is
324 * reflected, not just the characters, so RefIn
325 * and RefOut are not inverted as with -V.
326 * Consequently Init is the mirror image of the
327 * one resulting from -V, and so we have:
329 if (~model
.flags
& P_REFOUT
) {
334 // swap init and xorout
336 model
.init
= model
.xorout
;
337 model
.xorout
= apoly
;
341 /* in the Williams model, xorout is applied after the refout stage.
342 * as refout is part of ptostr(), we reverse xorout here.
344 if (model
.flags
& P_REFOUT
)
347 apoly
= strtop(inHexStr
, model
.flags
, ibperhx
);
352 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
357 string
= ptostr(crc
, model
.flags
, obperhx
);
358 for (int i
= 0; i
< 50; i
++) {
359 result
[i
] = string
[i
];
360 if (result
[i
] == 0) break;
368 //test call to RunModel
369 static int CmdrevengTestC(const char *Cmd) {
371 char inModel[30] = {0x00};
372 char inHexStr[30] = {0x00};
376 dataLen = param_getstr(Cmd, cmdp++, inModel, sizeof(inModel));
377 if (dataLen < 4) return 0;
378 dataLen = param_getstr(Cmd, cmdp++, inHexStr, sizeof(inHexStr));
379 if (dataLen < 4) return 0;
380 bool reverse = (param_get8(Cmd, cmdp++)) ? true : false;
381 endian = param_getchar(Cmd, cmdp++);
383 //PrintAndLogEx(NORMAL, "mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse);
384 int ans = RunModel(inModel, inHexStr, reverse, endian, result);
387 PrintAndLogEx(SUCCESS, "result: %s", result);
391 //returns a calloced string (needs to be freed)
392 static char *SwapEndianStr(const char *inStr
, const size_t len
, const uint8_t blockSize
) {
393 char *tmp
= calloc(len
+ 1, sizeof(char));
394 for (uint8_t block
= 0; block
< (uint8_t)(len
/ blockSize
); block
++) {
395 for (size_t i
= 0; i
< blockSize
; i
+= 2) {
396 tmp
[i
+ (blockSize
* block
)] = inStr
[(blockSize
- 1 - i
- 1) + (blockSize
* block
)];
397 tmp
[i
+ (blockSize
* block
) + 1] = inStr
[(blockSize
- 1 - i
) + (blockSize
* block
)];
403 // takes hex string in and searches for a matching result (hex string must include checksum)
404 static int CmdrevengSearch(const char *Cmd
) {
408 char inHexStr
[256] = {0x00};
409 int dataLen
= param_getstr(Cmd
, 0, inHexStr
, sizeof(inHexStr
));
410 if (dataLen
< 4) return 0;
412 // these two arrays, must match preset size.
413 char *Models
[NMODELS
];
414 uint8_t width
[NMODELS
] = {0};
419 int ans
= GetModels(Models
, &count
, width
);
422 for (int i
= 0; i
< count
; i
++) {
428 // try each model and get result
429 for (int i
= 0; i
< count
; i
++) {
434 // round up to # of characters in this model's crc
435 uint8_t crcChars
= ((width
[i
] + 7) / 8) * 2;
436 // can't test a model that has more crc digits than our data
437 if (crcChars
>= dataLen
) {
443 , "DEBUG: dataLen %d, crcChars %u, width[i] %u"
454 memset(result
, 0, 30);
455 char *inCRC
= calloc(crcChars
+ 1, sizeof(char));
456 memcpy(inCRC
, inHexStr
+ (dataLen
- crcChars
), crcChars
);
458 char *outHex
= calloc(dataLen
- crcChars
+ 1, sizeof(char));
459 memcpy(outHex
, inHexStr
, dataLen
- crcChars
);
461 ans
= RunModel(Models
[i
], outHex
, false, 0, result
);
464 if (memcmp(result
, inCRC
, crcChars
) == 0) {
465 PrintAndLogEx(SUCCESS
, "\nfound possible match\nmodel: %s | value: %s\n", Models
[i
], result
);
466 //optional - stop searching if found...
470 char *swapEndian
= SwapEndianStr(result
, crcChars
, crcChars
);
471 if (memcmp(swapEndian
, inCRC
, crcChars
) == 0) {
472 PrintAndLogEx(SUCCESS
, "\nfound possible match\nmodel: %s | value endian swapped: %s\n", Models
[i
], swapEndian
);
473 // optional - stop searching if found...
480 ans
= RunModel(Models
[i
], outHex
, true, 0, revResult
);
483 if (memcmp(revResult
, inCRC
, crcChars
) == 0) {
484 PrintAndLogEx(SUCCESS
, "\nfound possible match\nmodel reversed: %s | value: %s\n", Models
[i
], revResult
);
485 // optional - stop searching if found...
489 char *swapEndian
= SwapEndianStr(revResult
, crcChars
, crcChars
);
490 if (memcmp(swapEndian
, inCRC
, crcChars
) == 0) {
491 PrintAndLogEx(SUCCESS
, "\nfound possible match\nmodel reversed: %s | value endian swapped: %s\n", Models
[i
], swapEndian
);
492 // optional - stop searching if found...
505 PrintAndLogEx(FAILED
, "\nno matches found\n");
510 int CmdCrc(const char *Cmd
) {
512 snprintf(c
, sizeof(c
), "reveng ");
513 snprintf(c
+ strlen(c
), sizeof(c
) - strlen(c
), Cmd
, strlen(Cmd
));
515 char *argv
[MAX_ARGS
];
516 int argc
= split(c
, argv
);
518 if (argc
== 3 && memcmp(argv
[1], "-g", 2) == 0) {
519 CmdrevengSearch(argv
[2]);
521 reveng_main(argc
, argv
);
524 for (int i
= 0; i
< argc
; ++i
) {