strtoull.c revision 87020
1218885Sdim/*- 2218885Sdim * Copyright (c) 1992, 1993 3218885Sdim * The Regents of the University of California. All rights reserved. 4218885Sdim * 5218885Sdim * Redistribution and use in source and binary forms, with or without 6218885Sdim * modification, are permitted provided that the following conditions 7218885Sdim * are met: 8218885Sdim * 1. Redistributions of source code must retain the above copyright 9218885Sdim * notice, this list of conditions and the following disclaimer. 10218885Sdim * 2. Redistributions in binary form must reproduce the above copyright 11218885Sdim * notice, this list of conditions and the following disclaimer in the 12218885Sdim * documentation and/or other materials provided with the distribution. 13218885Sdim * 3. All advertising materials mentioning features or use of this software 14249423Sdim * must display the following acknowledgement: 15249423Sdim * This product includes software developed by the University of 16249423Sdim * California, Berkeley and its contributors. 17218885Sdim * 4. Neither the name of the University nor the names of its contributors 18218885Sdim * may be used to endorse or promote products derived from this software 19226584Sdim * without specific prior written permission. 20218885Sdim * 21249423Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22234353Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23218885Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24218885Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25263508Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26263508Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27218885Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28263508Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29218885Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30218885Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31263508Sdim * SUCH DAMAGE. 32234353Sdim * 33263508Sdim * $FreeBSD: head/lib/libc/stdlib/strtoull.c 87020 2001-11-28 02:17:22Z ache $ 34263508Sdim */ 35263508Sdim 36263508Sdim#if defined(LIBC_SCCS) && !defined(lint) 37263508Sdimstatic char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93"; 38234353Sdim#endif /* LIBC_SCCS and not lint */ 39263508Sdim 40226584Sdim#include <limits.h> 41249423Sdim#include <errno.h> 42249423Sdim#include <ctype.h> 43249423Sdim#include <stdlib.h> 44249423Sdim 45226584Sdim/* 46218885Sdim * Convert a string to an unsigned long long integer. 47218885Sdim * 48226584Sdim * Assumes that the upper and lower case 49218885Sdim * alphabets and digits are each contiguous. 50226584Sdim */ 51218885Sdimunsigned long long 52218885Sdimstrtoull(nptr, endptr, base) 53218885Sdim const char *nptr; 54218885Sdim char **endptr; 55218885Sdim int base; 56218885Sdim{ 57218885Sdim const char *s; 58226584Sdim unsigned long long acc; 59226584Sdim unsigned char c; 60218885Sdim unsigned long long cutoff; 61218885Sdim int neg, any, cutlim; 62218885Sdim 63234353Sdim /* 64218885Sdim * See strtoq for comments as to the logic used. 65263508Sdim */ 66218885Sdim s = nptr; 67218885Sdim do { 68218885Sdim c = *s++; 69226584Sdim } while (isspace(c)); 70226584Sdim if (c == '-') { 71218885Sdim neg = 1; 72226584Sdim c = *s++; 73226584Sdim } else { 74226584Sdim neg = 0; 75226584Sdim if (c == '+') 76226584Sdim c = *s++; 77226584Sdim } 78218885Sdim if ((base == 0 || base == 16) && 79226584Sdim c == '0' && (*s == 'x' || *s == 'X')) { 80226584Sdim c = s[1]; 81226584Sdim s += 2; 82226584Sdim base = 16; 83234353Sdim } 84234353Sdim if (base == 0) 85234353Sdim base = c == '0' ? 8 : 10; 86234353Sdim acc = any = 0; 87234353Sdim if (base < 2 || base > 35) 88234353Sdim goto noconv; 89249423Sdim 90226584Sdim cutoff = ULLONG_MAX / base; 91249423Sdim cutlim = ULLONG_MAX % base; 92226584Sdim for ( ; ; c = *s++) { 93226584Sdim if (isxdigit(c)) 94226584Sdim c = digittoint(c); 95226584Sdim else if (isascii(c) && isalpha(c)) 96226584Sdim c -= isupper(c) ? 'A' - 10 : 'a' - 10; 97226584Sdim else 98226584Sdim break; 99218885Sdim if (c >= base) 100218885Sdim break; 101226584Sdim if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 102226584Sdim any = -1; 103226584Sdim else { 104226584Sdim any = 1; 105226584Sdim acc *= base; 106226584Sdim acc += c; 107226584Sdim } 108226584Sdim } 109226584Sdim if (any < 0) { 110218885Sdim acc = ULLONG_MAX; 111243830Sdim errno = ERANGE; 112243830Sdim } else if (!any) { 113243830Sdimnoconv: 114243830Sdim errno = EINVAL; 115243830Sdim } else if (neg) 116243830Sdim acc = -acc; 117249423Sdim if (endptr != NULL) 118249423Sdim *endptr = (char *)(any ? s - 1 : nptr); 119249423Sdim return (acc); 120249423Sdim} 121249423Sdim