X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/a71ece51e49f739e0eff8022e6c5d66813d1aa56..fada2feaf782ec47909886e86219ad49818e603a:/client/reveng/cli.c

diff --git a/client/reveng/cli.c b/client/reveng/cli.c
index a8026f4b..5b91c3dc 100644
--- a/client/reveng/cli.c
+++ b/client/reveng/cli.c
@@ -1,9 +1,9 @@
 /* cli.c
- * Greg Cook, 9/Apr/2015
+ * Greg Cook, 27/Jun/2016
  */
 
-/* CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder
- * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015  Gregory Cook
+/* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  Gregory Cook
  *
  * This file is part of CRC RevEng.
  *
@@ -18,10 +18,11 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with CRC RevEng.  If not, see <http://www.gnu.org/licenses/>.
+ * along with CRC RevEng.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-/* 2015-04-03: added -z
+/* 2016-06-27: -P sets width like -k
+ * 2015-04-03: added -z
  * 2013-09-16: do not search with -M
  * 2013-06-11: uprog() suppresses first progress report
  * 2013-04-22: uprog() prints poly same as mtostr()
@@ -156,13 +157,16 @@ int reveng_main(int argc, char *argv[]) {
 			case 'i': /* i: Init value */
 				pptr = &model.init;
 				rflags |= R_HAVEI;
-				goto ippx;
+				goto ipqx;
+			case 'P': /* P: reversed polynomial */
 			case 'k': /* k: polynomial in Koopman notation */
 				pfree(&model.spoly);
 				model.spoly = strtop(optarg, 0, 4);
 				pkchop(&model.spoly);
 				width = plen(model.spoly);
 				rflags |= R_HAVEP;
+				if(c == 'P')
+					prcp(&model.spoly);
 				mnovel(&model);
 				break;
 			case 'l': /* l  little-endian input and output */
@@ -183,8 +187,10 @@ int reveng_main(int argc, char *argv[]) {
 					return 0;
 					//exit(EXIT_FAILURE);
 				}
-				if(c < 0)
+				if(c < 0){
 					uerror("no preset models available");
+					return 0;
+				}
 				/* must set width so that parameter to -ipx is not zeroed */
 				width = plen(model.spoly);
 				rflags |= R_HAVEP | R_HAVEI | R_HAVERI | R_HAVERO | R_HAVEX;
@@ -192,24 +198,21 @@ int reveng_main(int argc, char *argv[]) {
 			case 'M': /* M  non-augmenting algorithm */
 				model.flags &= ~P_MULXN;
 				break;
-			case 'P': /* P: reversed polynomial */
 			case 'p': /* p: polynomial */
 				pptr = &model.spoly;
 				rflags &= ~R_HAVEQ;
 				rflags |= R_HAVEP;
-ippx:
+ipqx:
 				pfree(pptr);
 				*pptr = strtop(optarg, 0, 4);
 				pright(pptr, width);
-				if(c == 'P')
-					prev(pptr);
 				mnovel(&model);
 				break;
 			case 'q': /* q: range end polynomial */
 				pptr = &qpoly;
 				rflags &= ~R_HAVEP;
 				rflags |= R_HAVEQ;
-				goto ippx;
+				goto ipqx;
 			case 'S': /* s  space between output characters */
 				model.flags |= P_SPACE;
 				break;
@@ -222,6 +225,7 @@ ippx:
 				mrev(&model);
 				break;
 			case 'w': /* w: CRC width = order - 1 */
+				/* no validation, WONTFIX */
 				width = (unsigned long) atol(optarg);
 				break;
 			case 'X': /* X  print uppercase hex */
@@ -230,7 +234,7 @@ ippx:
 			case 'x': /* x: XorOut value */
 				pptr = &model.xorout;
 				rflags |= R_HAVEX;
-				goto ippx;
+				goto ipqx;
 			case 'y': /* y  little-endian byte order in files */
 				model.flags |= P_LTLBYT;
 				break;
@@ -314,8 +318,10 @@ ippx:
 			break;
 		case 'D': /* D  dump all models */
 			args = mcount();
-			if(!args)
+			if(!args){
 				uerror("no preset models available");
+				return 0;
+			}
 			for(mode = 0; mode < args; ++mode) {
 				mbynum(&model, mode);
 				mcanon(&model);
@@ -327,8 +333,10 @@ ippx:
 			 * either attaching names to arbitrary models or forcing to a preset
 			 * mmatch(&model, M_OVERWR);
 			 */
-			if(~model.flags & P_MULXN)
+			if(~model.flags & P_MULXN){
 				uerror("not a Williams model compliant algorithm");
+				return 0;
+			}
 			string = mtostr(&model);
 			puts(string);
 			free(string);
@@ -348,10 +356,14 @@ ippx:
 			}
 			break;
 		case 's': /* s  search for algorithm */
-			if(!width)
+			if(!width){
 				uerror("must specify positive -k or -w before -s");
-			if(~model.flags & P_MULXN)
+				return 0;
+			}
+			if(~model.flags & P_MULXN){
 				uerror("cannot search for non-Williams compliant models");
+				return 0;
+			}
 			praloc(&model.spoly, width);
 			praloc(&model.init, width);
 			praloc(&model.xorout, width);
@@ -366,8 +378,10 @@ ippx:
 
 			/* allocate argument array */
 			args = argc - optind;
-			if(!(apolys = malloc(args * sizeof(poly_t))))
+			if(!(apolys = malloc(args * sizeof(poly_t)))){
 				uerror("cannot allocate memory for argument list");
+				return 0;
+			}
 
 			for(pptr = apolys; optind < argc; ++optind) {
 				if(uflags & C_INFILE)
@@ -435,8 +449,10 @@ ippx:
 				return 1;
 				//exit(EXIT_SUCCESS);
 			}
-			if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT))
+			if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)){
 				uerror("cannot search for crossed-endian models");
+				return 0;
+			}
 			pass = 0;
 			do {
 				mptr = candmods = reveng(&model, qpoly, rflags, args, apolys);
@@ -465,7 +481,6 @@ ippx:
 			break;
 		default:  /* no mode specified */
 			fprintf(stderr, "%s: no mode switch specified. Use %s -h for help.\n", myname, myname);
-	return 0;
 			//exit(EXIT_FAILURE);
 	}
 
@@ -558,7 +573,7 @@ static void
 usage(void) {
 	/* print usage if asked, or if syntax incorrect */
 	fprintf(stderr,
-			"CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder\n"
+			"CRC RevEng: arbitrary-precision CRC calculator and algorithm finder\n"
 			"Usage:\t");
 	fputs(myname, stderr);
 	fprintf(stderr,
@@ -573,7 +588,7 @@ usage(void) {
 			"\t-k KPOLY\tgenerator in Koopman notation (implies WIDTH)\n"
 			"\t-m MODEL\tpreset CRC algorithm\n"
 			"\t-p POLY\t\tgenerator or search range start polynomial\n"
-			"\t-P RPOLY\treversed generator polynomial\n",
+			"\t-P RPOLY\treversed generator polynomial (implies WIDTH)\n",
 			BMP_BIT, BMP_BIT);
 	fprintf(stderr,
 			"\t-q QPOLY\tsearch range end polynomial\n"
@@ -592,9 +607,21 @@ usage(void) {
 			"\t-c calculate CRCs\t\t-d dump algorithm parameters\n"
 			"\t-D list preset algorithms\t-e echo (and reformat) input\n"
 			"\t-s search for algorithm\t\t-v calculate reversed CRCs\n"
-			"\t-h | -u | -? show this help\n"
+			"\t-g search for alg given hex+crc\t-h | -u | -? show this help\n"
+			"Common Use Examples:\n"
+			"\t   reveng -g 01020304e3\n"
+			"\t      Searches for a known/common crc preset that computes the crc\n"
+			"\t      on the end of the given hex string\n"
+			"\t   reveng -w 8 -s 01020304e3 010204039d\n"
+			"\t      Searches for any possible 8 bit width crc calc that computes\n"
+			"\t      the crc on the end of the given hex string(s)\n"
+			"\t   reveng -m CRC-8 -c 01020304\n"
+			"\t      Calculates the crc-8 of the given hex string\n"
+			"\t   reveng -D\n"
+			"\t      Outputs a list of all known/common crc models with their\n"
+			"\t      preset values\n"
 			"\n"
-			"Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015  Gregory Cook\n"
+			"Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  Gregory Cook\n"
 			"This is free software; see the source for copying conditions.  There is NO\n"
 			"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
 			"Version "