safeFile*: accept when suffix is already provided
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user