4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync/* $NetBSD: strtoumax.c,v 1.1 2006/04/22 15:33:33 thorpej Exp $ */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync/*
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * Copyright (c) 1990, 1993
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * The Regents of the University of California. All rights reserved.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync *
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * Redistribution and use in source and binary forms, with or without
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * modification, are permitted provided that the following conditions
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * are met:
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * 1. Redistributions of source code must retain the above copyright
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * notice, this list of conditions and the following disclaimer.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * 2. Redistributions in binary form must reproduce the above copyright
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * notice, this list of conditions and the following disclaimer in the
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * documentation and/or other materials provided with the distribution.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * 3. Neither the name of the University nor the names of its contributors
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * may be used to endorse or promote products derived from this software
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * without specific prior written permission.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync *
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * SUCH DAMAGE.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <LibConfig.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <sys/EfiCdefs.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#if !defined(_KERNEL) && !defined(_STANDALONE)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#if defined(LIBC_SCCS) && !defined(lint)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#if 0
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsyncstatic char sccsid[] = "from: @(#)strtoul.c 8.1 (Berkeley) 6/4/93";
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#else
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync__RCSID("$NetBSD: strtoumax.c,v 1.1 2006/04/22 15:33:33 thorpej Exp $");
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif /* LIBC_SCCS and not lint */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include "namespace.h"
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <assert.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <ctype.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <errno.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <inttypes.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <stddef.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <Library/BaseLib.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#ifdef __weak_alias
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync__weak_alias(strtoumax, _strtoumax)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#else /* !_KERNEL && !_STANDALONE */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <sys/param.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#include <lib/libkern/libkern.h>
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif /* !_KERNEL && !_STANDALONE */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync/*
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * Convert a string to an uintmax_t.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync *
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * Ignores `locale' stuff. Assumes that the upper and lower case
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * alphabets and digits are each contiguous.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsyncuintmax_t
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsyncstrtoumax(const char *nptr, char **endptr, int base)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync{
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync const char *s;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync uintmax_t acc, cutoff;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync int c;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync int neg, any, cutlim;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync _DIAGASSERT(nptr != NULL);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync /* endptr may be NULL */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync /*
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync * See strtol for comments as to the logic used.
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync */
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync s = nptr;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync do {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c = (unsigned char) *s++;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync } while (isspace(c));
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (c == '-') {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync neg = 1;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c = *s++;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync } else {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync neg = 0;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (c == '+')
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c = *s++;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync }
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if ((base == 0 || base == 16) &&
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c == '0' && (*s == 'x' || *s == 'X')) {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c = s[1];
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync s += 2;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync base = 16;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync }
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (base == 0)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync base = c == '0' ? 8 : 10;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync cutoff = DivU64x32 ((UINT64) UINTMAX_MAX, (UINT32) base);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync cutlim = (int) ModU64x32 ((UINT64) UINTMAX_MAX, (UINT32) base);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync for (acc = 0, any = 0;; c = (unsigned char) *s++) {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (isdigit(c))
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c -= '0';
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync else if (isalpha(c)) {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#if defined(_KERNEL) || defined(_STANDALONE)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c = toupper(c) - 'A' + 10;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#else
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync } else
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync break;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (c >= base)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync break;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (any < 0)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync continue;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (acc > cutoff || (acc == cutoff && c > cutlim)) {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#if defined(_KERNEL) || defined(_STANDALONE)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (endptr)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync *endptr = __UNCONST(nptr);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync return UINTMAX_MAX;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#else
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync any = -1;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync acc = UINTMAX_MAX;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync errno = ERANGE;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync#endif
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync } else {
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync any = 1;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync acc *= (uintmax_t)base;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync acc += c;
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync }
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync }
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (neg && any > 0)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync acc = (uintmax_t)(-((intmax_t)acc));
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync if (endptr != 0)
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync *endptr = __UNCONST(any ? s - 1 : nptr);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync return (acc);
4fd606d1f5abe38e1f42c38de1d2e895166bd0f4vboxsync}