diff --git a/client/cmdflashmem.c b/client/cmdflashmem.c index b77161951..3a744a544 100644 --- a/client/cmdflashmem.c +++ b/client/cmdflashmem.c @@ -351,8 +351,8 @@ static int CmdFlashMemSave(const char *Cmd) { return 1; } - saveFile(filename, "bin", dump, len); - saveFileEML(filename, "eml", dump, len, 16); + saveFile(filename, ".bin", dump, len); + saveFileEML(filename, dump, len, 16); free(dump); return 0; } diff --git a/client/cmdhf14b.c b/client/cmdhf14b.c index b1aa78e64..8f222b1e8 100644 --- a/client/cmdhf14b.c +++ b/client/cmdhf14b.c @@ -985,8 +985,8 @@ static int CmdHF14BDump(const char *Cmd) { size_t datalen = (blocks + 1) * 4; - saveFileEML(filename, "eml", data, datalen, 4); - saveFile(filename, "bin", data, datalen); + saveFileEML(filename, data, datalen, 4); + saveFile(filename, ".bin", data, datalen); out: return switch_off_field_14b(); } diff --git a/client/cmdhf15.c b/client/cmdhf15.c index 496ea3073..db9f66735 100644 --- a/client/cmdhf15.c +++ b/client/cmdhf15.c @@ -842,8 +842,8 @@ static int CmdHF15Dump(const char *Cmd) { PrintAndLogEx(NORMAL, "\n"); size_t datalen = blocknum * 4; - saveFileEML(filename, "eml", data, datalen, 4); - saveFile(filename, "bin", data, datalen); + saveFileEML(filename, data, datalen, 4); + saveFile(filename, ".bin", data, datalen); return 0; } diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c index 5372022ab..b9a1b091d 100644 --- a/client/cmdhficlass.c +++ b/client/cmdhficlass.c @@ -425,7 +425,7 @@ static int CmdHFiClassSim(const char *Cmd) { memcpy(dump + i * 24 + 16, resp.d.asBytes + i * 16 + 8, 8); } /** Now, save to dumpfile **/ - saveFile("iclass_mac_attack", "bin", dump, datalen); + saveFile("iclass_mac_attack", ".bin", dump, datalen); free(dump); break; } @@ -479,7 +479,7 @@ static int CmdHFiClassSim(const char *Cmd) { // copy NR_MAC (eight bytes from the response) ( 8b csn + 8b epurse == 16) memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + i * 16 + 8, 8); } - saveFile("iclass_mac_attack_keyroll_A", "bin", dump, datalen); + saveFile("iclass_mac_attack_keyroll_A", ".bin", dump, datalen); //KEYROLL 2 memset(dump, 0, datalen); @@ -494,7 +494,7 @@ static int CmdHFiClassSim(const char *Cmd) { memcpy(dump + i * MAC_ITEM_SIZE + 16, resp.d.asBytes + resp_index + 8, 8); resp_index++; } - saveFile("iclass_mac_attack_keyroll_B", "bin", dump, datalen); + saveFile("iclass_mac_attack_keyroll_B", ".bin", dump, datalen); free(dump); break; } @@ -720,7 +720,7 @@ static int CmdHFiClassDecrypt(const char *Cmd) { } } - saveFile(outfilename, "bin", decrypted, fsize); + saveFile(outfilename, ".bin", decrypted, fsize); printIclassDumpContents(decrypted, 1, (fsize / 8), fsize); free(decrypted); return 0; @@ -1087,7 +1087,7 @@ static int CmdHFiClassReader_Dump(const char *Cmd) { // save the dump to .bin file PrintAndLogEx(SUCCESS, "saving dump file - %d blocks read", gotBytes / 8); - saveFile(filename, "bin", tag_data, gotBytes); + saveFile(filename, ".bin", tag_data, gotBytes); return 1; } diff --git a/client/cmdhflegic.c b/client/cmdhflegic.c index 0da3daaaa..3ea33f089 100644 --- a/client/cmdhflegic.c +++ b/client/cmdhflegic.c @@ -1212,8 +1212,8 @@ static int CmdLegicESave(const char *Cmd) { else sprintf(fnameptr + fileNlen, ".bin"); - saveFileEML(filename, "eml", data, numofbytes, 8); - saveFile(filename, "bin", data, numofbytes); + saveFileEML(filename, data, numofbytes, 8); + saveFile(filename, ".bin", data, numofbytes); return 0; } diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c index db1b7d798..120240e3e 100644 --- a/client/cmdhfmf.c +++ b/client/cmdhfmf.c @@ -917,9 +917,9 @@ static int CmdHF14AMfDump(const char *Cmd) { uint16_t bytes = 16 * (FirstBlockOfSector(numSectors - 1) + NumBlocksPerSector(numSectors - 1)); - saveFile(dataFilename, "bin", (uint8_t *)carddata, bytes); - saveFileEML(dataFilename, "eml", (uint8_t *)carddata, bytes, MFBLOCK_SIZE); - saveFileJSON(dataFilename, "json", jsfCardMemory, (uint8_t *)carddata, bytes); + saveFile(dataFilename, ".bin", (uint8_t *)carddata, bytes); + saveFileEML(dataFilename, (uint8_t *)carddata, bytes, MFBLOCK_SIZE); + saveFileJSON(dataFilename, jsfCardMemory, (uint8_t *)carddata, bytes); return 0; } @@ -2685,9 +2685,9 @@ static int CmdHF14AMfESave(const char *Cmd) { FillFileNameByUID(fnameptr, dump, "-dump", 4); } - saveFile(filename, "bin", dump, bytes); - saveFileEML(filename, "eml", dump, bytes, MFBLOCK_SIZE); - saveFileJSON(filename, "json", jsfCardMemory, dump, bytes); + saveFile(filename, ".bin", dump, bytes); + saveFileEML(filename, dump, bytes, MFBLOCK_SIZE); + saveFileJSON(filename, jsfCardMemory, dump, bytes); free(dump); return 0; } @@ -3119,9 +3119,9 @@ static int CmdHF14AMfCSave(const char *Cmd) { PrintAndLogEx(SUCCESS, "uploaded %d bytes to emulator memory", bytes); } - saveFile(filename, "bin", dump, bytes); - saveFileEML(filename, "eml", dump, bytes, MFBLOCK_SIZE); - saveFileJSON(filename, "json", jsfCardMemory, dump, bytes); + saveFile(filename, ".bin", dump, bytes); + saveFileEML(filename, dump, bytes, MFBLOCK_SIZE); + saveFileJSON(filename, jsfCardMemory, dump, bytes); free(dump); return 0; } diff --git a/client/cmdhfmfu.c b/client/cmdhfmfu.c index 39ee6a6b0..501bd9551 100644 --- a/client/cmdhfmfu.c +++ b/client/cmdhfmfu.c @@ -1974,8 +1974,8 @@ static int CmdHF14AMfUDump(const char *Cmd) { FillFileNameByUID(fptr, card.uid, "-dump", card.uidlen); } uint16_t datalen = pages * 4 + MFU_DUMP_PREFIX_LENGTH; - saveFile(filename, "bin", (uint8_t *)&dump_file_data, datalen); - saveFileJSON(filename, "json", jsfMfuMemory, (uint8_t *)&dump_file_data, datalen); + saveFile(filename, ".bin", (uint8_t *)&dump_file_data, datalen); + saveFileJSON(filename, jsfMfuMemory, (uint8_t *)&dump_file_data, datalen); if (is_partial) PrintAndLogEx(WARNING, "Partial dump created. (%d of %d blocks)", pages, card_mem_size); diff --git a/client/cmdlfhitag.c b/client/cmdlfhitag.c index f901a042c..be66b460b 100644 --- a/client/cmdlfhitag.c +++ b/client/cmdlfhitag.c @@ -583,9 +583,9 @@ static int CmdLFHitagReader(const char *Cmd) { fnameptr += sprintf(fnameptr, "lf-hitag-"); FillFileNameByUID(fnameptr, data, "-dump", 4); - saveFile(filename, "bin", data, 48); - saveFileEML(filename, "eml", data, 48, 4); - saveFileJSON(filename, "json", jsfHitag, data, 48); + saveFile(filename, ".bin", data, 48); + saveFileEML(filename, data, 48, 4); + saveFileJSON(filename, jsfHitag, data, 48); // block3, 1 byte printHitagConfiguration(data[4 * 3]); diff --git a/client/cmdtrace.c b/client/cmdtrace.c index 28605c08f..1582f2997 100644 --- a/client/cmdtrace.c +++ b/client/cmdtrace.c @@ -563,7 +563,7 @@ static int CmdTraceSave(const char *Cmd) { if (strlen(Cmd) < 1 || cmdp == 'h') return usage_trace_save(); param_getstr(Cmd, 0, filename, sizeof(filename)); - saveFile(filename, "bin", trace, traceLen); + saveFile(filename, ".bin", trace, traceLen); return 0; } diff --git a/client/loclass/fileutils.c b/client/loclass/fileutils.c index 5370c0c5b..5e3b423f7 100644 --- a/client/loclass/fileutils.c +++ b/client/loclass/fileutils.c @@ -57,15 +57,49 @@ int fileExists(const char *filename) { return result == 0; } -int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) { - int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10); - char *fileName = calloc(size, sizeof(char)); +static char *filenamemcopy(const char *preferredName, const char *suffix) { + if (preferredName == NULL) return NULL; + if (suffix == NULL) return NULL; + char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t)); + if (fileName == NULL) + return NULL; + strcpy(fileName, preferredName); + if (str_endswith(fileName, suffix)) + return fileName; + strcat(fileName, suffix); + return fileName; +} + +static char *newfilenamemcopy(const char *preferredName, const char *suffix) { + if (preferredName == NULL) return NULL; + if (suffix == NULL) return NULL; + char *preferredNameTmp = (char *) calloc(strlen(preferredName) + 1, sizeof(uint8_t)); + if (preferredNameTmp == NULL) + return NULL; + strcpy(preferredNameTmp, preferredName); + if (str_endswith(preferredNameTmp, suffix)) + preferredNameTmp[strlen(preferredNameTmp) - strlen(suffix)] = '\0'; + char *fileName = (char *) calloc(strlen(preferredNameTmp) + strlen(suffix) + 1 + 10, sizeof(uint8_t)); // 10: room for filenum to ensure new filename + if (fileName == NULL) { + free(preferredNameTmp); + return NULL; + } int num = 1; - sprintf(fileName, "%s.%s", preferredName, suffix); + sprintf(fileName, "%s%s", preferredNameTmp, suffix); while (fileExists(fileName)) { - sprintf(fileName, "%s-%d.%s", preferredName, num, suffix); + sprintf(fileName, "%s-%d%s", preferredNameTmp, num, suffix); num++; } + free(preferredNameTmp); + return fileName; +} + +int saveFile(const char *preferredName, const char *suffix, const void *data, size_t datalen) { + + if (data == NULL) return 1; + char *fileName = newfilenamemcopy(preferredName, suffix); + if (fileName == NULL) return 1; + /* We should have a valid filename now, e.g. dumpdata-3.bin */ /*Opening file for writing in binary mode*/ @@ -83,23 +117,15 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si return 0; } -int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize) { +int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize) { - if (preferredName == NULL) return 1; - if (suffix == NULL) return 1; if (data == NULL) return 1; + char *fileName = newfilenamemcopy(preferredName, ".eml"); + if (fileName == NULL) return 1; int retval = 0; int blocks = datalen / blocksize; uint16_t currblock = 1; - int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10); - char *fileName = calloc(size, sizeof(char)); - int num = 1; - sprintf(fileName, "%s.%s", preferredName, suffix); - while (fileExists(fileName)) { - sprintf(fileName, "%s-%d.%s", preferredName, num, suffix); - num++; - } /* We should have a valid filename now, e.g. dumpdata-3.bin */ @@ -136,20 +162,13 @@ out: return retval; } -int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen) { - if (preferredName == NULL) return 1; - if (suffix == NULL) return 1; +int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen) { + if (data == NULL) return 1; + char *fileName = newfilenamemcopy(preferredName, ".json"); + if (fileName == NULL) return 1; int retval = 0; - int size = sizeof(char) * (strlen(preferredName) + strlen(suffix) + 10); - char *fileName = calloc(size, sizeof(char)); - int num = 1; - sprintf(fileName, "%s.%s", preferredName, suffix); - while (fileExists(fileName)) { - sprintf(fileName, "%s-%d.%s", preferredName, num, suffix); - num++; - } json_t *root = json_object(); JsonSaveStr(root, "Created", "proxmark3"); diff --git a/client/loclass/fileutils.h b/client/loclass/fileutils.h index 6f6b5456a..dae23a1b1 100644 --- a/client/loclass/fileutils.h +++ b/client/loclass/fileutils.h @@ -70,7 +70,7 @@ int fileExists(const char *filename); * E.g. dumpdata-15.txt * * @param preferredName - * @param suffix the file suffix. Leave out the ".". + * @param suffix the file suffix. Including the ".". * @param data The binary data to write to the file * @param datalen the length of the data * @return 0 for ok, 1 for failz @@ -83,13 +83,12 @@ int saveFile(const char *preferredName, const char *suffix, const void *data, si * E.g. dumpdata-15.txt * * @param preferredName - * @param suffix the file suffix. Leave out the ".". * @param data The binary data to write to the file * @param datalen the length of the data * @param blocksize the length of one row * @return 0 for ok, 1 for failz */ -int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, size_t datalen, size_t blocksize); +int saveFileEML(const char *preferredName, uint8_t *data, size_t datalen, size_t blocksize); /** STUB * @brief Utility function to save JSON data to a file. This method takes a preferred name, but if that @@ -97,13 +96,12 @@ int saveFileEML(const char *preferredName, const char *suffix, uint8_t *data, si * E.g. dumpdata-15.json * * @param preferredName - * @param suffix the file suffix. Leave out the ".". * @param ftype type of file. * @param data The binary data to write to the file * @param datalen the length of the data * @return 0 for ok, 1 for failz */ -int saveFileJSON(const char *preferredName, const char *suffix, JSONFileType ftype, uint8_t *data, size_t datalen); +int saveFileJSON(const char *preferredName, JSONFileType ftype, uint8_t *data, size_t datalen); /** STUB * @brief Utility function to load data from a binary file. This method takes a preferred name. diff --git a/client/util.c b/client/util.c index 0d8781619..c215e05dd 100644 --- a/client/util.c +++ b/client/util.c @@ -938,16 +938,3 @@ char *strmcopy(const char *buf) { } return str; } - -char *filenamemcopy(const char *preferredName, const char *suffix) { - if (preferredName == NULL) return NULL; - if (suffix == NULL) return NULL; - char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t)); - if (fileName == NULL) - return NULL; - strcpy(fileName, preferredName); - if (str_endswith(fileName, suffix)) - return fileName; - strcat(fileName, suffix); - return fileName; -} diff --git a/client/util.h b/client/util.h index 6d0cc98aa..9674bd460 100644 --- a/client/util.h +++ b/client/util.h @@ -266,5 +266,4 @@ void clean_ascii(unsigned char *buf, size_t len); void strcleanrn(char *buf, size_t len); void strcreplace(char *buf, size_t len, char from, char to); char *strmcopy(const char *buf); -char *filenamemcopy(const char *preferredName, const char *suffix); #endif