| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * 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 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 | #include <stddef.h> | 
|  | 29 | #include <stdint.h> | 
|  | 30 | #include <ctype.h> | 
|  | 31 |  | 
|  | 32 | static inline int digitval(int ch) | 
|  | 33 | { | 
|  | 34 | unsigned  d; | 
|  | 35 |  | 
|  | 36 | d = (unsigned)(ch - '0'); | 
|  | 37 | if (d < 10) return (int)d; | 
|  | 38 |  | 
|  | 39 | d = (unsigned)(ch - 'a'); | 
|  | 40 | if (d < 6) return (int)(d+10); | 
|  | 41 |  | 
|  | 42 | d = (unsigned)(ch - 'A'); | 
|  | 43 | if (d < 6) return (int)(d+10); | 
|  | 44 |  | 
|  | 45 | return -1; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | uintmax_t | 
|  | 49 | strntoumax(const char *nptr, char **endptr, int base, size_t n) | 
|  | 50 | { | 
| David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 51 | const unsigned char*  p   = (const unsigned char *)nptr; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | const unsigned char*  end = p + n; | 
|  | 53 | int                   minus = 0; | 
|  | 54 | uintmax_t             v = 0; | 
|  | 55 | int                   d; | 
|  | 56 |  | 
|  | 57 | /* skip leading space */ | 
|  | 58 | while (p < end && isspace(*p)) | 
|  | 59 | p++; | 
|  | 60 |  | 
|  | 61 | /* Single optional + or - */ | 
|  | 62 | if (p < end) { | 
|  | 63 | char c = p[0]; | 
|  | 64 | if ( c == '-' || c == '+' ) { | 
|  | 65 | minus = (c == '-'); | 
|  | 66 | p++; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | if ( base == 0 ) { | 
|  | 71 | if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { | 
|  | 72 | p += 2; | 
|  | 73 | base = 16; | 
|  | 74 | } else if ( p+1 < end && p[0] == '0' ) { | 
|  | 75 | p   += 1; | 
|  | 76 | base = 8; | 
|  | 77 | } else { | 
|  | 78 | base = 10; | 
|  | 79 | } | 
|  | 80 | } else if ( base == 16 ) { | 
|  | 81 | if ( p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X') ) { | 
|  | 82 | p += 2; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | while ( p < end && (d = digitval(*p)) >= 0 && d < base ) { | 
|  | 87 | v = v*base + d; | 
|  | 88 | p += 1; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | if ( endptr ) | 
|  | 92 | *endptr = (char *)p; | 
|  | 93 |  | 
|  | 94 | return minus ? -v : v; | 
|  | 95 | } |