/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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "parsedate.h" |
27 | | #include "curlx/strparse.h" |
28 | | #include "curlx/strcopy.h" |
29 | | |
30 | | /* A brief summary of the date string formats this parser groks: |
31 | | |
32 | | RFC 2616 3.3.1 |
33 | | |
34 | | Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 |
35 | | Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 |
36 | | Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format |
37 | | |
38 | | we support dates without week day name: |
39 | | |
40 | | 06 Nov 1994 08:49:37 GMT |
41 | | 06-Nov-94 08:49:37 GMT |
42 | | Nov 6 08:49:37 1994 |
43 | | |
44 | | without the time zone: |
45 | | |
46 | | 06 Nov 1994 08:49:37 |
47 | | 06-Nov-94 08:49:37 |
48 | | |
49 | | weird order: |
50 | | |
51 | | 1994 Nov 6 08:49:37 (GNU date fails) |
52 | | GMT 08:49:37 06-Nov-94 Sunday |
53 | | 94 6 Nov 08:49:37 (GNU date fails) |
54 | | |
55 | | time left out: |
56 | | |
57 | | 1994 Nov 6 |
58 | | 06-Nov-94 |
59 | | Sun Nov 6 94 |
60 | | |
61 | | unusual separators: |
62 | | |
63 | | 1994.Nov.6 |
64 | | Sun/Nov/6/94/GMT |
65 | | |
66 | | commonly used time zone names: |
67 | | |
68 | | Sun, 06 Nov 1994 08:49:37 CET |
69 | | 06 Nov 1994 08:49:37 EST |
70 | | |
71 | | time zones specified using RFC822 style: |
72 | | |
73 | | Sun, 12 Sep 2004 15:05:58 -0700 |
74 | | Sat, 11 Sep 2004 21:32:11 +0200 |
75 | | |
76 | | compact numerical date strings: |
77 | | |
78 | | 20040912 15:05:58 -0700 |
79 | | 20040911 +0200 |
80 | | */ |
81 | | |
82 | | #if !defined(CURL_DISABLE_PARSEDATE) || !defined(CURL_DISABLE_FTP) || \ |
83 | | !defined(CURL_DISABLE_FILE) || defined(USE_GNUTLS) |
84 | | /* These names are also used by FTP and FILE code */ |
85 | | const char * const Curl_wkday[] = { |
86 | | "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" |
87 | | }; |
88 | | const char * const Curl_month[] = { |
89 | | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
90 | | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
91 | | }; |
92 | | #endif |
93 | | |
94 | 0 | #define PARSEDATE_OK 0 |
95 | 0 | #define PARSEDATE_FAIL (-1) |
96 | | |
97 | | #ifndef CURL_DISABLE_PARSEDATE |
98 | | |
99 | | #if SIZEOF_TIME_T < 5 |
100 | | #define PARSEDATE_LATER 1 |
101 | | #endif |
102 | | #if SIZEOF_TIME_T < 5 || defined(HAVE_TIME_T_UNSIGNED) |
103 | | #define PARSEDATE_SOONER 2 |
104 | | #endif |
105 | | |
106 | 0 | #define DST (-60) /* offset for daylight savings time */ |
107 | | |
108 | | /* two-letter time zones |
109 | | |
110 | | 11 * 60, NT - Nome spellchecker:disable-line |
111 | | 0, UT - Universal Time |
112 | | |
113 | | */ |
114 | | |
115 | 0 | #define MKTZ(a,b,c,d) (((a)<<24) | ((b)<<16) | ((c)<<8) | (d)) |
116 | | |
117 | 0 | #define LOWERCASE(x) ((x) | 0x20) |
118 | | |
119 | | /* Pack three lowercase characters into a uint32_t for O(1) name lookups */ |
120 | | #define MKNAME3(a,b,c) \ |
121 | 0 | ((uint32_t)(LOWERCASE(a) << 16) | (uint32_t)(LOWERCASE(b) << 8) | \ |
122 | 0 | (uint32_t)LOWERCASE(c)) |
123 | | |
124 | | /* returns: |
125 | | -1 no day |
126 | | 0 monday - 6 sunday |
127 | | |
128 | | @unittest 4781 |
129 | | */ |
130 | | UNITTEST int checkday(const char *check, size_t len); |
131 | | UNITTEST int checkday(const char *check, size_t len) |
132 | 0 | { |
133 | 0 | int day; |
134 | 0 | if(len < 3) |
135 | 0 | return -1; /* too short */ |
136 | | |
137 | 0 | switch(MKNAME3(check[0], check[1], check[2])) { |
138 | 0 | case MKNAME3('m','o','n'): |
139 | 0 | day = 0; |
140 | 0 | break; |
141 | 0 | case MKNAME3('t','u','e'): |
142 | 0 | day = 1; |
143 | 0 | break; |
144 | 0 | case MKNAME3('w','e','d'): |
145 | 0 | day = 2; |
146 | 0 | break; |
147 | 0 | case MKNAME3('t','h','u'): |
148 | 0 | day = 3; |
149 | 0 | break; |
150 | 0 | case MKNAME3('f','r','i'): |
151 | 0 | day = 4; |
152 | 0 | break; |
153 | 0 | case MKNAME3('s','a','t'): |
154 | 0 | day = 5; |
155 | 0 | break; |
156 | 0 | case MKNAME3('s','u','n'): |
157 | 0 | day = 6; |
158 | 0 | break; |
159 | 0 | default: |
160 | 0 | return -1; |
161 | 0 | } |
162 | | |
163 | 0 | if(len > 3) { |
164 | | /* when more than three letters are provided, verify the full name |
165 | | case-insensitively */ |
166 | 0 | static const struct { |
167 | 0 | const char *suffix; /* chars minus the first three */ |
168 | 0 | size_t slen; |
169 | 0 | } daysuffix[] = { |
170 | 0 | /* 0 Mon */ { "day", 3 }, |
171 | 0 | /* 1 Tue */ { "sday", 4 }, |
172 | 0 | /* 2 Wed */ { "nesday", 6 }, |
173 | 0 | /* 3 Thu */ { "rsday", 5 }, |
174 | 0 | /* 4 Fri */ { "day", 3 }, |
175 | 0 | /* 5 Sat */ { "urday", 5 }, |
176 | 0 | /* 6 Sun */ { "day", 3 }, |
177 | 0 | }; |
178 | 0 | size_t slen = daysuffix[day].slen; |
179 | 0 | const char *suffix = daysuffix[day].suffix; |
180 | 0 | size_t i; |
181 | 0 | if((len - 3) != slen) |
182 | 0 | return -1; |
183 | 0 | for(i = 0; i < slen; i++) { |
184 | 0 | if(LOWERCASE(check[3 + i]) != suffix[i]) |
185 | 0 | return -1; |
186 | 0 | } |
187 | 0 | } |
188 | 0 | return day; |
189 | 0 | } |
190 | | |
191 | | /* @unittest 4781 */ |
192 | | |
193 | | UNITTEST int checkmonth(const char *check, size_t len); |
194 | | UNITTEST int checkmonth(const char *check, size_t len) |
195 | 0 | { |
196 | 0 | if(len != 3) |
197 | 0 | return -1; /* not a month */ |
198 | | |
199 | 0 | switch(MKNAME3(check[0], check[1], check[2])) { |
200 | 0 | case MKNAME3('j','a','n'): |
201 | 0 | return 0; |
202 | 0 | case MKNAME3('f','e','b'): |
203 | 0 | return 1; |
204 | 0 | case MKNAME3('m','a','r'): |
205 | 0 | return 2; |
206 | 0 | case MKNAME3('a','p','r'): |
207 | 0 | return 3; |
208 | 0 | case MKNAME3('m','a','y'): |
209 | 0 | return 4; |
210 | 0 | case MKNAME3('j','u','n'): |
211 | 0 | return 5; |
212 | 0 | case MKNAME3('j','u','l'): |
213 | 0 | return 6; |
214 | 0 | case MKNAME3('a','u','g'): |
215 | 0 | return 7; |
216 | 0 | case MKNAME3('s','e','p'): |
217 | 0 | return 8; |
218 | 0 | case MKNAME3('o','c','t'): |
219 | 0 | return 9; |
220 | 0 | case MKNAME3('n','o','v'): |
221 | 0 | return 10; |
222 | 0 | case MKNAME3('d','e','c'): |
223 | 0 | return 11; |
224 | 0 | } |
225 | 0 | return -1; /* return the offset or -1, no real offset is -1 */ |
226 | 0 | } |
227 | | |
228 | | /* return the time zone offset between GMT and the input one, in number of |
229 | | seconds or -1 if the timezone was not found/legal |
230 | | |
231 | | @unittest 4781 |
232 | | */ |
233 | | UNITTEST int checktz(const char *check, size_t len); |
234 | | UNITTEST int checktz(const char *check, size_t len) |
235 | 0 | { |
236 | | /* alpha-sorted list of single-letter time zones, A - Z */ |
237 | 0 | static const int16_t tzone[] = { |
238 | 0 | -1 * 60, /* Alpha */ |
239 | 0 | -2 * 60, /* Bravo */ |
240 | 0 | -3 * 60, /* Charlie */ |
241 | 0 | -4 * 60, /* Delta */ |
242 | 0 | -5 * 60, /* Echo */ |
243 | 0 | -6 * 60, /* Foxtrot */ |
244 | 0 | -7 * 60, /* Golf */ |
245 | 0 | -8 * 60, /* Hotel */ |
246 | 0 | -9 * 60, /* India */ |
247 | 0 | -1, /* J - not a timezone */ |
248 | 0 | -10 * 60, /* Kilo */ |
249 | 0 | -11 * 60, /* Lima */ |
250 | 0 | -12 * 60, /* Mike */ |
251 | 0 | 60, /* November */ |
252 | 0 | 2 * 60, /* Oscar */ |
253 | 0 | 3 * 60, /* Papa */ |
254 | 0 | 4 * 60, /* Quebec */ |
255 | 0 | 5 * 60, /* Romeo */ |
256 | 0 | 6 * 60, /* Sierra */ |
257 | 0 | 7 * 60, /* Tango */ |
258 | 0 | 8 * 60, /* Uniform */ |
259 | 0 | 9 * 60, /* Victor */ |
260 | 0 | 10 * 60, /* Whiskey */ |
261 | 0 | 11 * 60, /* X-ray */ |
262 | 0 | 12 * 60, /* Yankee */ |
263 | 0 | 0 /* Zulu, zero meridian, a.k.a. UTC */ |
264 | 0 | }; |
265 | 0 | char first; |
266 | 0 | if(len > 4) |
267 | 0 | return -1; |
268 | 0 | first = check[0]; |
269 | 0 | if(len == 1) { |
270 | | /* short-cut single letter names */ |
271 | 0 | if((first > 'Z') || (first == 'J')) |
272 | 0 | return -1; /* no such tz */ |
273 | 0 | return tzone[first - 'A'] * 60; |
274 | 0 | } |
275 | 0 | else if(len == 2) { |
276 | | /* two-letter names */ |
277 | 0 | if(check[1] == 'T') { |
278 | 0 | if(first == 'N') |
279 | 0 | return 11 * 60 * 60; |
280 | 0 | else if(first == 'U') |
281 | 0 | return 0; |
282 | 0 | } |
283 | 0 | return -1; /* nope */ |
284 | 0 | } |
285 | 0 | else if(len == 3) { |
286 | 0 | uint32_t key; |
287 | 0 | if(first > 'Y') |
288 | 0 | return -1; |
289 | 0 | key = MKTZ(first, check[1], check[2], 0); |
290 | 0 | switch(key) { |
291 | 0 | case MKTZ('A','D','T','\0'): |
292 | 0 | return (240 + DST) * 60; /* Atlantic Daylight */ |
293 | 0 | case MKTZ('A','S','T','\0'): |
294 | 0 | return 240 * 60; /* Atlantic Standard */ |
295 | 0 | case MKTZ('B','S','T','\0'): |
296 | 0 | return (0 + DST) * 60; /* British Summer */ |
297 | 0 | case MKTZ('C','A','T','\0'): |
298 | 0 | return 600 * 60; /* Central Alaska */ |
299 | 0 | case MKTZ('C','C','T','\0'): |
300 | 0 | return -480 * 60; /* China Coast, USSR Zone 7 */ |
301 | 0 | case MKTZ('C','D','T','\0'): |
302 | 0 | return (360 + DST) * 60; /* Central Daylight */ |
303 | 0 | case MKTZ('C','E','T','\0'): |
304 | 0 | return -60 * 60; /* Central European */ |
305 | 0 | case MKTZ('C','S','T','\0'): |
306 | 0 | return 360 * 60; /* Central Standard */ |
307 | 0 | case MKTZ('E','D','T','\0'): |
308 | 0 | return (300 + DST) * 60; /* Eastern Daylight */ |
309 | 0 | case MKTZ('E','E','T','\0'): |
310 | 0 | return -120 * 60; /* Eastern Europe, USSR Zone 1 */ |
311 | 0 | case MKTZ('E','S','T','\0'): |
312 | 0 | return 300 * 60; /* Eastern Standard */ |
313 | 0 | case MKTZ('F','S','T','\0'): |
314 | 0 | return (-60 + DST) * 60; /* French Summer */ |
315 | 0 | case MKTZ('F','W','T','\0'): |
316 | 0 | return -60 * 60; /* French Winter */ |
317 | 0 | case MKTZ('G','M','T','\0'): |
318 | 0 | return 0; /* Greenwich Mean */ |
319 | 0 | case MKTZ('G','S','T','\0'): |
320 | 0 | return -600 * 60; /* Guam Standard, USSR Zone 9 */ |
321 | 0 | case MKTZ('H','D','T','\0'): |
322 | 0 | return (600 + DST) * 60; /* Hawaii Daylight */ |
323 | 0 | case MKTZ('H','S','T','\0'): |
324 | 0 | return 600 * 60; /* Hawaii Standard */ |
325 | 0 | case MKTZ('J','S','T','\0'): |
326 | 0 | return -540 * 60; /* Japan Standard, USSR Zone 8 */ |
327 | 0 | case MKTZ('M','D','T','\0'): |
328 | 0 | return (420 + DST) * 60; /* Mountain Daylight */ |
329 | 0 | case MKTZ('M','E','T','\0'): |
330 | 0 | return -60 * 60; /* Middle European */ |
331 | 0 | case MKTZ('M','S','T','\0'): |
332 | 0 | return 420 * 60; /* Mountain Standard */ |
333 | 0 | case MKTZ('N','Z','T','\0'): |
334 | 0 | return -720 * 60; /* New Zealand */ |
335 | 0 | case MKTZ('P','D','T','\0'): |
336 | 0 | return (480 + DST) * 60; /* Pacific Daylight */ |
337 | 0 | case MKTZ('P','S','T','\0'): |
338 | 0 | return 480 * 60; /* Pacific Standard */ |
339 | 0 | case MKTZ('U','T','C','\0'): |
340 | 0 | return 0; /* Universal (Coordinated) */ |
341 | 0 | case MKTZ('W','A','T','\0'): |
342 | 0 | return 60 * 60; /* West Africa */ |
343 | 0 | case MKTZ('W','E','T','\0'): |
344 | 0 | return 0; /* Western European */ |
345 | 0 | case MKTZ('Y','D','T','\0'): |
346 | 0 | return (540 + DST) * 60; /* Yukon Daylight */ |
347 | 0 | case MKTZ('Y','S','T','\0'): |
348 | 0 | return 540 * 60; /* Yukon Standard */ |
349 | 0 | } |
350 | 0 | } |
351 | 0 | else { |
352 | 0 | uint32_t key; |
353 | 0 | if(first > 'W') |
354 | 0 | return -1; |
355 | 0 | key = MKTZ(first, check[1], check[2], check[3]); |
356 | 0 | switch(key) { |
357 | 0 | case MKTZ('A','H','S','T'): |
358 | 0 | return 600 * 60; /* Alaska-Hawaii Standard */ |
359 | 0 | case MKTZ('C','E','S','T'): |
360 | 0 | return (-60 + DST) * 60; /* Central European Summer */ |
361 | 0 | case MKTZ('E','A','D','T'): |
362 | 0 | return (-600 + DST) * 60; /* Eastern Australian Daylight */ |
363 | 0 | case MKTZ('E','A','S','T'): |
364 | 0 | return -600 * 60; /* Eastern Australian Standard */ |
365 | 0 | case MKTZ('I','D','L','E'): |
366 | 0 | return -720 * 60; /* International Date Line East */ |
367 | 0 | case MKTZ('I','D','L','W'): |
368 | 0 | return 720 * 60; /* International Date Line West */ |
369 | 0 | case MKTZ('M','E','S','T'): |
370 | 0 | return (-60 + DST) * 60; /* Middle European Summer */ |
371 | 0 | case MKTZ('M','E','S','Z'): |
372 | 0 | return (-60 + DST) * 60; /* Middle European Summer */ |
373 | 0 | case MKTZ('M','E','W','T'): |
374 | 0 | return -60 * 60; /* Middle European Winter */ |
375 | 0 | case MKTZ('N','Z','D','T'): |
376 | 0 | return (-720 + DST) * 60; /* New Zealand Daylight */ |
377 | 0 | case MKTZ('N','Z','S','T'): |
378 | 0 | return -720 * 60; /* New Zealand Standard */ |
379 | 0 | case MKTZ('W','A','D','T'): |
380 | 0 | return (-420 + DST) * 60; /* West Australian Daylight */ |
381 | 0 | case MKTZ('W','A','S','T'): /* spellchecker:disable-line */ |
382 | 0 | return -420 * 60; /* West Australian Standard */ |
383 | 0 | } |
384 | 0 | } |
385 | 0 | return -1; |
386 | 0 | } |
387 | | |
388 | | /* each field is exactly -1 when unknown */ |
389 | | struct when { |
390 | | int wday; /* day of the week, 0-6 (mon-sun) */ |
391 | | int mon; /* month of the year, 0-11 */ |
392 | | int mday; /* day of month, 1 - 31 */ |
393 | | int dsecs; /* number of seconds into the day */ |
394 | | int year; /* year, >= 1583 */ |
395 | | int tzoff; /* time zone offset in seconds */ |
396 | | }; |
397 | | |
398 | | /* (1969 / 4) - (1969 / 100) + (1969 / 400) = 492 - 19 + 4 = 477 */ |
399 | 0 | #define LEAP_DAYS_BEFORE_1969 477 |
400 | 0 | #define SECONDS_PER_DAY 86400 |
401 | | |
402 | | /* |
403 | | * time2epoch: time stamp to seconds since epoch in GMT time zone. Similar to |
404 | | * mktime but for GMT only. |
405 | | */ |
406 | | static curl_off_t time2epoch(struct when *w) |
407 | 0 | { |
408 | 0 | static const int cumulative_days[12] = { |
409 | 0 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
410 | 0 | }; |
411 | 0 | int y = w->year - (w->mon <= 1); |
412 | 0 | int leap_days = (y / 4) - (y / 100) + (y / 400) - LEAP_DAYS_BEFORE_1969; |
413 | 0 | curl_off_t days = (curl_off_t)(w->year - 1970) * 365 + leap_days + |
414 | 0 | cumulative_days[w->mon] + w->mday - 1; |
415 | |
|
416 | 0 | return (days * SECONDS_PER_DAY) + w->dsecs; |
417 | 0 | } |
418 | | |
419 | | /* HH:MM:SS or HH:MM and accept single-digits too */ |
420 | | static bool match_time(const char *date, struct when *w, char **endp) |
421 | 0 | { |
422 | 0 | const unsigned char *p = (const unsigned char *)date; |
423 | 0 | unsigned int hh, mm, ss = 0; |
424 | |
|
425 | 0 | if(!ISDIGIT(*p)) |
426 | 0 | return FALSE; |
427 | | |
428 | 0 | hh = *p++ - '0'; |
429 | 0 | if(ISDIGIT(*p)) |
430 | 0 | hh = hh * 10 + (*p++ - '0'); |
431 | |
|
432 | 0 | if((hh >= 24) || (*p++ != ':') || !ISDIGIT(*p)) |
433 | 0 | return FALSE; |
434 | | |
435 | 0 | mm = *p++ - '0'; |
436 | 0 | if(ISDIGIT(*p)) |
437 | 0 | mm = mm * 10 + (*p++ - '0'); |
438 | |
|
439 | 0 | if(mm >= 60) |
440 | 0 | return FALSE; |
441 | | |
442 | 0 | if(*p == ':') { |
443 | 0 | p++; |
444 | 0 | if(!ISDIGIT(*p)) |
445 | 0 | return FALSE; |
446 | | |
447 | 0 | ss = *p++ - '0'; |
448 | 0 | if(ISDIGIT(*p)) |
449 | 0 | ss = ss * 10 + (*p++ - '0'); |
450 | |
|
451 | 0 | if(ss > 60) |
452 | 0 | return FALSE; |
453 | 0 | } |
454 | | |
455 | 0 | w->dsecs = (hh * 3600) + (mm * 60) + ss; |
456 | 0 | *endp = (char *)CURL_UNCONST(p); |
457 | 0 | return TRUE; |
458 | 0 | } |
459 | | |
460 | | /* |
461 | | * parsedate() |
462 | | * |
463 | | * Returns: |
464 | | * |
465 | | * PARSEDATE_OK - a fine conversion |
466 | | * PARSEDATE_FAIL - failed to convert |
467 | | * PARSEDATE_LATER - time overflow at the far end of time_t |
468 | | * PARSEDATE_SOONER - time underflow at the low end of time_t |
469 | | */ |
470 | | |
471 | | /* Wednesday is the longest name this parser knows about */ |
472 | 0 | #define NAME_LEN 10 |
473 | | |
474 | | static void initwhen(struct when *w) |
475 | 0 | { |
476 | 0 | w->wday = w->mon = w->mday = w->dsecs = w->year = w->tzoff = -1; |
477 | 0 | } |
478 | | |
479 | | static int datestring(const char **datep, struct when *w) |
480 | 0 | { |
481 | | /* a name coming up */ |
482 | 0 | size_t len = 0; |
483 | 0 | const char *p = *datep; |
484 | 0 | while(ISALPHA(p[len]) && (len < NAME_LEN)) |
485 | 0 | len++; |
486 | |
|
487 | 0 | if(len != NAME_LEN) { |
488 | 0 | if(w->wday == -1) { |
489 | 0 | w->wday = checkday(p, len); |
490 | 0 | if(w->wday != -1) |
491 | 0 | goto found; |
492 | 0 | } |
493 | 0 | if(w->mon == -1) { |
494 | 0 | w->mon = checkmonth(p, len); |
495 | 0 | if(w->mon != -1) |
496 | 0 | goto found; |
497 | 0 | } |
498 | 0 | if(w->tzoff == -1) { |
499 | | /* this must be a time zone string */ |
500 | 0 | w->tzoff = checktz(p, len); |
501 | 0 | if(w->tzoff != -1) |
502 | 0 | goto found; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | return PARSEDATE_FAIL; /* bad string */ |
506 | 0 | found: |
507 | 0 | *datep += len; |
508 | 0 | return PARSEDATE_OK; |
509 | 0 | } |
510 | | |
511 | | static int datenum(const char *indate, const char **datep, struct when *w) |
512 | 0 | { |
513 | | /* a digit */ |
514 | 0 | unsigned int val; |
515 | 0 | char *end; |
516 | 0 | const char *date = *datep; |
517 | |
|
518 | 0 | if((w->dsecs == -1) && match_time(date, w, &end)) { |
519 | | /* time stamp */ |
520 | 0 | date = end; |
521 | 0 | } |
522 | 0 | else { |
523 | | /* Parse up to 8 decimal digits inline - we know *date is a digit. |
524 | | Avoid the overhead of curlx_str_number(). */ |
525 | 0 | const char *p = date; |
526 | 0 | unsigned int n = (unsigned int)(*p++ - '0'); |
527 | 0 | int num_digits; |
528 | 0 | while(ISDIGIT(*p) && (p - date) < 8) |
529 | 0 | n = n * 10 + (unsigned int)(*p++ - '0'); |
530 | | /* reject a 9th digit */ |
531 | 0 | if(ISDIGIT(*p)) |
532 | 0 | return PARSEDATE_FAIL; |
533 | 0 | num_digits = (int)(p - date); |
534 | 0 | val = n; |
535 | |
|
536 | 0 | if((num_digits == 4) && (indate < date) && |
537 | 0 | (date[-1] == '+' || date[-1] == '-') && |
538 | 0 | (w->tzoff == -1) && (val <= 1400)) { |
539 | | /* four digits and a value less than or equal to 1400 (to take into |
540 | | account all sorts of funny time zone diffs) and it is preceded with a |
541 | | plus or minus. This is a time zone indication. 1400 is picked since |
542 | | +1300 is frequently used and +1400 is mentioned as an edge number in |
543 | | the document "ISO C 200X Proposal: Timezone Functions" */ |
544 | 0 | w->tzoff = ((val / 100 * 60) + (val % 100)) * 60; |
545 | | |
546 | | /* the + and - prefix indicates the local time compared to GMT, |
547 | | this we need their reversed math to get what we want */ |
548 | 0 | w->tzoff = date[-1] == '+' ? -w->tzoff : w->tzoff; |
549 | 0 | } |
550 | | |
551 | 0 | else if(num_digits == 8) { |
552 | | /* 8 digits, this is YYYYMMDD */ |
553 | 0 | if((w->year == -1) && (w->mon == -1) && (w->mday == -1)) { |
554 | 0 | w->year = val / 10000; |
555 | 0 | w->mon = ((val % 10000) / 100) - 1; /* month is 0 - 11 */ |
556 | 0 | if(w->mon > 11) |
557 | 0 | return PARSEDATE_FAIL; |
558 | 0 | w->mday = val % 100; |
559 | 0 | } |
560 | 0 | else |
561 | 0 | return PARSEDATE_FAIL; |
562 | 0 | } |
563 | | |
564 | 0 | else if(w->mday == -1 && val > 0 && val < 32) { |
565 | 0 | w->mday = val; |
566 | 0 | } |
567 | | |
568 | 0 | else if(w->year == -1) { |
569 | 0 | w->year = val; |
570 | 0 | if(w->year < 100) { |
571 | 0 | if(w->year >= 70) |
572 | 0 | w->year += 1900; |
573 | 0 | else |
574 | 0 | w->year += 2000; |
575 | 0 | } |
576 | 0 | } |
577 | | |
578 | 0 | else |
579 | 0 | return PARSEDATE_FAIL; |
580 | | |
581 | 0 | date = p; |
582 | 0 | } |
583 | 0 | *datep = date; |
584 | 0 | return PARSEDATE_OK; |
585 | 0 | } |
586 | | |
587 | | static bool datecheck(struct when *w) |
588 | 0 | { |
589 | 0 | if(w->dsecs == -1) |
590 | 0 | w->dsecs = 0; /* no time available, make it zero */ |
591 | |
|
592 | 0 | if((w->mday == -1) || (w->mon == -1) || (w->year == -1) || |
593 | | /* lacks vital info, fail */ |
594 | 0 | (w->year < 1583) || |
595 | | /* The Gregorian calendar was introduced 1582 */ |
596 | 0 | (w->mday > 31) /* too long month */ ) |
597 | 0 | return TRUE; |
598 | | |
599 | 0 | return FALSE; |
600 | 0 | } |
601 | | |
602 | | static void tzadjust(curl_off_t *tp, struct when *w) |
603 | 0 | { |
604 | 0 | if(w->tzoff != -1) { /* unknown tz means no offset */ |
605 | | /* Add the time zone diff between local time zone and GMT. */ |
606 | 0 | if((w->tzoff > 0) && (*tp > (curl_off_t)(CURL_OFF_T_MAX - w->tzoff))) |
607 | 0 | *tp = CURL_OFF_T_MAX; |
608 | 0 | else |
609 | 0 | *tp += w->tzoff; |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | static int mktimet(curl_off_t seconds, time_t *output) |
614 | 0 | { |
615 | | #if SIZEOF_TIME_T < 5 |
616 | | if(seconds > TIME_T_MAX) { |
617 | | *output = TIME_T_MAX; |
618 | | return PARSEDATE_LATER; |
619 | | } |
620 | | else if(seconds < TIME_T_MIN) { |
621 | | *output = TIME_T_MIN; |
622 | | return PARSEDATE_SOONER; |
623 | | } |
624 | | #elif defined(HAVE_TIME_T_UNSIGNED) |
625 | | if(seconds < 0) { |
626 | | *output = 0; |
627 | | return PARSEDATE_SOONER; |
628 | | } |
629 | | #endif |
630 | 0 | *output = (time_t)seconds; |
631 | 0 | return PARSEDATE_OK; |
632 | 0 | } |
633 | | |
634 | | static int parsedate(const char *date, time_t *output) |
635 | 0 | { |
636 | 0 | curl_off_t seconds = 0; |
637 | 0 | const char *indate = date; /* save the original pointer */ |
638 | 0 | int part = 0; /* max 6 parts */ |
639 | 0 | int rc = 0; |
640 | 0 | struct when w; |
641 | 0 | initwhen(&w); |
642 | |
|
643 | 0 | while(*date && (part < 6)) { |
644 | | /* skip everything that are not letters or digits */ |
645 | 0 | while(*date && !ISALNUM(*date)) |
646 | 0 | date++; |
647 | |
|
648 | 0 | if(ISALPHA(*date)) |
649 | 0 | rc = datestring(&date, &w); |
650 | 0 | else if(ISDIGIT(*date)) |
651 | 0 | rc = datenum(indate, &date, &w); |
652 | 0 | if(rc) |
653 | 0 | return rc; |
654 | | |
655 | 0 | part++; |
656 | 0 | } |
657 | | |
658 | 0 | if(datecheck(&w)) |
659 | 0 | return PARSEDATE_FAIL; |
660 | | |
661 | 0 | seconds = time2epoch(&w); /* get number of seconds */ |
662 | 0 | tzadjust(&seconds, &w); /* handle the time zone offset */ |
663 | 0 | rc = mktimet(seconds, output); /* squeeze seconds into a time_t */ |
664 | |
|
665 | 0 | return rc; |
666 | 0 | } |
667 | | #else |
668 | | /* disabled */ |
669 | | static int parsedate(const char *date, time_t *output) |
670 | | { |
671 | | (void)date; |
672 | | *output = 0; |
673 | | return PARSEDATE_OK; /* a lie */ |
674 | | } |
675 | | #endif |
676 | | |
677 | | time_t curl_getdate(const char *p, const time_t *unused) |
678 | 0 | { |
679 | 0 | time_t parsed = -1; |
680 | 0 | int rc; |
681 | 0 | (void)unused; /* legacy argument from the past that we ignore */ |
682 | |
|
683 | 0 | if(!p) |
684 | 0 | return -1; |
685 | | |
686 | 0 | rc = parsedate(p, &parsed); |
687 | 0 | if(rc == PARSEDATE_OK) { |
688 | 0 | if(parsed == (time_t)-1) |
689 | | /* avoid returning -1 for a working scenario */ |
690 | 0 | parsed++; |
691 | 0 | return parsed; |
692 | 0 | } |
693 | | /* everything else is fail */ |
694 | 0 | return -1; |
695 | 0 | } |
696 | | |
697 | | /* Curl_getdate_capped() differs from curl_getdate() in that this will return |
698 | | TIME_T_MAX in case the parsed time value was too big, instead of an |
699 | | error. Returns non-zero on error. */ |
700 | | |
701 | | int Curl_getdate_capped(const char *p, time_t *tp) |
702 | 0 | { |
703 | 0 | int rc = parsedate(p, tp); |
704 | 0 | return (rc == PARSEDATE_FAIL); |
705 | 0 | } |