Coverage Report

Created: 2026-07-30 07:03

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