Enhancement to add strtok implementation for platforms where it is not available such as MinGW.

This commit is contained in:
David Garske
2017-07-17 07:47:01 -07:00
parent 53e05786da
commit ff6a136b8a
3 changed files with 59 additions and 7 deletions

View File

@ -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 */

View File

@ -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

View File

@ -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)