mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-02-13 10:43:01 +08:00
better searchFile api
This commit is contained in:
parent
7ab7d68a26
commit
ec174a7232
3 changed files with 43 additions and 31 deletions
|
@ -66,16 +66,11 @@ static int CmdScriptRun(const char *Cmd) {
|
||||||
int arg_len = 0;
|
int arg_len = 0;
|
||||||
sscanf(Cmd, "%127s%n %255[^\n\r]%n", preferredName, &name_len, arguments, &arg_len);
|
sscanf(Cmd, "%127s%n %255[^\n\r]%n", preferredName, &name_len, arguments, &arg_len);
|
||||||
|
|
||||||
char *script_name = filenamemcopy(preferredName, ".lua");
|
char *script_path;
|
||||||
if (script_name == NULL) return PM3_EMALLOC;
|
int res = searchFile(&script_path, LUA_SCRIPTS_SUBDIR, preferredName, ".lua");
|
||||||
char *script_path = searchFile(LUA_SCRIPTS_SUBDIR, script_name);
|
if (res != PM3_SUCCESS)
|
||||||
|
return res;
|
||||||
|
|
||||||
if (script_path == NULL) {
|
|
||||||
PrintAndLogEx(FAILED, "Error - can't find script %s", script_name);
|
|
||||||
free(script_name);
|
|
||||||
return PM3_EFILE;
|
|
||||||
}
|
|
||||||
free(script_name);
|
|
||||||
int error;
|
int error;
|
||||||
PrintAndLogEx(SUCCESS, "Executing: %s, args '%s'\n", script_path, arguments);
|
PrintAndLogEx(SUCCESS, "Executing: %s, args '%s'\n", script_path, arguments);
|
||||||
error = luaL_loadfile(lua_state, script_path);
|
error = luaL_loadfile(lua_state, script_path);
|
||||||
|
|
|
@ -69,7 +69,7 @@ int fileExists(const char *filename) {
|
||||||
return result == 0;
|
return result == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *filenamemcopy(const char *preferredName, const char *suffix) {
|
static char *filenamemcopy(const char *preferredName, const char *suffix) {
|
||||||
if (preferredName == NULL) return NULL;
|
if (preferredName == NULL) return NULL;
|
||||||
if (suffix == NULL) return NULL;
|
if (suffix == NULL) return NULL;
|
||||||
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
|
char *fileName = (char *) calloc(strlen(preferredName) + strlen(suffix) + 1, sizeof(uint8_t));
|
||||||
|
@ -522,15 +522,10 @@ out:
|
||||||
int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint16_t *keycnt) {
|
int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, uint8_t keylen, uint16_t *keycnt) {
|
||||||
|
|
||||||
if (data == NULL) return PM3_ESOFT;
|
if (data == NULL) return PM3_ESOFT;
|
||||||
char *fileName = filenamemcopy(preferredName, ".dic");
|
char *path;
|
||||||
if (fileName == NULL) return PM3_EMALLOC;
|
if (searchFile(&path, DICTIONARIES_SUBDIR, preferredName, ".dic") != PM3_SUCCESS)
|
||||||
char *path = searchFile(DICTIONARIES_SUBDIR, fileName);
|
|
||||||
if (path == NULL) {
|
|
||||||
PrintAndLogEx(WARNING, "file not found or locked. '" _YELLOW_("%s")"'", fileName);
|
|
||||||
free(fileName);
|
|
||||||
return PM3_EFILE;
|
return PM3_EFILE;
|
||||||
}
|
|
||||||
free(fileName);
|
|
||||||
// t5577 == 4bytes
|
// t5577 == 4bytes
|
||||||
// mifare == 6 bytes
|
// mifare == 6 bytes
|
||||||
// iclass == 8 bytes
|
// iclass == 8 bytes
|
||||||
|
@ -667,25 +662,31 @@ int searchAndList(const char *pm3dir, const char *ext) {
|
||||||
return PM3_SUCCESS;
|
return PM3_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *searchFile(const char *pm3dir, const char *searchname) {
|
static int searchFinalFile(char **foundpath, const char *pm3dir, const char *searchname) {
|
||||||
|
if ((foundpath == NULL)||(pm3dir == NULL)||(searchname == NULL)) return PM3_ESOFT;
|
||||||
// explicit absolute (/) or relative path (./) => try only to match it directly
|
// explicit absolute (/) or relative path (./) => try only to match it directly
|
||||||
char *filename = calloc(strlen(searchname) + 1, sizeof(char));
|
char *filename = calloc(strlen(searchname) + 1, sizeof(char));
|
||||||
if (filename == NULL) return NULL;
|
if (filename == NULL) return PM3_EMALLOC;
|
||||||
strcpy(filename, searchname);
|
strcpy(filename, searchname);
|
||||||
if (((strlen(filename) > 1) && (filename[0] == '/')) ||
|
if (((strlen(filename) > 1) && (filename[0] == '/')) ||
|
||||||
((strlen(filename) > 2) && (filename[0] == '.') && (filename[1] == '/')))
|
((strlen(filename) > 2) && (filename[0] == '.') && (filename[1] == '/')))
|
||||||
{
|
{
|
||||||
if (fileExists(filename))
|
if (fileExists(filename)) {
|
||||||
return filename;
|
*foundpath = filename;
|
||||||
else
|
return PM3_SUCCESS;
|
||||||
|
}
|
||||||
|
else {
|
||||||
goto out;
|
goto out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// else
|
// else
|
||||||
|
|
||||||
// try implicit relative path
|
// try implicit relative path
|
||||||
{
|
{
|
||||||
if (fileExists(filename))
|
if (fileExists(filename)) {
|
||||||
return filename;
|
*foundpath = filename;
|
||||||
|
return PM3_SUCCESS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// try pm3 dirs in current workdir (dev mode)
|
// try pm3 dirs in current workdir (dev mode)
|
||||||
const char *exec_path = get_my_executable_directory();
|
const char *exec_path = get_my_executable_directory();
|
||||||
|
@ -698,7 +699,8 @@ char *searchFile(const char *pm3dir, const char *searchname) {
|
||||||
strcat(path, filename);
|
strcat(path, filename);
|
||||||
if (fileExists(path)) {
|
if (fileExists(path)) {
|
||||||
free(filename);
|
free(filename);
|
||||||
return path;
|
*foundpath = path;
|
||||||
|
return PM3_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
|
@ -715,7 +717,8 @@ char *searchFile(const char *pm3dir, const char *searchname) {
|
||||||
strcat(path, filename);
|
strcat(path, filename);
|
||||||
if (fileExists(path)) {
|
if (fileExists(path)) {
|
||||||
free(filename);
|
free(filename);
|
||||||
return path;
|
*foundpath = path;
|
||||||
|
return PM3_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
|
@ -730,12 +733,27 @@ char *searchFile(const char *pm3dir, const char *searchname) {
|
||||||
strcat(path, filename);
|
strcat(path, filename);
|
||||||
if (fileExists(path)) {
|
if (fileExists(path)) {
|
||||||
free(filename);
|
free(filename);
|
||||||
return path;
|
*foundpath = path;
|
||||||
|
return PM3_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
free(path);
|
free(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
free(filename);
|
free(filename);
|
||||||
return NULL;
|
return PM3_EFILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix) {
|
||||||
|
char *filename = filenamemcopy(searchname, suffix);
|
||||||
|
if (filename == NULL) return PM3_EMALLOC;
|
||||||
|
int res = searchFinalFile(foundpath, pm3dir, filename);
|
||||||
|
if (res != PM3_SUCCESS) {
|
||||||
|
if (res == PM3_EFILE)
|
||||||
|
PrintAndLogEx(FAILED, "Error - can't find %s", filename);
|
||||||
|
free(filename);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
free(filename);
|
||||||
|
return PM3_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,8 +160,7 @@ int loadFileDICTIONARY(const char *preferredName, void *data, size_t *datalen, u
|
||||||
*/
|
*/
|
||||||
int convertOldMfuDump(uint8_t **dump, size_t *dumplen);
|
int convertOldMfuDump(uint8_t **dump, size_t *dumplen);
|
||||||
|
|
||||||
char *filenamemcopy(const char *preferredName, const char *suffix);
|
|
||||||
int searchAndList(const char *pm3dir, const char *ext);
|
int searchAndList(const char *pm3dir, const char *ext);
|
||||||
char *searchFile(const char *pm3dir, const char *searchname);
|
int searchFile(char **foundpath, const char *pm3dir, const char *searchname, const char *suffix);
|
||||||
|
|
||||||
#endif // FILEUTILS_H
|
#endif // FILEUTILS_H
|
||||||
|
|
Loading…
Reference in a new issue