suricata
util-strptime.c
Go to the documentation of this file.
1 /* $NetBSD: strptime.c,v 1.36 2012/03/13 21:13:48 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code was contributed to The NetBSD Foundation by Klaus Klein.
8  * Heavily optimised by David Laight
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: strptime.c,v 1.36 2012/03/13 21:13:48 christos Exp $");
35 #endif
36 
37 #include "namespace.h"
38 #include <sys/localedef.h>
39 */
40 #include "suricata-common.h"
41 #ifndef HAVE_STRPTIME
42 #include <ctype.h>
43 #include <locale.h>
44 #include <string.h>
45 #include <time.h>
46 #include <stdint.h>
47 /*
48 #include <tzfile.h>
49 #include "private.h"
50 
51 #ifdef __weak_alias
52 __weak_alias(strptime,_strptime)
53 #endif
54 */
55 
56 #define _ctloc(x) (_CurrentTimeLocale->x)
57 
58 /*
59  * We do not implement alternate representations. However, we always
60  * check whether a given modifier is allowed for a certain conversion.
61  */
62 #define ALT_E 0x01
63 #define ALT_O 0x02
64 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
65 
66 static int TM_YEAR_BASE = 1900;
67 static char gmt[] = { "GMT" };
68 static char utc[] = { "UTC" };
69 /* RFC-822/RFC-2822 */
70 static const char * const nast[5] = {
71  "EST", "CST", "MST", "PST", "\0\0\0"
72 };
73 static const char * const nadt[5] = {
74  "EDT", "CDT", "MDT", "PDT", "\0\0\0"
75 };
76 static const char * const am_pm[2] = {
77  "am", "pm"
78 };
79 static const char * const day[7] = {
80  "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
81 };
82 static const char * const abday[7] = {
83  "sun", "mon", "tue", "wed", "thu", "fri", "sat"
84 };
85 static const char * const mon[12] = {
86  "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"
87 };
88 static const char * const abmon[12] = {
89  "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"
90 };
91 
92 static const u_char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
93 static const u_char *find_string(const u_char *, int *, const char * const *,
94  const char * const *, int);
95 
96 char *
97 strptime(const char *buf, const char *fmt, struct tm *tm)
98 {
99  unsigned char c;
100  const unsigned char *bp, *ep;
101  int alt_format, i, split_year = 0, neg = 0, offs;
102  const char *new_fmt;
103 
104  bp = (const u_char *)buf;
105 
106  while (bp != NULL && (c = *fmt++) != '\0') {
107  /* Clear `alternate' modifier prior to new conversion. */
108  alt_format = 0;
109  i = 0;
110 
111  /* Eat up white-space. */
112  if (isspace(c)) {
113  while (isspace(*bp))
114  bp++;
115  continue;
116  }
117 
118  if (c != '%')
119  goto literal;
120 
121 
122 again: switch (c = *fmt++) {
123  case '%': /* "%%" is converted to "%". */
124 literal:
125  if (c != *bp++)
126  return NULL;
127  LEGAL_ALT(0);
128  continue;
129 
130  /*
131  * "Alternative" modifiers. Just set the appropriate flag
132  * and start over again.
133  */
134  case 'E': /* "%E?" alternative conversion modifier. */
135  LEGAL_ALT(0);
136  alt_format |= ALT_E;
137  goto again;
138 
139  case 'O': /* "%O?" alternative conversion modifier. */
140  LEGAL_ALT(0);
141  alt_format |= ALT_O;
142  goto again;
143 
144  /*
145  * "Complex" conversion rules, implemented through recursion.
146  */
147  /* we do not need 'c'
148  case 'c': Date and time, using the locale's format.
149  new_fmt = _ctloc(d_t_fmt);
150  goto recurse;
151  */
152 
153  case 'D': /* The date as "%m/%d/%y". */
154  new_fmt = "%m/%d/%y";
155  LEGAL_ALT(0);
156  goto recurse;
157 
158  case 'F': /* The date as "%Y-%m-%d". */
159  new_fmt = "%Y-%m-%d";
160  LEGAL_ALT(0);
161  goto recurse;
162 
163  case 'R': /* The time as "%H:%M". */
164  new_fmt = "%H:%M";
165  LEGAL_ALT(0);
166  goto recurse;
167 
168  case 'r': /* The time in 12-hour clock representation. */
169  new_fmt = "%I:%M:S %p";//_ctloc(t_fmt_ampm);
170  LEGAL_ALT(0);
171  goto recurse;
172 
173  case 'T': /* The time as "%H:%M:%S". */
174  new_fmt = "%H:%M:%S";
175  LEGAL_ALT(0);
176  goto recurse;
177 
178  /* we don't use 'X'
179  case 'X': The time, using the locale's format.
180  new_fmt =_ctloc(t_fmt);
181  goto recurse;
182  */
183 
184  /* we do not need 'x'
185  case 'x': The date, using the locale's format.
186  new_fmt =_ctloc(d_fmt);*/
187 recurse:
188  bp = (const u_char *)strptime((const char *)bp,
189  new_fmt, tm);
190  LEGAL_ALT(ALT_E);
191  continue;
192 
193  /*
194  * "Elementary" conversion rules.
195  */
196  case 'A': /* The day of week, using the locale's form. */
197  case 'a':
198  bp = find_string(bp, &tm->tm_wday, day, abday, 7);
199  LEGAL_ALT(0);
200  continue;
201 
202  case 'B': /* The month, using the locale's form. */
203  case 'b':
204  case 'h':
205  bp = find_string(bp, &tm->tm_mon, mon, abmon, 12);
206  LEGAL_ALT(0);
207  continue;
208 
209  case 'C': /* The century number. */
210  i = 20;
211  bp = conv_num(bp, &i, 0, 99);
212 
213  i = i * 100 - TM_YEAR_BASE;
214  if (split_year)
215  i += tm->tm_year % 100;
216  split_year = 1;
217  tm->tm_year = i;
218  LEGAL_ALT(ALT_E);
219  continue;
220 
221  case 'd': /* The day of month. */
222  case 'e':
223  bp = conv_num(bp, &tm->tm_mday, 1, 31);
224  LEGAL_ALT(ALT_O);
225  continue;
226 
227  case 'k': /* The hour (24-hour clock representation). */
228  LEGAL_ALT(0);
229  /* FALLTHROUGH */
230  case 'H':
231  bp = conv_num(bp, &tm->tm_hour, 0, 23);
232  LEGAL_ALT(ALT_O);
233  continue;
234 
235  case 'l': /* The hour (12-hour clock representation). */
236  LEGAL_ALT(0);
237  /* FALLTHROUGH */
238  case 'I':
239  bp = conv_num(bp, &tm->tm_hour, 1, 12);
240  if (tm->tm_hour == 12)
241  tm->tm_hour = 0;
242  LEGAL_ALT(ALT_O);
243  continue;
244 
245  case 'j': /* The day of year. */
246  i = 1;
247  bp = conv_num(bp, &i, 1, 366);
248  tm->tm_yday = i - 1;
249  LEGAL_ALT(0);
250  continue;
251 
252  case 'M': /* The minute. */
253  bp = conv_num(bp, &tm->tm_min, 0, 59);
254  LEGAL_ALT(ALT_O);
255  continue;
256 
257  case 'm': /* The month. */
258  i = 1;
259  bp = conv_num(bp, &i, 1, 12);
260  tm->tm_mon = i - 1;
261  LEGAL_ALT(ALT_O);
262  continue;
263 
264  case 'p': /* The locale's equivalent of AM/PM. */
265  bp = find_string(bp, &i, am_pm, NULL, 2);
266  if (tm->tm_hour > 11)
267  return NULL;
268  tm->tm_hour += i * 12;
269  LEGAL_ALT(0);
270  continue;
271 
272  case 'S': /* The seconds. */
273  bp = conv_num(bp, &tm->tm_sec, 0, 61);
274  LEGAL_ALT(ALT_O);
275  continue;
276 
277 #ifndef TIME_MAX
278 #define TIME_MAX INT64_MAX
279 #endif
280  case 's': /* seconds since the epoch */
281  {
282  time_t sse = 0;
283  uint64_t rulim = TIME_MAX;
284 
285  if (*bp < '0' || *bp > '9') {
286  bp = NULL;
287  continue;
288  }
289 
290  do {
291  sse *= 10;
292  sse += *bp++ - '0';
293  rulim /= 10;
294  } while (((uint64_t)sse * 10 <= TIME_MAX) &&
295  rulim && *bp >= '0' && *bp <= '9');
296 
297  if (sse < 0 || (uint64_t)sse > TIME_MAX) {
298  bp = NULL;
299  continue;
300  }
301 
302  tm = localtime(&sse);
303  if (tm == NULL)
304  bp = NULL;
305  }
306  continue;
307 
308  case 'U': /* The week of year, beginning on sunday. */
309  case 'W': /* The week of year, beginning on monday. */
310  /*
311  * XXX This is bogus, as we can not assume any valid
312  * information present in the tm structure at this
313  * point to calculate a real value, so just check the
314  * range for now.
315  */
316  bp = conv_num(bp, &i, 0, 53);
317  LEGAL_ALT(ALT_O);
318  continue;
319 
320  case 'w': /* The day of week, beginning on sunday. */
321  bp = conv_num(bp, &tm->tm_wday, 0, 6);
322  LEGAL_ALT(ALT_O);
323  continue;
324 
325  case 'u': /* The day of week, monday = 1. */
326  bp = conv_num(bp, &i, 1, 7);
327  tm->tm_wday = i % 7;
328  LEGAL_ALT(ALT_O);
329  continue;
330 
331  case 'g': /* The year corresponding to the ISO week
332  * number but without the century.
333  */
334  bp = conv_num(bp, &i, 0, 99);
335  continue;
336 
337  case 'G': /* The year corresponding to the ISO week
338  * number with century.
339  */
340  do
341  bp++;
342  while (isdigit(*bp));
343  continue;
344 
345  case 'V': /* The ISO 8601:1988 week number as decimal */
346  bp = conv_num(bp, &i, 0, 53);
347  continue;
348 
349  case 'Y': /* The year. */
350  i = TM_YEAR_BASE; /* just for data sanity... */
351  bp = conv_num(bp, &i, 0, 9999);
352  tm->tm_year = i - TM_YEAR_BASE;
353  LEGAL_ALT(ALT_E);
354  continue;
355 
356  case 'y': /* The year within 100 years of the epoch. */
357  /* LEGAL_ALT(ALT_E | ALT_O); */
358  bp = conv_num(bp, &i, 0, 99);
359 
360  if (split_year)
361  /* preserve century */
362  i += (tm->tm_year / 100) * 100;
363  else {
364  split_year = 1;
365  if (i <= 68)
366  i = i + 2000 - TM_YEAR_BASE;
367  else
368  i = i + 1900 - TM_YEAR_BASE;
369  }
370  tm->tm_year = i;
371  continue;
372 
373  case 'Z':
374  tzset();
375  if (strncasecmp((const char *)bp, gmt, 3) == 0
376  || strncasecmp((const char *)bp, utc, 3) == 0) {
377  tm->tm_isdst = 0;
378 #ifdef TM_GMTOFF
379  tm->TM_GMTOFF = 0;
380 #endif
381 #ifdef TM_ZONE
382  tm->TM_ZONE = gmt;
383 #endif
384  bp += 3;
385  } else {
386  ep = find_string(bp, &i,
387  (const char * const *)tzname,
388  NULL, 2);
389  if (ep != NULL) {
390  tm->tm_isdst = i;
391 #ifdef TM_GMTOFF
392  tm->TM_GMTOFF = -(timezone);
393 #endif
394 #ifdef TM_ZONE
395  tm->TM_ZONE = tzname[i];
396 #endif
397  }
398  bp = ep;
399  }
400  continue;
401 
402  case 'z':
403  /*
404  * We recognize all ISO 8601 formats:
405  * Z = Zulu time/UTC
406  * [+-]hhmm
407  * [+-]hh:mm
408  * [+-]hh
409  * We recognize all RFC-822/RFC-2822 formats:
410  * UT|GMT
411  * North American : UTC offsets
412  * E[DS]T = Eastern : -4 | -5
413  * C[DS]T = Central : -5 | -6
414  * M[DS]T = Mountain: -6 | -7
415  * P[DS]T = Pacific : -7 | -8
416  * Military
417  * [A-IL-M] = -1 ... -9 (J not used)
418  * [N-Y] = +1 ... +12
419  */
420  while (isspace(*bp))
421  bp++;
422 
423  switch (*bp++) {
424  case 'G':
425  if (*bp++ != 'M')
426  return NULL;
427  /*FALLTHROUGH*/
428  case 'U':
429  if (*bp++ != 'T')
430  return NULL;
431  /*FALLTHROUGH*/
432  case 'Z':
433  tm->tm_isdst = 0;
434 #ifdef TM_GMTOFF
435  tm->TM_GMTOFF = 0;
436 #endif
437 #ifdef TM_ZONE
438  tm->TM_ZONE = utc;
439 #endif
440  continue;
441  case '+':
442  neg = 0;
443  break;
444  case '-':
445  neg = 1;
446  break;
447  default:
448  --bp;
449  ep = find_string(bp, &i, nast, NULL, 4);
450  if (ep != NULL) {
451 #ifdef TM_GMTOFF
452  tm->TM_GMTOFF = -5 - i;
453 #endif
454 #ifdef TM_ZONE
455  tm->TM_ZONE = __UNCONST(nast[i]);
456 #endif
457  bp = ep;
458  continue;
459  }
460  ep = find_string(bp, &i, nadt, NULL, 4);
461  if (ep != NULL) {
462  tm->tm_isdst = 1;
463 #ifdef TM_GMTOFF
464  tm->TM_GMTOFF = -4 - i;
465 #endif
466 #ifdef TM_ZONE
467  tm->TM_ZONE = __UNCONST(nadt[i]);
468 #endif
469  bp = ep;
470  continue;
471  }
472 
473  if ((*bp >= 'A' && *bp <= 'I') ||
474  (*bp >= 'L' && *bp <= 'Y')) {
475 #ifdef TM_GMTOFF
476  /* Argh! No 'J'! */
477  if (*bp >= 'A' && *bp <= 'I')
478  tm->TM_GMTOFF =
479  ('A' - 1) - (int)*bp;
480  else if (*bp >= 'L' && *bp <= 'M')
481  tm->TM_GMTOFF = 'A' - (int)*bp;
482  else if (*bp >= 'N' && *bp <= 'Y')
483  tm->TM_GMTOFF = (int)*bp - 'M';
484 #endif
485 #ifdef TM_ZONE
486  tm->TM_ZONE = NULL; /* XXX */
487 #endif
488  bp++;
489  continue;
490  }
491  return NULL;
492  }
493  offs = 0;
494  for (i = 0; i < 4; ) {
495  if (isdigit(*bp)) {
496  offs = offs * 10 + (*bp++ - '0');
497  i++;
498  continue;
499  }
500  if (i == 2 && *bp == ':') {
501  bp++;
502  continue;
503  }
504  break;
505  }
506  switch (i) {
507  case 2:
508  offs *= 100;
509  break;
510  case 4:
511  i = offs % 100;
512  if (i >= 60)
513  return NULL;
514  /* Convert minutes into decimal */
515  offs = (offs / 100) * 100 + (i * 50) / 30;
516  break;
517  default:
518  return NULL;
519  }
520  if (neg)
521  offs = -offs;
522  tm->tm_isdst = 0; /* XXX */
523 #ifdef TM_GMTOFF
524  tm->TM_GMTOFF = offs;
525 #endif
526 #ifdef TM_ZONE
527  tm->TM_ZONE = NULL; /* XXX */
528 #endif
529  continue;
530 
531  /*
532  * Miscellaneous conversions.
533  */
534  case 'n': /* Any kind of white-space. */
535  case 't':
536  while (isspace(*bp))
537  bp++;
538  LEGAL_ALT(0);
539  continue;
540 
541 
542  default: /* Unknown/unsupported conversion. */
543  return NULL;
544  }
545  }
546 
547  return (char *)(bp);
548 }
549 
550 
551 static const u_char *
552 conv_num(const unsigned char *buf, int *dest, unsigned int llim, unsigned int ulim)
553 {
554  unsigned int result = 0;
555  unsigned char ch;
556 
557  /* The limit also determines the number of valid digits. */
558  unsigned int rulim = ulim;
559 
560  ch = *buf;
561  if (ch < '0' || ch > '9')
562  return NULL;
563 
564  do {
565  result *= 10;
566  result += ch - '0';
567  rulim /= 10;
568  ch = *++buf;
569  } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
570 
571  if (result < llim || result > ulim)
572  return NULL;
573 
574  *dest = result;
575  return buf;
576 }
577 
578 static const u_char *
579 find_string(const u_char *bp, int *tgt, const char * const *n1,
580  const char * const *n2, int c)
581 {
582  int i;
583  size_t len;
584 
585  /* check full name - then abbreviated ones */
586  for (; n1 != NULL; n1 = n2, n2 = NULL) {
587  for (i = 0; i < c; i++, n1++) {
588  len = strlen(*n1);
589  if (strncasecmp(*n1, (const char *)bp, len) == 0) {
590  *tgt = i;
591  return bp + len;
592  }
593  }
594  }
595 
596  /* Nothing matched */
597  return NULL;
598 }
599 #endif /* HAVE_STRPTIME */
len
uint8_t len
Definition: app-layer-dnp3.h:2
LEGAL_ALT
#define LEGAL_ALT(x)
Definition: util-strptime.c:64
strptime
char * strptime(const char *buf, const char *fmt, struct tm *tm)
Definition: util-strptime.c:97
ALT_O
#define ALT_O
Definition: util-strptime.c:63
suricata-common.h
TIME_MAX
#define TIME_MAX
ALT_E
#define ALT_E
Definition: util-strptime.c:62