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