From 94eb741a4fa8780bef14b4c9e7fa7027bf307b74 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sun, 1 Dec 2019 22:33:50 +0200 Subject: [PATCH] refactoring + make dictionary works --- client/cmdhfmfp.c | 8 ++++++-- client/fileutils.c | 12 ++++++------ client/util.c | 5 ++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/client/cmdhfmfp.c b/client/cmdhfmfp.c index 80419fa40..541847897 100644 --- a/client/cmdhfmfp.c +++ b/client/cmdhfmfp.c @@ -842,8 +842,10 @@ static int CmdHFMFPChk(const char *cmd) { // dictionary mode if (dict_filenamelen) { size_t endFilePosition = 0; - res = loadFileDICTIONARYEx((char *)dict_filename, keyList, sizeof(keyList), &keyListLen, 16, NULL, 0, &endFilePosition); - printf("---endFilePosition %d", endFilePosition); + uint16_t keycnt = 0; + res = loadFileDICTIONARYEx((char *)dict_filename, keyList, sizeof(keyList), NULL, 16, &keycnt, 0, &endFilePosition); + keyListLen = keycnt; + printf("---endFilePosition %d\n", endFilePosition); } if (keyListLen == 0) { @@ -870,6 +872,8 @@ static int CmdHFMFPChk(const char *cmd) { Fill2bPattern(keyList, &keyListLen, &startPattern); continue; } + if (dict_filenamelen) { + } break; } diff --git a/client/fileutils.c b/client/fileutils.c index a69de8d96..ebe29f0bd 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -785,10 +785,10 @@ int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatale if (startFilePosition) fseek(f, startFilePosition, SEEK_SET); - + // read file - while (fgets(line, sizeof(line), f)) { - + while (!feof(f) && fgets(line, sizeof(line), f)) { + // add null terminator line[keylen] = 0; @@ -800,7 +800,7 @@ int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatale if (line[0] == '#') continue; - if (CheckStringIsHEXValue(line)) + if (!CheckStringIsHEXValue(line)) continue; // cant store more data @@ -811,10 +811,10 @@ int loadFileDICTIONARYEx(const char *preferredName, void *data, size_t maxdatale *endFilePosition = pos; break; } - + if (hex_to_bytes(line, data + counter, keylen >> 1) != (keylen >> 1)) continue; - + vkeycnt++; memset(line, 0, sizeof(line)); counter += (keylen >> 1); diff --git a/client/util.c b/client/util.c index c65f281fe..887f52857 100644 --- a/client/util.c +++ b/client/util.c @@ -395,7 +395,7 @@ void print_blocks(uint32_t *data, size_t len) { } int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen) { - char buf[3]; + char buf[4] = {0}; int indx = 0; int bytesValueLen = 0; while (hexValue[indx]) { @@ -405,7 +405,6 @@ int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValue } if (isxdigit(hexValue[indx])) { - buf[strlen(buf) + 1] = 0x00; buf[strlen(buf)] = hexValue[indx]; } else { // if we have symbols other than spaces and hex @@ -421,7 +420,7 @@ int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValue uint32_t temp = 0; sscanf(buf, "%x", &temp); bytesValue[bytesValueLen] = (uint8_t)(temp & 0xff); - buf[0] = 0; + memset(buf, 0, sizeof(buf)); bytesValueLen++; }