strptime.c revision 1.54 1 /* $NetBSD: strptime.c,v 1.54 2015/10/30 18:04:42 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.54 2015/10/30 18:04:42 christos Exp $");
35 #endif
36
37 #include "namespace.h"
38 #include <sys/localedef.h>
39 #include <sys/types.h>
40 #include <ctype.h>
41 #include <locale.h>
42 #include <string.h>
43 #include <time.h>
44 #include <tzfile.h>
45 #include "private.h"
46 #include "setlocale_local.h"
47
48 #ifdef __weak_alias
49 __weak_alias(strptime,_strptime)
50 __weak_alias(strptime_l, _strptime_l)
51 #endif
52
53 static const u_char *conv_num(const unsigned char *, int *, uint, uint);
54 static const u_char *find_string(const u_char *, int *, const char * const *,
55 const char * const *, int);
56
57 #define _TIME_LOCALE(loc) \
58 ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
59
60 /*
61 * We do not implement alternate representations. However, we always
62 * check whether a given modifier is allowed for a certain conversion.
63 */
64 #define ALT_E 0x01
65 #define ALT_O 0x02
66 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
67
68 #define S_YEAR (1 << 0)
69 #define S_MON (1 << 1)
70 #define S_YDAY (1 << 2)
71 #define S_MDAY (1 << 3)
72 #define S_WDAY (1 << 4)
73 #define S_HOUR (1 << 5)
74
75 #define HAVE_MDAY(s) (s & S_MDAY)
76 #define HAVE_MON(s) (s & S_MON)
77 #define HAVE_WDAY(s) (s & S_WDAY)
78 #define HAVE_YDAY(s) (s & S_YDAY)
79 #define HAVE_YEAR(s) (s & S_YEAR)
80 #define HAVE_HOUR(s) (s & S_HOUR)
81
82 static char gmt[] = { "GMT" };
83 static char utc[] = { "UTC" };
84 /* RFC-822/RFC-2822 */
85 static const char * const nast[5] = {
86 "EST", "CST", "MST", "PST", "\0\0\0"
87 };
88 static const char * const nadt[5] = {
89 "EDT", "CDT", "MDT", "PDT", "\0\0\0"
90 };
91
92 /*
93 * Table to determine the ordinal date for the start of a month.
94 * Ref: http://en.wikipedia.org/wiki/ISO_week_date
95 */
96 static const int start_of_month[2][13] = {
97 /* non-leap year */
98 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
99 /* leap year */
100 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
101 };
102
103 /*
104 * Calculate the week day of the first day of a year. Valid for
105 * the Gregorian calendar, which began Sept 14, 1752 in the UK
106 * and its colonies. Ref:
107 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
108 */
109
110 static int
111 first_wday_of(int yr)
112 {
113 return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
114 (isleap(yr) ? 6 : 0) + 1) % 7;
115 }
116
117 #define delim(p) ((p) == '\0' || isspace((unsigned char)(p)))
118
119 static int
120 fromzone(const unsigned char **bp, struct tm *tm)
121 {
122 timezone_t tz;
123 char buf[512], *p;
124
125 for (p = buf; !delim(**bp) && p < &buf[sizeof(buf) - 1]; (*bp)++)
126 *p++ = **bp;
127 *p = '\0';
128
129 tz = tzalloc(buf);
130 if (tz == NULL)
131 return 0;
132
133 tm->tm_isdst = 0; /* XXX */
134 #ifdef TM_GMTOFF
135 tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
136 #endif
137 #ifdef TM_ZONE
138 // Can't use tzgetname() here because we are going to free()
139 tm->TM_ZONE = NULL; /* XXX */
140 #endif
141 tzfree(tz);
142 return 1;
143 }
144
145 char *
146 strptime(const char *buf, const char *fmt, struct tm *tm)
147 {
148 return strptime_l(buf, fmt, tm, _current_locale());
149 }
150
151 char *
152 strptime_l(const char *buf, const char *fmt, struct tm *tm, locale_t loc)
153 {
154 unsigned char c;
155 const unsigned char *bp, *ep, *zname;
156 int alt_format, i, split_year = 0, neg = 0, state = 0,
157 day_offset = -1, week_offset = 0, offs;
158 const char *new_fmt;
159
160 bp = (const u_char *)buf;
161
162 while (bp != NULL && (c = *fmt++) != '\0') {
163 /* Clear `alternate' modifier prior to new conversion. */
164 alt_format = 0;
165 i = 0;
166
167 /* Eat up white-space. */
168 if (isspace(c)) {
169 while (isspace(*bp))
170 bp++;
171 continue;
172 }
173
174 if (c != '%')
175 goto literal;
176
177
178 again: switch (c = *fmt++) {
179 case '%': /* "%%" is converted to "%". */
180 literal:
181 if (c != *bp++)
182 return NULL;
183 LEGAL_ALT(0);
184 continue;
185
186 /*
187 * "Alternative" modifiers. Just set the appropriate flag
188 * and start over again.
189 */
190 case 'E': /* "%E?" alternative conversion modifier. */
191 LEGAL_ALT(0);
192 alt_format |= ALT_E;
193 goto again;
194
195 case 'O': /* "%O?" alternative conversion modifier. */
196 LEGAL_ALT(0);
197 alt_format |= ALT_O;
198 goto again;
199
200 /*
201 * "Complex" conversion rules, implemented through recursion.
202 */
203 case 'c': /* Date and time, using the locale's format. */
204 new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
205 state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
206 goto recurse;
207
208 case 'D': /* The date as "%m/%d/%y". */
209 new_fmt = "%m/%d/%y";
210 LEGAL_ALT(0);
211 state |= S_MON | S_MDAY | S_YEAR;
212 goto recurse;
213
214 case 'F': /* The date as "%Y-%m-%d". */
215 new_fmt = "%Y-%m-%d";
216 LEGAL_ALT(0);
217 state |= S_MON | S_MDAY | S_YEAR;
218 goto recurse;
219
220 case 'R': /* The time as "%H:%M". */
221 new_fmt = "%H:%M";
222 LEGAL_ALT(0);
223 goto recurse;
224
225 case 'r': /* The time in 12-hour clock representation. */
226 new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
227 LEGAL_ALT(0);
228 goto recurse;
229
230 case 'T': /* The time as "%H:%M:%S". */
231 new_fmt = "%H:%M:%S";
232 LEGAL_ALT(0);
233 goto recurse;
234
235 case 'X': /* The time, using the locale's format. */
236 new_fmt = _TIME_LOCALE(loc)->t_fmt;
237 goto recurse;
238
239 case 'x': /* The date, using the locale's format. */
240 new_fmt = _TIME_LOCALE(loc)->d_fmt;
241 state |= S_MON | S_MDAY | S_YEAR;
242 recurse:
243 bp = (const u_char *)strptime((const char *)bp,
244 new_fmt, tm);
245 LEGAL_ALT(ALT_E);
246 continue;
247
248 /*
249 * "Elementary" conversion rules.
250 */
251 case 'A': /* The day of week, using the locale's form. */
252 case 'a':
253 bp = find_string(bp, &tm->tm_wday,
254 _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
255 LEGAL_ALT(0);
256 state |= S_WDAY;
257 continue;
258
259 case 'B': /* The month, using the locale's form. */
260 case 'b':
261 case 'h':
262 bp = find_string(bp, &tm->tm_mon,
263 _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
264 12);
265 LEGAL_ALT(0);
266 state |= S_MON;
267 continue;
268
269 case 'C': /* The century number. */
270 i = 20;
271 bp = conv_num(bp, &i, 0, 99);
272
273 i = i * 100 - TM_YEAR_BASE;
274 if (split_year)
275 i += tm->tm_year % 100;
276 split_year = 1;
277 tm->tm_year = i;
278 LEGAL_ALT(ALT_E);
279 state |= S_YEAR;
280 continue;
281
282 case 'd': /* The day of month. */
283 case 'e':
284 bp = conv_num(bp, &tm->tm_mday, 1, 31);
285 LEGAL_ALT(ALT_O);
286 state |= S_MDAY;
287 continue;
288
289 case 'k': /* The hour (24-hour clock representation). */
290 LEGAL_ALT(0);
291 /* FALLTHROUGH */
292 case 'H':
293 bp = conv_num(bp, &tm->tm_hour, 0, 23);
294 LEGAL_ALT(ALT_O);
295 state |= S_HOUR;
296 continue;
297
298 case 'l': /* The hour (12-hour clock representation). */
299 LEGAL_ALT(0);
300 /* FALLTHROUGH */
301 case 'I':
302 bp = conv_num(bp, &tm->tm_hour, 1, 12);
303 if (tm->tm_hour == 12)
304 tm->tm_hour = 0;
305 LEGAL_ALT(ALT_O);
306 state |= S_HOUR;
307 continue;
308
309 case 'j': /* The day of year. */
310 i = 1;
311 bp = conv_num(bp, &i, 1, 366);
312 tm->tm_yday = i - 1;
313 LEGAL_ALT(0);
314 state |= S_YDAY;
315 continue;
316
317 case 'M': /* The minute. */
318 bp = conv_num(bp, &tm->tm_min, 0, 59);
319 LEGAL_ALT(ALT_O);
320 continue;
321
322 case 'm': /* The month. */
323 i = 1;
324 bp = conv_num(bp, &i, 1, 12);
325 tm->tm_mon = i - 1;
326 LEGAL_ALT(ALT_O);
327 state |= S_MON;
328 continue;
329
330 case 'p': /* The locale's equivalent of AM/PM. */
331 bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
332 NULL, 2);
333 if (HAVE_HOUR(state) && tm->tm_hour > 11)
334 return NULL;
335 tm->tm_hour += i * 12;
336 LEGAL_ALT(0);
337 continue;
338
339 case 'S': /* The seconds. */
340 bp = conv_num(bp, &tm->tm_sec, 0, 61);
341 LEGAL_ALT(ALT_O);
342 continue;
343
344 #ifndef TIME_MAX
345 #define TIME_MAX INT64_MAX
346 #endif
347 case 's': /* seconds since the epoch */
348 {
349 time_t sse = 0;
350 uint64_t rulim = TIME_MAX;
351
352 if (*bp < '0' || *bp > '9') {
353 bp = NULL;
354 continue;
355 }
356
357 do {
358 sse *= 10;
359 sse += *bp++ - '0';
360 rulim /= 10;
361 } while ((sse * 10 <= TIME_MAX) &&
362 rulim && *bp >= '0' && *bp <= '9');
363
364 if (sse < 0 || (uint64_t)sse > TIME_MAX) {
365 bp = NULL;
366 continue;
367 }
368
369 if (localtime_r(&sse, tm) == NULL)
370 bp = NULL;
371 else
372 state |= S_YDAY | S_WDAY |
373 S_MON | S_MDAY | S_YEAR;
374 }
375 continue;
376
377 case 'U': /* The week of year, beginning on sunday. */
378 case 'W': /* The week of year, beginning on monday. */
379 /*
380 * XXX This is bogus, as we can not assume any valid
381 * information present in the tm structure at this
382 * point to calculate a real value, so just check the
383 * range for now.
384 */
385 bp = conv_num(bp, &i, 0, 53);
386 LEGAL_ALT(ALT_O);
387 if (c == 'U')
388 day_offset = TM_SUNDAY;
389 else
390 day_offset = TM_MONDAY;
391 week_offset = i;
392 continue;
393
394 case 'w': /* The day of week, beginning on sunday. */
395 bp = conv_num(bp, &tm->tm_wday, 0, 6);
396 LEGAL_ALT(ALT_O);
397 state |= S_WDAY;
398 continue;
399
400 case 'u': /* The day of week, monday = 1. */
401 bp = conv_num(bp, &i, 1, 7);
402 tm->tm_wday = i % 7;
403 LEGAL_ALT(ALT_O);
404 state |= S_WDAY;
405 continue;
406
407 case 'g': /* The year corresponding to the ISO week
408 * number but without the century.
409 */
410 bp = conv_num(bp, &i, 0, 99);
411 continue;
412
413 case 'G': /* The year corresponding to the ISO week
414 * number with century.
415 */
416 do
417 bp++;
418 while (isdigit(*bp));
419 continue;
420
421 case 'V': /* The ISO 8601:1988 week number as decimal */
422 bp = conv_num(bp, &i, 0, 53);
423 continue;
424
425 case 'Y': /* The year. */
426 i = TM_YEAR_BASE; /* just for data sanity... */
427 bp = conv_num(bp, &i, 0, 9999);
428 tm->tm_year = i - TM_YEAR_BASE;
429 LEGAL_ALT(ALT_E);
430 state |= S_YEAR;
431 continue;
432
433 case 'y': /* The year within 100 years of the epoch. */
434 /* LEGAL_ALT(ALT_E | ALT_O); */
435 bp = conv_num(bp, &i, 0, 99);
436
437 if (split_year)
438 /* preserve century */
439 i += (tm->tm_year / 100) * 100;
440 else {
441 split_year = 1;
442 if (i <= 68)
443 i = i + 2000 - TM_YEAR_BASE;
444 else
445 i = i + 1900 - TM_YEAR_BASE;
446 }
447 tm->tm_year = i;
448 state |= S_YEAR;
449 continue;
450
451 case 'Z':
452 tzset();
453 if (strncmp((const char *)bp, gmt, 3) == 0 ||
454 strncmp((const char *)bp, utc, 3) == 0) {
455 tm->tm_isdst = 0;
456 #ifdef TM_GMTOFF
457 tm->TM_GMTOFF = 0;
458 #endif
459 #ifdef TM_ZONE
460 tm->TM_ZONE = gmt;
461 #endif
462 bp += 3;
463 } else {
464 ep = find_string(bp, &i,
465 (const char * const *)tzname,
466 NULL, 2);
467 if (ep != NULL) {
468 tm->tm_isdst = i;
469 #ifdef TM_GMTOFF
470 tm->TM_GMTOFF = -timezone;
471 #endif
472 #endif
473 #ifdef TM_ZONE
474 tm->TM_ZONE = tzname[i];
475 bp = ep;
476 #endif
477 } else
478 (void)fromzone(&bp, tm);
479 }
480 continue;
481
482 case 'z':
483 /*
484 * We recognize all ISO 8601 formats:
485 * Z = Zulu time/UTC
486 * [+-]hhmm
487 * [+-]hh:mm
488 * [+-]hh
489 * We recognize all RFC-822/RFC-2822 formats:
490 * UT|GMT
491 * North American : UTC offsets
492 * E[DS]T = Eastern : -4 | -5
493 * C[DS]T = Central : -5 | -6
494 * M[DS]T = Mountain: -6 | -7
495 * P[DS]T = Pacific : -7 | -8
496 * Military
497 * [A-IL-M] = -1 ... -9 (J not used)
498 * [N-Y] = +1 ... +12
499 */
500 while (isspace(*bp))
501 bp++;
502
503 zname = bp;
504 switch (*bp++) {
505 case 'G':
506 if (*bp++ != 'M')
507 goto namedzone;
508 /*FALLTHROUGH*/
509 case 'U':
510 if (*bp++ != 'T')
511 goto namedzone;
512 else if (!delim(*bp) && *bp++ != 'C')
513 goto namedzone;
514 /*FALLTHROUGH*/
515 case 'Z':
516 if (!delim(*bp))
517 goto namedzone;
518 tm->tm_isdst = 0;
519 #ifdef TM_GMTOFF
520 tm->TM_GMTOFF = 0;
521 #endif
522 #ifdef TM_ZONE
523 tm->TM_ZONE = utc;
524 #endif
525 continue;
526 case '+':
527 neg = 0;
528 break;
529 case '-':
530 neg = 1;
531 break;
532 default:
533 namedzone:
534 bp = zname;
535
536 /* Military style */
537 if (delim(bp[1]) &&
538 ((*bp >= 'A' && *bp <= 'I') ||
539 (*bp >= 'L' && *bp <= 'Y'))) {
540 #ifdef TM_GMTOFF
541 /* Argh! No 'J'! */
542 if (*bp >= 'A' && *bp <= 'I')
543 tm->TM_GMTOFF =
544 ('A' - 1) - (int)*bp;
545 else if (*bp >= 'L' && *bp <= 'M')
546 tm->TM_GMTOFF = 'A' - (int)*bp;
547 else if (*bp >= 'N' && *bp <= 'Y')
548 tm->TM_GMTOFF = (int)*bp - 'M';
549 tm->TM_GMTOFF *= SECSPERHOUR;
550 #endif
551 #ifdef TM_ZONE
552 tm->TM_ZONE = NULL; /* XXX */
553 #endif
554 bp++;
555 continue;
556 }
557
558 /*
559 * From our 3 letter hard-coded table
560 * XXX: Can be removed, handled by tzload()
561 */
562 if (delim(bp[0]) || delim(bp[1]) ||
563 delim(bp[2]) || !delim(bp[3]))
564 goto loadzone;
565 ep = find_string(bp, &i, nast, NULL, 4);
566 if (ep != NULL) {
567 #ifdef TM_GMTOFF
568 tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
569 #endif
570 #ifdef TM_ZONE
571 tm->TM_ZONE = __UNCONST(nast[i]);
572 #endif
573 bp = ep;
574 continue;
575 }
576 ep = find_string(bp, &i, nadt, NULL, 4);
577 if (ep != NULL) {
578 tm->tm_isdst = 1;
579 #ifdef TM_GMTOFF
580 tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
581 #endif
582 #ifdef TM_ZONE
583 tm->TM_ZONE = __UNCONST(nadt[i]);
584 #endif
585 bp = ep;
586 continue;
587 }
588
589 loadzone:
590 /*
591 * The hard way, load the zone!
592 */
593 if (fromzone(&bp, tm))
594 continue;
595 return NULL;
596 }
597 offs = 0;
598 for (i = 0; i < 4; ) {
599 if (isdigit(*bp)) {
600 offs = offs * 10 + (*bp++ - '0');
601 i++;
602 continue;
603 }
604 if (i == 2 && *bp == ':') {
605 bp++;
606 continue;
607 }
608 break;
609 }
610 if (isdigit(*bp))
611 return NULL;
612 switch (i) {
613 case 2:
614 offs *= SECSPERHOUR;
615 break;
616 case 4:
617 i = offs % 100;
618 if (i >= SECSPERMIN)
619 return NULL;
620 /* Convert minutes into decimal */
621 offs = (offs / 100) * SECSPERHOUR + i * SECSPERMIN;
622 break;
623 default:
624 return NULL;
625 }
626 if (offs >= (HOURSPERDAY * SECSPERHOUR))
627 return NULL;
628 if (neg)
629 offs = -offs;
630 tm->tm_isdst = 0; /* XXX */
631 #ifdef TM_GMTOFF
632 tm->TM_GMTOFF = offs;
633 #endif
634 #ifdef TM_ZONE
635 tm->TM_ZONE = NULL; /* XXX */
636 #endif
637 continue;
638
639 /*
640 * Miscellaneous conversions.
641 */
642 case 'n': /* Any kind of white-space. */
643 case 't':
644 while (isspace(*bp))
645 bp++;
646 LEGAL_ALT(0);
647 continue;
648
649
650 default: /* Unknown/unsupported conversion. */
651 return NULL;
652 }
653 }
654
655 if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
656 if (HAVE_MON(state) && HAVE_MDAY(state)) {
657 /* calculate day of year (ordinal date) */
658 tm->tm_yday = start_of_month[isleap_sum(tm->tm_year,
659 TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
660 state |= S_YDAY;
661 } else if (day_offset != -1) {
662 /*
663 * Set the date to the first Sunday (or Monday)
664 * of the specified week of the year.
665 */
666 if (!HAVE_WDAY(state)) {
667 tm->tm_wday = day_offset;
668 state |= S_WDAY;
669 }
670 tm->tm_yday = (7 -
671 first_wday_of(tm->tm_year + TM_YEAR_BASE) +
672 day_offset) % 7 + (week_offset - 1) * 7 +
673 tm->tm_wday - day_offset;
674 state |= S_YDAY;
675 }
676 }
677
678 if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
679 int isleap;
680
681 if (!HAVE_MON(state)) {
682 /* calculate month of day of year */
683 i = 0;
684 isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
685 while (tm->tm_yday >= start_of_month[isleap][i])
686 i++;
687 if (i > 12) {
688 i = 1;
689 tm->tm_yday -= start_of_month[isleap][12];
690 tm->tm_year++;
691 }
692 tm->tm_mon = i - 1;
693 state |= S_MON;
694 }
695
696 if (!HAVE_MDAY(state)) {
697 /* calculate day of month */
698 isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
699 tm->tm_mday = tm->tm_yday -
700 start_of_month[isleap][tm->tm_mon] + 1;
701 state |= S_MDAY;
702 }
703
704 if (!HAVE_WDAY(state)) {
705 /* calculate day of week */
706 i = 0;
707 week_offset = first_wday_of(tm->tm_year);
708 while (i++ <= tm->tm_yday) {
709 if (week_offset++ >= 6)
710 week_offset = 0;
711 }
712 tm->tm_wday = week_offset;
713 state |= S_WDAY;
714 }
715 }
716
717 return __UNCONST(bp);
718 }
719
720
721 static const u_char *
722 conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
723 {
724 uint result = 0;
725 unsigned char ch;
726
727 /* The limit also determines the number of valid digits. */
728 uint rulim = ulim;
729
730 ch = *buf;
731 if (ch < '0' || ch > '9')
732 return NULL;
733
734 do {
735 result *= 10;
736 result += ch - '0';
737 rulim /= 10;
738 ch = *++buf;
739 } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
740
741 if (result < llim || result > ulim)
742 return NULL;
743
744 *dest = result;
745 return buf;
746 }
747
748 static const u_char *
749 find_string(const u_char *bp, int *tgt, const char * const *n1,
750 const char * const *n2, int c)
751 {
752 int i;
753 size_t len;
754
755 /* check full name - then abbreviated ones */
756 for (; n1 != NULL; n1 = n2, n2 = NULL) {
757 for (i = 0; i < c; i++, n1++) {
758 len = strlen(*n1);
759 if (strncasecmp(*n1, (const char *)bp, len) == 0) {
760 *tgt = i;
761 return bp + len;
762 }
763 }
764 }
765
766 /* Nothing matched */
767 return NULL;
768 }
769