]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmf.c
Code cleanup:
[proxmark3-svn] / client / cmdhfmf.c
index cdac64764dd33dd554c6dbd1a7c785f7038db7ef..3fc3f8f7977fc657ecb8adfac8813d7db37b1be7 100644 (file)
@@ -8,6 +8,7 @@
 // High frequency MIFARE commands\r
 //-----------------------------------------------------------------------------\r
 \r
+#include <inttypes.h>\r
 #include "cmdhfmf.h"\r
 #include "./nonce2key/nonce2key.h"\r
 \r
@@ -34,8 +35,10 @@ int CmdHF14AMifare(const char *Cmd)
     SendCommand(&c);\r
        \r
        //flush queue\r
-       while (ukbhit())        getchar();\r
-\r
+       while (ukbhit()) {\r
+               int c = getchar(); (void) c;\r
+       }\r
+       \r
        // wait cycle\r
        while (true) {\r
         printf(".");\r
@@ -82,7 +85,7 @@ int CmdHF14AMifare(const char *Cmd)
        } else {\r
                isOK = 0;\r
                printf("------------------------------------------------------------------\n");\r
-               PrintAndLog("Found valid key:%012"llx" \n", r_key);\r
+               PrintAndLog("Found valid key:%012" PRIx64 " \n", r_key);\r
        }\r
        \r
        PrintAndLog("");\r
@@ -302,7 +305,8 @@ int CmdHF14AMfDump(const char *Cmd)
        \r
        // Read keys A from file\r
        for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
-               if (fread( keyA[sectorNo], 1, 6, fin ) == 0) {\r
+               size_t bytes_read = fread(keyA[sectorNo], 1, 6, fin);\r
+               if (bytes_read != 6) {\r
                        PrintAndLog("File reading error.");\r
                        fclose(fin);\r
                        return 2;\r
@@ -311,7 +315,8 @@ int CmdHF14AMfDump(const char *Cmd)
        \r
        // Read keys B from file\r
        for (sectorNo=0; sectorNo<numSectors; sectorNo++) {\r
-               if (fread( keyB[sectorNo], 1, 6, fin ) == 0) {\r
+               size_t bytes_read = fread(keyB[sectorNo], 1, 6, fin);\r
+               if (bytes_read != 6) {\r
                        PrintAndLog("File reading error.");\r
                        fclose(fin);\r
                        return 2;\r
@@ -466,16 +471,17 @@ int CmdHF14AMfRestore(const char *Cmd)
        }\r
        \r
        for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
-               if (fread(keyA[sectorNo], 1, 6, fkeys) == 0) {\r
+               size_t bytes_read = fread(keyA[sectorNo], 1, 6, fkeys);\r
+               if (bytes_read != 6) {\r
                        PrintAndLog("File reading error (dumpkeys.bin).");\r
-\r
                        fclose(fkeys);\r
                        return 2;\r
                }\r
        }\r
 \r
        for (sectorNo = 0; sectorNo < numSectors; sectorNo++) {\r
-               if (fread(keyB[sectorNo], 1, 6, fkeys) == 0) {\r
+               size_t bytes_read = fread(keyB[sectorNo], 1, 6, fkeys);\r
+               if (bytes_read != 6) {\r
                        PrintAndLog("File reading error (dumpkeys.bin).");\r
                        fclose(fkeys);\r
                        return 2;\r
@@ -495,7 +501,8 @@ int CmdHF14AMfRestore(const char *Cmd)
                        UsbCommand c = {CMD_MIFARE_WRITEBL, {FirstBlockOfSector(sectorNo) + blockNo, keyType, 0}};\r
                        memcpy(c.d.asBytes, key, 6);\r
                        \r
-                       if (fread(bldata, 1, 16, fdump) == 0) {\r
+                       size_t bytes_read = fread(bldata, 1, 16, fdump);\r
+                       if (bytes_read != 16) {\r
                                PrintAndLog("File reading error (dumpdata.bin).");\r
                                fclose(fdump);\r
                                return 2;\r
@@ -632,7 +639,7 @@ int CmdHF14AMfNested(const char *Cmd)
                }\r
                key64 = bytes_to_num(keyBlock, 6);\r
                if (key64) {\r
-                       PrintAndLog("Found valid key:%012"llx, key64);\r
+                       PrintAndLog("Found valid key:%012" PRIx64, key64);\r
 \r
                        // transfer key to the emulator\r
                        if (transferToEml) {\r
@@ -718,7 +725,7 @@ int CmdHF14AMfNested(const char *Cmd)
 \r
                                        key64 = bytes_to_num(keyBlock, 6);\r
                                        if (key64) {\r
-                                               PrintAndLog("Found valid key:%012"llx, key64);\r
+                                               PrintAndLog("Found valid key:%012" PRIx64, key64);\r
                                                e_sector[sectorNo].foundKey[trgKeyType] = 1;\r
                                                e_sector[sectorNo].Key[trgKeyType] = key64;\r
                                        }\r
@@ -734,7 +741,7 @@ int CmdHF14AMfNested(const char *Cmd)
                PrintAndLog("|sec|key A           |res|key B           |res|");\r
                PrintAndLog("|---|----------------|---|----------------|---|");\r
                for (i = 0; i < SectorsCnt; i++) {\r
-                       PrintAndLog("|%03d|  %012"llx"  | %d |  %012"llx"  | %d |", i,\r
+                       PrintAndLog("|%03d|  %012" PRIx64 "  | %d |  %012" PRIx64 "  | %d |", i,\r
                                e_sector[i].Key[0], e_sector[i].foundKey[0], e_sector[i].Key[1], e_sector[i].foundKey[1]);\r
                }\r
                PrintAndLog("|---|----------------|---|----------------|---|");\r
@@ -919,13 +926,14 @@ int CmdHF14AMfChk(const char *Cmd)
                                                if (!p) {\r
                                                        PrintAndLog("Cannot allocate memory for defKeys");\r
                                                        free(keyBlock);\r
+                                                       fclose(f);\r
                                                        return 2;\r
                                                }\r
                                                keyBlock = p;\r
                                        }\r
                                        memset(keyBlock + 6 * keycnt, 0, 6);\r
                                        num_to_bytes(strtoll(buf, NULL, 16), 6, keyBlock + 6*keycnt);\r
-                                       PrintAndLog("chk custom key[%2d] %012"llx, keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
+                                       PrintAndLog("chk custom key[%2d] %012" PRIx64 , keycnt, bytes_to_num(keyBlock + 6*keycnt, 6));\r
                                        keycnt++;\r
                                        memset(buf, 0, sizeof(buf));\r
                                }\r
@@ -969,7 +977,7 @@ int CmdHF14AMfChk(const char *Cmd)
                                res = mfCheckKeys(b, t, true, size, &keyBlock[6*c], &key64);\r
                                if (res != 1) {\r
                                        if (!res) {\r
-                                               PrintAndLog("Found valid key:[%012"llx"]",key64);\r
+                                               PrintAndLog("Found valid key:[%012" PRIx64 "]",key64);\r
                                                num_to_bytes(key64, 6, foundKey[t][i]);\r
                                                validKey[t][i] = true;\r
                                        } \r
@@ -1058,7 +1066,7 @@ void readerAttack(nonces_t ar_resp[], bool setEmulatorMem, bool doStandardAttack
                                uint8_t sectorNum = ar_resp[i+ATTACK_KEY_COUNT].sector;\r
                                uint8_t keyType = ar_resp[i+ATTACK_KEY_COUNT].keytype;\r
 \r
-                               PrintAndLog("M-Found Key%s for sector %02d: [%012"llx"]"\r
+                               PrintAndLog("M-Found Key%s for sector %02d: [%012" PRIx64 "]"\r
                                        , keyType ? "B" : "A"\r
                                        , sectorNum\r
                                        , key\r
@@ -1447,7 +1455,7 @@ int CmdHF14AMfELoad(const char *Cmd)
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+       if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
        fnameptr += len;\r
 \r
@@ -1546,7 +1554,7 @@ int CmdHF14AMfESave(const char *Cmd)
 \r
        len = param_getstr(Cmd,nameParamNo,filename);\r
        \r
-       if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+       if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
        \r
        // user supplied filename?\r
        if (len < 1) {\r
@@ -1670,7 +1678,7 @@ int CmdHF14AMfEKeyPrn(const char *Cmd)
                }\r
                keyA = bytes_to_num(data, 6);\r
                keyB = bytes_to_num(data + 10, 6);\r
-               PrintAndLog("|%03d|  %012"llx"  |  %012"llx"  |", i, keyA, keyB);\r
+               PrintAndLog("|%03d|  %012" PRIx64 "  |  %012" PRIx64 "  |", i, keyA, keyB);\r
        }\r
        PrintAndLog("|---|----------------|----------------|");\r
        \r
@@ -1822,7 +1830,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+               if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
 \r
                memcpy(filename, Cmd, len);\r
                fnameptr += len;\r
@@ -1863,6 +1871,7 @@ int CmdHF14AMfCLoad(const char *Cmd)
 \r
                        if (mfCSetBlock(blockNum, buf8, NULL, 0, flags)) {\r
                                PrintAndLog("Can't set magic card block: %d", blockNum);\r
+                               fclose(f);\r
                                return 3;\r
                        }\r
                        blockNum++;\r
@@ -1991,7 +2000,7 @@ int CmdHF14AMfCSave(const char *Cmd) {
                return 0;\r
        } else {\r
                len = strlen(Cmd);\r
-               if (len > FILE_PATH_SIZE - 4) len = FILE_PATH_SIZE - 4;\r
+               if (len > FILE_PATH_SIZE - 5) len = FILE_PATH_SIZE - 5;\r
        \r
                if (len < 1) {\r
                        // get filename\r
@@ -2111,11 +2120,14 @@ int CmdHF14AMfSniff(const char *Cmd){
                        uint16_t traceLen = resp.arg[1];\r
                        len = resp.arg[2];\r
 \r
-                       if (res == 0) return 0;                                         // we are done\r
+                       if (res == 0) {                                                         // we are done\r
+                               free(buf);\r
+                               return 0;\r
+                       }\r
 \r
                        if (res == 1) {                                                         // there is (more) data to be transferred\r
                                if (pckNum == 0) {                                              // first packet, (re)allocate necessary buffer\r
-                                       if (traceLen > bufsize) {\r
+                                       if (traceLen > bufsize || buf == NULL) {\r
                                                uint8_t *p;\r
                                                if (buf == NULL) {                              // not yet allocated\r
                                                        p = malloc(traceLen);\r
Impressum, Datenschutz