Coverage Report

Created: 2026-06-10 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jq/src/util.c
Line
Count
Source
1
/*-
2
 * Parts (strptime()) Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
3
 * All rights reserved.
4
 *
5
 * This code was contributed to The NetBSD Foundation by Klaus Klein.
6
 * Heavily optimised by David Laight
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#include <sys/types.h>
31
#include <sys/stat.h>
32
#include <assert.h>
33
#include <errno.h>
34
#include <fcntl.h>
35
#include <limits.h>
36
#include <string.h>
37
#include <unistd.h>
38
#include <stdlib.h>
39
#include <stddef.h>
40
#ifdef HAVE_ALLOCA_H
41
# include <alloca.h>
42
#elif !defined alloca
43
# ifdef __GNUC__
44
#  define alloca __builtin_alloca
45
# elif defined _MSC_VER
46
#  include <malloc.h>
47
#  define alloca _alloca
48
# elif !defined HAVE_ALLOCA
49
#  ifdef  __cplusplus
50
extern "C"
51
#  endif
52
void *alloca (size_t);
53
# endif
54
#endif
55
56
#ifdef WIN32
57
#include <windows.h>
58
#include <processenv.h>
59
#include <shellapi.h>
60
#include <wchar.h>
61
#include <wtypes.h>
62
#endif
63
64
65
#include "util.h"
66
#include "jq.h"
67
#include "jv_alloc.h"
68
#include "jv_unicode.h"
69
70
#ifdef WIN32
71
FILE *fopen(const char *fname, const char *mode) {
72
  size_t sz = sizeof(wchar_t) * MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
73
  wchar_t *wfname = alloca(sz + 2); // +2 is not needed, but just in case
74
  MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname, sz);
75
76
  sz = sizeof(wchar_t) * MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
77
  wchar_t *wmode = alloca(sz + 2); // +2 is not needed, but just in case
78
  MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, sz);
79
  return _wfopen(wfname, wmode);
80
}
81
#endif
82
83
0
jv expand_path(jv path) {
84
0
  assert(jv_get_kind(path) == JV_KIND_STRING);
85
0
  const char *pstr = jv_string_value(path);
86
0
  jv ret = path;
87
0
  if (jv_string_length_bytes(jv_copy(path)) > 1 && pstr[0] == '~' && pstr[1] == '/') {
88
0
    jv home = get_home();
89
0
    if (jv_is_valid(home)) {
90
0
      ret = jv_string_fmt("%s/%s",jv_string_value(home),pstr+2);
91
0
      jv_free(home);
92
0
    } else {
93
0
      jv emsg = jv_invalid_get_msg(home);
94
0
      ret = jv_invalid_with_msg(jv_string_fmt("Could not expand %s. (%s)", pstr, jv_string_value(emsg)));
95
0
      jv_free(emsg);
96
0
    }
97
0
    jv_free(path);
98
0
  }
99
0
  return ret;
100
0
}
101
102
0
jv get_home(void) {
103
0
  jv ret;
104
0
  char *home = getenv("HOME");
105
0
  if (!home) {
106
0
#ifndef WIN32
107
0
    ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
108
#else
109
    home = getenv("USERPROFILE");
110
    if (!home) {
111
      home = getenv("HOMEPATH");
112
      if (!home) {
113
        ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
114
      } else {
115
        const char *hd = getenv("HOMEDRIVE");
116
        if (!hd) hd = "";
117
        ret = jv_string_fmt("%s%s",hd,home);
118
      }
119
    } else {
120
      ret = jv_string(home);
121
    }
122
#endif
123
0
  } else {
124
0
    ret = jv_string(home);
125
0
  }
126
0
  return ret;
127
0
}
128
129
130
0
jv jq_realpath(jv path) {
131
0
  int path_max;
132
0
  char *buf = NULL;
133
0
#ifdef _PC_PATH_MAX
134
0
  path_max = pathconf(jv_string_value(path),_PC_PATH_MAX);
135
#else
136
  path_max = PATH_MAX;
137
#endif
138
0
  if (path_max > 0) {
139
0
     buf = jv_mem_alloc(path_max);
140
0
  }
141
#ifdef WIN32
142
  char *tmp = _fullpath(buf, jv_string_value(path), path_max);
143
#else
144
0
  char *tmp = realpath(jv_string_value(path), buf);
145
0
#endif
146
0
  if (tmp == NULL) {
147
0
    free(buf);
148
0
    return path;
149
0
  }
150
0
  jv_free(path);
151
0
  path = jv_string(tmp);
152
0
  free(tmp);
153
0
  return path;
154
0
}
155
156
const void *_jq_memmem(const void *haystack, size_t haystacklen,
157
0
                       const void *needle, size_t needlelen) {
158
0
#ifdef HAVE_MEMMEM
159
0
  return (const void*)memmem(haystack, haystacklen, needle, needlelen);
160
#else
161
  const char *h = haystack;
162
  const char *n = needle;
163
  size_t hi, hi2, ni;
164
165
  if (haystacklen < needlelen || haystacklen == 0)
166
    return NULL;
167
  for (hi = 0; hi < (haystacklen - needlelen + 1); hi++) {
168
    for (ni = 0, hi2 = hi; ni < needlelen; ni++, hi2++) {
169
      if (h[hi2] != n[ni])
170
        goto not_this;
171
    }
172
173
    return &h[hi];
174
175
not_this:
176
    continue;
177
  }
178
  return NULL;
179
#endif /* !HAVE_MEMMEM */
180
0
}
181
182
struct jq_util_input_state {
183
  jq_util_msg_cb err_cb;
184
  void *err_cb_data;
185
  jv_parser *parser;
186
  FILE* current_input;
187
  char **files;
188
  int nfiles;
189
  int curr_file;
190
  int failures;
191
  jv slurped;
192
  char buf[4096];
193
  size_t buf_valid_len;
194
  jv current_filename;
195
  size_t current_line;
196
};
197
198
0
static void fprinter(void *data, const char *fname) {
199
0
  fprintf((FILE *)data, "jq: error: Could not open file %s: %s\n", fname, strerror(errno));
200
0
}
201
202
// If parser == NULL -> RAW
203
0
jq_util_input_state *jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) {
204
0
  if (err_cb == NULL) {
205
0
    err_cb = fprinter;
206
0
    err_cb_data = stderr;
207
0
  }
208
0
  jq_util_input_state *new_state = jv_mem_calloc(1, sizeof(*new_state));
209
0
  new_state->err_cb = err_cb;
210
0
  new_state->err_cb_data = err_cb_data;
211
0
  new_state->slurped = jv_invalid();
212
0
  new_state->current_filename = jv_invalid();
213
214
0
  return new_state;
215
0
}
216
217
0
void jq_util_input_set_parser(jq_util_input_state *state, jv_parser *parser, int slurp) {
218
0
  assert(!jv_is_valid(state->slurped));
219
0
  state->parser = parser;
220
221
0
  if (parser == NULL && slurp)
222
0
    state->slurped = jv_string("");
223
0
  else if (slurp)
224
0
    state->slurped = jv_array();
225
0
  else
226
0
    state->slurped = jv_invalid();
227
0
}
228
229
0
void jq_util_input_free(jq_util_input_state **state) {
230
0
  jq_util_input_state *old_state = *state;
231
0
  *state = NULL;
232
0
  if (old_state == NULL)
233
0
    return;
234
235
0
  if (old_state->parser != NULL)
236
0
    jv_parser_free(old_state->parser);
237
0
  for (int i = 0; i < old_state->nfiles; i++)
238
0
    free(old_state->files[i]);
239
0
  free(old_state->files);
240
0
  jv_free(old_state->slurped);
241
0
  jv_free(old_state->current_filename);
242
0
  jv_mem_free(old_state);
243
0
}
244
245
0
void jq_util_input_add_input(jq_util_input_state *state, const char *fname) {
246
0
  state->files = jv_mem_realloc(state->files, (state->nfiles + 1) * sizeof(state->files[0]));
247
0
  state->files[state->nfiles++] = jv_mem_strdup(fname);
248
0
}
249
250
0
int jq_util_input_errors(jq_util_input_state *state) {
251
0
  return state->failures;
252
0
}
253
254
0
static const char *next_file(jq_util_input_state *state) {
255
0
  if (state->curr_file < state->nfiles)
256
0
    return state->files[state->curr_file++];
257
0
  return NULL;
258
0
}
259
260
0
static int jq_util_input_read_more(jq_util_input_state *state) {
261
0
  if (!state->current_input || feof(state->current_input) || ferror(state->current_input)) {
262
0
    if (state->current_input && ferror(state->current_input)) {
263
      // System-level input error on the stream. It will be closed (below).
264
      // TODO: report it. Can't use 'state->err_cb()' as it is hard-coded for
265
      //       'open' related problems.
266
0
      fprintf(stderr,"jq: error: %s\n", strerror(errno));
267
0
    }
268
0
    if (state->current_input) {
269
0
      if (state->current_input == stdin) {
270
0
        clearerr(stdin); // perhaps we can read again; anyways, we don't fclose(stdin)
271
0
      } else {
272
0
        fclose(state->current_input);
273
0
      }
274
0
      state->current_input = NULL;
275
0
    }
276
0
    const char *f = next_file(state);
277
0
    if (f != NULL) {
278
0
      jv_free(state->current_filename);
279
0
      state->current_line = 0;
280
0
      if (!strcmp(f, "-")) {
281
0
        state->current_input = stdin;
282
0
        state->current_filename = jv_string("<stdin>");
283
0
      } else {
284
0
        state->current_input = fopen(f, "r");
285
0
        state->current_filename = jv_string(f);
286
0
        if (!state->current_input) {
287
0
          state->err_cb(state->err_cb_data, f);
288
0
          state->failures++;
289
0
        }
290
0
      }
291
0
    }
292
0
  }
293
294
0
  state->buf[0] = 0;
295
0
  state->buf_valid_len = 0;
296
0
  if (state->current_input) {
297
0
    char *res;
298
0
    memset(state->buf, 0xff, sizeof(state->buf));
299
300
0
    const int max_utf8_len = 4;
301
0
    const int max_gets_len = sizeof(state->buf) - max_utf8_len;
302
0
    while (!(res = fgets(state->buf, max_gets_len, state->current_input)) &&
303
0
           ferror(state->current_input) && errno == EINTR)
304
0
      clearerr(state->current_input);
305
0
    if (res == NULL) {
306
0
      state->buf[0] = 0;
307
0
      if (ferror(state->current_input))
308
0
        state->failures++;
309
0
    } else {
310
0
      const char *p = memchr(state->buf, '\n', max_gets_len);
311
312
0
      if (p != NULL)
313
0
        state->current_line++;
314
315
0
      if (p == NULL && feof(state->current_input)) {
316
0
        size_t i;
317
318
        /*
319
         * XXX We don't know how many bytes we've read!
320
         *
321
         * We can't use getline() because there need not be any newlines
322
         * in the input.  The only entirely correct choices are: use
323
         * fgetc() or fread().  Using fread() will complicate buffer
324
         * management here.
325
         *
326
         * For now we check how much fgets() read by scanning backwards for the
327
         * terminating '\0'. This only works because we previously memset our
328
         * buffer with something nonzero.
329
         */
330
0
        for (i = max_gets_len - 1; i > 0; i--) {
331
0
          if (state->buf[i] == '\0')
332
0
            break;
333
0
        }
334
0
        state->buf_valid_len = i;
335
0
      } else if (p == NULL) {
336
0
        state->buf_valid_len = max_gets_len - 1;
337
0
        char *end = state->buf + state->buf_valid_len;
338
0
        int len = 0;
339
0
        if (jvp_utf8_backtrack(end - 1, state->buf, &len) && len > 0) {
340
0
          state->buf_valid_len += fread(end, 1, len, state->current_input);
341
0
        }
342
0
      } else {
343
0
        state->buf_valid_len = (p - state->buf) + 1;
344
0
      }
345
0
    }
346
0
  }
347
0
  return state->curr_file == state->nfiles && !state->current_input;
348
0
}
349
350
0
jv jq_util_input_next_input_cb(jq_state *jq, void *data) {
351
0
  return jq_util_input_next_input((jq_util_input_state *)data);
352
0
}
353
354
// Return the current_filename:current_line
355
0
jv jq_util_input_get_position(jq_state *jq) {
356
0
  jq_input_cb cb = NULL;
357
0
  void *cb_data = NULL;
358
0
  jq_get_input_cb(jq, &cb, &cb_data);
359
0
  assert(cb == jq_util_input_next_input_cb);
360
0
  if (cb != jq_util_input_next_input_cb)
361
0
    return jv_invalid_with_msg(jv_string("Invalid jq_util_input API usage"));
362
0
  jq_util_input_state *s = (jq_util_input_state *)cb_data;
363
364
  // We can't assert that current_filename is a string because if
365
  // the error was a JSON parser error then we may not have set
366
  // current_filename yet.
367
0
  if (jv_get_kind(s->current_filename) != JV_KIND_STRING)
368
0
    return jv_string("<unknown>");
369
370
0
  jv v = jv_string_fmt("%s:%lu", jv_string_value(s->current_filename), (unsigned long)s->current_line);
371
0
  return v;
372
0
}
373
374
0
jv jq_util_input_get_current_filename(jq_state* jq) {
375
0
  jq_input_cb cb=NULL;
376
0
  void *cb_data=NULL;
377
0
  jq_get_input_cb(jq, &cb, &cb_data);
378
0
  if (cb != jq_util_input_next_input_cb)
379
0
    return jv_invalid_with_msg(jv_string("Unknown input filename"));
380
0
  jq_util_input_state *s = (jq_util_input_state *)cb_data;
381
0
  jv v = jv_copy(s->current_filename);
382
0
  return v;
383
0
}
384
385
0
jv jq_util_input_get_current_line(jq_state* jq) {
386
0
  jq_input_cb cb=NULL;
387
0
  void *cb_data=NULL;
388
0
  jq_get_input_cb(jq, &cb, &cb_data);
389
0
  if (cb != jq_util_input_next_input_cb)
390
0
    return jv_invalid_with_msg(jv_string("Unknown input line number"));
391
0
  jq_util_input_state *s = (jq_util_input_state *)cb_data;
392
0
  jv v = jv_number(s->current_line);
393
0
  return v;
394
0
}
395
396
397
// Blocks to read one more input from stdin and/or given files
398
// When slurping, it returns just one value
399
0
jv jq_util_input_next_input(jq_util_input_state *state) {
400
0
  int is_last = 0;
401
0
  jv value = jv_invalid(); // need more input
402
0
  do {
403
0
    if (state->parser == NULL) {
404
      // Raw input
405
0
      is_last = jq_util_input_read_more(state);
406
0
      if (state->buf_valid_len == 0)
407
0
        continue;
408
0
      if (jv_is_valid(state->slurped)) {
409
        // Slurped raw input
410
0
        state->slurped = jv_string_concat(state->slurped, jv_string_sized(state->buf, state->buf_valid_len));
411
0
      } else {
412
0
        if (!jv_is_valid(value))
413
0
          value = jv_string("");
414
0
        if (state->buf[state->buf_valid_len-1] == '\n') {
415
          // whole line
416
0
          state->buf[state->buf_valid_len-1] = 0;
417
0
          return jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len-1));
418
0
        }
419
0
        value = jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len));
420
0
        state->buf[0] = '\0';
421
0
        state->buf_valid_len = 0;
422
0
      }
423
0
    } else {
424
0
      if (jv_parser_remaining(state->parser) == 0) {
425
0
        is_last = jq_util_input_read_more(state);
426
0
        jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_last);
427
0
      }
428
0
      value = jv_parser_next(state->parser);
429
0
      if (jv_is_valid(state->slurped)) {
430
0
        if (jv_is_valid(value)) {
431
0
          state->slurped = jv_array_append(state->slurped, value);
432
0
          value = jv_invalid();
433
0
        } else if (jv_invalid_has_msg(jv_copy(value)))
434
0
          return value; // Not slurped parsed input
435
0
      } else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) {
436
0
        return value;
437
0
      }
438
0
    }
439
0
  } while (!is_last);
440
441
0
  if (jv_is_valid(state->slurped)) {
442
0
    value = state->slurped;
443
0
    state->slurped = jv_invalid();
444
0
  }
445
0
  return value;
446
0
}
447
448
#ifndef HAVE_STRPTIME
449
/* http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/lib/libc/time/strptime.c?only_with_tag=HEAD
450
 * NetBSD implementation strptime().
451
 * Format description: https://netbsd.gw.com/cgi-bin/man-cgi?strptime+3+NetBSD-current
452
 * Adapted by https://github.com/res2001 (https://github.com/res2001/strptime).
453
*/
454
455
#include <ctype.h>
456
#include <string.h>
457
#include <time.h>
458
#include <stdint.h>
459
460
static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
461
static const unsigned char *find_string(const unsigned char *, int *, const char * const *, const char * const *, int);
462
463
/*
464
 * We do not implement alternate representations. However, we always
465
 * check whether a given modifier is allowed for a certain conversion.
466
 */
467
#define ALT_E     0x01
468
#define ALT_O     0x02
469
#define LEGAL_ALT(x)  { if (alt_format & ~(x)) return NULL; }
470
471
#define TM_YEAR_BASE  1900
472
473
#define TM_SUNDAY       0
474
#define TM_MONDAY       1
475
#define TM_TUESDAY      2
476
#define TM_WEDNESDAY    3
477
#define TM_THURSDAY     4
478
#define TM_FRIDAY       5
479
#define TM_SATURDAY     6
480
481
#define S_YEAR      (1 << 0)
482
#define S_MON     (1 << 1)
483
#define S_YDAY      (1 << 2)
484
#define S_MDAY      (1 << 3)
485
#define S_WDAY      (1 << 4)
486
#define S_HOUR      (1 << 5)
487
488
#define HAVE_MDAY(s)  (s & S_MDAY)
489
#define HAVE_MON(s)   (s & S_MON)
490
#define HAVE_WDAY(s)  (s & S_WDAY)
491
#define HAVE_YDAY(s)  (s & S_YDAY)
492
#define HAVE_YEAR(s)  (s & S_YEAR)
493
#define HAVE_HOUR(s)  (s & S_HOUR)
494
495
#define SECSPERMIN      60
496
#define MINSPERHOUR     60
497
#define SECSPERHOUR     (SECSPERMIN * MINSPERHOUR)
498
#define HOURSPERDAY     24
499
500
#define HERE_D_T_FMT    "%a %b %e %H:%M:%S %Y"
501
#define HERE_D_FMT      "%y/%m/%d"
502
#define HERE_T_FMT_AMPM "%I:%M:%S %p"
503
#define HERE_T_FMT      "%H:%M:%S"
504
505
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
506
507
/*
508
** Since everything in isleap is modulo 400 (or a factor of 400), we know that
509
**  isleap(y) == isleap(y % 400)
510
** and so
511
**  isleap(a + b) == isleap((a + b) % 400)
512
** or
513
**  isleap(a + b) == isleap(a % 400 + b % 400)
514
** This is true even if % means modulo rather than Fortran remainder
515
** (which is allowed by C89 but not by C99 or later).
516
** We use this to avoid addition overflow problems.
517
*/
518
519
#define isleap_sum(a, b)  isleap((a) % 400 + (b) % 400)
520
521
#ifdef _MSC_VER
522
#define tzname              _tzname
523
#define strncasecmp         _strnicmp
524
#endif
525
526
#ifdef TM_ZONE
527
static char* utc = "UTC";
528
#endif
529
/* RFC-822/RFC-2822 */
530
static const char *const nast[] = {
531
       "EST",    "CST",    "MST",    "PST",    "\0\0\0"
532
};
533
static const char *const nadt[] = {
534
       "EDT",    "CDT",    "MDT",    "PDT",    "\0\0\0"
535
};
536
static const char *const weekday_name[] =
537
{
538
    "Sunday", "Monday", "Tuesday", "Wednesday",
539
    "Thursday", "Friday", "Saturday"
540
};
541
static const char *const ab_weekday_name[] =
542
{
543
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
544
};
545
static const char *const month_name[] =
546
{
547
    "January", "February", "March", "April", "May", "June",
548
    "July", "August", "September", "October", "November", "December"
549
};
550
static const char *const ab_month_name[] =
551
{
552
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
553
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
554
};
555
static const char *const am_pm[] = {"AM", "PM"};
556
557
558
/*
559
 * Table to determine the ordinal date for the start of a month.
560
 * Ref: http://en.wikipedia.org/wiki/ISO_week_date
561
 */
562
static const int start_of_month[2][13] = {
563
    /* non-leap year */
564
    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
565
    /* leap year */
566
    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
567
};
568
569
/*
570
 * Calculate the week day of the first day of a year. Valid for
571
 * the Gregorian calendar, which began Sept 14, 1752 in the UK
572
 * and its colonies. Ref:
573
 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
574
 */
575
576
static int
577
first_wday_of(int yr)
578
{
579
    return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) /  4) +
580
        (isleap(yr) ? 6 : 0) + 1) % 7;
581
}
582
583
#define delim(p)  ((p) == '\0' || isspace((unsigned char)(p)))
584
585
static int
586
fromzone(const unsigned char **bp, struct tm *tm, int mandatory)
587
{
588
//    timezone_t tz;
589
    char buf[512], *p;
590
    const unsigned char *rp;
591
592
    for (p = buf, rp = *bp; !delim(*rp) && p < &buf[sizeof(buf) - 1]; rp++)
593
        *p++ = *rp;
594
    *p = '\0';
595
596
    if (mandatory)
597
        *bp = rp;
598
    if (!isalnum((unsigned char)*buf))
599
        return 0;
600
//    tz = tzalloc(buf);
601
//    if (tz == NULL)
602
//        return 0;
603
604
    *bp = rp;
605
    tm->tm_isdst = 0; /* XXX */
606
#ifdef TM_GMTOFF
607
    tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
608
#endif
609
#ifdef TM_ZONE
610
    // Can't use tzgetname() here because we are going to free()
611
    tm->TM_ZONE = NULL; /* XXX */
612
#endif
613
//    tzfree(tz);
614
    return 1;
615
}
616
617
char* strptime(const char *buf, const char *fmt, struct tm *tm)
618
{
619
    unsigned char c;
620
    const unsigned char *bp, *ep, *zname;
621
    int alt_format, i, split_year = 0, neg = 0, state = 0,
622
        day_offset = -1, week_offset = 0, offs, mandatory;
623
    const char *new_fmt;
624
625
    bp = (const unsigned char *)buf;
626
627
    while (bp != NULL && (c = *fmt++) != '\0') {
628
        /* Clear `alternate' modifier prior to new conversion. */
629
        alt_format = 0;
630
        i = 0;
631
632
        /* Eat up white-space. */
633
        if (isspace(c)) {
634
            while (isspace(*bp))
635
                bp++;
636
            continue;
637
        }
638
639
        if (c != '%')
640
            goto literal;
641
642
643
again:    switch (c = *fmt++) {
644
        case '%': /* "%%" is converted to "%". */
645
literal:
646
            if (c != *bp++)
647
                return NULL;
648
            LEGAL_ALT(0);
649
            continue;
650
651
        /*
652
         * "Alternative" modifiers. Just set the appropriate flag
653
         * and start over again.
654
         */
655
        case 'E': /* "%E?" alternative conversion modifier. */
656
            LEGAL_ALT(0);
657
            alt_format |= ALT_E;
658
            goto again;
659
660
        case 'O': /* "%O?" alternative conversion modifier. */
661
            LEGAL_ALT(0);
662
            alt_format |= ALT_O;
663
            goto again;
664
665
        /*
666
         * "Complex" conversion rules, implemented through recursion.
667
         */
668
        case 'c': /* Date and time, using the locale's format. */
669
//            new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
670
            new_fmt = HERE_D_T_FMT;
671
            state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
672
            goto recurse;
673
674
        case 'F': /* The date as "%Y-%m-%d". */
675
            new_fmt = "%Y-%m-%d";
676
            LEGAL_ALT(0);
677
            state |= S_MON | S_MDAY | S_YEAR;
678
            goto recurse;
679
680
        case 'R': /* The time as "%H:%M". */
681
            new_fmt = "%H:%M";
682
            LEGAL_ALT(0);
683
            goto recurse;
684
685
        case 'r': /* The time in 12-hour clock representation. */
686
//            new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
687
            new_fmt = HERE_T_FMT_AMPM;
688
            LEGAL_ALT(0);
689
            goto recurse;
690
691
        case 'X': /* The time, using the locale's format. */
692
            /* fall through */
693
694
        case 'T': /* The time as "%H:%M:%S". */
695
            new_fmt = HERE_T_FMT;
696
            LEGAL_ALT(0);
697
698
recurse:
699
            bp = (const unsigned char *)strptime((const char *)bp,
700
                                new_fmt, tm);
701
            LEGAL_ALT(ALT_E);
702
            continue;
703
704
        case 'x': /* The date, using the locale's format. */
705
            /* fall through */
706
707
        case 'D': /* The date as "%y/%m/%d". */
708
        {
709
            new_fmt = HERE_D_FMT;
710
            LEGAL_ALT(0);
711
            state |= S_MON | S_MDAY | S_YEAR;
712
            const int year = split_year ? tm->tm_year : 0;
713
714
            bp = (const unsigned char *)strptime((const char *)bp,
715
                                new_fmt, tm);
716
            LEGAL_ALT(ALT_E);
717
            tm->tm_year += year;
718
            if (split_year && tm->tm_year % (2000 - TM_YEAR_BASE) <= 68)
719
                tm->tm_year -= 2000 - TM_YEAR_BASE;
720
            split_year = 1;
721
            continue;
722
        }
723
        /*
724
         * "Elementary" conversion rules.
725
         */
726
        case 'A': /* The day of week, using the locale's form. */
727
        case 'a':
728
            bp = find_string(bp, &tm->tm_wday, weekday_name, ab_weekday_name, 7);
729
            LEGAL_ALT(0);
730
            state |= S_WDAY;
731
            continue;
732
733
        case 'B': /* The month, using the locale's form. */
734
        case 'b':
735
        case 'h':
736
            bp = find_string(bp, &tm->tm_mon, month_name, ab_month_name, 12);
737
            LEGAL_ALT(0);
738
            state |= S_MON;
739
            continue;
740
741
        case 'C': /* The century number. */
742
            i = 20;
743
            bp = conv_num(bp, &i, 0, 99);
744
745
            i = i * 100 - TM_YEAR_BASE;
746
            if (split_year)
747
                i += tm->tm_year % 100;
748
            split_year = 1;
749
            tm->tm_year = i;
750
            LEGAL_ALT(ALT_E);
751
            state |= S_YEAR;
752
            continue;
753
754
        case 'd': /* The day of month. */
755
        case 'e':
756
            bp = conv_num(bp, &tm->tm_mday, 1, 31);
757
            LEGAL_ALT(ALT_O);
758
            state |= S_MDAY;
759
            continue;
760
761
        case 'k': /* The hour (24-hour clock representation). */
762
            LEGAL_ALT(0);
763
            /* FALLTHROUGH */
764
        case 'H':
765
            bp = conv_num(bp, &tm->tm_hour, 0, 23);
766
            LEGAL_ALT(ALT_O);
767
            state |= S_HOUR;
768
            continue;
769
770
        case 'l': /* The hour (12-hour clock representation). */
771
            LEGAL_ALT(0);
772
            /* FALLTHROUGH */
773
        case 'I':
774
            bp = conv_num(bp, &tm->tm_hour, 1, 12);
775
            if (tm->tm_hour == 12)
776
                tm->tm_hour = 0;
777
            LEGAL_ALT(ALT_O);
778
            state |= S_HOUR;
779
            continue;
780
781
        case 'j': /* The day of year. */
782
            i = 1;
783
            bp = conv_num(bp, &i, 1, 366);
784
            tm->tm_yday = i - 1;
785
            LEGAL_ALT(0);
786
            state |= S_YDAY;
787
            continue;
788
789
        case 'M': /* The minute. */
790
            bp = conv_num(bp, &tm->tm_min, 0, 59);
791
            LEGAL_ALT(ALT_O);
792
            continue;
793
794
        case 'm': /* The month. */
795
            i = 1;
796
            bp = conv_num(bp, &i, 1, 12);
797
            tm->tm_mon = i - 1;
798
            LEGAL_ALT(ALT_O);
799
            state |= S_MON;
800
            continue;
801
802
        case 'p': /* The locale's equivalent of AM/PM. */
803
            bp = find_string(bp, &i, am_pm, NULL, 2);
804
            if (HAVE_HOUR(state) && tm->tm_hour > 11)
805
                return NULL;
806
            tm->tm_hour += i * 12;
807
            LEGAL_ALT(0);
808
            continue;
809
810
        case 'S': /* The seconds. */
811
            bp = conv_num(bp, &tm->tm_sec, 0, 61);
812
            LEGAL_ALT(ALT_O);
813
            continue;
814
815
        case 's': {     /* seconds since the epoch */
816
#ifdef _WIN32
817
            const time_t TIME_MAX = INT32_MAX;
818
#else
819
            const time_t TIME_MAX = INT64_MAX;
820
#endif
821
            time_t sse, d;
822
823
            if (*bp < '0' || *bp > '9') {
824
                bp = NULL;
825
                continue;
826
            }
827
828
            sse = *bp++ - '0';
829
            while (*bp >= '0' && *bp <= '9') {
830
                d = *bp++ - '0';
831
                if (sse > TIME_MAX/10) {
832
                    bp = NULL;
833
                    break;
834
                }
835
                sse *= 10;
836
                if (sse > TIME_MAX - d) {
837
                    bp = NULL;
838
                    break;
839
                }
840
                sse += d;
841
            }
842
            if (bp == NULL)
843
                continue;
844
845
#ifdef _WIN32
846
            if (localtime_s(tm, &sse))
847
#else
848
            if (localtime_r(&sse, tm) == NULL)
849
#endif
850
                bp = NULL;
851
            else
852
                state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;
853
            continue;
854
            }
855
856
        case 'U': /* The week of year, beginning on sunday. */
857
        case 'W': /* The week of year, beginning on monday. */
858
            /*
859
             * This is bogus, as we can not assume any valid
860
             * information present in the tm structure at this
861
             * point to calculate a real value, so save the
862
             * week for now in case it can be used later.
863
             */
864
            bp = conv_num(bp, &i, 0, 53);
865
            LEGAL_ALT(ALT_O);
866
            if (c == 'U')
867
                day_offset = TM_SUNDAY;
868
            else
869
                day_offset = TM_MONDAY;
870
            week_offset = i;
871
            continue;
872
873
        case 'w': /* The day of week, beginning on sunday. */
874
            bp = conv_num(bp, &tm->tm_wday, 0, 6);
875
            LEGAL_ALT(ALT_O);
876
            state |= S_WDAY;
877
            continue;
878
879
        case 'u': /* The day of week, monday = 1. */
880
            bp = conv_num(bp, &i, 1, 7);
881
            tm->tm_wday = i % 7;
882
            LEGAL_ALT(ALT_O);
883
            state |= S_WDAY;
884
            continue;
885
886
        case 'g': /* The year corresponding to the ISO week
887
                 * number but without the century.
888
                 */
889
            bp = conv_num(bp, &i, 0, 99);
890
            continue;
891
892
        case 'G': /* The year corresponding to the ISO week
893
                 * number with century.
894
                 */
895
            do
896
                bp++;
897
            while (isdigit(*bp));
898
            continue;
899
900
        case 'V': /* The ISO 8601:1988 week number as decimal */
901
            bp = conv_num(bp, &i, 0, 53);
902
            continue;
903
904
        case 'Y': /* The year. */
905
            i = TM_YEAR_BASE; /* just for data sanity... */
906
            bp = conv_num(bp, &i, 0, 9999);
907
            tm->tm_year = i - TM_YEAR_BASE;
908
            LEGAL_ALT(ALT_E);
909
            state |= S_YEAR;
910
            continue;
911
912
        case 'y': /* The year within 100 years of the epoch. */
913
            /* LEGAL_ALT(ALT_E | ALT_O); */
914
            bp = conv_num(bp, &i, 0, 99);
915
916
            if (split_year)
917
                /* preserve century */
918
                i += (tm->tm_year / 100) * 100;
919
            else {
920
                split_year = 1;
921
                if (i <= 68)
922
                    i = i + 2000 - TM_YEAR_BASE;
923
            }
924
            tm->tm_year = i;
925
            state |= S_YEAR;
926
            continue;
927
928
        case 'Z':       // time zone name
929
        case 'z':       //
930
#ifdef _WIN32
931
            _tzset();
932
#else
933
            tzset();
934
#endif
935
            mandatory = c == 'z';
936
            /*
937
             * We recognize all ISO 8601 formats:
938
             * Z  = Zulu time/UTC
939
             * [+-]hhmm
940
             * [+-]hh:mm
941
             * [+-]hh
942
             * We recognize all RFC-822/RFC-2822 formats:
943
             * UT|GMT
944
             *          North American : UTC offsets
945
             * E[DS]T = Eastern : -4 | -5
946
             * C[DS]T = Central : -5 | -6
947
             * M[DS]T = Mountain: -6 | -7
948
             * P[DS]T = Pacific : -7 | -8
949
             *          Nautical/Military
950
             * [A-IL-M] = -1 ... -9 (J not used)
951
             * [N-Y]  = +1 ... +12
952
             * Note: J maybe used to denote non-nautical
953
             *       local time
954
             */
955
            if (mandatory)
956
                while (isspace(*bp))
957
                    bp++;
958
959
            zname = bp;
960
            switch (*bp++) {
961
            case 'G':
962
                if (*bp++ != 'M')
963
                    goto namedzone;
964
                /*FALLTHROUGH*/
965
            case 'U':
966
                if (*bp++ != 'T')
967
                    goto namedzone;
968
                else if (!delim(*bp) && *bp++ != 'C')
969
                    goto namedzone;
970
                /*FALLTHROUGH*/
971
            case 'Z':
972
                if (!delim(*bp))
973
                    goto namedzone;
974
                tm->tm_isdst = 0;
975
#ifdef TM_GMTOFF
976
                tm->TM_GMTOFF = 0;
977
#endif
978
#ifdef TM_ZONE
979
                tm->TM_ZONE = utc;
980
#endif
981
                continue;
982
            case '+':
983
                neg = 0;
984
                break;
985
            case '-':
986
                neg = 1;
987
                break;
988
            default:
989
namedzone:
990
                bp = zname;
991
992
                /* Nautical / Military style */
993
                if (delim(bp[1]) &&
994
                    ((*bp >= 'A' && *bp <= 'I') ||
995
                     (*bp >= 'L' && *bp <= 'Y'))) {
996
#ifdef TM_GMTOFF
997
                    /* Argh! No 'J'! */
998
                    if (*bp >= 'A' && *bp <= 'I')
999
                        tm->TM_GMTOFF =
1000
                            (int)*bp - ('A' - 1);
1001
                    else if (*bp >= 'L' && *bp <= 'M')
1002
                        tm->TM_GMTOFF = (int)*bp - 'A';
1003
                    else if (*bp >= 'N' && *bp <= 'Y')
1004
                        tm->TM_GMTOFF = 'M' - (int)*bp;
1005
                    tm->TM_GMTOFF *= SECSPERHOUR;
1006
#endif
1007
#ifdef TM_ZONE
1008
                    tm->TM_ZONE = NULL; /* XXX */
1009
#endif
1010
                    bp++;
1011
                    continue;
1012
                }
1013
                /* 'J' is local time */
1014
                if (delim(bp[1]) && *bp == 'J') {
1015
#ifdef TM_GMTOFF
1016
                    tm->TM_GMTOFF = -timezone;
1017
#endif
1018
#ifdef TM_ZONE
1019
                    tm->TM_ZONE = NULL; /* XXX */
1020
#endif
1021
                    bp++;
1022
                    continue;
1023
                }
1024
1025
                /*
1026
                 * From our 3 letter hard-coded table
1027
                 * XXX: Can be removed, handled by tzload()
1028
                 */
1029
                if (delim(bp[0]) || delim(bp[1]) ||
1030
                    delim(bp[2]) || !delim(bp[3]))
1031
                    goto loadzone;
1032
                ep = find_string(bp, &i, nast, NULL, 4);
1033
                if (ep != NULL) {
1034
#ifdef TM_GMTOFF
1035
                    tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
1036
#endif
1037
#ifdef TM_ZONE
1038
                    tm->TM_ZONE = __UNCONST(nast[i]);
1039
#endif
1040
                    bp = ep;
1041
                    continue;
1042
                }
1043
                ep = find_string(bp, &i, nadt, NULL, 4);
1044
                if (ep != NULL) {
1045
                    tm->tm_isdst = 1;
1046
#ifdef TM_GMTOFF
1047
                    tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
1048
#endif
1049
#ifdef TM_ZONE
1050
                    tm->TM_ZONE = __UNCONST(nadt[i]);
1051
#endif
1052
                    bp = ep;
1053
                    continue;
1054
                }
1055
                /*
1056
                 * Our current timezone
1057
                 */
1058
                ep = find_string(bp, &i,
1059
                             (const char * const *)tzname,
1060
                              NULL, 2);
1061
                if (ep != NULL) {
1062
                    tm->tm_isdst = i;
1063
#ifdef TM_GMTOFF
1064
                    tm->TM_GMTOFF = -timezone;
1065
#endif
1066
#ifdef TM_ZONE
1067
                    tm->TM_ZONE = tzname[i];
1068
#endif
1069
                    bp = ep;
1070
                    continue;
1071
                }
1072
loadzone:
1073
                /*
1074
                 * The hard way, load the zone!
1075
                 */
1076
                if (fromzone(&bp, tm, mandatory))
1077
                    continue;
1078
                goto out;
1079
            }
1080
            offs = 0;
1081
            for (i = 0; i < 4; ) {
1082
                if (isdigit(*bp)) {
1083
                    offs = offs * 10 + (*bp++ - '0');
1084
                    i++;
1085
                    continue;
1086
                }
1087
                if (i == 2 && *bp == ':') {
1088
                    bp++;
1089
                    continue;
1090
                }
1091
                break;
1092
            }
1093
            if (isdigit(*bp))
1094
                goto out;
1095
            switch (i) {
1096
            case 2:
1097
                offs *= SECSPERHOUR;
1098
                break;
1099
            case 4:
1100
                i = offs % 100;
1101
                offs /= 100;
1102
                if (i >= SECSPERMIN)
1103
                    goto out;
1104
                /* Convert minutes into decimal */
1105
                offs = offs * SECSPERHOUR + i * SECSPERMIN;
1106
                break;
1107
            default:
1108
out:
1109
                if (mandatory)
1110
                    return NULL;
1111
                bp = zname;
1112
                continue;
1113
            }
1114
            /* ISO 8601 & RFC 3339 limit to 23:59 max */
1115
            if (offs >= (HOURSPERDAY * SECSPERHOUR))
1116
                goto out;
1117
            if (neg)
1118
                offs = -offs;
1119
            tm->tm_isdst = 0; /* XXX */
1120
#ifdef TM_GMTOFF
1121
            tm->TM_GMTOFF = offs;
1122
#endif
1123
#ifdef TM_ZONE
1124
            tm->TM_ZONE = NULL; /* XXX */
1125
#endif
1126
            continue;
1127
1128
        /*
1129
         * Miscellaneous conversions.
1130
         */
1131
        case 'n': /* Any kind of white-space. */
1132
        case 't':
1133
            while (isspace(*bp))
1134
                bp++;
1135
            LEGAL_ALT(0);
1136
            continue;
1137
1138
1139
        default:  /* Unknown/unsupported conversion. */
1140
            return NULL;
1141
        }
1142
    }
1143
1144
    if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
1145
        if (HAVE_MON(state) && HAVE_MDAY(state)) {
1146
            /* calculate day of year (ordinal date) */
1147
            tm->tm_yday =  start_of_month[isleap_sum(tm->tm_year,
1148
                TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
1149
            state |= S_YDAY;
1150
        } else if (day_offset != -1) {
1151
            /*
1152
             * Set the date to the first Sunday (or Monday)
1153
             * of the specified week of the year.
1154
             */
1155
            if (!HAVE_WDAY(state)) {
1156
                tm->tm_wday = day_offset;
1157
                state |= S_WDAY;
1158
            }
1159
            tm->tm_yday = (7 -
1160
                first_wday_of(tm->tm_year + TM_YEAR_BASE) +
1161
                day_offset) % 7 + (week_offset - 1) * 7 +
1162
                tm->tm_wday  - day_offset;
1163
            state |= S_YDAY;
1164
        }
1165
    }
1166
1167
    if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
1168
        int isleap;
1169
1170
        if (!HAVE_MON(state)) {
1171
            /* calculate month of day of year */
1172
            i = 0;
1173
            isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
1174
            while (tm->tm_yday >= start_of_month[isleap][i])
1175
                i++;
1176
            if (i > 12) {
1177
                i = 1;
1178
                tm->tm_yday -= start_of_month[isleap][12];
1179
                tm->tm_year++;
1180
            }
1181
            tm->tm_mon = i - 1;
1182
            state |= S_MON;
1183
        }
1184
1185
        if (!HAVE_MDAY(state)) {
1186
            /* calculate day of month */
1187
            isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
1188
            tm->tm_mday = tm->tm_yday -
1189
                start_of_month[isleap][tm->tm_mon] + 1;
1190
            state |= S_MDAY;
1191
        }
1192
1193
        if (!HAVE_WDAY(state)) {
1194
            /* calculate day of week */
1195
            i = 0;
1196
            week_offset = first_wday_of(tm->tm_year);
1197
            while (i++ <= tm->tm_yday) {
1198
                if (week_offset++ >= 6)
1199
                    week_offset = 0;
1200
            }
1201
            tm->tm_wday = week_offset;
1202
            state |= S_WDAY;
1203
        }
1204
    }
1205
1206
    return (char*)bp;
1207
}
1208
1209
1210
static const unsigned char *
1211
conv_num(const unsigned char *buf, int *dest, unsigned int llim, unsigned int ulim)
1212
{
1213
    unsigned int result = 0;
1214
    unsigned char ch;
1215
1216
    /* The limit also determines the number of valid digits. */
1217
    unsigned int rulim = ulim;
1218
1219
    ch = *buf;
1220
    if (ch < '0' || ch > '9')
1221
        return NULL;
1222
1223
    do {
1224
        result *= 10;
1225
        result += ch - '0';
1226
        rulim /= 10;
1227
        ch = *++buf;
1228
    } while ((result <= ulim) && rulim && ch >= '0' && ch <= '9');
1229
1230
    if (result < llim || result > ulim)
1231
        return NULL;
1232
1233
    *dest = result;
1234
    return buf;
1235
}
1236
1237
static const unsigned char *
1238
find_string(const unsigned char *bp, int *tgt, const char * const *n1,
1239
        const char * const *n2, int c)
1240
{
1241
    int i;
1242
    size_t len;
1243
1244
    /* check full name - then abbreviated ones */
1245
    for (; n1 != NULL; n1 = n2, n2 = NULL) {
1246
        for (i = 0; i < c; i++, n1++) {
1247
            len = strlen(*n1);
1248
            if (strncasecmp(*n1, (const char *)bp, len) == 0) {
1249
                *tgt = i;
1250
                return bp + len;
1251
            }
1252
        }
1253
    }
1254
1255
    /* Nothing matched */
1256
    return NULL;
1257
}
1258
#endif