Home | History | Annotate | Line # | Download | only in libiberty
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 2014 Regents of the University of California.
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that the following conditions
      7  1.1  christos  * are met:
      8  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  christos  *    documentation and/or other materials provided with the distribution.
     13  1.1  christos  * 3. [rescinded 22 July 1999]
     14  1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     15  1.1  christos  *    may be used to endorse or promote products derived from this software
     16  1.1  christos  *    without specific prior written permission.
     17  1.1  christos  *
     18  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  christos  * SUCH DAMAGE.
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #ifdef HAVE_CONFIG_H
     32  1.1  christos #include "config.h"
     33  1.1  christos #endif
     34  1.1  christos #ifdef HAVE_LIMITS_H
     35  1.1  christos #include <limits.h>
     36  1.1  christos #endif
     37  1.1  christos #ifdef HAVE_SYS_PARAM_H
     38  1.1  christos #include <sys/param.h>
     39  1.1  christos #endif
     40  1.1  christos #include <errno.h>
     41  1.1  christos #ifdef NEED_DECLARATION_ERRNO
     42  1.1  christos extern int errno;
     43  1.1  christos #endif
     44  1.1  christos #if 0
     45  1.1  christos #include <stdlib.h>
     46  1.1  christos #endif
     47  1.1  christos #include "ansidecl.h"
     48  1.1  christos #include "safe-ctype.h"
     49  1.1  christos 
     50  1.1  christos #ifdef HAVE_LONG_LONG
     51  1.1  christos 
     52  1.1  christos __extension__
     53  1.1  christos typedef unsigned long long ullong_type;
     54  1.1  christos 
     55  1.1  christos #ifndef ULLONG_MAX
     56  1.1  christos #define ULLONG_MAX (~(ullong_type)0) /* 0xFFFFFFFFFFFFFFFF */
     57  1.1  christos #endif
     58  1.1  christos 
     59  1.1  christos /*
     60  1.1  christos  * Convert a string to an unsigned long long integer.
     61  1.1  christos  *
     62  1.1  christos  * Ignores `locale' stuff.  Assumes that the upper and lower case
     63  1.1  christos  * alphabets and digits are each contiguous.
     64  1.1  christos  */
     65  1.1  christos ullong_type
     66  1.1  christos strtoull(const char *nptr, char **endptr, register int base)
     67  1.1  christos {
     68  1.1  christos 	register const char *s = nptr;
     69  1.1  christos 	register ullong_type acc;
     70  1.1  christos 	register int c;
     71  1.1  christos 	register ullong_type cutoff;
     72  1.1  christos 	register int neg = 0, any, cutlim;
     73  1.1  christos 
     74  1.1  christos 	/*
     75  1.1  christos 	 * See strtol for comments as to the logic used.
     76  1.1  christos 	 */
     77  1.1  christos 	do {
     78  1.1  christos 		c = *s++;
     79  1.1  christos 	} while (ISSPACE(c));
     80  1.1  christos 	if (c == '-') {
     81  1.1  christos 		neg = 1;
     82  1.1  christos 		c = *s++;
     83  1.1  christos 	} else if (c == '+')
     84  1.1  christos 		c = *s++;
     85  1.1  christos 	if ((base == 0 || base == 16) &&
     86  1.1  christos 	    c == '0' && (*s == 'x' || *s == 'X')) {
     87  1.1  christos 		c = s[1];
     88  1.1  christos 		s += 2;
     89  1.1  christos 		base = 16;
     90  1.1  christos 	}
     91  1.1  christos 	if (base == 0)
     92  1.1  christos 		base = c == '0' ? 8 : 10;
     93  1.1  christos 	cutoff = (ullong_type)ULLONG_MAX / (ullong_type)base;
     94  1.1  christos 	cutlim = (ullong_type)ULLONG_MAX % (ullong_type)base;
     95  1.1  christos 	for (acc = 0, any = 0;; c = *s++) {
     96  1.1  christos 		if (ISDIGIT(c))
     97  1.1  christos 			c -= '0';
     98  1.1  christos 		else if (ISALPHA(c))
     99  1.1  christos 			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
    100  1.1  christos 		else
    101  1.1  christos 			break;
    102  1.1  christos 		if (c >= base)
    103  1.1  christos 			break;
    104  1.1  christos 		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
    105  1.1  christos 			any = -1;
    106  1.1  christos 		else {
    107  1.1  christos 			any = 1;
    108  1.1  christos 			acc *= base;
    109  1.1  christos 			acc += c;
    110  1.1  christos 		}
    111  1.1  christos 	}
    112  1.1  christos 	if (any < 0) {
    113  1.1  christos 		acc = ULLONG_MAX;
    114  1.1  christos 		errno = ERANGE;
    115  1.1  christos 	} else if (neg)
    116  1.1  christos 		acc = -acc;
    117  1.1  christos 	if (endptr != 0)
    118  1.1  christos 		*endptr = (char *) (any ? s - 1 : nptr);
    119  1.1  christos 	return (acc);
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos #endif /* ifdef HAVE_LONG_LONG */
    123