123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- #include "curl_setup.h"
- #include <limits.h>
- #include <curl/curl.h>
- #include "strcase.h"
- #include "warnless.h"
- #include "parsedate.h"
- static int parsedate(const char *date, time_t *output);
- #define PARSEDATE_OK 0
- #define PARSEDATE_FAIL -1
- #define PARSEDATE_LATER 1
- #define PARSEDATE_SOONER 2
- #if !defined(CURL_DISABLE_PARSEDATE) || !defined(CURL_DISABLE_FTP) || \
- !defined(CURL_DISABLE_FILE)
- const char * const Curl_wkday[] =
- {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
- const char * const Curl_month[]=
- { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
- #endif
- #ifndef CURL_DISABLE_PARSEDATE
- static const char * const weekday[] =
- { "Monday", "Tuesday", "Wednesday", "Thursday",
- "Friday", "Saturday", "Sunday" };
- struct tzinfo {
- char name[5];
- int offset;
- };
- #define tDAYZONE -60
- static const struct tzinfo tz[]= {
- {"GMT", 0},
- {"UT", 0},
- {"UTC", 0},
- {"WET", 0},
- {"BST", 0 tDAYZONE},
- {"WAT", 60},
- {"AST", 240},
- {"ADT", 240 tDAYZONE},
- {"EST", 300},
- {"EDT", 300 tDAYZONE},
- {"CST", 360},
- {"CDT", 360 tDAYZONE},
- {"MST", 420},
- {"MDT", 420 tDAYZONE},
- {"PST", 480},
- {"PDT", 480 tDAYZONE},
- {"YST", 540},
- {"YDT", 540 tDAYZONE},
- {"HST", 600},
- {"HDT", 600 tDAYZONE},
- {"CAT", 600},
- {"AHST", 600},
- {"NT", 660},
- {"IDLW", 720},
- {"CET", -60},
- {"MET", -60},
- {"MEWT", -60},
- {"MEST", -60 tDAYZONE},
- {"CEST", -60 tDAYZONE},
- {"MESZ", -60 tDAYZONE},
- {"FWT", -60},
- {"FST", -60 tDAYZONE},
- {"EET", -120},
- {"WAST", -420},
- {"WADT", -420 tDAYZONE},
- {"CCT", -480},
- {"JST", -540},
- {"EAST", -600},
- {"EADT", -600 tDAYZONE},
- {"GST", -600},
- {"NZT", -720},
- {"NZST", -720},
- {"NZDT", -720 tDAYZONE},
- {"IDLE", -720},
-
- {"A", 1 * 60},
- {"B", 2 * 60},
- {"C", 3 * 60},
- {"D", 4 * 60},
- {"E", 5 * 60},
- {"F", 6 * 60},
- {"G", 7 * 60},
- {"H", 8 * 60},
- {"I", 9 * 60},
-
- {"K", 10 * 60},
- {"L", 11 * 60},
- {"M", 12 * 60},
- {"N", -1 * 60},
- {"O", -2 * 60},
- {"P", -3 * 60},
- {"Q", -4 * 60},
- {"R", -5 * 60},
- {"S", -6 * 60},
- {"T", -7 * 60},
- {"U", -8 * 60},
- {"V", -9 * 60},
- {"W", -10 * 60},
- {"X", -11 * 60},
- {"Y", -12 * 60},
- {"Z", 0},
- };
- static int checkday(const char *check, size_t len)
- {
- int i;
- const char * const *what;
- if(len > 3)
- what = &weekday[0];
- else if(len == 3)
- what = &Curl_wkday[0];
- else
- return -1;
- for(i = 0; i < 7; i++) {
- size_t ilen = strlen(what[0]);
- if((ilen == len) &&
- strncasecompare(check, what[0], len))
- return i;
- what++;
- }
- return -1;
- }
- static int checkmonth(const char *check, size_t len)
- {
- int i;
- const char * const *what = &Curl_month[0];
- if(len != 3)
- return -1;
- for(i = 0; i < 12; i++) {
- if(strncasecompare(check, what[0], 3))
- return i;
- what++;
- }
- return -1;
- }
- static int checktz(const char *check, size_t len)
- {
- unsigned int i;
- const struct tzinfo *what = tz;
- if(len > 4)
- return -1;
- for(i = 0; i < sizeof(tz)/sizeof(tz[0]); i++) {
- size_t ilen = strlen(what->name);
- if((ilen == len) &&
- strncasecompare(check, what->name, len))
- return what->offset*60;
- what++;
- }
- return -1;
- }
- static void skip(const char **date)
- {
-
- while(**date && !ISALNUM(**date))
- (*date)++;
- }
- enum assume {
- DATE_MDAY,
- DATE_YEAR,
- DATE_TIME
- };
- static time_t time2epoch(int sec, int min, int hour,
- int mday, int mon, int year)
- {
- static const int month_days_cumulative [12] =
- { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
- int leap_days = year - (mon <= 1);
- leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
- - (1969 / 4) + (1969 / 100) - (1969 / 400));
- return ((((time_t) (year - 1970) * 365
- + leap_days + month_days_cumulative[mon] + mday - 1) * 24
- + hour) * 60 + min) * 60 + sec;
- }
- static int oneortwodigit(const char *date, const char **endp)
- {
- int num = date[0] - '0';
- if(ISDIGIT(date[1])) {
- *endp = &date[2];
- return num*10 + (date[1] - '0');
- }
- *endp = &date[1];
- return num;
- }
- static bool match_time(const char *date,
- int *h, int *m, int *s, char **endp)
- {
- const char *p;
- int hh, mm, ss = 0;
- hh = oneortwodigit(date, &p);
- if((hh < 24) && (*p == ':') && ISDIGIT(p[1])) {
- mm = oneortwodigit(&p[1], &p);
- if(mm < 60) {
- if((*p == ':') && ISDIGIT(p[1])) {
- ss = oneortwodigit(&p[1], &p);
- if(ss <= 60) {
-
- goto match;
- }
- }
- else {
-
- goto match;
- }
- }
- }
- return FALSE;
- match:
- *h = hh;
- *m = mm;
- *s = ss;
- *endp = (char *)p;
- return TRUE;
- }
- #define NAME_LEN 12
- static int parsedate(const char *date, time_t *output)
- {
- time_t t = 0;
- int wdaynum = -1;
- int monnum = -1;
- int mdaynum = -1;
- int hournum = -1;
- int minnum = -1;
- int secnum = -1;
- int yearnum = -1;
- int tzoff = -1;
- enum assume dignext = DATE_MDAY;
- const char *indate = date;
- int part = 0;
- while(*date && (part < 6)) {
- bool found = FALSE;
- skip(&date);
- if(ISALPHA(*date)) {
-
- size_t len = 0;
- const char *p = date;
- while(ISALPHA(*p) && (len < NAME_LEN)) {
- p++;
- len++;
- }
- if(len != NAME_LEN) {
- if(wdaynum == -1) {
- wdaynum = checkday(date, len);
- if(wdaynum != -1)
- found = TRUE;
- }
- if(!found && (monnum == -1)) {
- monnum = checkmonth(date, len);
- if(monnum != -1)
- found = TRUE;
- }
- if(!found && (tzoff == -1)) {
-
- tzoff = checktz(date, len);
- if(tzoff != -1)
- found = TRUE;
- }
- }
- if(!found)
- return PARSEDATE_FAIL;
- date += len;
- }
- else if(ISDIGIT(*date)) {
-
- int val;
- char *end;
- if((secnum == -1) &&
- match_time(date, &hournum, &minnum, &secnum, &end)) {
-
- date = end;
- }
- else {
- long lval;
- int error;
- int old_errno;
- old_errno = errno;
- errno = 0;
- lval = strtol(date, &end, 10);
- error = errno;
- if(errno != old_errno)
- errno = old_errno;
- if(error)
- return PARSEDATE_FAIL;
- #if LONG_MAX != INT_MAX
- if((lval > (long)INT_MAX) || (lval < (long)INT_MIN))
- return PARSEDATE_FAIL;
- #endif
- val = curlx_sltosi(lval);
- if((tzoff == -1) &&
- ((end - date) == 4) &&
- (val <= 1400) &&
- (indate < date) &&
- ((date[-1] == '+' || date[-1] == '-'))) {
-
- found = TRUE;
- tzoff = (val/100 * 60 + val%100)*60;
-
- tzoff = date[-1]=='+' ? -tzoff : tzoff;
- }
- if(((end - date) == 8) &&
- (yearnum == -1) &&
- (monnum == -1) &&
- (mdaynum == -1)) {
-
- found = TRUE;
- yearnum = val/10000;
- monnum = (val%10000)/100-1;
- mdaynum = val%100;
- }
- if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
- if((val > 0) && (val < 32)) {
- mdaynum = val;
- found = TRUE;
- }
- dignext = DATE_YEAR;
- }
- if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
- yearnum = val;
- found = TRUE;
- if(yearnum < 100) {
- if(yearnum > 70)
- yearnum += 1900;
- else
- yearnum += 2000;
- }
- if(mdaynum == -1)
- dignext = DATE_MDAY;
- }
- if(!found)
- return PARSEDATE_FAIL;
- date = end;
- }
- }
- part++;
- }
- if(-1 == secnum)
- secnum = minnum = hournum = 0;
- if((-1 == mdaynum) ||
- (-1 == monnum) ||
- (-1 == yearnum))
-
- return PARSEDATE_FAIL;
- #ifdef HAVE_TIME_T_UNSIGNED
- if(yearnum < 1970) {
-
- *output = TIME_T_MIN;
- return PARSEDATE_SOONER;
- }
- #endif
- #if (SIZEOF_TIME_T < 5)
- #ifdef HAVE_TIME_T_UNSIGNED
-
- if(yearnum > 2105) {
- *output = TIME_T_MAX;
- return PARSEDATE_LATER;
- }
- #else
-
- if(yearnum > 2037) {
- *output = TIME_T_MAX;
- return PARSEDATE_LATER;
- }
- if(yearnum < 1903) {
- *output = TIME_T_MIN;
- return PARSEDATE_SOONER;
- }
- #endif
- #else
-
- if(yearnum < 1583)
- return PARSEDATE_FAIL;
- #endif
- if((mdaynum > 31) || (monnum > 11) ||
- (hournum > 23) || (minnum > 59) || (secnum > 60))
- return PARSEDATE_FAIL;
-
- t = time2epoch(secnum, minnum, hournum, mdaynum, monnum, yearnum);
-
- if(tzoff == -1)
- tzoff = 0;
- if((tzoff > 0) && (t > (time_t)(TIME_T_MAX - tzoff))) {
- *output = TIME_T_MAX;
- return PARSEDATE_LATER;
- }
- t += tzoff;
- *output = t;
- return PARSEDATE_OK;
- }
- #else
- static int parsedate(const char *date, time_t *output)
- {
- (void)date;
- *output = 0;
- return PARSEDATE_OK;
- }
- #endif
- time_t curl_getdate(const char *p, const time_t *now)
- {
- time_t parsed = -1;
- int rc = parsedate(p, &parsed);
- (void)now;
- if(rc == PARSEDATE_OK) {
- if(parsed == (time_t)-1)
-
- parsed++;
- return parsed;
- }
-
- return -1;
- }
- time_t Curl_getdate_capped(const char *p)
- {
- time_t parsed = -1;
- int rc = parsedate(p, &parsed);
- switch(rc) {
- case PARSEDATE_OK:
- if(parsed == (time_t)-1)
-
- parsed++;
- return parsed;
- case PARSEDATE_LATER:
-
- return parsed;
- default:
- return -1;
- }
-
- }
- CURLcode Curl_gmtime(time_t intime, struct tm *store)
- {
- const struct tm *tm;
- #ifdef HAVE_GMTIME_R
-
- tm = (struct tm *)gmtime_r(&intime, store);
- #else
-
- tm = gmtime(&intime);
- if(tm)
- *store = *tm;
- #endif
- if(!tm)
- return CURLE_BAD_FUNCTION_ARGUMENT;
- return CURLE_OK;
- }
|