FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/wc_port.c
/* File Handling Helpers */
/* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
File Handling Helpers
returns 0 if file found, WC_READDIR_NOFILE if no files or negative error
File Handling Helpers returns 0 if file found WC_READDIR_NOFILE if no files or negative error
int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
{
    int ret = WC_READDIR_NOFILE; 
    int pathLen = 0;
    int dnameLen = 0;
    if (name)
        *name = NULL;
    if (ctx == NULL || path == NULL) {
        return BAD_FUNC_ARG;
    }
    XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
    pathLen = (int)XSTRLEN(path);
#ifdef USE_WINDOWS_API
    if (pathLen > MAX_FILENAME_SZ - 3)
        return BAD_PATH_ERROR;
    XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
    XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
    ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
    if (ctx->hFind == INVALID_HANDLE_VALUE) {
        WOLFSSL_MSG("FindFirstFile for path verify locations failed");
        return BAD_PATH_ERROR;
    }
    do {
        if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
            dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
            if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
                return BAD_PATH_ERROR;
            }
            XSTRNCPY(ctx->name, path, pathLen + 1);
            ctx->name[pathLen] = '\\';
            XSTRNCPY(ctx->name + pathLen + 1,
                     ctx->FindFileData.cFileName,
                     MAX_FILENAME_SZ - pathLen - 1);
            if (name)
                *name = ctx->name;
            return 0;
        }
    } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
#elif defined(WOLFSSL_ZEPHYR)
    if (fs_opendir(&ctx->dir, path) != 0) {
        WOLFSSL_MSG("opendir path verify locations failed");
        return BAD_PATH_ERROR;
    }
    ctx->dirp = &ctx->dir;
    while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
        dnameLen = (int)XSTRLEN(ctx->entry.name);
        if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
            ret = BAD_PATH_ERROR;
            break;
        }
        XSTRNCPY(ctx->name, path, pathLen + 1);
        ctx->name[pathLen] = '/';
        XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
        if (fs_stat(ctx->name, &ctx->s) != 0) {
            WOLFSSL_MSG("stat on name failed");
            ret = BAD_PATH_ERROR;
            break;
        } else if (ctx->s.type == FS_DIR_ENTRY_FILE) {
            if (name)
                *name = ctx->name;
            return 0;
        }
    }
#elif defined(WOLFSSL_TELIT_M2MB)
    ctx->dir = m2mb_fs_opendir((const CHAR*)path);
    if (ctx->dir == NULL) {
        WOLFSSL_MSG("opendir path verify locations failed");
        return BAD_PATH_ERROR;
    }
    while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
        dnameLen = (int)XSTRLEN(ctx->entry->d_name);
        if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
            ret = BAD_PATH_ERROR;
            break;
        }
        XSTRNCPY(ctx->name, path, pathLen + 1);
        ctx->name[pathLen] = '/';
        XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
        if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
            WOLFSSL_MSG("stat on name failed");
            ret = BAD_PATH_ERROR;
            break;
        }
        else if (ctx->s.st_mode & M2MB_S_IFREG) {
            if (name)
                *name = ctx->name;
            return 0;
        }
    }
#else
    ctx->dir = opendir(path);
    if (ctx->dir == NULL) {
        WOLFSSL_MSG("opendir path verify locations failed");
        return BAD_PATH_ERROR;
    }
    while ((ctx->entry = readdir(ctx->dir)) != NULL) {
        dnameLen = (int)XSTRLEN(ctx->entry->d_name);
        if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
            ret = BAD_PATH_ERROR;
            break;
        }
        XSTRNCPY(ctx->name, path, pathLen + 1);
        ctx->name[pathLen] = '/';
        XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
        if (stat(ctx->name, &ctx->s) != 0) {
            WOLFSSL_MSG("stat on name failed");
            ret = BAD_PATH_ERROR;
            break;
        } else if (S_ISREG(ctx->s.st_mode)) {
            if (name)
                *name = ctx->name;
            return 0;
        }
    }
#endif
    wc_ReadDirClose(ctx);
    return ret;
}
int wc_ReadDirFirst ( ReadDirCtx * ctx , const char * path , char * * name ) { int ret = WC_READDIR_NOFILE ; int pathLen = 0 ; int dnameLen = 0 ; if ( name ) * name = NULL ; if ( ctx == NULL || path == NULL ) { return BAD_FUNC_ARG ; } XMEMSET ( ctx -> name , 0 , MAX_FILENAME_SZ ) ; pathLen = ( int ) XSTRLEN ( path ) ; #ifdef USE_WINDOWS_API if ( pathLen > MAX_FILENAME_SZ - 3 ) return BAD_PATH_ERROR ; XSTRNCPY ( ctx -> name , path , MAX_FILENAME_SZ - 3 ) ; XSTRNCPY ( ctx -> name + pathLen , " \\ " , MAX_FILENAME_SZ - pathLen ) ; ctx -> hFind = FindFirstFileA ( ctx -> name , & ctx -> FindFileData ) ; if ( ctx -> hFind == INVALID_HANDLE_VALUE ) { WOLFSSL_MSG ( " " ) ; return BAD_PATH_ERROR ; } do { if ( ! ( ctx -> FindFileData . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) { dnameLen = ( int ) XSTRLEN ( ctx -> FindFileData . cFileName ) ; if ( pathLen + dnameLen + 2 > MAX_FILENAME_SZ ) { return BAD_PATH_ERROR ; } XSTRNCPY ( ctx -> name , path , pathLen + 1 ) ; ctx -> name [ pathLen ] = ' \\ ' ; XSTRNCPY ( ctx -> name + pathLen + 1 , ctx -> FindFileData . cFileName , MAX_FILENAME_SZ - pathLen - 1 ) ; if ( name ) * name = ctx -> name ; return 0 ; } } while ( FindNextFileA ( ctx -> hFind , & ctx -> FindFileData ) ) ; #elif defined ( WOLFSSL_ZEPHYR ) if ( fs_opendir ( & ctx -> dir , path ) != 0 ) { WOLFSSL_MSG ( " " ) ; return BAD_PATH_ERROR ; } ctx -> dirp = & ctx -> dir ; while ( ( fs_readdir ( & ctx -> dir , & ctx -> entry ) ) != 0 ) { dnameLen = ( int ) XSTRLEN ( ctx -> entry . name ) ; if ( pathLen + dnameLen + 2 >= MAX_FILENAME_SZ ) { ret = BAD_PATH_ERROR ; break ; } XSTRNCPY ( ctx -> name , path , pathLen + 1 ) ; ctx -> name [ pathLen ] = ' ' ; XSTRNCPY ( ctx -> name + pathLen + 1 , ctx -> entry . name , dnameLen + 1 ) ; if ( fs_stat ( ctx -> name , & ctx -> s ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = BAD_PATH_ERROR ; break ; } else if ( ctx -> s . type == FS_DIR_ENTRY_FILE ) { if ( name ) * name = ctx -> name ; return 0 ; } } #elif defined ( WOLFSSL_TELIT_M2MB ) ctx -> dir = m2mb_fs_opendir ( ( const CHAR * ) path ) ; if ( ctx -> dir == NULL ) { WOLFSSL_MSG ( " " ) ; return BAD_PATH_ERROR ; } while ( ( ctx -> entry = m2mb_fs_readdir ( ctx -> dir ) ) != NULL ) { dnameLen = ( int ) XSTRLEN ( ctx -> entry -> d_name ) ; if ( pathLen + dnameLen + 2 >= MAX_FILENAME_SZ ) { ret = BAD_PATH_ERROR ; break ; } XSTRNCPY ( ctx -> name , path , pathLen + 1 ) ; ctx -> name [ pathLen ] = ' ' ; XSTRNCPY ( ctx -> name + pathLen + 1 , ctx -> entry -> d_name , dnameLen + 1 ) ; if ( m2mb_fs_stat ( ctx -> name , & ctx -> s ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = BAD_PATH_ERROR ; break ; } else if ( ctx -> s . st_mode & M2MB_S_IFREG ) { if ( name ) * name = ctx -> name ; return 0 ; } } #else ctx -> dir = opendir ( path ) ; if ( ctx -> dir == NULL ) { WOLFSSL_MSG ( " " ) ; return BAD_PATH_ERROR ; } while ( ( ctx -> entry = readdir ( ctx -> dir ) ) != NULL ) { dnameLen = ( int ) XSTRLEN ( ctx -> entry -> d_name ) ; if ( pathLen + dnameLen + 2 >= MAX_FILENAME_SZ ) { ret = BAD_PATH_ERROR ; break ; } XSTRNCPY ( ctx -> name , path , pathLen + 1 ) ; ctx -> name [ pathLen ] = ' ' ; XSTRNCPY ( ctx -> name + pathLen + 1 , ctx -> entry -> d_name , dnameLen + 1 ) ; if ( stat ( ctx -> name , & ctx -> s ) != 0 ) { WOLFSSL_MSG ( " " ) ; ret = BAD_PATH_ERROR ; break ; } else if ( S_ISREG ( ctx -> s . st_mode ) ) { if ( name ) * name = ctx -> name ; return 0 ; } } #endif wc_ReadDirClose ( ctx ) ; return ret ; }
File Handling Helpers
returns 0 if file found, WC_READDIR_NOFILE if no files or negative error
File Handling Helpers returns 0 if file found WC_READDIR_NOFILE if no files or negative error
/* default to no files found */ /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
         * of earlier check it is known that dnameLen is less than
         * MAX_FILENAME_SZ - (pathLen + 2)  so dnameLen +1 will fit */ /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
         * of earlier check it is known that dnameLen is less than
         * MAX_FILENAME_SZ - (pathLen + 2)  so dnameLen +1 will fit */ /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
         * of earlier check it is known that dnameLen is less than
         * MAX_FILENAME_SZ - (pathLen + 2)  so dnameLen +1 will fit */