]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/reveng/cli.c
2 * Greg Cook, 24/Feb/2016
5 /* CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder
6 * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Gregory Cook
8 * This file is part of CRC RevEng.
10 * CRC RevEng is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * CRC RevEng is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with CRC RevEng. If not, see <http://www.gnu.org/licenses/>.
24 /* 2015-04-03: added -z
25 * 2013-09-16: do not search with -M
26 * 2013-06-11: uprog() suppresses first progress report
27 * 2013-04-22: uprog() prints poly same as mtostr()
28 * 2013-02-07: added -q, uprog(), removed -W, R_ODDLY
29 * 2012-05-24: -D dumps parameters of all models
30 * 2012-03-03: added code to test sort order of model table
31 * 2012-02-20: set stdin to binary (MinGW). offer -D if preset unknown.
32 * 2011-09-06: -s reads arguments once. stdin not closed.
33 * 2011-09-06: fixed bad argument-freeing loops.
34 * 2011-08-27: validates BMP_C()
35 * 2011-08-26: validates BMPBIT and BMPSUB
36 * 2011-08-25: fixed Init/Xorout reflection logic in -V and -v
37 * 2011-01-17: fixed ANSI C warnings
38 * 2011-01-15: added NOFORCE
39 * 2011-01-14: added -k, -P
40 * 2011-01-10: reorganised switches, added -V, -X
41 * 2010-12-26: renamed CRC RevEng
42 * 2010-12-18: implemented -c, -C
43 * 2010-12-14: added and implemented -d, -D, fixed -ipx entry
44 * 2010-12-11: implemented -e. first tests
45 * 2010-12-10: finalised option processing. started input validation
46 * 2010-12-07: started cli
56 # define STDIN_FILENO 0
57 # endif /* STDIN_FILENO */
62 static FILE *oread(const char *);
63 static poly_t
rdpoly(const char *, int, int);
64 static void usage(void);
66 static const char *myname
= "reveng"; /* name of our program */
68 int reveng_main(int argc
, char *argv
[]) {
69 /* Command-line interface for CRC RevEng.
70 * Process options and switches in the argument list and
71 * run the required function.
76 PZERO
, /* no CRC polynomial, user must specify */
78 P_BE
, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
79 PZERO
, /* XorOut = 0 */
80 PZERO
, /* check value unused */
81 NULL
/* no model name */
83 int ibperhx
= 8, obperhx
= 8;
84 int rflags
= 0, uflags
= 0; /* search and UI flags */
86 unsigned long width
= 0UL;
87 int c
, mode
= 0, args
, psets
, pass
;
88 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
, *pptr
= NULL
, *qptr
= NULL
;
89 model_t pset
= model
, *candmods
, *mptr
;
94 /* stdin must be binary */
96 _setmode(STDIN_FILENO
, _O_BINARY
);
104 c
=getopt(argc
, argv
, "?A:BDFLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:yz");
106 case 'A': /* A: bits per output character */
107 case 'a': /* a: bits per character */
108 if((obperhx
= atoi(optarg
)) > BMP_BIT
) {
109 fprintf(stderr
,"%s: argument to -%c must be between 1 and %d\n", myname
, c
, BMP_BIT
);
111 //exit(EXIT_FAILURE);
113 if(c
== 'a') ibperhx
= obperhx
;
115 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
116 model
.flags
&= ~P_REFIN
;
119 case 'B': /* B big-endian output (RefOut = false) */
120 model
.flags
&= ~P_REFOUT
;
124 case 'r': /* r right-justified */
125 model
.flags
|= P_RTJUST
;
127 case 'c': /* c calculate CRC */
128 case 'D': /* D list primary model names */
129 case 'd': /* d dump CRC model */
130 case 'e': /* e echo arguments */
131 case 's': /* s search for algorithm */
132 case 'v': /* v calculate reversed CRC */
134 fprintf(stderr
,"%s: more than one mode switch specified. Use %s -h for help.\n", myname
, myname
);
136 //exit(EXIT_FAILURE);
140 case 'F': /* F force search */
145 case 'f': /* f arguments are filenames */
148 case 'h': /* h get help / usage */
149 case 'u': /* u get help / usage */
150 case '?': /* ? get help / usage */
154 //exit(EXIT_FAILURE);
156 case 'i': /* i: Init value */
160 case 'k': /* k: polynomial in Koopman notation */
162 model
.spoly
= strtop(optarg
, 0, 4);
163 pkchop(&model
.spoly
);
164 width
= plen(model
.spoly
);
168 case 'l': /* l little-endian input and output */
169 model
.flags
|= P_REFIN
;
172 case 'L': /* L little-endian output */
173 model
.flags
|= P_REFOUT
;
177 case 't': /* t left-justified */
178 model
.flags
&= ~P_RTJUST
;
180 case 'm': /* m: select preset CRC model */
181 if(!(c
= mbynam(&model
, optarg
))) {
182 fprintf(stderr
,"%s: preset model '%s' not found. Use %s -D to list presets.\n", myname
, optarg
, myname
);
184 //exit(EXIT_FAILURE);
187 uerror("no preset models available");
190 /* must set width so that parameter to -ipx is not zeroed */
191 width
= plen(model
.spoly
);
192 rflags
|= R_HAVEP
| R_HAVEI
| R_HAVERI
| R_HAVERO
| R_HAVEX
;
194 case 'M': /* M non-augmenting algorithm */
195 model
.flags
&= ~P_MULXN
;
197 case 'P': /* P: reversed polynomial */
198 case 'p': /* p: polynomial */
204 *pptr
= strtop(optarg
, 0, 4);
210 case 'q': /* q: range end polynomial */
215 case 'S': /* s space between output characters */
216 model
.flags
|= P_SPACE
;
218 case 'V': /* v reverse algorithm */
219 /* Distinct from the -v switch as the
220 * user will have to reverse his or her
221 * own arguments. The user cannot dump
222 * the model generated by -v either.
226 case 'w': /* w: CRC width = order - 1 */
227 width
= (unsigned long) atol(optarg
);
229 case 'X': /* X print uppercase hex */
230 model
.flags
|= P_UPPER
;
232 case 'x': /* x: XorOut value */
233 pptr
= &model
.xorout
;
236 case 'y': /* y little-endian byte order in files */
237 model
.flags
|= P_LTLBYT
;
239 case 'z': /* z raw binary arguments */
240 model
.flags
|= P_DIRECT
;
242 case -1: /* no more options, continue */
247 /* canonicalise the model, so the one we dump is the one we
248 * calculate with (not with -s, spoly may be blank which will
249 * normalise to zero and clear init and xorout.)
255 case 'v': /* v calculate reversed CRC */
256 /* Distinct from the -V switch as this causes
257 * the arguments and output to be reversed as well.
259 /* reciprocate Poly */
263 * if(refout) prev(init); else prev(xorout);
264 * but here the entire argument polynomial is
265 * reflected, not just the characters, so RefIn
266 * and RefOut are not inverted as with -V.
267 * Consequently Init is the mirror image of the
268 * one resulting from -V, and so we have:
270 if(~model
.flags
& P_REFOUT
) {
275 /* swap init and xorout */
277 model
.init
= model
.xorout
;
278 model
.xorout
= apoly
;
281 case 'c': /* c calculate CRC */
283 /* validate inputs */
284 /* if(plen(model.spoly) == 0) {
285 * fprintf(stderr,"%s: no polynomial specified for -%c (add -w WIDTH -p POLY)\n", myname, mode);
286 * exit(EXIT_FAILURE);
290 /* in the Williams model, xorout is applied after the refout stage.
291 * as refout is part of ptostr(), we reverse xorout here.
293 if(model
.flags
& P_REFOUT
)
296 for(; optind
< argc
; ++optind
) {
297 if(uflags
& C_INFILE
)
298 apoly
= rdpoly(argv
[optind
], model
.flags
, ibperhx
);
300 apoly
= strtop(argv
[optind
], model
.flags
, ibperhx
);
305 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
310 string
= ptostr(crc
, model
.flags
, obperhx
);
317 case 'D': /* D dump all models */
320 uerror("no preset models available");
323 for(mode
= 0; mode
< args
; ++mode
) {
324 mbynum(&model
, mode
);
329 case 'd': /* d dump CRC model */
330 /* maybe we don't want to do this:
331 * either attaching names to arbitrary models or forcing to a preset
332 * mmatch(&model, M_OVERWR);
334 if(~model
.flags
& P_MULXN
){
335 uerror("not a Williams model compliant algorithm");
338 string
= mtostr(&model
);
342 case 'e': /* e echo arguments */
343 for(; optind
< argc
; ++optind
) {
344 if(uflags
& C_INFILE
)
345 apoly
= rdpoly(argv
[optind
], model
.flags
, ibperhx
);
347 apoly
= strtop(argv
[optind
], model
.flags
, ibperhx
);
349 psum(&apoly
, model
.init
, 0UL);
350 string
= ptostr(apoly
, model
.flags
, obperhx
);
356 case 's': /* s search for algorithm */
358 uerror("must specify positive -k or -w before -s");
361 if(~model
.flags
& P_MULXN
){
362 uerror("cannot search for non-Williams compliant models");
365 praloc(&model
.spoly
, width
);
366 praloc(&model
.init
, width
);
367 praloc(&model
.xorout
, width
);
368 if(!plen(model
.spoly
))
369 palloc(&model
.spoly
, width
);
371 width
= plen(model
.spoly
);
373 /* special case if qpoly is zero, search to end of range */
377 /* allocate argument array */
378 args
= argc
- optind
;
379 if(!(apolys
= malloc(args
* sizeof(poly_t
)))){
380 uerror("cannot allocate memory for argument list");
384 for(pptr
= apolys
; optind
< argc
; ++optind
) {
385 if(uflags
& C_INFILE
)
386 *pptr
++ = rdpoly(argv
[optind
], model
.flags
, ibperhx
);
388 *pptr
++ = strtop(argv
[optind
], model
.flags
, ibperhx
);
390 /* exit value of pptr is used hereafter! */
392 /* if endianness not specified, try
393 * little-endian then big-endian.
394 * NB: crossed-endian algorithms will not be
398 /* scan against preset models */
399 if(~uflags
& C_FORCE
) {
404 mbynum(&pset
, --psets
);
405 /* skip if different width, or refin or refout don't match */
406 if(plen(pset
.spoly
) != width
|| (model
.flags
^ pset
.flags
) & (P_REFIN
| P_REFOUT
))
408 /* skip if the preset doesn't match specified parameters */
409 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
411 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
413 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
415 apoly
= pclone(pset
.xorout
);
416 if(pset
.flags
& P_REFOUT
)
418 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
419 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
428 /* the selected model solved all arguments */
436 /* toggle refIn/refOut and reflect arguments */
437 if(~rflags
& R_HAVERI
) {
438 model
.flags
^= P_REFIN
| P_REFOUT
;
439 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
440 prevch(qptr
, ibperhx
);
442 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
444 if(uflags
& C_RESULT
) {
445 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
448 //exit(EXIT_SUCCESS);
450 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
)){
451 uerror("cannot search for crossed-endian models");
456 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
457 if(mptr
&& plen(mptr
->spoly
))
459 while(mptr
&& plen(mptr
->spoly
)) {
460 /* results were printed by the callback
461 * string = mtostr(mptr);
468 if(~rflags
& R_HAVERI
) {
469 model
.flags
^= P_REFIN
| P_REFOUT
;
470 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
471 prevch(qptr
, ibperhx
);
473 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
474 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
477 if(~uflags
& C_RESULT
)
478 uerror("no models found");
480 default: /* no mode specified */
481 fprintf(stderr
, "%s: no mode switch specified. Use %s -h for help.\n", myname
, myname
);
482 //exit(EXIT_FAILURE);
486 //exit(EXIT_SUCCESS);
490 ufound(const model_t
*model
) {
491 /* Callback function to report each model found */
495 /* generated models will be canonical */
496 string
= mtostr(model
);
502 uerror(const char *msg
) {
503 /* Callback function to report fatal errors */
504 fprintf(stderr
, "%s: %s\n", myname
, msg
);
506 //exit(EXIT_FAILURE);
510 uprog(const poly_t gpoly
, int flags
, unsigned long seq
) {
511 /* Callback function to report search progress */
514 /* Suppress first report in CLI */
517 string
= ptostr(gpoly
, P_RTJUST
, 4);
518 fprintf(stderr
, "%s: searching: width=%ld poly=0x%s refin=%s refout=%s\n",
519 myname
, plen(gpoly
), string
,
520 (flags
& P_REFIN
? "true" : "false"),
521 (flags
& P_REFOUT
? "true" : "false")
527 rdpoly(const char *name
, int flags
, int bperhx
) {
528 /* read poly from file in chunks and report errors */
530 poly_t apoly
= PZERO
, chunk
= PZERO
;
534 while(!feof(input
) && !ferror(input
)) {
535 chunk
= filtop(input
, BUFFER
, flags
, bperhx
);
536 psum(&apoly
, chunk
, plen(apoly
));
540 fprintf(stderr
,"%s: error condition on file '%s'\n", myname
, name
);
543 /* close file unless stdin */
545 /* reset EOF condition */
547 else if(fclose(input
)) {
548 fprintf(stderr
,"%s: error closing file '%s'\n", myname
, name
);
555 oread(const char *name
) {
556 /* open file for reading and report errors */
559 /* recognise special name '-' as standard input */
560 if(*name
== '-' && name
[1] == '\0')
562 if(!(handle
= fopen(name
, "rb"))) {
563 fprintf(stderr
, "%s: cannot open '%s' for reading\n", myname
, name
);
565 //exit(EXIT_FAILURE);
572 /* print usage if asked, or if syntax incorrect */
574 "CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder\n"
576 fputs(myname
, stderr
);
578 "\t-cdDesvhu? [-bBfFlLMrStVXyz]\n"
579 "\t\t[-a BITS] [-A OBITS] [-i INIT] [-k KPOLY] [-m MODEL]\n"
580 "\t\t[-p POLY] [-P RPOLY] [-q QPOLY] [-w WIDTH] [-x XOROUT]\n"
583 "\t-a BITS\t\tbits per character (1 to %d)\n"
584 "\t-A OBITS\tbits per output character (1 to %d)\n"
585 "\t-i INIT\t\tinitial register value\n"
586 "\t-k KPOLY\tgenerator in Koopman notation (implies WIDTH)\n"
587 "\t-m MODEL\tpreset CRC algorithm\n"
588 "\t-p POLY\t\tgenerator or search range start polynomial\n"
589 "\t-P RPOLY\treversed generator polynomial\n",
592 "\t-q QPOLY\tsearch range end polynomial\n"
593 "\t-w WIDTH\tregister size, in bits\n"
594 "\t-x XOROUT\tfinal register XOR value\n"
595 "Modifier switches:\n"
596 "\t-b big-endian CRC\t\t-B big-endian CRC output\n"
597 "\t-f read files named in STRINGs\t-F find presets less quickly\n"
598 "\t-l little-endian CRC\t\t-L little-endian CRC output\n"
599 "\t-M non-augmenting algorithm\t-r right-justified output\n"
600 "\t-S print spaces between chars\t-t left-justified output\n"
601 "\t-V reverse algorithm only\t-X print uppercase hex\n"
602 "\t-y low bytes first in files\t-z raw binary STRINGs\n");
605 "\t-c calculate CRCs\t\t-d dump algorithm parameters\n"
606 "\t-D list preset algorithms\t-e echo (and reformat) input\n"
607 "\t-s search for algorithm\t\t-v calculate reversed CRCs\n"
608 "\t-g search for alg given hex+crc\t-h | -u | -? show this help\n"
609 "Common Use Examples:\n"
610 "\t reveng -g 01020304e3\n"
611 "\t Searches for a known/common crc preset that computes the crc\n"
612 "\t on the end of the given hex string\n"
613 "\t reveng -w 8 -s 01020304e3 010204039d\n"
614 "\t Searches for any possible 8 bit width crc calc that computes\n"
615 "\t the crc on the end of the given hex string(s)\n"
616 "\t reveng -m CRC-8 -c 01020304\n"
617 "\t Calculates the crc-8 of the given hex string\n"
619 "\t Outputs a list of all known/common crc models with their\n"
622 "Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Gregory Cook\n"
623 "This is free software; see the source for copying conditions. There is NO\n"
624 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
627 "\t\t\t\t <http://reveng.sourceforge.net/>\n");