-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasn1-se.c
347 lines (276 loc) · 6.32 KB
/
asn1-se.c
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* ASN.1 Parser node types
*
* Copyright (c) 2017 Alexei A. Smekalkine
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "asn1-se.h"
static void indent (int level)
{
printf ("%*s", level * 4, "");
}
void se_free (struct se *o)
{
if (o == NULL)
return;
o->class->free (o);
}
void se_show (int level, const struct se *o)
{
if (o == NULL) {
indent (level);
printf ("(empty)");
}
else
o->class->show (level, o);
}
/* define terminals */
static struct se *se_zero (const struct se_class *class)
{
struct se *o;
if ((o = malloc (sizeof (*o))) == NULL)
return NULL;
o->class = class;
return o;
}
static void se_zero_show (int level, const struct se *o)
{
indent (level);
printf ("(%s)", o->class->name);
}
#define DECL_SE_ZERO(type) \
static const struct se_class se_##type##_class = { \
.name = #type, \
.free = (void (*) ()) free, \
.show = se_zero_show, \
}; \
\
struct se *se_##type (void) \
{ \
return se_zero (&se_##type##_class); \
}
DECL_SE_ZERO (ellipsis)
DECL_SE_ZERO (optional)
DECL_SE_ZERO (true)
DECL_SE_ZERO (false)
#undef DECL_SE_ZERO
struct se_one {
struct se base;
char *value;
};
static struct se *se_one (const struct se_class *class, const char *content)
{
struct se_one *o;
if ((o = malloc (sizeof (*o))) == NULL)
goto no_object;
if ((o->value = strdup (content)) == NULL)
goto no_value;
o->base.class = class;
return &o->base;
no_value:
free (o);
no_object:
return NULL;
}
static void se_one_free (struct se *se)
{
struct se_one *o = (void *) se;
free (o->value);
free (o);
}
static void se_one_show (int level, const struct se *se)
{
struct se_one *o = (void *) se;
indent (level);
printf ("(%s %s)", o->base.class->name, o->value);
}
#define DECL_SE_ONE(one) \
static const struct se_class se_##one##_class = { \
.name = #one, \
.free = se_one_free, \
.show = se_one_show, \
}; \
\
struct se *se_##one (const char *content) \
{ \
return se_one (&se_##one##_class, content); \
}
DECL_SE_ONE (name)
DECL_SE_ONE (comment)
DECL_SE_ONE (number)
DECL_SE_ONE (string)
DECL_SE_ONE (range)
DECL_SE_ONE (real)
#undef DECL_SE_ONE
/* define non-terminals */
struct se_nt_one {
struct se base;
struct se *value;
};
static struct se *se_nt_one (const struct se_class *class, struct se *se)
{
struct se_nt_one *o;
if ((o = malloc (sizeof (*o))) == NULL)
return NULL;
o->base.class = class;
o->value = se;
return &o->base;
}
static void se_nt_one_free (struct se *se)
{
struct se_nt_one *o = (void *) se;
se_free (o->value);
free (o);
}
static void se_nt_one_show (int level, const struct se *se)
{
struct se_nt_one *o = (void *) se;
indent (level);
printf ("(%s ", o->base.class->name);
se_show (0, o->value);
putchar (')');
}
#define DECL_SE_ONE(type, typename) \
static const struct se_class se_##type##_class = { \
.name = typename, \
.free = se_nt_one_free, \
.show = se_nt_one_show, \
}; \
\
struct se *se_##type (struct se *se) \
{ \
return se_nt_one (&se_##type##_class, se); \
}
DECL_SE_ONE (seq_of, "seq-of")
DECL_SE_ONE (set_of, "set-of")
DECL_SE_ONE (default, "default")
DECL_SE_ONE (size, "size")
#undef DECL_SE_ONE
struct se_nt_list {
struct se base;
struct se *head, *tail;
};
static struct se *se_nt_list (const struct se_class *class,
struct se *head, struct se *tail)
{
struct se_nt_list *o;
if ((o = malloc (sizeof (*o))) == NULL)
return NULL;
o->base.class = class;
o->head = head;
o->tail = tail;
return &o->base;
}
static void se_nt_list_free (struct se *se)
{
struct se_nt_list *o = (void *) se;
se_free (o->head);
se_free (o->tail);
free (o);
}
static void se_nt_list_show (int level, const struct se *se)
{
struct se_nt_list *o = (void *) se;
indent (level);
printf ("(%s", o->base.class->name);
for (; o != NULL; o = (void *) o->tail) {
putchar ('\n');
se_show (level + 1, o->head);
}
putchar (')');
}
#define DECL_SE_LIST(type) \
static const struct se_class se_##type##_class = { \
.name = #type, \
.free = se_nt_list_free, \
.show = se_nt_list_show, \
}; \
\
struct se *se_##type (struct se *head, struct se *tail) \
{ \
return se_nt_list (&se_##type##_class, head, tail); \
}
DECL_SE_LIST (list)
DECL_SE_LIST (oid)
DECL_SE_LIST (enum)
DECL_SE_LIST (seq)
DECL_SE_LIST (set)
DECL_SE_LIST (choice)
#undef DECL_SE_LIST
static void se_nt_two_show (int level, const struct se *se)
{
struct se_nt_list *o = (void *) se;
indent (level);
printf ("(%s", o->base.class->name);
putchar ('\n'); se_show (level + 1, o->head);
putchar ('\n'); se_show (level + 1, o->tail);
putchar (')');
}
#define DECL_SE_TWO(type) \
static const struct se_class se_##type##_class = { \
.name = #type, \
.free = se_nt_list_free, \
.show = se_nt_two_show, \
}; \
\
struct se *se_##type (struct se *head, struct se *tail) \
{ \
return se_nt_list (&se_##type##_class, head, tail); \
}
DECL_SE_TWO (label)
DECL_SE_TWO (module)
DECL_SE_TWO (type)
DECL_SE_TWO (field)
#undef DECL_SE_TWO
struct se_nt_three {
struct se base;
struct se *a, *b, *c;
};
static struct se *se_nt_three (const struct se_class *class,
struct se *a, struct se *b, struct se *c)
{
struct se_nt_three *o;
if ((o = malloc (sizeof (*o))) == NULL)
return NULL;
o->base.class = class;
o->a = a;
o->b = b;
o->c = c;
return &o->base;
}
static void se_nt_three_free (struct se *se)
{
struct se_nt_three *o = (void *) se;
se_free (o->a);
se_free (o->b);
se_free (o->c);
free (o);
}
static void se_nt_three_show (int level, const struct se *se)
{
struct se_nt_three *o = (void *) se;
indent (level);
printf ("(%s", o->base.class->name);
putchar ('\n'); se_show (level + 1, o->a);
putchar ('\n'); se_show (level + 1, o->b);
putchar ('\n'); se_show (level + 1, o->c);
putchar (')');
}
#define DECL_SE_THREE(type, typename) \
static const struct se_class se_##type##_class = { \
.name = typename, \
.free = se_nt_three_free, \
.show = se_nt_three_show, \
}; \
\
struct se *se_##type (struct se *a, struct se *b, struct se *c) \
{ \
return se_nt_three (&se_##type##_class, a, b, c); \
}
DECL_SE_THREE (typeref, "type-ref")
DECL_SE_THREE (value, "value")
#undef DECL_SE_THREE