1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2008, 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 http://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  * $Id: parsedate.c,v 1.36 2008-10-23 11:49:19 bagder Exp $
22  ***************************************************************************/
23 /*
24   A brief summary of the date string formats this parser groks:
25 
26   RFC 2616 3.3.1
27 
28   Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123
29   Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
30   Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format
31 
32   we support dates without week day name:
33 
34   06 Nov 1994 08:49:37 GMT
35   06-Nov-94 08:49:37 GMT
36   Nov  6 08:49:37 1994
37 
38   without the time zone:
39 
40   06 Nov 1994 08:49:37
41   06-Nov-94 08:49:37
42 
43   weird order:
44 
45   1994 Nov 6 08:49:37  (GNU date fails)
46   GMT 08:49:37 06-Nov-94 Sunday
47   94 6 Nov 08:49:37    (GNU date fails)
48 
49   time left out:
50 
51   1994 Nov 6
52   06-Nov-94
53   Sun Nov 6 94
54 
55   unusual separators:
56 
57   1994.Nov.6
58   Sun/Nov/6/94/GMT
59 
60   commonly used time zone names:
61 
62   Sun, 06 Nov 1994 08:49:37 CET
63   06 Nov 1994 08:49:37 EST
64 
65   time zones specified using RFC822 style:
66 
67   Sun, 12 Sep 2004 15:05:58 -0700
68   Sat, 11 Sep 2004 21:32:11 +0200
69 
70   compact numerical date strings:
71 
72   20040912 15:05:58 -0700
73   20040911 +0200
74 
75 */
76 #include "setup.h"
77 #include <stdio.h>
78 #include <ctype.h>
79 #include <string.h>
80 
81 #ifdef HAVE_STDLIB_H
82 #include <stdlib.h> /* for strtol() */
83 #endif
84 
85 #include <curl/curl.h>
86 #include "rawstr.h"
87 #include "parsedate.h"
88 
89 const char * const Curl_wkday[] =
90 {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
91 static const char * const weekday[] =
92 { "Monday", "Tuesday", "Wednesday", "Thursday",
93   "Friday", "Saturday", "Sunday" };
94 const char * const Curl_month[]=
95 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
96   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
97 
98 struct tzinfo {
99   char name[5];
100   int offset; /* +/- in minutes */
101 };
102 
103 /* Here's a bunch of frequently used time zone names. These were supported
104    by the old getdate parser. */
105 #define tDAYZONE -60       /* offset for daylight savings time */
106 static const struct tzinfo tz[]= {
107   {"GMT", 0},              /* Greenwich Mean */
108   {"UTC", 0},              /* Universal (Coordinated) */
109   {"WET", 0},              /* Western European */
110   {"BST", 0 tDAYZONE},     /* British Summer */
111   {"WAT", 60},             /* West Africa */
112   {"AST", 240},            /* Atlantic Standard */
113   {"ADT", 240 tDAYZONE},   /* Atlantic Daylight */
114   {"EST", 300},            /* Eastern Standard */
115   {"EDT", 300 tDAYZONE},   /* Eastern Daylight */
116   {"CST", 360},            /* Central Standard */
117   {"CDT", 360 tDAYZONE},   /* Central Daylight */
118   {"MST", 420},            /* Mountain Standard */
119   {"MDT", 420 tDAYZONE},   /* Mountain Daylight */
120   {"PST", 480},            /* Pacific Standard */
121   {"PDT", 480 tDAYZONE},   /* Pacific Daylight */
122   {"YST", 540},            /* Yukon Standard */
123   {"YDT", 540 tDAYZONE},   /* Yukon Daylight */
124   {"HST", 600},            /* Hawaii Standard */
125   {"HDT", 600 tDAYZONE},   /* Hawaii Daylight */
126   {"CAT", 600},            /* Central Alaska */
127   {"AHST", 600},           /* Alaska-Hawaii Standard */
128   {"NT",  660},            /* Nome */
129   {"IDLW", 720},           /* International Date Line West */
130   {"CET", -60},            /* Central European */
131   {"MET", -60},            /* Middle European */
132   {"MEWT", -60},           /* Middle European Winter */
133   {"MEST", -60 tDAYZONE},  /* Middle European Summer */
134   {"CEST", -60 tDAYZONE},  /* Central European Summer */
135   {"MESZ", -60 tDAYZONE},  /* Middle European Summer */
136   {"FWT", -60},            /* French Winter */
137   {"FST", -60 tDAYZONE},   /* French Summer */
138   {"EET", -120},           /* Eastern Europe, USSR Zone 1 */
139   {"WAST", -420},          /* West Australian Standard */
140   {"WADT", -420 tDAYZONE}, /* West Australian Daylight */
141   {"CCT", -480},           /* China Coast, USSR Zone 7 */
142   {"JST", -540},           /* Japan Standard, USSR Zone 8 */
143   {"EAST", -600},          /* Eastern Australian Standard */
144   {"EADT", -600 tDAYZONE}, /* Eastern Australian Daylight */
145   {"GST", -600},           /* Guam Standard, USSR Zone 9 */
146   {"NZT", -720},           /* New Zealand */
147   {"NZST", -720},          /* New Zealand Standard */
148   {"NZDT", -720 tDAYZONE}, /* New Zealand Daylight */
149   {"IDLE", -720},          /* International Date Line East */
150 };
151 
152 /* returns:
153    -1 no day
154    0 monday - 6 sunday
155 */
156 
checkday(const char * check,size_t len)157 static int checkday(const char *check, size_t len)
158 {
159   int i;
160   const char * const *what;
161   bool found= FALSE;
162   if(len > 3)
163     what = &weekday[0];
164   else
165     what = &Curl_wkday[0];
166   for(i=0; i<7; i++) {
167     if(Curl_raw_equal(check, what[0])) {
168       found=TRUE;
169       break;
170     }
171     what++;
172   }
173   return found?i:-1;
174 }
175 
checkmonth(const char * check)176 static int checkmonth(const char *check)
177 {
178   int i;
179   const char * const *what;
180   bool found= FALSE;
181 
182   what = &Curl_month[0];
183   for(i=0; i<12; i++) {
184     if(Curl_raw_equal(check, what[0])) {
185       found=TRUE;
186       break;
187     }
188     what++;
189   }
190   return found?i:-1; /* return the offset or -1, no real offset is -1 */
191 }
192 
193 /* return the time zone offset between GMT and the input one, in number
194    of seconds or -1 if the timezone wasn't found/legal */
195 
checktz(const char * check)196 static int checktz(const char *check)
197 {
198   unsigned int i;
199   const struct tzinfo *what;
200   bool found= FALSE;
201 
202   what = tz;
203   for(i=0; i< sizeof(tz)/sizeof(tz[0]); i++) {
204     if(Curl_raw_equal(check, what->name)) {
205       found=TRUE;
206       break;
207     }
208     what++;
209   }
210   return found?what->offset*60:-1;
211 }
212 
skip(const char ** date)213 static void skip(const char **date)
214 {
215   /* skip everything that aren't letters or digits */
216   while(**date && !ISALNUM(**date))
217     (*date)++;
218 }
219 
220 enum assume {
221   DATE_MDAY,
222   DATE_YEAR,
223   DATE_TIME
224 };
225 
226 /* this is a clone of 'struct tm' but with all fields we don't need or use
227    cut out */
228 struct my_tm {
229   int tm_sec;
230   int tm_min;
231   int tm_hour;
232   int tm_mday;
233   int tm_mon;
234   int tm_year;
235 };
236 
237 /* struct tm to time since epoch in GMT time zone.
238  * This is similar to the standard mktime function but for GMT only, and
239  * doesn't suffer from the various bugs and portability problems that
240  * some systems' implementations have.
241  */
my_timegm(struct my_tm * tm)242 static time_t my_timegm(struct my_tm *tm)
243 {
244   static const int month_days_cumulative [12] =
245     { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
246   int month, year, leap_days;
247 
248   if(tm->tm_year < 70)
249     /* we don't support years before 1970 as they will cause this function
250        to return a negative value */
251     return -1;
252 
253   year = tm->tm_year + 1900;
254   month = tm->tm_mon;
255   if (month < 0) {
256     year += (11 - month) / 12;
257     month = 11 - (11 - month) % 12;
258   }
259   else if (month >= 12) {
260     year -= month / 12;
261     month = month % 12;
262   }
263 
264   leap_days = year - (tm->tm_mon <= 1);
265   leap_days = ((leap_days / 4) - (leap_days / 100) + (leap_days / 400)
266                - (1969 / 4) + (1969 / 100) - (1969 / 400));
267 
268   return ((((time_t) (year - 1970) * 365
269             + leap_days + month_days_cumulative [month] + tm->tm_mday - 1) * 24
270            + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
271 }
272 
parsedate(const char * date)273 static time_t parsedate(const char *date)
274 {
275   time_t t = 0;
276   int wdaynum=-1;  /* day of the week number, 0-6 (mon-sun) */
277   int monnum=-1;   /* month of the year number, 0-11 */
278   int mdaynum=-1; /* day of month, 1 - 31 */
279   int hournum=-1;
280   int minnum=-1;
281   int secnum=-1;
282   int yearnum=-1;
283   int tzoff=-1;
284   struct my_tm tm;
285   enum assume dignext = DATE_MDAY;
286   const char *indate = date; /* save the original pointer */
287   int part = 0; /* max 6 parts */
288 
289   while(*date && (part < 6)) {
290     bool found=FALSE;
291 
292     skip(&date);
293 
294     if(ISALPHA(*date)) {
295       /* a name coming up */
296       char buf[32]="";
297       size_t len;
298       sscanf(date, "%31[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",
299              buf);
300       len = strlen(buf);
301 
302       if(wdaynum == -1) {
303         wdaynum = checkday(buf, len);
304         if(wdaynum != -1)
305           found = TRUE;
306       }
307       if(!found && (monnum == -1)) {
308         monnum = checkmonth(buf);
309         if(monnum != -1)
310           found = TRUE;
311       }
312 
313       if(!found && (tzoff == -1)) {
314         /* this just must be a time zone string */
315         tzoff = checktz(buf);
316         if(tzoff != -1)
317           found = TRUE;
318       }
319 
320       if(!found)
321         return -1; /* bad string */
322 
323       date += len;
324     }
325     else if(ISDIGIT(*date)) {
326       /* a digit */
327       int val;
328       char *end;
329       if((secnum == -1) &&
330          (3 == sscanf(date, "%02d:%02d:%02d", &hournum, &minnum, &secnum))) {
331         /* time stamp! */
332         date += 8;
333         found = TRUE;
334       }
335       else {
336         val = (int)strtol(date, &end, 10);
337 
338         if((tzoff == -1) &&
339            ((end - date) == 4) &&
340            (val <= 1400) &&
341            (indate< date) &&
342            ((date[-1] == '+' || date[-1] == '-'))) {
343           /* four digits and a value less than or equal to 1400 (to take into
344              account all sorts of funny time zone diffs) and it is preceeded
345              with a plus or minus. This is a time zone indication.  1400 is
346              picked since +1300 is frequently used and +1400 is mentioned as
347              an edge number in the document "ISO C 200X Proposal: Timezone
348              Functions" at http://david.tribble.com/text/c0xtimezone.html If
349              anyone has a more authoritative source for the exact maximum time
350              zone offsets, please speak up! */
351           found = TRUE;
352           tzoff = (val/100 * 60 + val%100)*60;
353 
354           /* the + and - prefix indicates the local time compared to GMT,
355              this we need ther reversed math to get what we want */
356           tzoff = date[-1]=='+'?-tzoff:tzoff;
357         }
358 
359         if(((end - date) == 8) &&
360            (yearnum == -1) &&
361            (monnum == -1) &&
362            (mdaynum == -1)) {
363           /* 8 digits, no year, month or day yet. This is YYYYMMDD */
364           found = TRUE;
365           yearnum = val/10000;
366           monnum = (val%10000)/100-1; /* month is 0 - 11 */
367           mdaynum = val%100;
368         }
369 
370         if(!found && (dignext == DATE_MDAY) && (mdaynum == -1)) {
371           if((val > 0) && (val<32)) {
372             mdaynum = val;
373             found = TRUE;
374           }
375           dignext = DATE_YEAR;
376         }
377 
378         if(!found && (dignext == DATE_YEAR) && (yearnum == -1)) {
379           yearnum = val;
380           found = TRUE;
381           if(yearnum < 1900) {
382             if(yearnum > 70)
383               yearnum += 1900;
384             else
385               yearnum += 2000;
386           }
387           if(mdaynum == -1)
388             dignext = DATE_MDAY;
389         }
390 
391         if(!found)
392           return -1;
393 
394         date = end;
395       }
396     }
397 
398     part++;
399   }
400 
401   if(-1 == secnum)
402     secnum = minnum = hournum = 0; /* no time, make it zero */
403 
404   if((-1 == mdaynum) ||
405      (-1 == monnum) ||
406      (-1 == yearnum))
407     /* lacks vital info, fail */
408     return -1;
409 
410 #if SIZEOF_TIME_T < 5
411   /* 32 bit time_t can only hold dates to the beginning of 2038 */
412   if(yearnum > 2037)
413     return 0x7fffffff;
414 #endif
415 
416   tm.tm_sec = secnum;
417   tm.tm_min = minnum;
418   tm.tm_hour = hournum;
419   tm.tm_mday = mdaynum;
420   tm.tm_mon = monnum;
421   tm.tm_year = yearnum - 1900;
422 
423   /* my_timegm() returns a time_t. time_t is often 32 bits, even on many
424      architectures that feature 64 bit 'long'.
425 
426      Some systems have 64 bit time_t and deal with years beyond 2038. However,
427      even on some of the systems with 64 bit time_t mktime() returns -1 for
428      dates beyond 03:14:07 UTC, January 19, 2038. (Such as AIX 5100-06)
429   */
430   t = my_timegm(&tm);
431 
432   /* time zone adjust (cast t to int to compare to negative one) */
433   if(-1 != (int)t) {
434 
435     /* Add the time zone diff between local time zone and GMT. */
436     long delta = (long)(tzoff!=-1?tzoff:0);
437 
438     if((delta>0) && (t + delta < t))
439       return -1; /* time_t overflow */
440 
441     t += delta;
442   }
443 
444   return t;
445 }
446 
curl_getdate(const char * p,const time_t * now)447 time_t curl_getdate(const char *p, const time_t *now)
448 {
449   (void)now;
450   return parsedate(p);
451 }
452