~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to src/cmpt.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-10-13 16:59:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051013165903-3e12j4gn6znkhmdb
Tags: 1.10.2-1
new upstream release which fixes a NTLM Buffer Overflow Vulnerability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1628
1628
}
1629
1629
 
1630
1630
#endif /* not SYSTEM_FNMATCH */
 
1631
 
 
1632
#ifndef HAVE_TIMEGM
 
1633
/* timegm is a GNU extension, but lately also available on *BSD
 
1634
   systems and possibly elsewhere. */
 
1635
 
 
1636
/* True if YEAR is a leap year. */
 
1637
#define ISLEAP(year)                                            \
 
1638
  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 
1639
 
 
1640
/* Number of leap years in the range [y1, y2). */
 
1641
#define LEAPYEARS(y1, y2)                                               \
 
1642
  ((y2-1)/4 - (y1-1)/4) - ((y2-1)/100 - (y1-1)/100) + ((y2-1)/400 - (y1-1)/400)
 
1643
 
 
1644
/* Inverse of gmtime: converts struct tm to time_t, assuming the data
 
1645
   in tm is UTC rather than local timezone.  This implementation
 
1646
   returns the number of seconds elapsed since midnight 1970-01-01,
 
1647
   converted to time_t.  */
 
1648
 
 
1649
time_t
 
1650
timegm (struct tm *t)
 
1651
{
 
1652
  static const unsigned short int month_to_days[][13] = {
 
1653
    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, /* normal */
 
1654
    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }  /* leap */
 
1655
  };
 
1656
  const int year = 1900 + t->tm_year;
 
1657
  unsigned long secs;  /* until 2106-02-07 for 32-bit unsigned long */
 
1658
  int days;
 
1659
 
 
1660
  if (year < 1970)
 
1661
    return (time_t) -1;
 
1662
 
 
1663
  days = 365 * (year - 1970);
 
1664
  /* Take into account leap years between 1970 and YEAR, not counting
 
1665
     YEAR itself.  */
 
1666
  days += LEAPYEARS (1970, year);
 
1667
  if (t->tm_mon < 0 || t->tm_mon >= 12)
 
1668
    return (time_t) -1;
 
1669
  days += month_to_days[ISLEAP (year)][t->tm_mon];
 
1670
  days += t->tm_mday - 1;
 
1671
 
 
1672
  secs = days * 86400 + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
 
1673
  return (time_t) secs;
 
1674
}
 
1675
#endif /* HAVE_TIMEGM */
 
1676
 
 
1677
#ifdef NEED_STRTOLL
 
1678
/* strtoll is required by C99 and used by Wget only on systems with
 
1679
   LFS.  Unfortunately, some systems have LFS, but no strtoll or
 
1680
   equivalent.  These include HPUX 11.0 and Windows.
 
1681
 
 
1682
   We use #ifdef NEED_STRTOLL instead of #ifndef HAVE_STRTOLL because
 
1683
   of the systems which have a suitable replacement (e.g. _strtoi64 on
 
1684
   Windows), on which Wget's str_to_wgint is instructed to use that
 
1685
   instead.  */
 
1686
 
 
1687
static inline int
 
1688
char_value (char c, int base)
 
1689
{
 
1690
  int value;
 
1691
  if (c < '0')
 
1692
    return -1;
 
1693
  if ('0' <= c && c <= '9')
 
1694
    value = c - '0';
 
1695
  else if ('a' <= c && c <= 'z')
 
1696
    value = c - 'a' + 10;
 
1697
  else if ('A' <= c && c <= 'Z')
 
1698
    value = c - 'A' + 10;
 
1699
  else
 
1700
    return -1;
 
1701
  if (value >= base)
 
1702
    return -1;
 
1703
  return value;
 
1704
}
 
1705
 
 
1706
#define LL strtoll_return       /* long long or __int64 */
 
1707
 
 
1708
/* These constants assume 64-bit strtoll_return. */
 
1709
 
 
1710
/* A roundabout way of writing 2**63-1 = 9223372036854775807 */
 
1711
#define STRTOLL_OVERFLOW (((LL) 1 << 62) - 1 + ((LL) 1 << 62))
 
1712
/* A roundabout way of writing -2**63 = -9223372036854775808 */
 
1713
#define STRTOLL_UNDERFLOW (-STRTOLL_OVERFLOW - 1)
 
1714
 
 
1715
/* A strtoll replacement for systems that have LFS but don't supply
 
1716
   strtoll.  The headers typedef strtoll_return to long long or to
 
1717
   __int64.  */
 
1718
 
 
1719
strtoll_return
 
1720
strtoll (const char *nptr, char **endptr, int base)
 
1721
{
 
1722
  strtoll_return result = 0;
 
1723
  int negative;
 
1724
 
 
1725
  if (base != 0 && (base < 2 || base > 36))
 
1726
    {
 
1727
      errno = EINVAL;
 
1728
      return 0;
 
1729
    }
 
1730
 
 
1731
  while (*nptr == ' ' || *nptr == '\t')
 
1732
    ++nptr;
 
1733
  if (*nptr == '-')
 
1734
    {
 
1735
      negative = 1;
 
1736
      ++nptr;
 
1737
    }
 
1738
  else if (*nptr == '+')
 
1739
    {
 
1740
      negative = 0;
 
1741
      ++nptr;
 
1742
    }
 
1743
  else
 
1744
    negative = 0;
 
1745
 
 
1746
  /* If base is 0, determine the real base based on the beginning on
 
1747
     the number; octal numbers begin with "0", hexadecimal with "0x",
 
1748
     and the others are considered octal.  */
 
1749
  if (*nptr == '0')
 
1750
    {
 
1751
      if ((base == 0 || base == 16)
 
1752
          &&
 
1753
          (*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
 
1754
        {
 
1755
          base = 16;
 
1756
          nptr += 2;
 
1757
        }
 
1758
      else if (base == 0)
 
1759
        base = 8;
 
1760
    }
 
1761
  else if (base == 0)
 
1762
    base = 10;
 
1763
 
 
1764
  if (!negative)
 
1765
    {
 
1766
      /* Parse positive number, checking for overflow. */
 
1767
      int val;
 
1768
      for (; (val = char_value (*nptr, base)) != -1; ++nptr)
 
1769
        {
 
1770
          strtoll_return newresult = base * result + val;
 
1771
          if (newresult < result)
 
1772
            {
 
1773
              result = STRTOLL_OVERFLOW;
 
1774
              errno = ERANGE;
 
1775
              break;
 
1776
            }
 
1777
          result = newresult;
 
1778
        }
 
1779
    }
 
1780
  else
 
1781
    {
 
1782
      /* Parse negative number, checking for underflow. */
 
1783
      int val;
 
1784
      for (; (val = char_value (*nptr, base)) != -1; ++nptr)
 
1785
        {
 
1786
          strtoll_return newresult = base * result - val;
 
1787
          if (newresult > result)
 
1788
            {
 
1789
              result = STRTOLL_UNDERFLOW;
 
1790
              errno = ERANGE;
 
1791
              break;
 
1792
            }
 
1793
          result = newresult;
 
1794
        }
 
1795
    }
 
1796
  if (endptr)
 
1797
    *endptr = (char *) nptr;
 
1798
  return result;
 
1799
}
 
1800
#endif  /* NEED_STRTOLL */