From: serassio <> Date: Sat, 18 Aug 2007 00:56:24 +0000 (+0000) Subject: strtoll() is not available on any platform, this add an own implementation. X-Git-Tag: SQUID_3_0_PRE7~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2733d4267f7ad78dcefce40cb645fb270a64ea47;p=thirdparty%2Fsquid.git strtoll() is not available on any platform, this add an own implementation. --- diff --git a/configure.in b/configure.in index 672adcab61..941d838a89 100644 --- a/configure.in +++ b/configure.in @@ -1,7 +1,7 @@ dnl Configuration input file for Squid dnl -dnl $Id: configure.in,v 1.463 2007/08/11 13:27:27 serassio Exp $ +dnl $Id: configure.in,v 1.464 2007/08/17 18:56:24 serassio Exp $ dnl dnl dnl @@ -11,7 +11,7 @@ AM_CONFIG_HEADER(include/autoconf.h) AC_CONFIG_AUX_DIR(cfgaux) AC_CONFIG_SRCDIR([src/main.cc]) AM_INIT_AUTOMAKE([tar-ustar]) -AC_REVISION($Revision: 1.463 $)dnl +AC_REVISION($Revision: 1.464 $)dnl AC_PREFIX_DEFAULT(/usr/local/squid) AM_MAINTAINER_MODE @@ -2621,6 +2621,7 @@ AC_CHECK_FUNCS(\ srandom \ statfs \ strsep \ + strtoll \ sysconf \ syslog \ timegm \ @@ -2760,6 +2761,11 @@ if test "$ac_cv_func_strsep" = "no" ; then AM_CONDITIONAL(NEED_OWN_STRSEP, true) fi +AM_CONDITIONAL(NEED_OWN_STRTOLL, false) +if test "$ac_cv_func_strtoll" = "no" ; then + AM_CONDITIONAL(NEED_OWN_STRTOLL, true) +fi + dnl dnl Test for va_copy dnl diff --git a/include/strtoll.h b/include/strtoll.h new file mode 100755 index 0000000000..ba9622af4c --- /dev/null +++ b/include/strtoll.h @@ -0,0 +1,19 @@ +#if HAVE_STRTOLL + +/* + * Get strtoll() declaration. + */ +#include + +#else + +/* + * Convert a string to a int64 integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ + +SQUIDCEXTERN int64_t strtoll (const char *nptr, char **endptr, int base); + +#endif diff --git a/lib/Makefile.am b/lib/Makefile.am index ebba939287..9b8084be57 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in # -# $Id: Makefile.am,v 1.25 2007/04/24 06:30:37 wessels Exp $ +# $Id: Makefile.am,v 1.26 2007/08/17 18:56:25 serassio Exp $ # DIST_SUBDIRS = libTrie @@ -28,6 +28,11 @@ STRSEPSOURCE=strsep.c else STRSEPSOURCE= endif +if NEED_OWN_STRTOLL +STRTOLLSOURCE=strtoll.c +else +STRTOLLSOURCE= +endif if NEED_OWN_MD5 MD5SOURCE=md5.c else @@ -57,6 +62,7 @@ EXTRA_libmiscutil_a_SOURCES = \ Profiler.c \ snprintf.c \ strsep.c \ + strtoll.c \ win32lib.c libmiscutil_a_SOURCES = \ MemPool.cc \ @@ -76,6 +82,7 @@ libmiscutil_a_SOURCES = \ $(SNPRINTFSOURCE) \ Splay.cc \ $(STRSEPSOURCE) \ + $(STRTOLLSOURCE) \ stub_memaccount.c \ util.c \ uudecode.c \ diff --git a/lib/strtoll.c b/lib/strtoll.c new file mode 100755 index 0000000000..f8145bbbe7 --- /dev/null +++ b/lib/strtoll.c @@ -0,0 +1,161 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* modified for long long 1999-08-12 */ + +#ifdef HAVE_CONFIG_H +#include +#endif +#if HAVE_CTYPE_H +#include +#endif +#if HAVE_ERRNO_H +#include +#endif + +/* Specification. */ +#include "strtoll.h" + + +#ifndef INT64_MIN +/* Native 64 bit system without strtoll() */ +#if defined(LONG_MIN) && (SIZEOF_LONG == 8) +#define INT64_MIN LONG_MIN +#else +/* 32 bit system */ +#define INT64_MIN -9223372036854775807L-1L +#endif +#endif + +#ifndef INT64_MAX +/* Native 64 bit system without strtoll() */ +#if defined(LONG_MAX) && (SIZEOF_LONG == 8) +#define INT64_MAX LONG_MAX +#else +/* 32 bit system */ +#define INT64_MAX 9223372036854775807L +#endif +#endif + + +/* + * Convert a string to a int64 integer. + * + * Ignores `locale' stuff. Assumes that the upper and lower case + * alphabets and digits are each contiguous. + */ +int64_t +strtoll (const char *nptr, char **endptr, int base) +{ + register const char *s = nptr; + register u_int64_t acc; + register int c; + register u_int64_t cutoff; + register int neg = 0, any, cutlim; + + /* + * Skip white space and pick up leading +/- sign if any. + * If base is 0, allow 0x for hex and 0 for octal, else + * assume decimal; if base is already 16, allow 0x. + */ + do + { + c = *s++; + } while (xisspace(c)); + if (c == '-') + { + neg = 1; + c = *s++; + } + else if (c == '+') + c = *s++; + if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) + { + c = s[1]; + s += 2; + base = 16; + } + if (base == 0) + base = c == '0' ? 8 : 10; + + /* + * Compute the cutoff value between legal numbers and illegal + * numbers. That is the largest legal value, divided by the + * base. An input number that is greater than this value, if + * followed by a legal input character, is too big. One that + * is equal to this value may be valid or not; the limit + * between valid and invalid numbers is then based on the last + * digit. For instance, if the range for longs is + * [-2147483648..2147483647] and the input base is 10, + * cutoff will be set to 214748364 and cutlim to either + * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated + * a value > 214748364, or equal but the next digit is > 7 (or 8), + * the number is too big, and we will return a range error. + * + * Set any if any `digits' consumed; make it negative to indicate + * overflow. + */ + cutoff = neg ? -(u_int64_t) INT64_MIN : INT64_MAX; + cutlim = cutoff % (u_int64_t) base; + cutoff /= (u_int64_t) base; + for (acc = 0, any = 0;; c = *s++) + { + if (xisdigit(c)) + c -= '0'; + else if (xisalpha(c)) + c -= xisupper(c) ? 'A' - 10 : 'a' - 10; + else + break; + if (c >= base) + break; + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + any = -1; + else + { + any = 1; + acc *= base; + acc += c; + } + } + if (any < 0) + { + acc = neg ? INT64_MIN : INT64_MAX; + errno = ERANGE; + } + else if (neg) + acc = -acc; + if (endptr != 0) + *endptr = (char *) (any ? s - 1 : nptr); + return acc; +} + diff --git a/lib/win32lib.c b/lib/win32lib.c index a6f7916b1e..34df1334e6 100755 --- a/lib/win32lib.c +++ b/lib/win32lib.c @@ -1,6 +1,6 @@ /* - * $Id: win32lib.c,v 1.4 2007/08/14 16:12:40 serassio Exp $ + * $Id: win32lib.c,v 1.5 2007/08/17 18:56:26 serassio Exp $ * * Windows support * AUTHOR: Guido Serassio @@ -65,91 +65,6 @@ getpagesize() { return 4096; } - -int64_t -WIN32_strtoll(const char *nptr, char **endptr, int base) -{ - const char *s; - int64_t acc; - int64_t val; - int neg, any; - char c; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = *s++; - } while (xisspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - acc = any = 0; - if (base < 2 || base > 36) { - errno = EINVAL; - if (endptr != NULL) - *endptr = (char *) (any ? s - 1 : nptr); - return acc; - } - /* The classic bsd implementation requires div/mod operators - * to compute a cutoff. Benchmarking proves that is very, very - * evil to some 32 bit processors. Instead, look for underflow - * in both the mult and add/sub operation. Unlike the bsd impl, - * we also work strictly in a signed int64 word as we haven't - * implemented the unsigned type in win32. - * - * Set 'any' if any `digits' consumed; make it negative to indicate - * overflow. - */ - val = 0; - for (;; c = *s++) { - if (c >= '0' && c <= '9') - c -= '0'; - else if (c >= 'A' && c <= 'Z') - c -= 'A' - 10; - else if (c >= 'a' && c <= 'z') - c -= 'a' - 10; - else - break; - if (c >= base) - break; - val *= base; - if ((any < 0) /* already noted an over/under flow - short circuit */ - ||(neg && (val > acc || (val -= c) > acc)) /* underflow */ - ||(!neg && (val < acc || (val += c) < acc))) { /* overflow */ - any = -1; /* once noted, over/underflows never go away */ - } else { - acc = val; - any = 1; - } - } - - if (any < 0) { - acc = neg ? INT64_MIN : INT64_MAX; - errno = ERANGE; - } else if (!any) { - errno = EINVAL; - } - if (endptr != NULL) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} #endif uid_t diff --git a/src/squid.h b/src/squid.h index 518ce6554b..0845b09b3f 100644 --- a/src/squid.h +++ b/src/squid.h @@ -1,6 +1,6 @@ /* - * $Id: squid.h,v 1.265 2007/08/14 19:17:45 serassio Exp $ + * $Id: squid.h,v 1.266 2007/08/17 18:56:31 serassio Exp $ * * AUTHOR: Duane Wessels * @@ -294,14 +294,9 @@ struct rusage #define getpagesize( ) sysconf(_SC_PAGE_SIZE) #endif -#if defined(_SQUID_MSWIN_) +#if defined(_SQUID_MSWIN_) && !defined(getpagesize) /* Windows may lack getpagesize() prototype */ -#ifndef getpagesize SQUIDCEXTERN size_t getpagesize(void); -#endif -#if defined(_MSC_VER) /* Microsoft C Compiler ONLY */ -#define strtoll WIN32_strtoll -#endif #endif /* _SQUID_MSWIN_ */ #ifndef BUFSIZ @@ -393,6 +388,10 @@ extern "C" #include "strsep.h" #endif +#if !HAVE_STRTOLL +#include "strtoll.h" +#endif + #if !HAVE_INITGROUPS #include "initgroups.h" #endif