1*0a6a1f1dSLionel Sambuc /* $NetBSD: gen_encode.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "gen_locl.h"
37ebfedea0SLionel Sambuc
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc static void
encode_primitive(const char * typename,const char * name)41ebfedea0SLionel Sambuc encode_primitive (const char *typename, const char *name)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc fprintf (codefile,
44ebfedea0SLionel Sambuc "e = der_put_%s(p, len, %s, &l);\n"
45ebfedea0SLionel Sambuc "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
46ebfedea0SLionel Sambuc typename,
47ebfedea0SLionel Sambuc name);
48ebfedea0SLionel Sambuc }
49ebfedea0SLionel Sambuc
50ebfedea0SLionel Sambuc const char *
classname(Der_class class)51ebfedea0SLionel Sambuc classname(Der_class class)
52ebfedea0SLionel Sambuc {
53ebfedea0SLionel Sambuc const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
54ebfedea0SLionel Sambuc "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
55ebfedea0SLionel Sambuc if(class < ASN1_C_UNIV || class > ASN1_C_PRIVATE)
56ebfedea0SLionel Sambuc return "???";
57ebfedea0SLionel Sambuc return cn[class];
58ebfedea0SLionel Sambuc }
59ebfedea0SLionel Sambuc
60ebfedea0SLionel Sambuc
61ebfedea0SLionel Sambuc const char *
valuename(Der_class class,int value)62ebfedea0SLionel Sambuc valuename(Der_class class, int value)
63ebfedea0SLionel Sambuc {
64ebfedea0SLionel Sambuc static char s[32];
65ebfedea0SLionel Sambuc struct {
66ebfedea0SLionel Sambuc int value;
67ebfedea0SLionel Sambuc const char *s;
68ebfedea0SLionel Sambuc } *p, values[] = {
69ebfedea0SLionel Sambuc #define X(Y) { Y, #Y }
70ebfedea0SLionel Sambuc X(UT_BMPString),
71ebfedea0SLionel Sambuc X(UT_BitString),
72ebfedea0SLionel Sambuc X(UT_Boolean),
73ebfedea0SLionel Sambuc X(UT_EmbeddedPDV),
74ebfedea0SLionel Sambuc X(UT_Enumerated),
75ebfedea0SLionel Sambuc X(UT_External),
76ebfedea0SLionel Sambuc X(UT_GeneralString),
77ebfedea0SLionel Sambuc X(UT_GeneralizedTime),
78ebfedea0SLionel Sambuc X(UT_GraphicString),
79ebfedea0SLionel Sambuc X(UT_IA5String),
80ebfedea0SLionel Sambuc X(UT_Integer),
81ebfedea0SLionel Sambuc X(UT_Null),
82ebfedea0SLionel Sambuc X(UT_NumericString),
83ebfedea0SLionel Sambuc X(UT_OID),
84ebfedea0SLionel Sambuc X(UT_ObjectDescriptor),
85ebfedea0SLionel Sambuc X(UT_OctetString),
86ebfedea0SLionel Sambuc X(UT_PrintableString),
87ebfedea0SLionel Sambuc X(UT_Real),
88ebfedea0SLionel Sambuc X(UT_RelativeOID),
89ebfedea0SLionel Sambuc X(UT_Sequence),
90ebfedea0SLionel Sambuc X(UT_Set),
91ebfedea0SLionel Sambuc X(UT_TeletexString),
92ebfedea0SLionel Sambuc X(UT_UTCTime),
93ebfedea0SLionel Sambuc X(UT_UTF8String),
94ebfedea0SLionel Sambuc X(UT_UniversalString),
95ebfedea0SLionel Sambuc X(UT_VideotexString),
96ebfedea0SLionel Sambuc X(UT_VisibleString),
97ebfedea0SLionel Sambuc #undef X
98ebfedea0SLionel Sambuc { -1, NULL }
99ebfedea0SLionel Sambuc };
100ebfedea0SLionel Sambuc if(class == ASN1_C_UNIV) {
101ebfedea0SLionel Sambuc for(p = values; p->value != -1; p++)
102ebfedea0SLionel Sambuc if(p->value == value)
103ebfedea0SLionel Sambuc return p->s;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc snprintf(s, sizeof(s), "%d", value);
106ebfedea0SLionel Sambuc return s;
107ebfedea0SLionel Sambuc }
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc static int
encode_type(const char * name,const Type * t,const char * tmpstr)110ebfedea0SLionel Sambuc encode_type (const char *name, const Type *t, const char *tmpstr)
111ebfedea0SLionel Sambuc {
112ebfedea0SLionel Sambuc int constructed = 1;
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc switch (t->type) {
115ebfedea0SLionel Sambuc case TType:
116ebfedea0SLionel Sambuc #if 0
117ebfedea0SLionel Sambuc encode_type (name, t->symbol->type);
118ebfedea0SLionel Sambuc #endif
119ebfedea0SLionel Sambuc fprintf (codefile,
120ebfedea0SLionel Sambuc "e = encode_%s(p, len, %s, &l);\n"
121ebfedea0SLionel Sambuc "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
122ebfedea0SLionel Sambuc t->symbol->gen_name, name);
123ebfedea0SLionel Sambuc break;
124ebfedea0SLionel Sambuc case TInteger:
125ebfedea0SLionel Sambuc if(t->members) {
126ebfedea0SLionel Sambuc fprintf(codefile,
127ebfedea0SLionel Sambuc "{\n"
128ebfedea0SLionel Sambuc "int enumint = (int)*%s;\n",
129ebfedea0SLionel Sambuc name);
130ebfedea0SLionel Sambuc encode_primitive ("integer", "&enumint");
131ebfedea0SLionel Sambuc fprintf(codefile, "}\n;");
132ebfedea0SLionel Sambuc } else if (t->range == NULL) {
133ebfedea0SLionel Sambuc encode_primitive ("heim_integer", name);
134ebfedea0SLionel Sambuc } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
135ebfedea0SLionel Sambuc encode_primitive ("integer", name);
136ebfedea0SLionel Sambuc } else if (t->range->min == 0 && t->range->max == UINT_MAX) {
137ebfedea0SLionel Sambuc encode_primitive ("unsigned", name);
138ebfedea0SLionel Sambuc } else if (t->range->min == 0 && t->range->max == INT_MAX) {
139ebfedea0SLionel Sambuc encode_primitive ("unsigned", name);
140ebfedea0SLionel Sambuc } else
141ebfedea0SLionel Sambuc errx(1, "%s: unsupported range %d -> %d",
142ebfedea0SLionel Sambuc name, t->range->min, t->range->max);
143ebfedea0SLionel Sambuc constructed = 0;
144ebfedea0SLionel Sambuc break;
145ebfedea0SLionel Sambuc case TBoolean:
146ebfedea0SLionel Sambuc encode_primitive ("boolean", name);
147ebfedea0SLionel Sambuc constructed = 0;
148ebfedea0SLionel Sambuc break;
149ebfedea0SLionel Sambuc case TOctetString:
150ebfedea0SLionel Sambuc encode_primitive ("octet_string", name);
151ebfedea0SLionel Sambuc constructed = 0;
152ebfedea0SLionel Sambuc break;
153ebfedea0SLionel Sambuc case TBitString: {
154ebfedea0SLionel Sambuc Member *m;
155ebfedea0SLionel Sambuc int pos;
156ebfedea0SLionel Sambuc
157ebfedea0SLionel Sambuc if (ASN1_TAILQ_EMPTY(t->members)) {
158ebfedea0SLionel Sambuc encode_primitive("bit_string", name);
159ebfedea0SLionel Sambuc constructed = 0;
160ebfedea0SLionel Sambuc break;
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc
163ebfedea0SLionel Sambuc fprintf (codefile, "{\n"
164ebfedea0SLionel Sambuc "unsigned char c = 0;\n");
165ebfedea0SLionel Sambuc if (!rfc1510_bitstring)
166ebfedea0SLionel Sambuc fprintf (codefile,
167ebfedea0SLionel Sambuc "int rest = 0;\n"
168ebfedea0SLionel Sambuc "int bit_set = 0;\n");
169ebfedea0SLionel Sambuc #if 0
170ebfedea0SLionel Sambuc pos = t->members->prev->val;
171ebfedea0SLionel Sambuc /* fix for buggy MIT (and OSF?) code */
172ebfedea0SLionel Sambuc if (pos > 31)
173ebfedea0SLionel Sambuc abort ();
174ebfedea0SLionel Sambuc #endif
175ebfedea0SLionel Sambuc /*
176ebfedea0SLionel Sambuc * It seems that if we do not always set pos to 31 here, the MIT
177ebfedea0SLionel Sambuc * code will do the wrong thing.
178ebfedea0SLionel Sambuc *
179ebfedea0SLionel Sambuc * I hate ASN.1 (and DER), but I hate it even more when everybody
180ebfedea0SLionel Sambuc * has to screw it up differently.
181ebfedea0SLionel Sambuc */
182ebfedea0SLionel Sambuc pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
183ebfedea0SLionel Sambuc if (rfc1510_bitstring) {
184ebfedea0SLionel Sambuc if (pos < 31)
185ebfedea0SLionel Sambuc pos = 31;
186ebfedea0SLionel Sambuc }
187ebfedea0SLionel Sambuc
188ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
189ebfedea0SLionel Sambuc while (m->val / 8 < pos / 8) {
190ebfedea0SLionel Sambuc if (!rfc1510_bitstring)
191ebfedea0SLionel Sambuc fprintf (codefile,
192ebfedea0SLionel Sambuc "if (c != 0 || bit_set) {\n");
193ebfedea0SLionel Sambuc fprintf (codefile,
194ebfedea0SLionel Sambuc "if (len < 1) return ASN1_OVERFLOW;\n"
195ebfedea0SLionel Sambuc "*p-- = c; len--; ret++;\n");
196ebfedea0SLionel Sambuc if (!rfc1510_bitstring)
197ebfedea0SLionel Sambuc fprintf (codefile,
198ebfedea0SLionel Sambuc "if (!bit_set) {\n"
199ebfedea0SLionel Sambuc "rest = 0;\n"
200ebfedea0SLionel Sambuc "while(c) { \n"
201ebfedea0SLionel Sambuc "if (c & 1) break;\n"
202ebfedea0SLionel Sambuc "c = c >> 1;\n"
203ebfedea0SLionel Sambuc "rest++;\n"
204ebfedea0SLionel Sambuc "}\n"
205ebfedea0SLionel Sambuc "bit_set = 1;\n"
206ebfedea0SLionel Sambuc "}\n"
207ebfedea0SLionel Sambuc "}\n");
208ebfedea0SLionel Sambuc fprintf (codefile,
209ebfedea0SLionel Sambuc "c = 0;\n");
210ebfedea0SLionel Sambuc pos -= 8;
211ebfedea0SLionel Sambuc }
212ebfedea0SLionel Sambuc fprintf (codefile,
213ebfedea0SLionel Sambuc "if((%s)->%s) {\n"
214ebfedea0SLionel Sambuc "c |= 1<<%d;\n",
215ebfedea0SLionel Sambuc name, m->gen_name, 7 - m->val % 8);
216ebfedea0SLionel Sambuc fprintf (codefile,
217ebfedea0SLionel Sambuc "}\n");
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc if (!rfc1510_bitstring)
221ebfedea0SLionel Sambuc fprintf (codefile,
222ebfedea0SLionel Sambuc "if (c != 0 || bit_set) {\n");
223ebfedea0SLionel Sambuc fprintf (codefile,
224ebfedea0SLionel Sambuc "if (len < 1) return ASN1_OVERFLOW;\n"
225ebfedea0SLionel Sambuc "*p-- = c; len--; ret++;\n");
226ebfedea0SLionel Sambuc if (!rfc1510_bitstring)
227ebfedea0SLionel Sambuc fprintf (codefile,
228ebfedea0SLionel Sambuc "if (!bit_set) {\n"
229ebfedea0SLionel Sambuc "rest = 0;\n"
230ebfedea0SLionel Sambuc "if(c) { \n"
231ebfedea0SLionel Sambuc "while(c) { \n"
232ebfedea0SLionel Sambuc "if (c & 1) break;\n"
233ebfedea0SLionel Sambuc "c = c >> 1;\n"
234ebfedea0SLionel Sambuc "rest++;\n"
235ebfedea0SLionel Sambuc "}\n"
236ebfedea0SLionel Sambuc "}\n"
237ebfedea0SLionel Sambuc "}\n"
238ebfedea0SLionel Sambuc "}\n");
239ebfedea0SLionel Sambuc
240ebfedea0SLionel Sambuc fprintf (codefile,
241ebfedea0SLionel Sambuc "if (len < 1) return ASN1_OVERFLOW;\n"
242ebfedea0SLionel Sambuc "*p-- = %s;\n"
243ebfedea0SLionel Sambuc "len -= 1;\n"
244ebfedea0SLionel Sambuc "ret += 1;\n"
245ebfedea0SLionel Sambuc "}\n\n",
246ebfedea0SLionel Sambuc rfc1510_bitstring ? "0" : "rest");
247ebfedea0SLionel Sambuc constructed = 0;
248ebfedea0SLionel Sambuc break;
249ebfedea0SLionel Sambuc }
250ebfedea0SLionel Sambuc case TEnumerated : {
251ebfedea0SLionel Sambuc encode_primitive ("enumerated", name);
252ebfedea0SLionel Sambuc constructed = 0;
253ebfedea0SLionel Sambuc break;
254ebfedea0SLionel Sambuc }
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc case TSet:
257ebfedea0SLionel Sambuc case TSequence: {
258ebfedea0SLionel Sambuc Member *m;
259ebfedea0SLionel Sambuc
260ebfedea0SLionel Sambuc if (t->members == NULL)
261ebfedea0SLionel Sambuc break;
262ebfedea0SLionel Sambuc
263ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
264ebfedea0SLionel Sambuc char *s = NULL;
265ebfedea0SLionel Sambuc
266ebfedea0SLionel Sambuc if (m->ellipsis)
267ebfedea0SLionel Sambuc continue;
268ebfedea0SLionel Sambuc
269ebfedea0SLionel Sambuc if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
270ebfedea0SLionel Sambuc errx(1, "malloc");
271ebfedea0SLionel Sambuc fprintf(codefile, "/* %s */\n", m->name);
272ebfedea0SLionel Sambuc if (m->optional)
273ebfedea0SLionel Sambuc fprintf (codefile,
274ebfedea0SLionel Sambuc "if(%s) ",
275ebfedea0SLionel Sambuc s);
276ebfedea0SLionel Sambuc else if(m->defval)
277ebfedea0SLionel Sambuc gen_compare_defval(s + 1, m->defval);
278ebfedea0SLionel Sambuc fprintf (codefile, "{\n");
279*0a6a1f1dSLionel Sambuc fprintf (codefile, "size_t %s_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;\n", tmpstr);
280ebfedea0SLionel Sambuc fprintf (codefile, "ret = 0;\n");
281ebfedea0SLionel Sambuc encode_type (s, m->type, m->gen_name);
282ebfedea0SLionel Sambuc fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
283ebfedea0SLionel Sambuc fprintf (codefile, "}\n");
284ebfedea0SLionel Sambuc free (s);
285ebfedea0SLionel Sambuc }
286ebfedea0SLionel Sambuc break;
287ebfedea0SLionel Sambuc }
288ebfedea0SLionel Sambuc case TSetOf: {
289ebfedea0SLionel Sambuc
290ebfedea0SLionel Sambuc fprintf(codefile,
291ebfedea0SLionel Sambuc "{\n"
292ebfedea0SLionel Sambuc "struct heim_octet_string *val;\n"
293ebfedea0SLionel Sambuc "size_t elen = 0, totallen = 0;\n"
294ebfedea0SLionel Sambuc "int eret = 0;\n");
295ebfedea0SLionel Sambuc
296ebfedea0SLionel Sambuc fprintf(codefile,
297ebfedea0SLionel Sambuc "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
298ebfedea0SLionel Sambuc "return ERANGE;\n",
299ebfedea0SLionel Sambuc name);
300ebfedea0SLionel Sambuc
301ebfedea0SLionel Sambuc fprintf(codefile,
302ebfedea0SLionel Sambuc "val = malloc(sizeof(val[0]) * (%s)->len);\n"
303ebfedea0SLionel Sambuc "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
304ebfedea0SLionel Sambuc name, name);
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc fprintf(codefile,
307*0a6a1f1dSLionel Sambuc "for(i = 0; i < (int)(%s)->len; i++) {\n",
308ebfedea0SLionel Sambuc name);
309ebfedea0SLionel Sambuc
310ebfedea0SLionel Sambuc fprintf(codefile,
311ebfedea0SLionel Sambuc "ASN1_MALLOC_ENCODE(%s, val[i].data, "
312ebfedea0SLionel Sambuc "val[i].length, &(%s)->val[i], &elen, eret);\n",
313ebfedea0SLionel Sambuc t->subtype->symbol->gen_name,
314ebfedea0SLionel Sambuc name);
315ebfedea0SLionel Sambuc
316ebfedea0SLionel Sambuc fprintf(codefile,
317ebfedea0SLionel Sambuc "if(eret) {\n"
318ebfedea0SLionel Sambuc "i--;\n"
319ebfedea0SLionel Sambuc "while (i >= 0) {\n"
320ebfedea0SLionel Sambuc "free(val[i].data);\n"
321ebfedea0SLionel Sambuc "i--;\n"
322ebfedea0SLionel Sambuc "}\n"
323ebfedea0SLionel Sambuc "free(val);\n"
324ebfedea0SLionel Sambuc "return eret;\n"
325ebfedea0SLionel Sambuc "}\n"
326ebfedea0SLionel Sambuc "totallen += elen;\n"
327ebfedea0SLionel Sambuc "}\n");
328ebfedea0SLionel Sambuc
329ebfedea0SLionel Sambuc fprintf(codefile,
330ebfedea0SLionel Sambuc "if (totallen > len) {\n"
331*0a6a1f1dSLionel Sambuc "for (i = 0; i < (int)(%s)->len; i++) {\n"
332ebfedea0SLionel Sambuc "free(val[i].data);\n"
333ebfedea0SLionel Sambuc "}\n"
334ebfedea0SLionel Sambuc "free(val);\n"
335ebfedea0SLionel Sambuc "return ASN1_OVERFLOW;\n"
336ebfedea0SLionel Sambuc "}\n",
337ebfedea0SLionel Sambuc name);
338ebfedea0SLionel Sambuc
339ebfedea0SLionel Sambuc fprintf(codefile,
340ebfedea0SLionel Sambuc "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
341ebfedea0SLionel Sambuc name);
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc fprintf (codefile,
344*0a6a1f1dSLionel Sambuc "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
345ebfedea0SLionel Sambuc "p -= val[i].length;\n"
346ebfedea0SLionel Sambuc "ret += val[i].length;\n"
347ebfedea0SLionel Sambuc "memcpy(p + 1, val[i].data, val[i].length);\n"
348ebfedea0SLionel Sambuc "free(val[i].data);\n"
349ebfedea0SLionel Sambuc "}\n"
350ebfedea0SLionel Sambuc "free(val);\n"
351ebfedea0SLionel Sambuc "}\n",
352ebfedea0SLionel Sambuc name);
353ebfedea0SLionel Sambuc break;
354ebfedea0SLionel Sambuc }
355ebfedea0SLionel Sambuc case TSequenceOf: {
356ebfedea0SLionel Sambuc char *sname = NULL;
357ebfedea0SLionel Sambuc char *n = NULL;
358ebfedea0SLionel Sambuc
359ebfedea0SLionel Sambuc fprintf (codefile,
360*0a6a1f1dSLionel Sambuc "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
361ebfedea0SLionel Sambuc "size_t %s_for_oldret = ret;\n"
362ebfedea0SLionel Sambuc "ret = 0;\n",
363ebfedea0SLionel Sambuc name, tmpstr);
364ebfedea0SLionel Sambuc if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)
365ebfedea0SLionel Sambuc errx(1, "malloc");
366ebfedea0SLionel Sambuc if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
367ebfedea0SLionel Sambuc errx(1, "malloc");
368ebfedea0SLionel Sambuc encode_type (n, t->subtype, sname);
369ebfedea0SLionel Sambuc fprintf (codefile,
370ebfedea0SLionel Sambuc "ret += %s_for_oldret;\n"
371ebfedea0SLionel Sambuc "}\n",
372ebfedea0SLionel Sambuc tmpstr);
373ebfedea0SLionel Sambuc free (n);
374ebfedea0SLionel Sambuc free (sname);
375ebfedea0SLionel Sambuc break;
376ebfedea0SLionel Sambuc }
377ebfedea0SLionel Sambuc case TGeneralizedTime:
378ebfedea0SLionel Sambuc encode_primitive ("generalized_time", name);
379ebfedea0SLionel Sambuc constructed = 0;
380ebfedea0SLionel Sambuc break;
381ebfedea0SLionel Sambuc case TGeneralString:
382ebfedea0SLionel Sambuc encode_primitive ("general_string", name);
383ebfedea0SLionel Sambuc constructed = 0;
384ebfedea0SLionel Sambuc break;
385ebfedea0SLionel Sambuc case TTeletexString:
386ebfedea0SLionel Sambuc encode_primitive ("general_string", name);
387ebfedea0SLionel Sambuc constructed = 0;
388ebfedea0SLionel Sambuc break;
389ebfedea0SLionel Sambuc case TTag: {
390ebfedea0SLionel Sambuc char *tname = NULL;
391ebfedea0SLionel Sambuc int c;
392ebfedea0SLionel Sambuc if (asprintf (&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
393ebfedea0SLionel Sambuc errx(1, "malloc");
394ebfedea0SLionel Sambuc c = encode_type (name, t->subtype, tname);
395ebfedea0SLionel Sambuc fprintf (codefile,
396ebfedea0SLionel Sambuc "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
397ebfedea0SLionel Sambuc "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
398ebfedea0SLionel Sambuc classname(t->tag.tagclass),
399ebfedea0SLionel Sambuc c ? "CONS" : "PRIM",
400ebfedea0SLionel Sambuc valuename(t->tag.tagclass, t->tag.tagvalue));
401ebfedea0SLionel Sambuc free (tname);
402ebfedea0SLionel Sambuc break;
403ebfedea0SLionel Sambuc }
404ebfedea0SLionel Sambuc case TChoice:{
405ebfedea0SLionel Sambuc Member *m, *have_ellipsis = NULL;
406ebfedea0SLionel Sambuc char *s = NULL;
407ebfedea0SLionel Sambuc
408ebfedea0SLionel Sambuc if (t->members == NULL)
409ebfedea0SLionel Sambuc break;
410ebfedea0SLionel Sambuc
411ebfedea0SLionel Sambuc fprintf(codefile, "\n");
412ebfedea0SLionel Sambuc
413ebfedea0SLionel Sambuc if (asprintf (&s, "(%s)", name) < 0 || s == NULL)
414ebfedea0SLionel Sambuc errx(1, "malloc");
415ebfedea0SLionel Sambuc fprintf(codefile, "switch(%s->element) {\n", s);
416ebfedea0SLionel Sambuc
417ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
418ebfedea0SLionel Sambuc char *s2 = NULL;
419ebfedea0SLionel Sambuc
420ebfedea0SLionel Sambuc if (m->ellipsis) {
421ebfedea0SLionel Sambuc have_ellipsis = m;
422ebfedea0SLionel Sambuc continue;
423ebfedea0SLionel Sambuc }
424ebfedea0SLionel Sambuc
425ebfedea0SLionel Sambuc fprintf (codefile, "case %s: {", m->label);
426ebfedea0SLionel Sambuc if (asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
427ebfedea0SLionel Sambuc s, m->gen_name) < 0 || s2 == NULL)
428ebfedea0SLionel Sambuc errx(1, "malloc");
429ebfedea0SLionel Sambuc if (m->optional)
430ebfedea0SLionel Sambuc fprintf (codefile, "if(%s) {\n", s2);
431ebfedea0SLionel Sambuc fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
432ebfedea0SLionel Sambuc fprintf (codefile, "ret = 0;\n");
433ebfedea0SLionel Sambuc constructed = encode_type (s2, m->type, m->gen_name);
434ebfedea0SLionel Sambuc fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
435ebfedea0SLionel Sambuc if(m->optional)
436ebfedea0SLionel Sambuc fprintf (codefile, "}\n");
437ebfedea0SLionel Sambuc fprintf(codefile, "break;\n");
438ebfedea0SLionel Sambuc fprintf(codefile, "}\n");
439ebfedea0SLionel Sambuc free (s2);
440ebfedea0SLionel Sambuc }
441ebfedea0SLionel Sambuc free (s);
442ebfedea0SLionel Sambuc if (have_ellipsis) {
443ebfedea0SLionel Sambuc fprintf(codefile,
444ebfedea0SLionel Sambuc "case %s: {\n"
445ebfedea0SLionel Sambuc "if (len < (%s)->u.%s.length)\n"
446ebfedea0SLionel Sambuc "return ASN1_OVERFLOW;\n"
447ebfedea0SLionel Sambuc "p -= (%s)->u.%s.length;\n"
448ebfedea0SLionel Sambuc "ret += (%s)->u.%s.length;\n"
449ebfedea0SLionel Sambuc "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
450ebfedea0SLionel Sambuc "break;\n"
451ebfedea0SLionel Sambuc "}\n",
452ebfedea0SLionel Sambuc have_ellipsis->label,
453ebfedea0SLionel Sambuc name, have_ellipsis->gen_name,
454ebfedea0SLionel Sambuc name, have_ellipsis->gen_name,
455ebfedea0SLionel Sambuc name, have_ellipsis->gen_name,
456ebfedea0SLionel Sambuc name, have_ellipsis->gen_name,
457ebfedea0SLionel Sambuc name, have_ellipsis->gen_name);
458ebfedea0SLionel Sambuc }
459ebfedea0SLionel Sambuc fprintf(codefile, "};\n");
460ebfedea0SLionel Sambuc break;
461ebfedea0SLionel Sambuc }
462ebfedea0SLionel Sambuc case TOID:
463ebfedea0SLionel Sambuc encode_primitive ("oid", name);
464ebfedea0SLionel Sambuc constructed = 0;
465ebfedea0SLionel Sambuc break;
466ebfedea0SLionel Sambuc case TUTCTime:
467ebfedea0SLionel Sambuc encode_primitive ("utctime", name);
468ebfedea0SLionel Sambuc constructed = 0;
469ebfedea0SLionel Sambuc break;
470ebfedea0SLionel Sambuc case TUTF8String:
471ebfedea0SLionel Sambuc encode_primitive ("utf8string", name);
472ebfedea0SLionel Sambuc constructed = 0;
473ebfedea0SLionel Sambuc break;
474ebfedea0SLionel Sambuc case TPrintableString:
475ebfedea0SLionel Sambuc encode_primitive ("printable_string", name);
476ebfedea0SLionel Sambuc constructed = 0;
477ebfedea0SLionel Sambuc break;
478ebfedea0SLionel Sambuc case TIA5String:
479ebfedea0SLionel Sambuc encode_primitive ("ia5_string", name);
480ebfedea0SLionel Sambuc constructed = 0;
481ebfedea0SLionel Sambuc break;
482ebfedea0SLionel Sambuc case TBMPString:
483ebfedea0SLionel Sambuc encode_primitive ("bmp_string", name);
484ebfedea0SLionel Sambuc constructed = 0;
485ebfedea0SLionel Sambuc break;
486ebfedea0SLionel Sambuc case TUniversalString:
487ebfedea0SLionel Sambuc encode_primitive ("universal_string", name);
488ebfedea0SLionel Sambuc constructed = 0;
489ebfedea0SLionel Sambuc break;
490ebfedea0SLionel Sambuc case TVisibleString:
491ebfedea0SLionel Sambuc encode_primitive ("visible_string", name);
492ebfedea0SLionel Sambuc constructed = 0;
493ebfedea0SLionel Sambuc break;
494ebfedea0SLionel Sambuc case TNull:
495ebfedea0SLionel Sambuc fprintf (codefile, "/* NULL */\n");
496ebfedea0SLionel Sambuc constructed = 0;
497ebfedea0SLionel Sambuc break;
498ebfedea0SLionel Sambuc default:
499ebfedea0SLionel Sambuc abort ();
500ebfedea0SLionel Sambuc }
501ebfedea0SLionel Sambuc return constructed;
502ebfedea0SLionel Sambuc }
503ebfedea0SLionel Sambuc
504ebfedea0SLionel Sambuc void
generate_type_encode(const Symbol * s)505ebfedea0SLionel Sambuc generate_type_encode (const Symbol *s)
506ebfedea0SLionel Sambuc {
507ebfedea0SLionel Sambuc fprintf (codefile, "int ASN1CALL\n"
508*0a6a1f1dSLionel Sambuc "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
509ebfedea0SLionel Sambuc " const %s *data, size_t *size)\n"
510ebfedea0SLionel Sambuc "{\n",
511ebfedea0SLionel Sambuc s->gen_name, s->gen_name);
512ebfedea0SLionel Sambuc
513ebfedea0SLionel Sambuc switch (s->type->type) {
514ebfedea0SLionel Sambuc case TInteger:
515ebfedea0SLionel Sambuc case TBoolean:
516ebfedea0SLionel Sambuc case TOctetString:
517ebfedea0SLionel Sambuc case TGeneralizedTime:
518ebfedea0SLionel Sambuc case TGeneralString:
519ebfedea0SLionel Sambuc case TTeletexString:
520ebfedea0SLionel Sambuc case TUTCTime:
521ebfedea0SLionel Sambuc case TUTF8String:
522ebfedea0SLionel Sambuc case TPrintableString:
523ebfedea0SLionel Sambuc case TIA5String:
524ebfedea0SLionel Sambuc case TBMPString:
525ebfedea0SLionel Sambuc case TUniversalString:
526ebfedea0SLionel Sambuc case TVisibleString:
527ebfedea0SLionel Sambuc case TNull:
528ebfedea0SLionel Sambuc case TBitString:
529ebfedea0SLionel Sambuc case TEnumerated:
530ebfedea0SLionel Sambuc case TOID:
531ebfedea0SLionel Sambuc case TSequence:
532ebfedea0SLionel Sambuc case TSequenceOf:
533ebfedea0SLionel Sambuc case TSet:
534ebfedea0SLionel Sambuc case TSetOf:
535ebfedea0SLionel Sambuc case TTag:
536ebfedea0SLionel Sambuc case TType:
537ebfedea0SLionel Sambuc case TChoice:
538ebfedea0SLionel Sambuc fprintf (codefile,
539*0a6a1f1dSLionel Sambuc "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
540*0a6a1f1dSLionel Sambuc "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
541*0a6a1f1dSLionel Sambuc "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
542ebfedea0SLionel Sambuc
543ebfedea0SLionel Sambuc encode_type("data", s->type, "Top");
544ebfedea0SLionel Sambuc
545ebfedea0SLionel Sambuc fprintf (codefile, "*size = ret;\n"
546ebfedea0SLionel Sambuc "return 0;\n");
547ebfedea0SLionel Sambuc break;
548ebfedea0SLionel Sambuc default:
549ebfedea0SLionel Sambuc abort ();
550ebfedea0SLionel Sambuc }
551ebfedea0SLionel Sambuc fprintf (codefile, "}\n\n");
552ebfedea0SLionel Sambuc }
553