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