Coverage Report

Created: 2025-07-11 06:33

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