parsedate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. A brief summary of the date string formats this parser groks:
  26. RFC 2616 3.3.1
  27. Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
  28. Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
  29. Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
  30. we support dates without week day name:
  31. 06 Nov 1994 08:49:37 GMT
  32. 06-Nov-94 08:49:37 GMT
  33. Nov 6 08:49:37 1994
  34. without the time zone:
  35. 06 Nov 1994 08:49:37
  36. 06-Nov-94 08:49:37
  37. weird order:
  38. 1994 Nov 6 08:49:37 (GNU date fails)
  39. GMT 08:49:37 06-Nov-94 Sunday
  40. 94 6 Nov 08:49:37 (GNU date fails)
  41. time left out:
  42. 1994 Nov 6
  43. 06-Nov-94
  44. Sun Nov 6 94
  45. unusual separators:
  46. 1994.Nov.6
  47. Sun/Nov/6/94/GMT
  48. commonly used time zone names:
  49. Sun, 06 Nov 1994 08:49:37 CET
  50. 06 Nov 1994 08:49:37 EST
  51. time zones specified using RFC822 style:
  52. Sun, 12 Sep 2004 15:05:58 -0700
  53. Sat, 11 Sep 2004 21:32:11 +0200
  54. compact numerical date strings:
  55. 20040912 15:05:58 -0700
  56. 20040911 +0200
  57. */
  58. #include "curl_setup.h"
  59. #include <limits.h>
  60. #include <curl/curl.h>
  61. #include "strcase.h"
  62. #include "warnless.h"
  63. #include "parsedate.h"
  64. /*
  65. * parsedate()
  66. *
  67. * Returns:
  68. *
  69. * PARSEDATE_OK - a fine conversion
  70. * PARSEDATE_FAIL - failed to convert
  71. * PARSEDATE_LATER - time overflow at the far end of time_t
  72. * PARSEDATE_SOONER - time underflow at the low end of time_t
  73. */
  74. static int parsedate(const char *date, time_t *output);
  75. #define PARSEDATE_OK 0
  76. #define PARSEDATE_FAIL -1
  77. #define PARSEDATE_LATER 1
  78. #define PARSEDATE_SOONER 2
  79. #if !defined(CURL_DISABLE_PARSEDATE) || !defined(CURL_DISABLE_FTP) || \
  80. !defined(CURL_DISABLE_FILE)
  81. /* These names are also used by FTP and FILE code */
  82. const char * const Curl_wkday[] =
  83. {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
  84. const char * const Curl_month[]=
  85. { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  86. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  87. #endif
  88. #ifndef CURL_DISABLE_PARSEDATE
  89. static const char * const weekday[] =
  90. { "Monday", "Tuesday", "Wednesday", "Thursday",
  91. "Friday", "Saturday", "Sunday" };
  92. struct tzinfo {
  93. char name[5];
  94. int offset; /* +/- in minutes */
  95. };
  96. /* Here's a bunch of frequently used time zone names. These were supported
  97. by the old getdate parser. */
  98. #define tDAYZONE -60 /* offset for daylight savings time */
  99. static const struct tzinfo tz[]= {
  100. {"GMT", 0}, /* Greenwich Mean */
  101. {"UT", 0}, /* Universal Time */
  102. {"UTC", 0}, /* Universal (Coordinated) */
  103. {"WET", 0}, /* Western European */
  104. {"BST", 0 tDAYZONE}, /* British Summer */
  105. {"WAT", 60}, /* West Africa */
  106. {"AST", 240}, /* Atlantic Standard */
  107. {"ADT", 240 tDAYZONE}, /* Atlantic Daylight */
  108. {"EST", 300}, /* Eastern Standard */
  109. {"EDT", 300 tDAYZONE}, /* Eastern Daylight */
  110. {"CST", 360}, /* Central Standard */
  111. {"CDT", 360 tDAYZONE}, /* Central Daylight */
  112. {"MST", 420}, /* Mountain Standard */
  113. {"MDT", 420 tDAYZONE}, /* Mountain Daylight */
  114. {"PST", 480}, /* Pacific Standard */
  115. {"PDT", 480 tDAYZONE}, /* Pacific Daylight */
  116. {"YST", 540}, /* Yukon Standard */
  117. {"YDT", 540 tDAYZONE}, /* Yukon Daylight */
  118. {"HST", 600}, /* Hawaii Standard */
  119. {"HDT", 600 tDAYZONE}, /* Hawaii Daylight */
  120. {"CAT", 600}, /* Central Alaska */
  121. {"AHST", 600}, /* Alaska-Hawaii Standard */
  122. {"NT", 660}, /* Nome */
  123. {"IDLW", 720}, /* International Date Line West */
  124. {"CET", -60}, /* Central European */
  125. {"MET", -60}, /* Middle European */
  126. {"MEWT", -60}, /* Middle European Winter */
  127. {"MEST", -60 tDAYZONE}, /* Middle European Summer */
  128. {"CEST", -60 tDAYZONE}, /* Central European Summer */
  129. {"MESZ", -60 tDAYZONE}, /* Middle European Summer */
  130. {"FWT", -60}, /* French Winter */
  131. {"FST", -60 tDAYZONE}, /* French Summer */
  132. {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
  133. {"WAST", -420}, /* West Australian Standard */
  134. {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
  135. {"CCT", -480}, /* China Coast, USSR Zone 7 */
  136. {"JST", -540}, /* Japan Standard, USSR Zone 8 */
  137. {"EAST", -600}, /* Eastern Australian Standard */
  138. {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
  139. {"GST", -600}, /* Guam Standard, USSR Zone 9 */
  140. {"NZT", -720}, /* New Zealand */
  141. {"NZST", -720}, /* New Zealand Standard */
  142. {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
  143. {"IDLE", -720}, /* International Date Line East */
  144. /* Next up: Military timezone names. RFC822 allowed these, but (as noted in
  145. RFC 1123) had their signs wrong. Here we use the correct signs to match
  146. actual military usage.
  147. */
  148. {"A", 1 * 60}, /* Alpha */
  149. {"B", 2 * 60}, /* Bravo */
  150. {"C", 3 * 60}, /* Charlie */
  151. {"D", 4 * 60}, /* Delta */
  152. {"E", 5 * 60}, /* Echo */
  153. {"F", 6 * 60}, /* Foxtrot */
  154. {"G", 7 * 60}, /* Golf */
  155. {"H", 8 * 60}, /* Hotel */
  156. {"I", 9 * 60}, /* India */
  157. /* "J", Juliet is not used as a timezone, to indicate the observer's local
  158. time */
  159. {"K", 10 * 60}, /* Kilo */
  160. {"L", 11 * 60}, /* Lima */
  161. {"M", 12 * 60}, /* Mike */
  162. {"N", -1 * 60}, /* November */
  163. {"O", -2 * 60}, /* Oscar */
  164. {"P", -3 * 60}, /* Papa */
  165. {"Q", -4 * 60}, /* Quebec */
  166. {"R", -5 * 60}, /* Romeo */
  167. {"S", -6 * 60}, /* Sierra */
  168. {"T", -7 * 60}, /* Tango */
  169. {"U", -8 * 60}, /* Uniform */
  170. {"V", -9 * 60}, /* Victor */
  171. {"W", -10 * 60}, /* Whiskey */
  172. {"X", -11 * 60}, /* X-ray */
  173. {"Y", -12 * 60}, /* Yankee */
  174. {"Z", 0}, /* Zulu, zero meridian, a.k.a. UTC */
  175. };
  176. /* returns:
  177. -1 no day
  178. 0 monday - 6 sunday
  179. */
  180. static int checkday(const char *check, size_t len)
  181. {
  182. int i;
  183. const char * const *what;
  184. if(len > 3)
  185. what = &weekday[0];
  186. else if(len == 3)
  187. what = &Curl_wkday[0];
  188. else
  189. return -1; /* too short */
  190. for(i = 0; i<7; i++) {
  191. size_t ilen = strlen(what[0]);
  192. if((ilen == len) &&
  193. strncasecompare(check, what[0], len))
  194. return i;
  195. what++;
  196. }
  197. return -1;
  198. }
  199. static int checkmonth(const char *check, size_t len)
  200. {
  201. int i;
  202. const char * const *what = &Curl_month[0];
  203. if(len != 3)
  204. return -1; /* not a month */
  205. for(i = 0; i<12; i++) {
  206. if(strncasecompare(check, what[0], 3))
  207. return i;
  208. what++;
  209. }
  210. return -1; /* return the offset or -1, no real offset is -1 */
  211. }
  212. /* return the time zone offset between GMT and the input one, in number
  213. of seconds or -1 if the timezone wasn't found/legal */
  214. static int checktz(const char *check, size_t len)
  215. {
  216. unsigned int i;
  217. const struct tzinfo *what = tz;
  218. if(len > 4) /* longer than any valid timezone */
  219. return -1;
  220. for(i = 0; i< sizeof(tz)/sizeof(tz[0]); i++) {
  221. size_t ilen = strlen(what->name);
  222. if((ilen == len) &&
  223. strncasecompare(check, what->name, len))
  224. return what->offset*60;
  225. what++;
  226. }
  227. return -1;
  228. }
  229. static void skip(const char **date)
  230. {
  231. /* skip everything that aren't letters or digits */
  232. while(**date && !ISALNUM(**date))
  233. (*date)++;
  234. }
  235. enum assume {
  236. DATE_MDAY,
  237. DATE_YEAR,
  238. DATE_TIME
  239. };
  240. /*
  241. * time2epoch: time stamp to seconds since epoch in GMT time zone. Similar to
  242. * mktime but for GMT only.
  243. */
  244. static time_t time2epoch(int sec, int min, int hour,
  245. int mday, int mon, int year)
  246. {
  247. static const int month_days_cumulative [12] =
  248. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  249. int leap_days = year - (mon <= 1);
  250. leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
  251. - (1969 / 4) + (1969 / 100) - (1969 / 400));
  252. return ((((time_t) (year - 1970) * 365
  253. + leap_days + month_days_cumulative[mon] + mday - 1) * 24
  254. + hour) * 60 + min) * 60 + sec;
  255. }
  256. /* Returns the value of a single-digit or two-digit decimal number, return
  257. then pointer to after the number. The 'date' pointer is known to point to a
  258. digit. */
  259. static int oneortwodigit(const char *date, const char **endp)
  260. {
  261. int num = date[0] - '0';
  262. if(ISDIGIT(date[1])) {
  263. *endp = &date[2];
  264. return num*10 + (date[1] - '0');
  265. }
  266. *endp = &date[1];
  267. return num;
  268. }
  269. /* HH:MM:SS or HH:MM and accept single-digits too */
  270. static bool match_time(const char *date,
  271. int *h, int *m, int *s, char **endp)
  272. {
  273. const char *p;
  274. int hh, mm, ss = 0;
  275. hh = oneortwodigit(date, &p);
  276. if((hh < 24) && (*p == ':') && ISDIGIT(p[1])) {
  277. mm = oneortwodigit(&p[1], &p);
  278. if(mm < 60) {
  279. if((*p == ':') && ISDIGIT(p[1])) {
  280. ss = oneortwodigit(&p[1], &p);
  281. if(ss <= 60) {
  282. /* valid HH:MM:SS */
  283. goto match;
  284. }
  285. }
  286. else {
  287. /* valid HH:MM */
  288. goto match;
  289. }
  290. }
  291. }
  292. return FALSE; /* not a time string */
  293. match:
  294. *h = hh;
  295. *m = mm;
  296. *s = ss;
  297. *endp = (char *)p;
  298. return TRUE;
  299. }
  300. /*
  301. * parsedate()
  302. *
  303. * Returns:
  304. *
  305. * PARSEDATE_OK - a fine conversion
  306. * PARSEDATE_FAIL - failed to convert
  307. * PARSEDATE_LATER - time overflow at the far end of time_t
  308. * PARSEDATE_SOONER - time underflow at the low end of time_t
  309. */
  310. /* Wednesday is the longest name this parser knows about */
  311. #define NAME_LEN 12
  312. static int parsedate(const char *date, time_t *output)
  313. {
  314. time_t t = 0;
  315. int wdaynum = -1; /* day of the week number, 0-6 (mon-sun) */
  316. int monnum = -1; /* month of the year number, 0-11 */
  317. int mdaynum = -1; /* day of month, 1 - 31 */
  318. int hournum = -1;
  319. int minnum = -1;
  320. int secnum = -1;
  321. int yearnum = -1;
  322. int tzoff = -1;
  323. enum assume dignext = DATE_MDAY;
  324. const char *indate = date; /* save the original pointer */
  325. int part = 0; /* max 6 parts */
  326. while(*date && (part < 6)) {
  327. bool found = FALSE;
  328. skip(&date);
  329. if(ISALPHA(*date)) {
  330. /* a name coming up */
  331. size_t len = 0;
  332. const char *p = date;
  333. while(ISALPHA(*p) && (len < NAME_LEN)) {
  334. p++;
  335. len++;
  336. }
  337. if(len != NAME_LEN) {
  338. if(wdaynum == -1) {
  339. wdaynum = checkday(date, len);
  340. if(wdaynum != -1)
  341. found = TRUE;
  342. }
  343. if(!found && (monnum == -1)) {
  344. monnum = checkmonth(date, len);
  345. if(monnum != -1)
  346. found = TRUE;
  347. }
  348. if(!found && (tzoff == -1)) {
  349. /* this just must be a time zone string */
  350. tzoff = checktz(date, len);
  351. if(tzoff != -1)
  352. found = TRUE;
  353. }
  354. }
  355. if(!found)
  356. return PARSEDATE_FAIL; /* bad string */
  357. date += len;
  358. }
  359. else if(ISDIGIT(*date)) {
  360. /* a digit */
  361. int val;
  362. char *end;
  363. if((secnum == -1) &&
  364. match_time(date, &hournum, &minnum, &secnum, &end)) {
  365. /* time stamp */
  366. date = end;
  367. }
  368. else {
  369. long lval;
  370. int error;
  371. int old_errno;
  372. old_errno = errno;
  373. errno = 0;
  374. lval = strtol(date, &end, 10);
  375. error = errno;
  376. if(errno != old_errno)
  377. errno = old_errno;
  378. if(error)
  379. return PARSEDATE_FAIL;
  380. #if LONG_MAX != INT_MAX
  381. if((lval > (long)INT_MAX) || (lval < (long)INT_MIN))
  382. return PARSEDATE_FAIL;
  383. #endif
  384. val = curlx_sltosi(lval);
  385. if((tzoff == -1) &&
  386. ((end - date) == 4) &&
  387. (val <= 1400) &&
  388. (indate< date) &&
  389. ((date[-1] == '+' || date[-1] == '-'))) {
  390. /* four digits and a value less than or equal to 1400 (to take into
  391. account all sorts of funny time zone diffs) and it is preceded
  392. with a plus or minus. This is a time zone indication. 1400 is
  393. picked since +1300 is frequently used and +1400 is mentioned as
  394. an edge number in the document "ISO C 200X Proposal: Timezone
  395. Functions" at http://david.tribble.com/text/c0xtimezone.html If
  396. anyone has a more authoritative source for the exact maximum time
  397. zone offsets, please speak up! */
  398. found = TRUE;
  399. tzoff = (val/100 * 60 + val%100)*60;
  400. /* the + and - prefix indicates the local time compared to GMT,
  401. this we need their reversed math to get what we want */
  402. tzoff = date[-1]=='+'?-tzoff:tzoff;
  403. }
  404. if(((end - date) == 8) &&
  405. (yearnum == -1) &&
  406. (monnum == -1) &&
  407. (mdaynum == -1)) {
  408. /* 8 digits, no year, month or day yet. This is YYYYMMDD */
  409. found = TRUE;
  410. yearnum = val/10000;
  411. monnum = (val%10000)/100-1; /* month is 0 - 11 */
  412. mdaynum = val%100;
  413. }
  414. if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
  415. if((val > 0) && (val<32)) {
  416. mdaynum = val;
  417. found = TRUE;
  418. }
  419. dignext = DATE_YEAR;
  420. }
  421. if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
  422. yearnum = val;
  423. found = TRUE;
  424. if(yearnum < 100) {
  425. if(yearnum > 70)
  426. yearnum += 1900;
  427. else
  428. yearnum += 2000;
  429. }
  430. if(mdaynum == -1)
  431. dignext = DATE_MDAY;
  432. }
  433. if(!found)
  434. return PARSEDATE_FAIL;
  435. date = end;
  436. }
  437. }
  438. part++;
  439. }
  440. if(-1 == secnum)
  441. secnum = minnum = hournum = 0; /* no time, make it zero */
  442. if((-1 == mdaynum) ||
  443. (-1 == monnum) ||
  444. (-1 == yearnum))
  445. /* lacks vital info, fail */
  446. return PARSEDATE_FAIL;
  447. #ifdef HAVE_TIME_T_UNSIGNED
  448. if(yearnum < 1970) {
  449. /* only positive numbers cannot return earlier */
  450. *output = TIME_T_MIN;
  451. return PARSEDATE_SOONER;
  452. }
  453. #endif
  454. #if (SIZEOF_TIME_T < 5)
  455. #ifdef HAVE_TIME_T_UNSIGNED
  456. /* an unsigned 32 bit time_t can only hold dates to 2106 */
  457. if(yearnum > 2105) {
  458. *output = TIME_T_MAX;
  459. return PARSEDATE_LATER;
  460. }
  461. #else
  462. /* a signed 32 bit time_t can only hold dates to the beginning of 2038 */
  463. if(yearnum > 2037) {
  464. *output = TIME_T_MAX;
  465. return PARSEDATE_LATER;
  466. }
  467. if(yearnum < 1903) {
  468. *output = TIME_T_MIN;
  469. return PARSEDATE_SOONER;
  470. }
  471. #endif
  472. #else
  473. /* The Gregorian calendar was introduced 1582 */
  474. if(yearnum < 1583)
  475. return PARSEDATE_FAIL;
  476. #endif
  477. if((mdaynum > 31) || (monnum > 11) ||
  478. (hournum > 23) || (minnum > 59) || (secnum > 60))
  479. return PARSEDATE_FAIL; /* clearly an illegal date */
  480. /* time2epoch() returns a time_t. time_t is often 32 bits, sometimes even on
  481. architectures that feature 64 bit 'long' but ultimately time_t is the
  482. correct data type to use.
  483. */
  484. t = time2epoch(secnum, minnum, hournum, mdaynum, monnum, yearnum);
  485. /* Add the time zone diff between local time zone and GMT. */
  486. if(tzoff == -1)
  487. tzoff = 0;
  488. if((tzoff > 0) && (t > TIME_T_MAX - tzoff)) {
  489. *output = TIME_T_MAX;
  490. return PARSEDATE_LATER; /* time_t overflow */
  491. }
  492. t += tzoff;
  493. *output = t;
  494. return PARSEDATE_OK;
  495. }
  496. #else
  497. /* disabled */
  498. static int parsedate(const char *date, time_t *output)
  499. {
  500. (void)date;
  501. *output = 0;
  502. return PARSEDATE_OK; /* a lie */
  503. }
  504. #endif
  505. time_t curl_getdate(const char *p, const time_t *now)
  506. {
  507. time_t parsed = -1;
  508. int rc = parsedate(p, &parsed);
  509. (void)now; /* legacy argument from the past that we ignore */
  510. if(rc == PARSEDATE_OK) {
  511. if(parsed == -1)
  512. /* avoid returning -1 for a working scenario */
  513. parsed++;
  514. return parsed;
  515. }
  516. /* everything else is fail */
  517. return -1;
  518. }
  519. /* Curl_getdate_capped() differs from curl_getdate() in that this will return
  520. TIME_T_MAX in case the parsed time value was too big, instead of an
  521. error. */
  522. time_t Curl_getdate_capped(const char *p)
  523. {
  524. time_t parsed = -1;
  525. int rc = parsedate(p, &parsed);
  526. switch(rc) {
  527. case PARSEDATE_OK:
  528. if(parsed == -1)
  529. /* avoid returning -1 for a working scenario */
  530. parsed++;
  531. return parsed;
  532. case PARSEDATE_LATER:
  533. /* this returns the maximum time value */
  534. return parsed;
  535. default:
  536. return -1; /* everything else is fail */
  537. }
  538. /* UNREACHABLE */
  539. }
  540. /*
  541. * Curl_gmtime() is a gmtime() replacement for portability. Do not use the
  542. * gmtime_r() or gmtime() functions anywhere else but here.
  543. *
  544. */
  545. CURLcode Curl_gmtime(time_t intime, struct tm *store)
  546. {
  547. const struct tm *tm;
  548. #ifdef HAVE_GMTIME_R
  549. /* thread-safe version */
  550. tm = (struct tm *)gmtime_r(&intime, store);
  551. #else
  552. /* !checksrc! disable BANNEDFUNC 1 */
  553. tm = gmtime(&intime);
  554. if(tm)
  555. *store = *tm; /* copy the pointed struct to the local copy */
  556. #endif
  557. if(!tm)
  558. return CURLE_BAD_FUNCTION_ARGUMENT;
  559. return CURLE_OK;
  560. }