diff --git a/HISTORY b/HISTORY index 2564596..0df82e3 100644 --- a/HISTORY +++ b/HISTORY @@ -1,10 +1,11 @@ -Version 1.49 2021-03-21 +Version 1.49 2021-03-28 * add macros: FC_ABS and FC_NEGATIVE * uniq_skiplist.c: add uniq_skiplist_pair struct and init function * add functions: fc_mkdirs and str_replace * add FilenameString type and macro * add functions: fc_string_case_compare, fc_string_case_equal etc. + * add functions: fc_check_filename_ex Version 1.48 2021-02-01 * fast_buffer.[hc]: add function fast_buffer_append_binary diff --git a/src/shared_func.c b/src/shared_func.c index 43c39be..9bffc1e 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2178,7 +2178,8 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) if (*bytes < 0) { logError("file: "__FILE__", line: %d, " \ - "bytes: %"PRId64" < 0", __LINE__, *bytes); + "bytes: %"PRId64" < 0, input string: %s", + __LINE__, *bytes, pStr); return EINVAL; } @@ -2186,6 +2187,10 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) { *bytes *= default_unit_bytes; } + else if (*pReservedEnd == 'T' || *pReservedEnd == 't') + { + *bytes *= 1024 * 1024 * 1024 * 1024LL; + } else if (*pReservedEnd == 'G' || *pReservedEnd == 'g') { *bytes *= 1024 * 1024 * 1024; @@ -2198,6 +2203,13 @@ int parse_bytes(const char *pStr, const int default_unit_bytes, int64_t *bytes) { *bytes *= 1024; } + else + { + logError("file: "__FILE__", line: %d, " + "unkown byte unit: %c (0x%02x), input string: %s", + __LINE__, *pReservedEnd, *pReservedEnd, pStr); + return EINVAL; + } return 0; } @@ -3382,3 +3394,52 @@ bool fc_path_contains(const string_t *path, const string_t *needle, return true; } + +int fc_check_filename_ex(const string_t *filename, const char *caption, + char *error_info, int *error_len, const int error_size) +{ + if (filename->len <= 0) { + *error_len = snprintf(error_info, error_size, + "invalid %s, length: %d <= 0", + caption, filename->len); + return EINVAL; + } + + if (fc_string_equal2(filename, ".", 1) || + fc_string_equal2(filename, "..", 2)) + { + *error_len = snprintf(error_info, error_size, + "invalid %s: %.*s", caption, + filename->len, filename->str); + return EINVAL; + } + + if (memchr(filename->str, '/', filename->len) != NULL) { + *error_len = snprintf(error_info, error_size, + "%s is invalid because contains /", caption); + return EINVAL; + } + + if (memchr(filename->str, '\0', filename->len) != NULL) { + *error_len = snprintf(error_info, error_size, + "%s is invalid because contains 0x0", caption); + return EINVAL; + } + + return 0; +} + +int fc_check_filename(const string_t *filename, const char *caption) +{ + char error_info[256]; + int error_len; + int result; + + if ((result=fc_check_filename_ex(filename, caption, error_info, + &error_len, sizeof(error_info))) != 0) + { + logError("file: "__FILE__", line: %d, " + "%s", __LINE__, error_info); + } + return result; +} diff --git a/src/shared_func.h b/src/shared_func.h index 537a7b9..3a38810 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -1056,6 +1056,11 @@ static inline int fc_sleep_ms(const int milliseconds) } } +int fc_check_filename_ex(const string_t *filename, const char *caption, + char *error_info, int *error_len, const int error_size); + +int fc_check_filename(const string_t *filename, const char *caption); + #ifdef __cplusplus } #endif