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