From ff6a136b8afafc342799a8a82d9f1afdcbe4c17c Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 17 Jul 2017 07:47:01 -0700 Subject: [PATCH 1/3] Enhancement to add strtok implementation for platforms where it is not available such as MinGW. --- wolfcrypt/src/wc_port.c | 53 +++++++++++++++++++++++++++++++++++++ wolfssl/wolfcrypt/types.h | 10 +++---- wolfssl/wolfcrypt/wc_port.h | 3 +++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 335675cf1..6d28158f8 100755 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -348,6 +348,59 @@ wolfSSL_Mutex* wc_InitAndAllocMutex(void) return m; } +#ifdef USE_WOLF_STRTOK +/* String token (delim) search. If str is null use nextp. */ +char* wc_strtok(char *str, const char *delim, char **nextp) +{ + char* ret; + int i, j; + + /* Use next if str is NULL */ + if (str == NULL && nextp) + str = *nextp; + + /* verify str input */ + if (str == NULL || *str == '\0') + return NULL; + + /* match on entire delim */ + for (i = 0; str[i]; i++) { + for (j = 0; delim[j]; j++) { + if (delim[j] == str[i]) + break; + } + if (!delim[j]) + break; + } + str += i; + /* if end of string, not found so return NULL */ + if (*str == '\0') + return NULL; + + ret = str; + + /* match on first delim */ + for (i = 0; str[i]; i++) { + for (j = 0; delim[j]; j++) { + if (delim[j] == str[i]) + break; + } + if (delim[j] == str[i]) + break; + } + str += i; + + /* null terminate found string */ + if (*str) + *str++ = '\0'; + + /* return pointer to next */ + if (nextp) + *nextp = str; + + return ret; +} +#endif /* USE_WOLF_STRTOK */ #if WOLFSSL_CRYPT_HW_MUTEX /* Mutex for protection of cryptography hardware */ diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 917e0f2b1..942787191 100755 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -326,15 +326,11 @@ /* use only Thread Safe version of strtok */ #if !defined(USE_WINDOWS_API) && !defined(INTIME_RTOS) #define XSTRTOK strtok_r + #elif defined(__MINGW32__) + #define USE_WOLF_STRTOK + #define XSTRTOK wc_strtok #else #define XSTRTOK strtok_s - - #ifdef __MINGW32__ - #pragma GCC diagnostic push - #pragma GCC diagnostic warning "-Wcpp" - #warning "MinGW may be missing strtok_s. You can find a public domain implementation here: https://github.com/fletcher/MultiMarkdown-4/blob/master/strtok.c" - #pragma GCC diagnostic pop - #endif #endif #endif #endif diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 2247d5360..5a4d56a23 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -308,6 +308,9 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #endif /* !NO_FILESYSTEM */ +#ifdef USE_WOLF_STRTOK + WOLFSSL_LOCAL char* wc_strtok(char *str, const char *delim, char **nextp); +#endif /* Windows API defines its own min() macro. */ #if defined(USE_WINDOWS_API) From 9fb0862bc14e32370912975d2fbd2d00dd47e4bb Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 25 Jul 2017 08:54:24 -0700 Subject: [PATCH 2/3] Fix for TI RTOS (WOLFSSL_TIRTOS) to use our `wc_strtok` for the XSTRTOK macro. --- wolfssl/wolfcrypt/types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 942787191..142722f65 100755 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -326,7 +326,7 @@ /* use only Thread Safe version of strtok */ #if !defined(USE_WINDOWS_API) && !defined(INTIME_RTOS) #define XSTRTOK strtok_r - #elif defined(__MINGW32__) + #elif defined(__MINGW32__) || defined(WOLFSSL_TIRTOS) #define USE_WOLF_STRTOK #define XSTRTOK wc_strtok #else From 3ca679c1d73da33a907f7d149d6d04a6fa7b5f2b Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 25 Jul 2017 08:57:17 -0700 Subject: [PATCH 3/3] Added ability to force use of `USE_WOLF_STRTOK. --- wolfssl/wolfcrypt/types.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 142722f65..d9bf9a7b7 100755 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -326,8 +326,11 @@ /* use only Thread Safe version of strtok */ #if !defined(USE_WINDOWS_API) && !defined(INTIME_RTOS) #define XSTRTOK strtok_r - #elif defined(__MINGW32__) || defined(WOLFSSL_TIRTOS) - #define USE_WOLF_STRTOK + #elif defined(__MINGW32__) || defined(WOLFSSL_TIRTOS) || \ + defined(USE_WOLF_STRTOK) + #ifndef USE_WOLF_STRTOK + #define USE_WOLF_STRTOK + #endif #define XSTRTOK wc_strtok #else #define XSTRTOK strtok_s