/* $NetBSD: _strtol.h,v 1.11 2017/07/06 21:08:44 joerg Exp $ */12/*-3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* Original version ID:31* NetBSD: src/lib/libc/locale/_wcstol.h,v 1.2 2003/08/07 16:43:03 agc Exp32*/3334/*35* function template for strtol, strtoll and strtoimax.36*37* parameters:38* _FUNCNAME : function name39* __INT : return type40* __INT_MIN : lower limit of the return type41* __INT_MAX : upper limit of the return type42*/43#if defined(_KERNEL) || defined(_STANDALONE) || defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)44__INT45_FUNCNAME(const char *nptr, char **endptr, int base)46#else47#include <locale.h>48#include "setlocale_local.h"49#define INT_FUNCNAME_(pre, name, post) pre ## name ## post50#define INT_FUNCNAME(pre, name, post) INT_FUNCNAME_(pre, name, post)5152static __INT53INT_FUNCNAME(_int_, _FUNCNAME, _l)(const char *nptr, char **endptr,54int base, locale_t loc)55#endif56{57const char *s;58__INT acc, cutoff;59unsigned char c;60int i, neg, any, cutlim;6162_DIAGASSERT(nptr != NULL);63/* endptr may be NULL */6465/* check base value */66if (base && (base < 2 || base > 36)) {67#if !defined(_KERNEL) && !defined(_STANDALONE)68errno = EINVAL;69if (endptr != NULL)70/* LINTED interface specification */71*endptr = __UNCONST(nptr);72return 0;73#else74panic("%s: invalid base %d", __func__, base);75#endif76}7778/*79* Skip white space and pick up leading +/- sign if any.80* If base is 0, allow 0x for hex and 0 for octal, else81* assume decimal; if base is already 16, allow 0x.82*/83s = nptr;84#if defined(_KERNEL) || defined(_STANDALONE) || \85defined(HAVE_NBTOOL_CONFIG_H) || defined(BCS_ONLY)86do {87c = *s++;88} while (isspace(c));89#else90do {91c = *s++;92} while (isspace_l(c, loc));93#endif94if (c == '-') {95neg = 1;96c = *s++;97} else {98neg = 0;99if (c == '+')100c = *s++;101}102if ((base == 0 || base == 16) &&103c == '0' && (*s == 'x' || *s == 'X') &&104((s[1] >= '0' && s[1] <= '9') ||105(s[1] >= 'a' && s[1] <= 'f') ||106(s[1] >= 'A' && s[1] <= 'F'))) {107c = s[1];108s += 2;109base = 16;110#if 0111} else if ((base == 0 || base == 2) &&112c == '0' && (*s == 'b' || *s == 'B') &&113(s[1] >= '0' && s[1] <= '1')) {114c = s[1];115s += 2;116base = 2;117#endif118} else if (base == 0)119base = (c == '0' ? 8 : 10);120121/*122* Compute the cutoff value between legal numbers and illegal123* numbers. That is the largest legal value, divided by the124* base. An input number that is greater than this value, if125* followed by a legal input character, is too big. One that126* is equal to this value may be valid or not; the limit127* between valid and invalid numbers is then based on the last128* digit. For instance, if the range for longs is129* [-2147483648..2147483647] and the input base is 10,130* cutoff will be set to 214748364 and cutlim to either131* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated132* a value > 214748364, or equal but the next digit is > 7 (or 8),133* the number is too big, and we will return a range error.134*135* Set any if any `digits' consumed; make it negative to indicate136* overflow.137*/138cutoff = (__INT)(neg ? __INT_MIN : __INT_MAX);139cutlim = (int)(cutoff % base);140cutoff /= base;141if (neg) {142if (cutlim > 0) {143cutlim -= base;144cutoff += 1;145}146cutlim = -cutlim;147}148for (acc = 0, any = 0;; c = *s++) {149if (c >= '0' && c <= '9')150i = c - '0';151else if (c >= 'a' && c <= 'z')152i = (c - 'a') + 10;153else if (c >= 'A' && c <= 'Z')154i = (c - 'A') + 10;155else156break;157if (i >= base)158break;159if (any < 0)160continue;161if (neg) {162if (acc < cutoff || (acc == cutoff && i > cutlim)) {163acc = __INT_MIN;164#if !defined(_KERNEL) && !defined(_STANDALONE)165any = -1;166errno = ERANGE;167#else168any = 0;169break;170#endif171} else {172any = 1;173acc *= base;174acc -= i;175}176} else {177if (acc > cutoff || (acc == cutoff && i > cutlim)) {178acc = __INT_MAX;179#if !defined(_KERNEL) && !defined(_STANDALONE)180any = -1;181errno = ERANGE;182#else183any = 0;184break;185#endif186} else {187any = 1;188acc *= base;189acc += i;190}191}192}193if (endptr != NULL)194/* LINTED interface specification */195*endptr = __UNCONST(any ? s - 1 : nptr);196return(acc);197}198199#if !defined(_KERNEL) && !defined(_STANDALONE) && \200!defined(HAVE_NBTOOL_CONFIG_H) && !defined(BCS_ONLY)201__INT202_FUNCNAME(const char *nptr, char **endptr, int base)203{204return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, _current_locale());205}206207__INT208INT_FUNCNAME(, _FUNCNAME, _l)(const char *nptr, char **endptr, int base, locale_t loc)209{210return INT_FUNCNAME(_int_, _FUNCNAME, _l)(nptr, endptr, base, loc);211}212#endif213214215