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