1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/*
* lib/krb5/krb/deltat.y
*
* Copyright 1999 by the Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
*
* krb5_string_to_deltat()
*/
/* For a clean, thread-safe interface, we must use the "pure parser"
facility of GNU Bison. Unfortunately, standard YACC has no such
option. */
/* N.B.: For simplicity in dealing with the distribution, the
Makefile.in listing for deltat.c does *not* normally list this
file. If you change this file, tweak the Makefile so it'll rebuild
deltat.c, or do it manually. */
%{
#include <ctype.h>
#include <errno.h>
#include "k5-int.h"
struct param {
krb5_int32 delta;
char *p;
};
#define YYPARSE_PARAM tmv
#define MAX_TIME KRB5_INT32_MAX
#define MIN_TIME KRB5_INT32_MIN
#define DAY (24 * 3600)
#define HOUR 3600
#define MAX_DAY (MAX_TIME / DAY)
#define MIN_DAY (MIN_TIME / DAY)
#define MAX_HOUR (MAX_TIME / HOUR)
#define MIN_HOUR (MIN_TIME / HOUR)
#define MAX_MIN (MAX_TIME / 60)
#define MIN_MIN (MIN_TIME / 60)
/* An explanation of the tests being performed.
We do not want to overflow a 32 bit integer with out manipulations,
even for testing for overflow. Therefore we rely on the following:
The lex parser will not return a number > MAX_TIME (which is out 32
bit limit).
Therefore, seconds (s) will require
MIN_TIME < s < MAX_TIME
For subsequent tests, the logic is as follows:
If A < MAX_TIME and B < MAX_TIME
If we want to test if A+B < MAX_TIME, there are two cases
if (A > 0)
then A + B < MAX_TIME if B < MAX_TIME - A
else A + B < MAX_TIME always.
if we want to test if MIN_TIME < A + B
if A > 0 - then nothing to test
otherwise, we test if MIN_TIME - A < B.
We of course are testing for:
MIN_TIME < A + B < MAX_TIME
*/
#define DAY_NOT_OK(d) (d) > MAX_DAY || (d) < MIN_DAY
#define HOUR_NOT_OK(h) (h) > MAX_HOUR || (h) < MIN_HOUR
#define MIN_NOT_OK(m) (m) > MAX_MIN || (m) < MIN_MIN
#define SUM_OK(a, b) (((a) > 0) ? ( (b) <= MAX_TIME - (a)) : (MIN_TIME - (a) <= (b)))
#define DO_SUM(res, a, b) if (!SUM_OK((a), (b))) YYERROR; \
res = (a) + (b)
#define OUT_D ((struct param *)tmv)->delta
#define DO(D,H,M,S) \
{ \
/* Overflow testing - this does not handle negative values well.. */ \
if (DAY_NOT_OK(D) || HOUR_NOT_OK(H) || MIN_NOT_OK(M)) YYERROR; \
OUT_D = D * DAY; \
DO_SUM(OUT_D, OUT_D, H * HOUR); \
DO_SUM(OUT_D, OUT_D, M * 60); \
DO_SUM(OUT_D, OUT_D, S); \
}
static int mylex (int *, char **);
#define YYLEX_PARAM (&((struct param *)tmv)->p)
#undef yylex
#define yylex(U, P) mylex (&(U)->val, (P))
#undef yyerror
#define yyerror(MSG)
static int yyparse (void *);
%}
%pure_parser
%union { int val; }
%token <val> NUM LONGNUM OVERFLOW
%token '-' ':' 'd' 'h' 'm' 's' WS
%type <val> num opt_hms opt_ms opt_s wsnum posnum
%start start
%%
start: deltat;
posnum: NUM | LONGNUM ;
num: posnum | '-' posnum { $$ = - $2; } ;
ws: /* nothing */ | WS ;
wsnum: ws num { $$ = $2; }
| ws OVERFLOW { YYERROR };
deltat:
wsnum 'd' opt_hms { DO ($1, 0, 0, $3); }
| wsnum 'h' opt_ms { DO ( 0, $1, 0, $3); }
| wsnum 'm' opt_s { DO ( 0, 0, $1, $3); }
| wsnum 's' { DO ( 0, 0, 0, $1); }
| wsnum '-' NUM ':' NUM ':' NUM { DO ($1, $3, $5, $7); }
| wsnum ':' NUM ':' NUM { DO ( 0, $1, $3, $5); }
| wsnum ':' NUM { DO ( 0, $1, $3, 0); }
;
opt_hms:
opt_ms
| wsnum 'h' opt_ms { if (HOUR_NOT_OK($1)) YYERROR;
DO_SUM($$, $1 * 3600, $3); };
opt_ms:
opt_s
| wsnum 'm' opt_s { if (MIN_NOT_OK($1)) YYERROR;
DO_SUM($$, $1 * 60, $3); };
opt_s:
ws { $$ = 0; }
| wsnum 's' ;
%%
static int
mylex (krb5_int32 *intp, char **pp)
{
int num, c;
#define P (*pp)
char *orig_p = P;
#ifdef isascii
if (!isascii (*P))
return 0;
#endif
switch (c = *P++) {
case '-':
case ':':
case 'd':
case 'h':
case 'm':
case 's':
return c;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* XXX assumes ASCII */
num = c - '0';
while (isdigit ((int) *P)) {
if (num > MAX_TIME / 10)
return OVERFLOW;
num *= 10;
if (num > MAX_TIME - (*P - '0'))
return OVERFLOW;
num += *P++ - '0';
}
*intp = num;
return (P - orig_p > 2) ? LONGNUM : NUM;
case ' ':
case '\t':
case '\n':
while (isspace ((int) *P))
P++;
return WS;
default:
return YYEOF;
}
}
krb5_error_code KRB5_CALLCONV
krb5_string_to_deltat(char *string, krb5_deltat *deltatp)
{
struct param p;
p.delta = 0;
p.p = string;
if (yyparse (&p))
return EINVAL;
*deltatp = p.delta;
return 0;
}
|