--- /dev/null
+/*-\r
+ * Copyright (c) 1990 The Regents of the University of California.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * 3. All advertising materials mentioning features or use of this software\r
+ * must display the following acknowledgement:\r
+ * This product includes software developed by the University of\r
+ * California, Berkeley and its contributors.\r
+ * 4. Neither the name of the University nor the names of its contributors\r
+ * may be used to endorse or promote products derived from this software\r
+ * without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+ * SUCH DAMAGE.\r
+ */\r
+\r
+/* modified for long long <mgd@swarm.org> 1999-08-12 */\r
+\r
+#ifdef HAVE_CONFIG_H\r
+#include <config.h>\r
+#endif\r
+#if HAVE_CTYPE_H\r
+#include <ctype.h>\r
+#endif\r
+#if HAVE_ERRNO_H\r
+#include <errno.h>\r
+#endif\r
+\r
+/* Specification. */\r
+#include "strtoll.h"\r
+\r
+\r
+#ifndef INT64_MIN\r
+/* Native 64 bit system without strtoll() */\r
+#if defined(LONG_MIN) && (SIZEOF_LONG == 8)\r
+#define INT64_MIN LONG_MIN\r
+#else\r
+/* 32 bit system */\r
+#define INT64_MIN -9223372036854775807L-1L\r
+#endif\r
+#endif\r
+\r
+#ifndef INT64_MAX\r
+/* Native 64 bit system without strtoll() */\r
+#if defined(LONG_MAX) && (SIZEOF_LONG == 8)\r
+#define INT64_MAX LONG_MAX\r
+#else\r
+/* 32 bit system */\r
+#define INT64_MAX 9223372036854775807L\r
+#endif\r
+#endif\r
+\r
+\r
+/*\r
+ * Convert a string to a int64 integer.\r
+ *\r
+ * Ignores `locale' stuff. Assumes that the upper and lower case\r
+ * alphabets and digits are each contiguous.\r
+ */\r
+int64_t\r
+strtoll (const char *nptr, char **endptr, int base)\r
+{\r
+ register const char *s = nptr;\r
+ register u_int64_t acc;\r
+ register int c;\r
+ register u_int64_t cutoff;\r
+ register int neg = 0, any, cutlim;\r
+ \r
+ /*\r
+ * Skip white space and pick up leading +/- sign if any.\r
+ * If base is 0, allow 0x for hex and 0 for octal, else\r
+ * assume decimal; if base is already 16, allow 0x.\r
+ */\r
+ do\r
+ {\r
+ c = *s++;\r
+ } while (xisspace(c));\r
+ if (c == '-')\r
+ {\r
+ neg = 1;\r
+ c = *s++;\r
+ }\r
+ else if (c == '+')\r
+ c = *s++;\r
+ if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X'))\r
+ {\r
+ c = s[1];\r
+ s += 2;\r
+ base = 16;\r
+ }\r
+ if (base == 0)\r
+ base = c == '0' ? 8 : 10;\r
+ \r
+ /*\r
+ * Compute the cutoff value between legal numbers and illegal\r
+ * numbers. That is the largest legal value, divided by the\r
+ * base. An input number that is greater than this value, if\r
+ * followed by a legal input character, is too big. One that\r
+ * is equal to this value may be valid or not; the limit\r
+ * between valid and invalid numbers is then based on the last\r
+ * digit. For instance, if the range for longs is\r
+ * [-2147483648..2147483647] and the input base is 10,\r
+ * cutoff will be set to 214748364 and cutlim to either\r
+ * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated\r
+ * a value > 214748364, or equal but the next digit is > 7 (or 8),\r
+ * the number is too big, and we will return a range error.\r
+ *\r
+ * Set any if any `digits' consumed; make it negative to indicate\r
+ * overflow.\r
+ */\r
+ cutoff = neg ? -(u_int64_t) INT64_MIN : INT64_MAX;\r
+ cutlim = cutoff % (u_int64_t) base;\r
+ cutoff /= (u_int64_t) base;\r
+ for (acc = 0, any = 0;; c = *s++)\r
+ {\r
+ if (xisdigit(c))\r
+ c -= '0';\r
+ else if (xisalpha(c))\r
+ c -= xisupper(c) ? 'A' - 10 : 'a' - 10;\r
+ else\r
+ break;\r
+ if (c >= base)\r
+ break;\r
+ if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))\r
+ any = -1;\r
+ else\r
+ {\r
+ any = 1;\r
+ acc *= base;\r
+ acc += c;\r
+ }\r
+ }\r
+ if (any < 0)\r
+ {\r
+ acc = neg ? INT64_MIN : INT64_MAX;\r
+ errno = ERANGE;\r
+ }\r
+ else if (neg)\r
+ acc = -acc;\r
+ if (endptr != 0)\r
+ *endptr = (char *) (any ? s - 1 : nptr);\r
+ return acc;\r
+}\r
+\r
/*
- * $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 <serassio@squid-cache.org>
{
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