diff options
author | Nikias Bassen | 2013-11-12 18:17:00 +0100 |
---|---|---|
committer | Nikias Bassen | 2013-11-12 18:17:00 +0100 |
commit | 86ae825fb67734391fc7cc72d5c9b004570ab0a9 (patch) | |
tree | f95f0ab59def65e733c78359a782c83d9286175e | |
parent | 4a7a0877fb91c5aefa82ac8ae63fdbb8f01ae7a4 (diff) | |
download | libplist-86ae825fb67734391fc7cc72d5c9b004570ab0a9.tar.gz libplist-86ae825fb67734391fc7cc72d5c9b004570ab0a9.tar.bz2 |
base64: get rid of strtok_r and use strspn+strcspn instead
strtok_r is not available on win32 and the designated strtok_s
function is reported to not work on windows xp. Hence we use an
easier an non-destructive implementation with strspn and strcspn
to strip out the whitespace.
-rw-r--r-- | src/base64.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/src/base64.c b/src/base64.c index acec723..83205c2 100644 --- a/src/base64.c +++ b/src/base64.c @@ -21,10 +21,6 @@ #include <string.h> #include "base64.h" -#ifdef WIN32 -#define strtok_r strtok_s -#endif - static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char base64_pad = '='; @@ -112,18 +108,23 @@ unsigned char *base64decode(const char *buf, size_t *size) size_t len = strlen(buf); if (len <= 0) return NULL; unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); - - unsigned char *line; + const char *ptr = buf; int p = 0; - char* saveptr = NULL; - line = (unsigned char*)strtok_r((char*)buf, "\r\n\t ", &saveptr); - while (line) { - p+=base64decode_block(outbuf+p, (const char*)line, strlen((char*)line)); + do { + ptr += strspn(ptr, "\r\n\t "); + if (*ptr == '\0') { + break; + } + len = strcspn(ptr, "\r\n\t "); + if (len > 0) { + p+=base64decode_block(outbuf+p, ptr, len); + ptr += len; + } else { + break; + } + } while (1); - // get next line of base64 encoded block - line = (unsigned char*)strtok_r(NULL, "\r\n\t ", &saveptr); - } outbuf[p] = 0; *size = p; return outbuf; |