parsedate.c 17 KB

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