From 37f4270a244eae21f8a423e81b384e62a7a2582d Mon Sep 17 00:00:00 2001 From: marshmellow42 Date: Mon, 8 Jun 2015 21:22:18 -0400 Subject: [PATCH] reveng add api RunModel --- client/cmdcrc.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++ client/cmdcrc.h | 2 + client/cmdmain.c | 13 ++++- 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/client/cmdcrc.c b/client/cmdcrc.c index 82504db7..f93ab99d 100644 --- a/client/cmdcrc.c +++ b/client/cmdcrc.c @@ -293,3 +293,126 @@ int CmdrevengTest(const char *Cmd){ } return 1; } + +//-c || -v +int RunModel(char *inModel, char *inHexStr, bool reverse, char *result){ + /* default values */ + static model_t model = { + PZERO, // no CRC polynomial, user must specify + PZERO, // Init = 0 + P_BE, // RefIn = false, RefOut = false, plus P_RTJUST setting in reveng.h + PZERO, // XorOut = 0 + PZERO, // check value unused + NULL // no model name + }; + int ibperhx = 8, obperhx = 8; + int rflags = 0; // search flags + int c; + unsigned long width = 0UL; + poly_t apoly, crc; + + char *string; + + // stdin must be binary + #ifdef _WIN32 + _setmode(STDIN_FILENO, _O_BINARY); + #endif /* _WIN32 */ + + SETBMP(); + //set model + if(!(c = mbynam(&model, inModel))) { + fprintf(stderr,"error: preset model '%s' not found. Use reveng -D to list presets.\n", inModel); + return 0; + } + if(c < 0) + return uerr("no preset models available"); + + // 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; + + mcanon(&model); + + if (reverse) { + // v calculate reversed CRC + /* Distinct from the -V switch as this causes + * the arguments and output to be reversed as well. + */ + // reciprocate Poly + prcp(&model.spoly); + + /* mrev() does: + * if(refout) prev(init); else prev(xorout); + * but here the entire argument polynomial is + * reflected, not just the characters, so RefIn + * and RefOut are not inverted as with -V. + * Consequently Init is the mirror image of the + * one resulting from -V, and so we have: + */ + if(~model.flags & P_REFOUT) { + prev(&model.init); + prev(&model.xorout); + } + + // swap init and xorout + apoly = model.init; + model.init = model.xorout; + model.xorout = apoly; + } + // c calculate CRC + + // validate inputs + /* if(plen(model.spoly) == 0) { + * fprintf(stderr,"%s: no polynomial specified for -%c (add -w WIDTH -p POLY)\n", myname, mode); + * exit(EXIT_FAILURE); + * } + */ + + /* in the Williams model, xorout is applied after the refout stage. + * as refout is part of ptostr(), we reverse xorout here. + */ + if(model.flags & P_REFOUT) + prev(&model.xorout); + + apoly = strtop(inHexStr, model.flags, ibperhx); + + if(reverse) + prev(&apoly); + + crc = pcrc(apoly, model.spoly, model.init, model.xorout, model.flags); + + if(reverse) + prev(&crc); + + string = ptostr(crc, model.flags, obperhx); + for (int i = 0; i < 50; i++){ + result[i] = string[i]; + if (result[i]==0) break; + } + free(string); + pfree(&crc); + pfree(&apoly); + return 1; +} + +//test call to RunModel +int CmdrevengTestC(const char *Cmd){ + int cmdp = 0; + char inModel[30] = {0x00}; + char inHexStr[30] = {0x00}; + char result[30]; + int dataLen; + + dataLen = param_getstr(Cmd, cmdp++, inModel); + if (dataLen < 4) return 0; + dataLen = param_getstr(Cmd, cmdp++, inHexStr); + if (dataLen < 4) return 0; + bool reverse = (param_get8(Cmd, cmdp)) ? true : false; + + //PrintAndLog("mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse); + int ans = RunModel(inModel, inHexStr, reverse, result); + if (!ans) return 0; + + PrintAndLog("Result: %s",result); + return 1; +} diff --git a/client/cmdcrc.h b/client/cmdcrc.h index f542fa27..53aa935e 100644 --- a/client/cmdcrc.h +++ b/client/cmdcrc.h @@ -13,5 +13,7 @@ int CmdCrc(const char *Cmd); int CmdrevengTest(const char *Cmd); +int CmdrevengTestC(const char *Cmd); int GetModels(char *Models[], int *count, uint32_t *width); +int RunModel(char *inModel, char *inHexStr, bool reverse, char *result); #endif diff --git a/client/cmdmain.c b/client/cmdmain.c index c3331fe0..6d4d9c1f 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -33,7 +33,10 @@ unsigned int current_command = CMD_UNKNOWN; static int CmdHelp(const char *Cmd); static int CmdQuit(const char *Cmd); static int CmdRev(const char *Cmd); +/* // for testing reveng api - cmdcrc.c static int CmdrevengT(const char *Cmd); +static int CmdrevengC(const char *Cmd); +*/ //For storing command that are received from the device static UsbCommand cmdBuffer[CMD_BUFFER_SIZE]; @@ -50,7 +53,10 @@ static command_t CommandTable[] = {"hw", CmdHW, 1, "{ Hardware commands... }"}, {"lf", CmdLF, 1, "{ Low Frequency commands... }"}, {"reveng",CmdRev, 1, "Crc calculations from the software reveng1-30"}, + /* // for testing reveng api - cmdcrc.c {"revengt",CmdrevengT,1, "TEST Crc calculations from the software reveng1-30"}, + {"revengc",CmdrevengC,1, "TEST Crc calculations from the software reveng1-30"}, + */ {"script",CmdScript,1, "{ Scripting commands }"}, {"quit", CmdQuit, 1, "Exit program"}, {"exit", CmdQuit, 1, "Exit program"}, @@ -78,11 +84,16 @@ int CmdRev(const char *Cmd) CmdCrc(Cmd); return 0; } - +/* // for testing reveng api - cmdcrc.c int CmdrevengT(const char *Cmd) { return CmdrevengTest(Cmd); } +int CmdrevengC(const char *Cmd) +{ + return CmdrevengTestC(Cmd); +} +*/ /** * @brief This method should be called when sending a new command to the pm3. In case any old * responses from previous commands are stored in the buffer, a call to this method should clear them. -- 2.39.2