+
+//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);
+ 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);
+ free(Models[i]);
+ }
+ if (!found) PrintAndLog("\nNo matches found\n");
+ return 1;
+}