From 2d3c535ec9eed70422bdf46c64d421f87185beee Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Mon, 8 Jun 2015 13:37:22 -0400 Subject: [PATCH] fixed bug in util getpraram8 + reveng adjustments reveng: fixed error handling some. added -s option to GetModels() --- client/cmdcrc.c | 168 ++++++++++++++++++++++++++++++++++++++++---- client/reveng/cli.c | 29 +++++--- client/util.c | 2 +- 3 files changed, 178 insertions(+), 21 deletions(-) diff --git a/client/cmdcrc.c b/client/cmdcrc.c index 28d2cde0..82504db7 100644 --- a/client/cmdcrc.c +++ b/client/cmdcrc.c @@ -29,6 +29,11 @@ #define MAX_ARGS 20 +int uerr(char *msg){ + PrintAndLog("%s",msg); + return 0; +} + int split(char *str, char *arr[MAX_ARGS]){ int beginIndex = 0; int endIndex; @@ -85,13 +90,13 @@ int GetModels(char *Models[], int *count, uint32_t *width){ PZERO, /* check value unused */ NULL /* no model name */ }; - //int ibperhx = 8, obperhx = 8; - //int rflags = 0, uflags = 0; /* search and UI flags */ + int ibperhx = 8;//, obperhx = 8; + int rflags = 0, uflags = 0; /* search and UI flags */ //unsigned long width = 0UL; //int c, mode = 0, args, psets, pass; - //poly_t apoly, crc, qpoly = PZERO, *apolys, *pptr = NULL, *qptr = NULL; - //model_t pset = model, *candmods, *mptr; + poly_t apoly, crc, qpoly = PZERO, *apolys = NULL, *pptr = NULL, *qptr = NULL; + model_t pset = model, *candmods, *mptr; //char *string; //myname = argv[0]; @@ -106,29 +111,163 @@ int GetModels(char *Models[], int *count, uint32_t *width){ //pos=0; //optind=1; + int args = 0, psets, pass; + int Cnt = 0; if (*width == 0) { //reveng -D *count = mcount(); //PrintAndLog("Count: %d",*count); - if(!*count){ - PrintAndLog("no preset models available"); - return 0; - } + if(!*count) + return uerr("no preset models available"); + for(int mode = 0; mode < *count; ++mode) { mbynum(&model, mode); mcanon(&model); size_t size = (model.name && *model.name) ? strlen(model.name) : 6; //PrintAndLog("Size: %d, %s",size,model.name); char *tmp = calloc(size+1, sizeof(char)); - if (tmp==NULL){ - PrintAndLog("out of memory?"); - return 0; - } + if (tmp==NULL) + return uerr("out of memory?"); + memcpy(tmp, model.name, size); Models[mode] = tmp; //ufound(&model); } } else { //reveng -s + if(~model.flags & P_MULXN) + return uerr("cannot search for non-Williams compliant models"); + + praloc(&model.spoly, *width); + praloc(&model.init, *width); + praloc(&model.xorout, *width); + if(!plen(model.spoly)) + palloc(&model.spoly, *width); + else + *width = plen(model.spoly); + + /* special case if qpoly is zero, search to end of range */ + if(!ptst(qpoly)) + rflags &= ~R_HAVEQ; + + + /* not going to be sending additional args + + // allocate argument array + args = argc - optind; + if(!(apolys = malloc(args * sizeof(poly_t)))) + return uerr("cannot allocate memory for argument list"); + + for(pptr = apolys; optind < argc; ++optind) { + if(uflags & C_INFILE) + *pptr++ = rdpoly(argv[optind], model.flags, ibperhx); + else + *pptr++ = strtop(argv[optind], model.flags, ibperhx); + } + // exit value of pptr is used hereafter! + + */ + + /* if endianness not specified, try + * little-endian then big-endian. + * NB: crossed-endian algorithms will not be + * searched. + */ + /* scan against preset models */ + if(~uflags & C_FORCE) { + pass = 0; + Cnt = 0; + do { + psets = mcount(); + //PrintAndLog("psets: %d",psets); + while(psets) { + mbynum(&pset, --psets); + + /* skip if different width, or refin or refout don't match */ + if(plen(pset.spoly) != *width || (model.flags ^ pset.flags) & (P_REFIN | P_REFOUT)) + continue; + /* skip if the preset doesn't match specified parameters */ + if(rflags & R_HAVEP && pcmp(&model.spoly, &pset.spoly)) + continue; + if(rflags & R_HAVEI && psncmp(&model.init, &pset.init)) + continue; + if(rflags & R_HAVEX && psncmp(&model.xorout, &pset.xorout)) + continue; + + apoly = pclone(pset.xorout); + if(pset.flags & P_REFOUT) + prev(&apoly); + for(qptr = apolys; qptr < pptr; ++qptr) { + crc = pcrc(*qptr, pset.spoly, pset.init, apoly, 0); + if(ptst(crc)) { + pfree(&crc); + break; + } else + pfree(&crc); + } + pfree(&apoly); + if(qptr == pptr) { + /* the selected model solved all arguments */ + mcanon(&pset); + + size_t size = (pset.name && *pset.name) ? strlen(pset.name) : 6; + //PrintAndLog("Size: %d, %s, count: %d",size,pset.name, Cnt); + char *tmp = calloc(size+1, sizeof(char)); + if (tmp==NULL){ + PrintAndLog("out of memory?"); + return 0; + } + memcpy(tmp, pset.name, size); + Models[Cnt++] = tmp; + *count = Cnt; + //ufound(&pset); + uflags |= C_RESULT; + } + } + mfree(&pset); + + /* toggle refIn/refOut and reflect arguments */ + if(~rflags & R_HAVERI) { + model.flags ^= P_REFIN | P_REFOUT; + for(qptr = apolys; qptr < pptr; ++qptr) + prevch(qptr, ibperhx); + } + } while(~rflags & R_HAVERI && ++pass < 2); + } + if(uflags & C_RESULT) { + for(qptr = apolys; qptr < pptr; ++qptr) + pfree(qptr); + return 1; + //exit(EXIT_SUCCESS); + } + if(!(model.flags & P_REFIN) != !(model.flags & P_REFOUT)) + return uerr("cannot search for crossed-endian models"); + pass = 0; + do { + mptr = candmods = reveng(&model, qpoly, rflags, args, apolys); + if(mptr && plen(mptr->spoly)) + uflags |= C_RESULT; + while(mptr && plen(mptr->spoly)) { + /* results were printed by the callback + * string = mtostr(mptr); + * puts(string); + * free(string); + */ + mfree(mptr++); + } + free(candmods); + if(~rflags & R_HAVERI) { + model.flags ^= P_REFIN | P_REFOUT; + for(qptr = apolys; qptr < pptr; ++qptr) + prevch(qptr, ibperhx); + } + } while(~rflags & R_HAVERI && ++pass < 2); + for(qptr = apolys; qptr < pptr; ++qptr) + pfree(qptr); + free(apolys); + if(~uflags & C_RESULT) + return uerr("no models found"); + + } //PrintAndLog("DONE"); return 1; @@ -139,6 +278,11 @@ int CmdrevengTest(const char *Cmd){ char *Models[80]; int count = 0; uint32_t width = 0; + width = param_get8(Cmd, 0); + //PrintAndLog("width: %d",width); + if (width > 89) + return uerr("Width cannot exceed 89"); + int ans = GetModels(Models, &count, &width); if (!ans) return 0; diff --git a/client/reveng/cli.c b/client/reveng/cli.c index 7747b9af..7b936c67 100644 --- a/client/reveng/cli.c +++ b/client/reveng/cli.c @@ -183,8 +183,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; @@ -314,8 +316,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 +331,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 +354,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 +376,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 +447,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 +479,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); } diff --git a/client/util.c b/client/util.c index 9f2142c6..b114fc49 100644 --- a/client/util.c +++ b/client/util.c @@ -248,7 +248,7 @@ char param_getchar(const char *line, int paramnum) uint8_t param_get8(const char *line, int paramnum) { - return param_get8ex(line, paramnum, 10, 0); + return param_get8ex(line, paramnum, 0, 10); } /** -- 2.39.2