Coverage Report

Created: 2025-07-23 06:58

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