+
+#ifdef NEED_STRTOLL
+/* strtoll is required by C99 and used by Wget only on systems with
+ LFS. Unfortunately, some systems have LFS, but no strtoll or
+ equivalent. These include HPUX 11.0 and Windows.
+
+ We use #ifdef NEED_STRTOLL instead of #ifndef HAVE_STRTOLL because
+ of the systems which have a suitable replacement (e.g. _strtoi64 on
+ Windows), on which Wget's str_to_wgint is instructed to use that
+ instead. */
+
+static inline int
+char_value (char c, int base)
+{
+ int value;
+ if (c < '0')
+ return -1;
+ if ('0' <= c && c <= '9')
+ value = c - '0';
+ else if ('a' <= c && c <= 'z')
+ value = c - 'a' + 10;
+ else if ('A' <= c && c <= 'Z')
+ value = c - 'A' + 10;
+ else
+ return -1;
+ if (value >= base)
+ return -1;
+ return value;
+}
+
+#define STRTOLL_MAX TYPE_MAXIMUM (strtoll_type)
+/* This definition assumes two's complement arithmetic */
+#define STRTOLL_MIN (-STRTOLL_MAX - 1)
+
+/* Like a%b, but always returns a positive number when A is negative.
+ (C doesn't guarantee the sign of the result.) */
+#define MOD(a, b) ((strtoll_type) -1 % 2 == 1 ? (a) % (b) : - ((a) % (b)))
+
+/* A strtoll-like replacement for systems that have an integral type
+ larger than long but don't supply strtoll. This implementation
+ makes no assumptions about the size of strtoll_type. */
+
+strtoll_type
+strtoll (const char *nptr, char **endptr, int base)
+{
+ strtoll_type result = 0;
+ bool negative;
+
+ if (base != 0 && (base < 2 || base > 36))
+ {
+ errno = EINVAL;
+ return 0;
+ }
+
+ while (*nptr == ' ' || *nptr == '\t')
+ ++nptr;
+ if (*nptr == '-')
+ {
+ negative = true;
+ ++nptr;
+ }
+ else if (*nptr == '+')
+ {
+ negative = false;
+ ++nptr;
+ }
+ else
+ negative = false;
+
+ /* If BASE is 0, determine the real base based on the beginning on
+ the number; octal numbers begin with "0", hexadecimal with "0x",
+ and the others are considered octal. */
+ if (*nptr == '0')
+ {
+ if ((base == 0 || base == 16)
+ &&
+ (*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
+ {
+ base = 16;
+ nptr += 2;
+ /* "0x" must be followed by at least one hex char. If not,
+ return 0 and place ENDPTR on 'x'. */
+ if (!c_isxdigit (*nptr))
+ {
+ --nptr;
+ goto out;
+ }
+ }
+ else if (base == 0)
+ base = 8;
+ }
+ else if (base == 0)
+ base = 10;
+
+ if (!negative)
+ {
+ /* Parse positive number, checking for overflow. */
+ int digit;
+ /* Overflow watermark. If RESULT exceeds it, overflow occurs on
+ this digit. If result==WATERMARK, current digit may not
+ exceed the last digit of maximum value. */
+ const strtoll_type WATERMARK = STRTOLL_MAX / base;
+ for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
+ {
+ if (result > WATERMARK
+ || (result == WATERMARK && digit > STRTOLL_MAX % base))
+ {
+ result = STRTOLL_MAX;
+ errno = ERANGE;
+ break;
+ }
+ result = base * result + digit;
+ }
+ }
+ else
+ {
+ /* Parse negative number, checking for underflow. */
+ int digit;
+ const strtoll_type WATERMARK = STRTOLL_MIN / base;
+ for (; (digit = char_value (*nptr, base)) != -1; ++nptr)
+ {
+ if (result < WATERMARK
+ || (result == WATERMARK && digit > MOD (STRTOLL_MIN, base)))
+ {
+ result = STRTOLL_MIN;
+ errno = ERANGE;
+ break;
+ }
+ result = base * result - digit;
+ }
+ }
+ out:
+ if (endptr)
+ *endptr = (char *) nptr;
+ return result;
+}
+
+#undef STRTOLL_MAX
+#undef STRTOLL_MIN
+#undef ABS
+
+#endif /* NEED_STRTOLL */