From e08f28da20a2da83b460beacc5d826a16b8dc3e3 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Wed, 26 Jan 2022 13:36:25 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15408 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/common/utils/include/paths.h | 12 ++++++ os/common/utils/src/paths.c | 26 +++++++++++- os/vfs/drivers/overlay/drvoverlay.c | 12 ++++-- os/vfs/drivers/streams/drvstreams.c | 2 +- os/vfs/include/vfsparser.h | 39 ----------------- os/vfs/src/vfsparser.c | 66 +---------------------------- 6 files changed, 47 insertions(+), 110 deletions(-) diff --git a/os/common/utils/include/paths.h b/os/common/utils/include/paths.h index b8415d471..857e40f11 100644 --- a/os/common/utils/include/paths.h +++ b/os/common/utils/include/paths.h @@ -59,6 +59,7 @@ extern "C" { size_t path_prepend(char *dst, const char *src, size_t size); size_t path_add_separator(char *dst, size_t size); size_t path_add_extension(char *dst, const char *ext, size_t size); + size_t path_copy_element(const char **pathp, char *dst, size_t size); size_t path_get_element(const char **pathp, char *dst, size_t size); size_t path_normalize(char *dst, const char *src, size_t size); #ifdef __cplusplus @@ -69,6 +70,17 @@ extern "C" { /* Module inline functions. */ /*===========================================================================*/ +/** + * @brief Checks for a valid path separator. + * + * @param[in] c The character to be checked. + * @return The operation result. + */ +static inline bool path_is_separator(char c) { + + return (bool)(c == '/'); +} + #endif /* vfspaths.h */ /** @} */ diff --git a/os/common/utils/src/paths.c b/os/common/utils/src/paths.c index 52d8210d0..63dd9cd4f 100644 --- a/os/common/utils/src/paths.c +++ b/os/common/utils/src/paths.c @@ -218,7 +218,7 @@ size_t path_add_extension(char *dst, const char *ext, size_t size) { * @retval 0 Null element. * @retval size Buffer overflow. */ -size_t path_get_element(const char **pathp, char *dst, size_t size) { +size_t path_copy_element(const char **pathp, char *dst, size_t size) { size_t n; const char *p; @@ -248,6 +248,30 @@ size_t path_get_element(const char **pathp, char *dst, size_t size) { } } +/** + * @brief Fetches the next path element. + * @note Does not consume the next separator, if any. + * @note It can return an empty element, it has to be detected outside. + * + * @param[in, out] pathp Pointer to the path under parsing. + * @param[out] dst Buffer for the extracted path element + * @param[in] size Destination buffer size. + * @return The size of the fetched path element, it does + * not fetch beyond @p size. + * @retval 0 Null element. + * @retval size Buffer overflow. + */ +size_t path_get_element(const char **pathp, char *dst, size_t size) { + size_t n; + + n = path_copy_element(pathp, dst, size); + if (n < size) { + dst[n] = '\0'; + } + + return n; +} + /** * @brief Normalizes an absolute path. * @note The destination buffer can be the same of the source buffer. diff --git a/os/vfs/drivers/overlay/drvoverlay.c b/os/vfs/drivers/overlay/drvoverlay.c index 600564b23..19137de72 100644 --- a/os/vfs/drivers/overlay/drvoverlay.c +++ b/os/vfs/drivers/overlay/drvoverlay.c @@ -101,18 +101,22 @@ static msg_t match_driver(vfs_overlay_driver_c *odp, const char **pathp, vfs_driver_c **vdpp) { char fname[VFS_CFG_NAMELEN_MAX + 1]; + size_t n; msg_t err; do { unsigned i; - err = vfs_parse_get_fname(pathp, fname, VFS_CFG_PATHLEN_MAX); - CH_BREAK_ON_ERROR(err); + n = path_get_element(pathp, fname, VFS_CFG_NAMELEN_MAX + 1); + if (n >= VFS_CFG_NAMELEN_MAX + 1) { + err = CH_RET_ENAMETOOLONG; + break; + } /* Searching among registered drivers.*/ i = 0U; while (i < odp->next_driver) { - if (strncmp(fname, odp->names[i], VFS_CFG_NAMELEN_MAX) == 0) { + if (memcmp(fname, odp->names[i], n) == 0) { *vdpp = odp->drivers[i]; return CH_RET_SUCCESS; } @@ -149,7 +153,7 @@ static msg_t build_absolute_path(vfs_overlay_driver_c *drvp, *buf = '\0'; /* Relative paths handling.*/ - if (!vfs_parse_is_separator(*path)) { + if (!path_is_separator(*path)) { if (path_append(buf, get_current_directory(drvp), VFS_CFG_PATHLEN_MAX + 1) == (size_t)0) { diff --git a/os/vfs/drivers/streams/drvstreams.c b/os/vfs/drivers/streams/drvstreams.c index 5e567f1ea..6e3a52108 100644 --- a/os/vfs/drivers/streams/drvstreams.c +++ b/os/vfs/drivers/streams/drvstreams.c @@ -204,7 +204,7 @@ static msg_t drv_open_file(void *instance, err = vfs_parse_match_separator(&path); CH_BREAK_ON_ERROR(err); - err = vfs_parse_get_fname(&path, fname, VFS_CFG_PATHLEN_MAX); + err = path_get_element(&path, fname, VFS_CFG_NAMELEN_MAX + 1); CH_BREAK_ON_ERROR(err); err = vfs_parse_match_end(&path); diff --git a/os/vfs/include/vfsparser.h b/os/vfs/include/vfsparser.h index 300bf0be3..a3b94e4cb 100644 --- a/os/vfs/include/vfsparser.h +++ b/os/vfs/include/vfsparser.h @@ -57,8 +57,6 @@ extern "C" { #endif msg_t vfs_parse_match_separator(const char **pathp); msg_t vfs_parse_match_end(const char **pathp); - msg_t vfs_parse_copy_fname(const char **pathp, char *fname, size_t n); - msg_t vfs_parse_get_fname(const char **pathp, char *fname, size_t n); #ifdef __cplusplus } #endif @@ -67,43 +65,6 @@ extern "C" { /* Module inline functions. */ /*===========================================================================*/ -/** - * @brief Checks for a valid path separator. - * - * @param[in] c The character to be checked. - * @return The operation result. - */ -CC_FORCE_INLINE -static inline bool vfs_parse_is_separator(char c) { - - return (bool)(c == '/'); -} - -/** - * @brief Checks for a valid path terminator. - * - * @param[in] c The character to be checked. - * @return The operation result. - */ -CC_FORCE_INLINE -static inline bool vfs_parse_is_terminator(char c) { - - return (bool)(c == '\0'); -} - -/** - * @brief Checks for a path element character. - * - * @param[in] c The character to be checked. - * @return The operation result. - */ -CC_FORCE_INLINE -static inline bool vfs_parse_is_filechar(char c) { - - /* Restrictive Posix set.*/ - return (bool)(isalnum(c) || (c == '_') || (c == '-') || (c == '.')); -} - #endif /* VFSPARSER_H */ /** @} */ diff --git a/os/vfs/src/vfsparser.c b/os/vfs/src/vfsparser.c index 898b3c1f5..fe35cd336 100644 --- a/os/vfs/src/vfsparser.c +++ b/os/vfs/src/vfsparser.c @@ -60,7 +60,7 @@ msg_t vfs_parse_match_separator(const char **pathp) { msg_t err; const char *p = *pathp; - if (!vfs_parse_is_separator(*p++)) { + if (!path_is_separator(*p++)) { err = CH_RET_ENOENT; } else { @@ -89,68 +89,4 @@ msg_t vfs_parse_match_end(const char **pathp) { return err; } -/** - * @brief Fetches the next path element. - * @note Does not consume the next separator, if any. - * @note Does not add a final zero to the fetched fname. - * @note It can return an empty element, it has to be detected outside. - * - * @param[in, out] pathp Pointer to the path under parsing. - * @param[out] fname Extracted file name. - * @param[in] n Maximum size in @fname. - */ -msg_t vfs_parse_copy_fname(const char **pathp, char *fname, size_t n) { - size_t size; - const char *p; - - p = *pathp; - size = 0U; - while (true) { - char c = *p; - - /* Path elements must be terminated by a separator or an end-of-string.*/ - if (vfs_parse_is_separator(c) || vfs_parse_is_terminator(c)) { - - /* Advancing the path pointer past the file name in the path and - closing the file name string.*/ - *pathp = p; - return (msg_t)size; - } - - /* Valid characters for path names.*/ - if (!vfs_parse_is_filechar(c)) { - return CH_RET_EINVAL; - } - - /* Exceeding the maximum length.*/ - if (size > n) { - return CH_RET_ENAMETOOLONG; - } - - *fname++ = c; - p++; - size++; - } -} - -/** - * @brief Fetches the next path element. - * @note Does not consume the next separator, if any. - * @note It can return an empty element, it has to be detected outside. - * - * @param[in, out] pathp Pointer to the path under parsing. - * @param[out] fname Extracted file name. - * @param[in] n Maximum size in @fname. - */ -msg_t vfs_parse_get_fname(const char **pathp, char *fname, size_t n) { - msg_t ret; - - ret = vfs_parse_copy_fname(pathp, fname, n); - CH_RETURN_ON_ERROR(ret); - - fname[ret] = '\0'; - - return ret; -} - /** @} */