+ PrintAndLog("Model %d: %s, width: %d",i,Models[i], widtharr[i]);
+ free(Models[i]);
+ }
+ return 1;
+}
+
+//-c || -v
+//inModel = valid model name string - CRC-8
+//inHexStr = input hex string to calculate crc on
+//reverse = reverse calc option if true
+//endian = {0 = calc default endian input and output, b = big endian input and output, B = big endian output, r = right justified
+// l = little endian input and output, L = little endian output only, t = left justified}
+//result = calculated crc hex string
+int RunModel(char *inModel, char *inHexStr, bool reverse, char endian, 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;
+ 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;
+
+ //set flags
+ switch (endian) {
+ case 'b': /* b big-endian (RefIn = false, RefOut = false ) */
+ model.flags &= ~P_REFIN;
+ rflags |= R_HAVERI;
+ /* fall through: */
+ case 'B': /* B big-endian output (RefOut = false) */
+ model.flags &= ~P_REFOUT;
+ rflags |= R_HAVERO;
+ mnovel(&model);
+ /* fall through: */
+ case 'r': /* r right-justified */
+ model.flags |= P_RTJUST;
+ break;
+ case 'l': /* l little-endian input and output */
+ model.flags |= P_REFIN;
+ rflags |= R_HAVERI;
+ /* fall through: */
+ case 'L': /* L little-endian output */
+ model.flags |= P_REFOUT;
+ rflags |= R_HAVERO;
+ mnovel(&model);
+ /* fall through: */
+ case 't': /* t left-justified */
+ model.flags &= ~P_RTJUST;
+ break;
+ }
+
+ 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;
+ char endian = 0;
+ dataLen = param_getstr(Cmd, cmdp++, inModel, sizeof(inModel));
+ if (dataLen < 4) return 0;
+ dataLen = param_getstr(Cmd, cmdp++, inHexStr, sizeof(inHexStr));
+ if (dataLen < 4) return 0;
+ bool reverse = (param_get8(Cmd, cmdp++)) ? true : false;
+ endian = param_getchar(Cmd, cmdp++);
+
+ //PrintAndLog("mod: %s, hex: %s, rev %d", inModel, inHexStr, reverse);
+ int ans = RunModel(inModel, inHexStr, reverse, endian, result);
+ if (!ans) return 0;
+
+ PrintAndLog("Result: %s",result);
+ return 1;
+}
+
+//returns a calloced string (needs to be freed)
+char *SwapEndianStr(const char *inStr, const size_t len, const uint8_t blockSize){
+ char *tmp = calloc(len+1, sizeof(char));
+ for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
+ for (size_t i = 0; i < blockSize; i+=2){
+ tmp[i+(blockSize*block)] = inStr[(blockSize-1-i-1)+(blockSize*block)];
+ tmp[i+(blockSize*block)+1] = inStr[(blockSize-1-i)+(blockSize*block)];
+ }
+ }
+ return tmp;
+}
+
+// takes hex string in and searches for a matching result (hex string must include checksum)
+int CmdrevengSearch(const char *Cmd){
+ char inHexStr[50] = {0x00};
+ int dataLen = param_getstr(Cmd, 0, inHexStr, sizeof(inHexStr));
+ if (dataLen < 4) return 0;
+
+ char *Models[80];
+ int count = 0;
+ uint8_t width[80];
+ width[0] = 0;
+ uint8_t crcChars = 0;
+ char result[30];
+ char revResult[30];
+ int ans = GetModels(Models, &count, width);
+ bool found = false;
+ if (!ans) return 0;
+
+ // try each model and get result
+ for (int i = 0; i < count; i++){
+ /*if (found) {
+ free(Models[i]);
+ continue;
+ }*/
+ // round up to # of characters in this model's crc
+ crcChars = ((width[i]+7)/8)*2;
+ // can't test a model that has more crc digits than our data
+ if (crcChars >= dataLen)
+ continue;
+ memset(result, 0, 30);
+ char *inCRC = calloc(crcChars+1, sizeof(char));
+ memcpy(inCRC, inHexStr+(dataLen-crcChars), crcChars);
+
+ char *outHex = calloc(dataLen-crcChars+1, sizeof(char));
+ memcpy(outHex, inHexStr, dataLen-crcChars);
+
+ //PrintAndLog("DEBUG: dataLen: %d, crcChars: %d, Model: %s, CRC: %s, width: %d, outHex: %s",dataLen, crcChars, Models[i], inCRC, width[i], outHex);
+ ans = RunModel(Models[i], outHex, false, 0, result);
+ if (ans) {
+ //test for match
+ if (memcmp(result, inCRC, crcChars)==0){
+ PrintAndLog("\nFound a possible match!\nModel: %s\nValue: %s\n",Models[i], result);
+ //optional - stop searching if found...
+ found = true;
+ } else {
+ if (crcChars > 2){
+ char *swapEndian = SwapEndianStr(result, crcChars, crcChars);
+ if (memcmp(swapEndian, inCRC, crcChars)==0){
+ PrintAndLog("\nFound a possible match!\nModel: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
+ //optional - stop searching if found...
+ found = true;
+ }
+ free(swapEndian);
+ }
+ }
+ }
+
+ //if (!found){
+ ans = RunModel(Models[i], outHex, true, 0, revResult);
+ if (ans) {
+ //test for match
+ if (memcmp(revResult, inCRC, crcChars)==0){
+ PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue: %s\n",Models[i], revResult);
+ //optional - stop searching if found...
+ found = true;
+ } else {
+ if (crcChars > 2){
+ char *swapEndian = SwapEndianStr(revResult, crcChars, crcChars);
+ if (memcmp(swapEndian, inCRC, crcChars)==0){
+ PrintAndLog("\nFound a possible match!\nModel Reversed: %s\nValue EndianSwapped: %s\n",Models[i], swapEndian);
+ //optional - stop searching if found...
+ found = true;
+ }
+ free(swapEndian);
+ }
+ }
+ }
+ //}
+ free(inCRC);
+ free(outHex);