]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/reveng/cli.c
2 * Greg Cook, 27/Jun/2016
5 /* CRC RevEng: 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 <https://www.gnu.org/licenses/>.
24 /* 2016-06-27: -P sets width like -k
25 * 2015-04-03: added -z
26 * 2013-09-16: do not search with -M
27 * 2013-06-11: uprog() suppresses first progress report
28 * 2013-04-22: uprog() prints poly same as mtostr()
29 * 2013-02-07: added -q, uprog(), removed -W, R_ODDLY
30 * 2012-05-24: -D dumps parameters of all models
31 * 2012-03-03: added code to test sort order of model table
32 * 2012-02-20: set stdin to binary (MinGW). offer -D if preset unknown.
33 * 2011-09-06: -s reads arguments once. stdin not closed.
34 * 2011-09-06: fixed bad argument-freeing loops.
35 * 2011-08-27: validates BMP_C()
36 * 2011-08-26: validates BMPBIT and BMPSUB
37 * 2011-08-25: fixed Init/Xorout reflection logic in -V and -v
38 * 2011-01-17: fixed ANSI C warnings
39 * 2011-01-15: added NOFORCE
40 * 2011-01-14: added -k, -P
41 * 2011-01-10: reorganised switches, added -V, -X
42 * 2010-12-26: renamed CRC RevEng
43 * 2010-12-18: implemented -c, -C
44 * 2010-12-14: added and implemented -d, -D, fixed -ipx entry
45 * 2010-12-11: implemented -e. first tests
46 * 2010-12-10: finalised option processing. started input validation
47 * 2010-12-07: started cli
57 # define STDIN_FILENO 0
58 # endif /* STDIN_FILENO */
63 static FILE *oread(const char *);
64 static poly_t
rdpoly(const char *, int, int);
65 static void usage(void);
67 static const char *myname
= "reveng"; /* name of our program */
69 int reveng_main(int argc
, char *argv
[]) {
70 /* Command-line interface for CRC RevEng.
71 * Process options and switches in the argument list and
72 * run the required function.
77 PZERO
, /* no CRC polynomial, user must specify */
79 P_BE
, /* RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h */
80 PZERO
, /* XorOut = 0 */
81 PZERO
, /* check value unused */
82 NULL
/* no model name */
84 int ibperhx
= 8, obperhx
= 8;
85 int rflags
= 0, uflags
= 0; /* search and UI flags */
87 unsigned long width
= 0UL;
88 int c
, mode
= 0, args
, psets
, pass
;
89 poly_t apoly
, crc
, qpoly
= PZERO
, *apolys
, *pptr
= NULL
, *qptr
= NULL
;
90 model_t pset
= model
, *candmods
, *mptr
;
95 /* stdin must be binary */
97 _setmode(STDIN_FILENO
, _O_BINARY
);
105 c
=getopt(argc
, argv
, "?A:BDFLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:yz");
107 case 'A': /* A: bits per output character */
108 case 'a': /* a: bits per character */
109 if((obperhx
= atoi(optarg
)) > BMP_BIT
) {
110 fprintf(stderr
,"%s: argument to -%c must be between 1 and %d\n", myname
, c
, BMP_BIT
);
112 //exit(EXIT_FAILURE);
114 if(c
== 'a') ibperhx
= obperhx
;
116 case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
117 model
.flags
&= ~P_REFIN
;
120 case 'B': /* B big-endian output (RefOut = false) */
121 model
.flags
&= ~P_REFOUT
;
125 case 'r': /* r right-justified */
126 model
.flags
|= P_RTJUST
;
128 case 'c': /* c calculate CRC */
129 case 'D': /* D list primary model names */
130 case 'd': /* d dump CRC model */
131 case 'e': /* e echo arguments */
132 case 's': /* s search for algorithm */
133 case 'v': /* v calculate reversed CRC */
135 fprintf(stderr
,"%s: more than one mode switch specified. Use %s -h for help.\n", myname
, myname
);
137 //exit(EXIT_FAILURE);
141 case 'F': /* F force search */
146 case 'f': /* f arguments are filenames */
149 case 'h': /* h get help / usage */
150 case 'u': /* u get help / usage */
151 case '?': /* ? get help / usage */
155 //exit(EXIT_FAILURE);
157 case 'i': /* i: Init value */
161 case 'P': /* P: reversed polynomial */
162 case 'k': /* k: polynomial in Koopman notation */
164 model
.spoly
= strtop(optarg
, 0, 4);
165 pkchop(&model
.spoly
);
166 width
= plen(model
.spoly
);
172 case 'l': /* l little-endian input and output */
173 model
.flags
|= P_REFIN
;
176 case 'L': /* L little-endian output */
177 model
.flags
|= P_REFOUT
;
181 case 't': /* t left-justified */
182 model
.flags
&= ~P_RTJUST
;
184 case 'm': /* m: select preset CRC model */
185 if(!(c
= mbynam(&model
, optarg
))) {
186 fprintf(stderr
,"%s: preset model '%s' not found. Use %s -D to list presets.\n", myname
, optarg
, myname
);
188 //exit(EXIT_FAILURE);
191 uerror("no preset models available");
194 /* must set width so that parameter to -ipx is not zeroed */
195 width
= plen(model
.spoly
);
196 rflags
|= R_HAVEP
| R_HAVEI
| R_HAVERI
| R_HAVERO
| R_HAVEX
;
198 case 'M': /* M non-augmenting algorithm */
199 model
.flags
&= ~P_MULXN
;
201 case 'p': /* p: polynomial */
207 *pptr
= strtop(optarg
, 0, 4);
211 case 'q': /* q: range end polynomial */
216 case 'S': /* s space between output characters */
217 model
.flags
|= P_SPACE
;
219 case 'V': /* v reverse algorithm */
220 /* Distinct from the -v switch as the
221 * user will have to reverse his or her
222 * own arguments. The user cannot dump
223 * the model generated by -v either.
227 case 'w': /* w: CRC width = order - 1 */
228 /* no validation, WONTFIX */
229 width
= (unsigned long) atol(optarg
);
231 case 'X': /* X print uppercase hex */
232 model
.flags
|= P_UPPER
;
234 case 'x': /* x: XorOut value */
235 pptr
= &model
.xorout
;
238 case 'y': /* y little-endian byte order in files */
239 model
.flags
|= P_LTLBYT
;
241 case 'z': /* z raw binary arguments */
242 model
.flags
|= P_DIRECT
;
244 case -1: /* no more options, continue */
249 /* canonicalise the model, so the one we dump is the one we
250 * calculate with (not with -s, spoly may be blank which will
251 * normalise to zero and clear init and xorout.)
257 case 'v': /* v calculate reversed CRC */
258 /* Distinct from the -V switch as this causes
259 * the arguments and output to be reversed as well.
261 /* reciprocate Poly */
265 * if(refout) prev(init); else prev(xorout);
266 * but here the entire argument polynomial is
267 * reflected, not just the characters, so RefIn
268 * and RefOut are not inverted as with -V.
269 * Consequently Init is the mirror image of the
270 * one resulting from -V, and so we have:
272 if(~model
.flags
& P_REFOUT
) {
277 /* swap init and xorout */
279 model
.init
= model
.xorout
;
280 model
.xorout
= apoly
;
283 case 'c': /* c calculate CRC */
285 /* validate inputs */
286 /* if(plen(model.spoly) == 0) {
287 * fprintf(stderr,"%s: no polynomial specified for -%c (add -w WIDTH -p POLY)\n", myname, mode);
288 * exit(EXIT_FAILURE);
292 /* in the Williams model, xorout is applied after the refout stage.
293 * as refout is part of ptostr(), we reverse xorout here.
295 if(model
.flags
& P_REFOUT
)
298 for(; optind
< argc
; ++optind
) {
299 if(uflags
& C_INFILE
)
300 apoly
= rdpoly(argv
[optind
], model
.flags
, ibperhx
);
302 apoly
= strtop(argv
[optind
], model
.flags
, ibperhx
);
307 crc
= pcrc(apoly
, model
.spoly
, model
.init
, model
.xorout
, model
.flags
);
312 string
= ptostr(crc
, model
.flags
, obperhx
);
319 case 'D': /* D dump all models */
322 uerror("no preset models available");
325 for(mode
= 0; mode
< args
; ++mode
) {
326 mbynum(&model
, mode
);
331 case 'd': /* d dump CRC model */
332 /* maybe we don't want to do this:
333 * either attaching names to arbitrary models or forcing to a preset
334 * mmatch(&model, M_OVERWR);
336 if(~model
.flags
& P_MULXN
){
337 uerror("not a Williams model compliant algorithm");
340 string
= mtostr(&model
);
344 case 'e': /* e echo arguments */
345 for(; optind
< argc
; ++optind
) {
346 if(uflags
& C_INFILE
)
347 apoly
= rdpoly(argv
[optind
], model
.flags
, ibperhx
);
349 apoly
= strtop(argv
[optind
], model
.flags
, ibperhx
);
351 psum(&apoly
, model
.init
, 0UL);
352 string
= ptostr(apoly
, model
.flags
, obperhx
);
358 case 's': /* s search for algorithm */
360 uerror("must specify positive -k or -w before -s");
363 if(~model
.flags
& P_MULXN
){
364 uerror("cannot search for non-Williams compliant models");
367 praloc(&model
.spoly
, width
);
368 praloc(&model
.init
, width
);
369 praloc(&model
.xorout
, width
);
370 if(!plen(model
.spoly
))
371 palloc(&model
.spoly
, width
);
373 width
= plen(model
.spoly
);
375 /* special case if qpoly is zero, search to end of range */
379 /* allocate argument array */
380 args
= argc
- optind
;
381 if(!(apolys
= malloc(args
* sizeof(poly_t
)))){
382 uerror("cannot allocate memory for argument list");
386 for(pptr
= apolys
; optind
< argc
; ++optind
) {
387 if(uflags
& C_INFILE
)
388 *pptr
++ = rdpoly(argv
[optind
], model
.flags
, ibperhx
);
390 *pptr
++ = strtop(argv
[optind
], model
.flags
, ibperhx
);
392 /* exit value of pptr is used hereafter! */
394 /* if endianness not specified, try
395 * little-endian then big-endian.
396 * NB: crossed-endian algorithms will not be
400 /* scan against preset models */
401 if(~uflags
& C_FORCE
) {
406 mbynum(&pset
, --psets
);
407 /* skip if different width, or refin or refout don't match */
408 if(plen(pset
.spoly
) != width
|| (model
.flags
^ pset
.flags
) & (P_REFIN
| P_REFOUT
))
410 /* skip if the preset doesn't match specified parameters */
411 if(rflags
& R_HAVEP
&& pcmp(&model
.spoly
, &pset
.spoly
))
413 if(rflags
& R_HAVEI
&& psncmp(&model
.init
, &pset
.init
))
415 if(rflags
& R_HAVEX
&& psncmp(&model
.xorout
, &pset
.xorout
))
417 apoly
= pclone(pset
.xorout
);
418 if(pset
.flags
& P_REFOUT
)
420 for(qptr
= apolys
; qptr
< pptr
; ++qptr
) {
421 crc
= pcrc(*qptr
, pset
.spoly
, pset
.init
, apoly
, 0);
430 /* the selected model solved all arguments */
438 /* toggle refIn/refOut and reflect arguments */
439 if(~rflags
& R_HAVERI
) {
440 model
.flags
^= P_REFIN
| P_REFOUT
;
441 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
442 prevch(qptr
, ibperhx
);
444 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
446 if(uflags
& C_RESULT
) {
447 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
450 //exit(EXIT_SUCCESS);
452 if(!(model
.flags
& P_REFIN
) != !(model
.flags
& P_REFOUT
)){
453 uerror("cannot search for crossed-endian models");
458 mptr
= candmods
= reveng(&model
, qpoly
, rflags
, args
, apolys
);
459 if(mptr
&& plen(mptr
->spoly
))
461 while(mptr
&& plen(mptr
->spoly
)) {
462 /* results were printed by the callback
463 * string = mtostr(mptr);
470 if(~rflags
& R_HAVERI
) {
471 model
.flags
^= P_REFIN
| P_REFOUT
;
472 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
473 prevch(qptr
, ibperhx
);
475 } while(~rflags
& R_HAVERI
&& ++pass
< 2);
476 for(qptr
= apolys
; qptr
< pptr
; ++qptr
)
479 if(~uflags
& C_RESULT
)
480 uerror("no models found");
482 default: /* no mode specified */
483 fprintf(stderr
, "%s: no mode switch specified. Use %s -h for help.\n", myname
, myname
);
484 //exit(EXIT_FAILURE);
488 //exit(EXIT_SUCCESS);
492 ufound(const model_t
*model
) {
493 /* Callback function to report each model found */
497 /* generated models will be canonical */
498 string
= mtostr(model
);
504 uerror(const char *msg
) {
505 /* Callback function to report fatal errors */
506 fprintf(stderr
, "%s: %s\n", myname
, msg
);
508 //exit(EXIT_FAILURE);
512 uprog(const poly_t gpoly
, int flags
, unsigned long seq
) {
513 /* Callback function to report search progress */
516 /* Suppress first report in CLI */
519 string
= ptostr(gpoly
, P_RTJUST
, 4);
520 fprintf(stderr
, "%s: searching: width=%ld poly=0x%s refin=%s refout=%s\n",
521 myname
, plen(gpoly
), string
,
522 (flags
& P_REFIN
? "true" : "false"),
523 (flags
& P_REFOUT
? "true" : "false")
529 rdpoly(const char *name
, int flags
, int bperhx
) {
530 /* read poly from file in chunks and report errors */
532 poly_t apoly
= PZERO
, chunk
= PZERO
;
536 while(!feof(input
) && !ferror(input
)) {
537 chunk
= filtop(input
, BUFFER
, flags
, bperhx
);
538 psum(&apoly
, chunk
, plen(apoly
));
542 fprintf(stderr
,"%s: error condition on file '%s'\n", myname
, name
);
545 /* close file unless stdin */
547 /* reset EOF condition */
549 else if(fclose(input
)) {
550 fprintf(stderr
,"%s: error closing file '%s'\n", myname
, name
);
557 oread(const char *name
) {
558 /* open file for reading and report errors */
561 /* recognise special name '-' as standard input */
562 if(*name
== '-' && name
[1] == '\0')
564 if(!(handle
= fopen(name
, "rb"))) {
565 fprintf(stderr
, "%s: cannot open '%s' for reading\n", myname
, name
);
567 //exit(EXIT_FAILURE);
574 /* print usage if asked, or if syntax incorrect */
576 "CRC RevEng: arbitrary-precision CRC calculator and algorithm finder\n"
578 fputs(myname
, stderr
);
580 "\t-cdDesvhu? [-bBfFlLMrStVXyz]\n"
581 "\t\t[-a BITS] [-A OBITS] [-i INIT] [-k KPOLY] [-m MODEL]\n"
582 "\t\t[-p POLY] [-P RPOLY] [-q QPOLY] [-w WIDTH] [-x XOROUT]\n"
585 "\t-a BITS\t\tbits per character (1 to %d)\n"
586 "\t-A OBITS\tbits per output character (1 to %d)\n"
587 "\t-i INIT\t\tinitial register value\n"
588 "\t-k KPOLY\tgenerator in Koopman notation (implies WIDTH)\n"
589 "\t-m MODEL\tpreset CRC algorithm\n"
590 "\t-p POLY\t\tgenerator or search range start polynomial\n"
591 "\t-P RPOLY\treversed generator polynomial (implies WIDTH)\n",
594 "\t-q QPOLY\tsearch range end polynomial\n"
595 "\t-w WIDTH\tregister size, in bits\n"
596 "\t-x XOROUT\tfinal register XOR value\n"
597 "Modifier switches:\n"
598 "\t-b big-endian CRC\t\t-B big-endian CRC output\n"
599 "\t-f read files named in STRINGs\t-F find presets less quickly\n"
600 "\t-l little-endian CRC\t\t-L little-endian CRC output\n"
601 "\t-M non-augmenting algorithm\t-r right-justified output\n"
602 "\t-S print spaces between chars\t-t left-justified output\n"
603 "\t-V reverse algorithm only\t-X print uppercase hex\n"
604 "\t-y low bytes first in files\t-z raw binary STRINGs\n");
607 "\t-c calculate CRCs\t\t-d dump algorithm parameters\n"
608 "\t-D list preset algorithms\t-e echo (and reformat) input\n"
609 "\t-s search for algorithm\t\t-v calculate reversed CRCs\n"
610 "\t-g search for alg given hex+crc\t-h | -u | -? show this help\n"
611 "Common Use Examples:\n"
612 "\t reveng -g 01020304e3\n"
613 "\t Searches for a known/common crc preset that computes the crc\n"
614 "\t on the end of the given hex string\n"
615 "\t reveng -w 8 -s 01020304e3 010204039d\n"
616 "\t Searches for any possible 8 bit width crc calc that computes\n"
617 "\t the crc on the end of the given hex string(s)\n"
618 "\t reveng -m CRC-8 -c 01020304\n"
619 "\t Calculates the crc-8 of the given hex string\n"
621 "\t Outputs a list of all known/common crc models with their\n"
624 "Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Gregory Cook\n"
625 "This is free software; see the source for copying conditions. There is NO\n"
626 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
629 "\t\t\t\t <http://reveng.sourceforge.net/>\n");