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