#ifdef LOCAL_TEST #include "../config.h" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "libc.h" // libc code lifted from the android open source project https://android.googlesource.com/platform/bionic/+/ics-mr0/libc /* * Copyright (C) 2008 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ wchar_t * wcschr(const wchar_t *s, wchar_t c) { while (*s != c && *s != L'\0') s++; if (*s == c) return ((wchar_t *)s); return (NULL); } size_t wcslen(const wchar_t *s) { const wchar_t *p; p = s; while (*p) p++; return p - s; } int strncmp(const char *s1, const char *s2, size_t n) { if (n == 0) return (0); do { if (*s1 != *s2++) return (*(unsigned char *)s1 - *(unsigned char *)--s2); if (*s1++ == 0) break; } while (--n != 0); return (0); } char * strstr(const char *s, const char *find) { char c, sc; size_t len; if ((c = *find++) != 0) { len = strlen(find); do { do { if ((sc = *s++) == 0) return (NULL); } while (sc != c); } while (strncmp(s, find, len) != 0); s--; } return ((char *)s); } char * strchr (register const char *s, int c) { do { if (*s == c) { return (char*)s; } } while (*s++); return (0); } char * strcpy(char *to, const char *from) { char *save = to; for (; (*to = *from) != '\0'; ++from, ++to); return(save); } int strcmp(const char *s1, const char *s2) { while (*s1 == *s2++) if (*s1++ == 0) return (0); return (*(unsigned char *)s1 - *(unsigned char *)--s2); } char * strcat(char *s, const char *append) { char *save = s; for (; *s; ++s); while ((*s++ = *append++) != '\0'); return(save); } size_t strlen(const char *str) { const char *s; for (s = str; *s; ++s) ; return (s - str); } char * strdup(const char *str) { size_t siz; char *copy; siz = strlen(str) + 1; if ((copy = malloc(siz)) == NULL) return(NULL); (void)memcpy(copy, str, siz); return(copy); } char * strncpy(char *dst, const char *src, size_t n) { if (n != 0) { char *d = dst; const char *s = src; do { if ((*d++ = *s++) == 0) { /* NUL pad the remaining n-1 bytes */ while (--n != 0) *d++ = 0; break; } } while (--n != 0); } return (dst); } long strtol(const char *nptr, char **endptr, int base) { const char *s; long acc, cutoff; int c; int neg, any, cutlim; /* * Skip white space and pick up leading +/- sign if any. * If base is 0, allow 0x for hex and 0 for octal, else * assume decimal; if base is already 16, allow 0x. */ s = nptr; do { c = (unsigned char) *s++; } while (isspace(c)); if (c == '-') { neg = 1; c = *s++; } else { neg = 0; if (c == '+') c = *s++; } if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == '0' ? 8 : 10; /* * Compute the cutoff value between legal numbers and illegal * numbers. That is the largest legal value, divided by the * base. An input number that is greater than this value, if * followed by a legal input character, is too big. One that * is equal to this value may be valid or not; the limit * between valid and invalid numbers is then based on the last * digit. For instance, if the range for longs is * [-2147483648..2147483647] and the input base is 10, * cutoff will be set to 214748364 and cutlim to either * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated * a value > 214748364, or equal but the next digit is > 7 (or 8), * the number is too big, and we will return a range error. * * Set any if any `digits' consumed; make it negative to indicate * overflow. */ cutoff = neg ? LONG_MIN : LONG_MAX; cutlim = cutoff % base; cutoff /= base; if (neg) { if (cutlim > 0) { cutlim -= base; cutoff += 1; } cutlim = -cutlim; } for (acc = 0, any = 0;; c = (unsigned char) *s++) { if (isdigit(c)) c -= '0'; else if (isalpha(c)) c -= isupper(c) ? 'A' - 10 : 'a' - 10; else break; if (c >= base) break; if (any < 0) continue; if (neg) { if (acc < cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = LONG_MIN; errno = ERANGE; } else { any = 1; acc *= base; acc -= c; } } else { if (acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = LONG_MAX; errno = ERANGE; } else { any = 1; acc *= base; acc += c; } } } if (endptr != 0) *endptr = (char *) (any ? s - 1 : nptr); return (acc); } unsigned long strtoul(const char *nptr, char **endptr, int base) { const char *s; unsigned long acc, cutoff; int c; int neg, any, cutlim; /* * See strtol for comments as to the logic used. */ s = nptr; do { c = (unsigned char) *s++; } while (isspace(c)); if (c == '-') { neg = 1; c = *s++; } else { neg = 0; if (c == '+') c = *s++; } if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == '0' ? 8 : 10; cutoff = ULONG_MAX / (unsigned long)base; cutlim = ULONG_MAX % (unsigned long)base; for (acc = 0, any = 0;; c = (unsigned char) *s++) { if (isdigit(c)) c -= '0'; else if (isalpha(c)) c -= isupper(c) ? 'A' - 10 : 'a' - 10; else break; if (c >= base) break; if (any < 0) continue; if (acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = ULONG_MAX; errno = ERANGE; } else { any = 1; acc *= (unsigned long)base; acc += c; } } if (neg && any > 0) acc = -acc; if (endptr != 0) *endptr = (char *) (any ? s - 1 : nptr); return (acc); } uintmax_t strtoumax(const char *nptr, char **endptr, int base) { const char *s; uintmax_t acc, cutoff; int c; int neg, any, cutlim; /* * See strtoq for comments as to the logic used. */ s = nptr; do { c = (unsigned char) *s++; } while (isspace(c)); if (c == '-') { neg = 1; c = *s++; } else { neg = 0; if (c == '+') c = *s++; } if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == '0' ? 8 : 10; /* BIONIC: avoid division and modulo for common cases */ #define CASE_BASE(x) \ case x: cutoff = UINTMAX_MAX / x; \ cutlim = UINTMAX_MAX % x; \ break switch (base) { CASE_BASE(8); CASE_BASE(10); CASE_BASE(16); default: cutoff = UINTMAX_MAX / base; cutlim = UINTMAX_MAX % base; } for (acc = 0, any = 0;; c = (unsigned char) *s++) { if (isdigit(c)) c -= '0'; else if (isalpha(c)) c -= isupper(c) ? 'A' - 10 : 'a' - 10; else break; if (c >= base) break; if (any < 0) continue; if (acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = UINTMAX_MAX; errno = ERANGE; } else { any = 1; acc *= (uintmax_t)base; acc += c; } } if (neg && any > 0) acc = -acc; if (endptr != 0) *endptr = (char *) (any ? s - 1 : nptr); return (acc); } static inline int digitval(int ch) { unsigned d; d = (unsigned)(ch - '0'); if (d < 10) return (int)d; d = (unsigned)(ch - 'a'); if (d < 6) return (int)(d+10); d = (unsigned)(ch - 'A'); if (d < 6) return (int)(d+10); return -1; } uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) { const unsigned char* p = (const unsigned char *)nptr; const unsigned char* end = p + n; int minus = 0; uintmax_t v = 0; int d; /* skip leading space */ while (p < end && isspace(*p)) p++; /* Single optional + or - */ if (p < end) { char c = p[0]; if ( c == '-' || c == '+' ) { minus = (c == '-'); p++; } } if ( base == 0 ) { if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { p += 2; base = 16; } else if ( p+1 < end && p[0] == '0' ) { p += 1; base = 8; } else { base = 10; } } else if ( base == 16 ) { if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { p += 2; } } while ( p < end && (d = digitval(*p)) >= 0 && d < base ) { v = v*base + d; p += 1; } if ( endptr ) *endptr = (char *)p; return minus ? -v : v; } intmax_t strntoimax(const char *nptr, char **endptr, int base, size_t n) { return (intmax_t) strntoumax(nptr, endptr, base, n); } intmax_t strtoimax(const char *nptr, char **endptr, int base) { const char *s; intmax_t acc, cutoff; int c; int neg, any, cutlim; /* * Skip white space and pick up leading +/- sign if any. * If base is 0, allow 0x for hex and 0 for octal, else * assume decimal; if base is already 16, allow 0x. */ s = nptr; do { c = (unsigned char) *s++; } while (isspace(c)); if (c == '-') { neg = 1; c = *s++; } else { neg = 0; if (c == '+') c = *s++; } if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == '0' ? 8 : 10; /* * Compute the cutoff value between legal numbers and illegal * numbers. That is the largest legal value, divided by the * base. An input number that is greater than this value, if * followed by a legal input character, is too big. One that * is equal to this value may be valid or not; the limit * between valid and invalid numbers is then based on the last * digit. For instance, if the range for intmax_t is * [-9223372036854775808..9223372036854775807] and the input base * is 10, cutoff will be set to 922337203685477580 and cutlim to * either 7 (neg==0) or 8 (neg==1), meaning that if we have * accumulated a value > 922337203685477580, or equal but the * next digit is > 7 (or 8), the number is too big, and we will * return a range error. * * Set any if any `digits' consumed; make it negative to indicate * overflow. */ /* BIONIC: avoid division and module for common cases */ #define CASE_BASE2(x) \ case x: \ if (neg) { \ cutlim = INTMAX_MIN % x; \ cutoff = INTMAX_MIN / x; \ } else { \ cutlim = INTMAX_MAX % x; \ cutoff = INTMAX_MAX / x; \ }; \ break switch (base) { case 4: if (neg) { cutlim = (int)(INTMAX_MIN % 4); cutoff = INTMAX_MIN / 4; } else { cutlim = (int)(INTMAX_MAX % 4); cutoff = INTMAX_MAX / 4; } break; CASE_BASE2(8); CASE_BASE2(10); CASE_BASE2(16); default: cutoff = neg ? INTMAX_MIN : INTMAX_MAX; cutlim = cutoff % base; cutoff /= base; } #undef CASE_BASE if (neg) { if (cutlim > 0) { cutlim -= base; cutoff += 1; } cutlim = -cutlim; } for (acc = 0, any = 0;; c = (unsigned char) *s++) { if (isdigit(c)) c -= '0'; else if (isalpha(c)) c -= isupper(c) ? 'A' - 10 : 'a' - 10; else break; if (c >= base) break; if (any < 0) continue; if (neg) { if (acc < cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = INTMAX_MIN; errno = ERANGE; } else { any = 1; acc *= base; acc -= c; } } else { if (acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; acc = INTMAX_MAX; errno = ERANGE; } else { any = 1; acc *= base; acc += c; } } } if (endptr != 0) *endptr = (char *) (any ? s - 1 : nptr); return (acc); } int atoi(const char* s) { return (int)strtoimax(s, NULL, 10); } char * strtok(char *s, const char *delim) { static char *last; return strtok_r(s, delim, &last); } char * strtok_r(char *s, const char *delim, char **last) { char *spanp; int c, sc; char *tok; if (s == NULL && (s = *last) == NULL) return (NULL); /* * Skip (span) leading delimiters (s += strspn(s, delim), sort of). */ cont: c = *s++; for (spanp = (char *)delim; (sc = *spanp++) != 0;) { if (c == sc) goto cont; } if (c == 0) { /* no non-delimiter characters */ *last = NULL; return (NULL); } tok = s - 1; /* * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). * Note that delim must have one NUL; we stop if we see that, too. */ for (;;) { c = *s++; spanp = (char *)delim; do { if ((sc = *spanp++) == c) { if (c == 0) s = NULL; else s[-1] = 0; *last = s; return (tok); } } while (sc != 0); } /* NOTREACHED */ }