gen_length.c revision 178826
1/*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska H�gskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "gen_locl.h"
35
36RCSID("$Id: gen_length.c 21503 2007-07-12 11:57:19Z lha $");
37
38static void
39length_primitive (const char *typename,
40		  const char *name,
41		  const char *variable)
42{
43    fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name);
44}
45
46static size_t
47length_tag(unsigned int tag)
48{
49    size_t len = 0;
50
51    if(tag <= 30)
52	return 1;
53    while(tag) {
54	tag /= 128;
55	len++;
56    }
57    return len + 1;
58}
59
60
61static int
62length_type (const char *name, const Type *t,
63	     const char *variable, const char *tmpstr)
64{
65    switch (t->type) {
66    case TType:
67#if 0
68	length_type (name, t->symbol->type);
69#endif
70	fprintf (codefile, "%s += length_%s(%s);\n",
71		 variable, t->symbol->gen_name, name);
72	break;
73    case TInteger:
74	if(t->members) {
75	    fprintf(codefile,
76		    "{\n"
77		    "int enumint = *%s;\n", name);
78	    length_primitive ("integer", "&enumint", variable);
79	    fprintf(codefile, "}\n");
80	} else if (t->range == NULL) {
81	    length_primitive ("heim_integer", name, variable);
82	} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
83	    length_primitive ("integer", name, variable);
84	} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
85	    length_primitive ("unsigned", name, variable);
86	} else if (t->range->min == 0 && t->range->max == INT_MAX) {
87	    length_primitive ("unsigned", name, variable);
88	} else
89	    errx(1, "%s: unsupported range %d -> %d",
90		 name, t->range->min, t->range->max);
91
92	break;
93    case TBoolean:
94	fprintf (codefile, "%s += 1;\n", variable);
95	break;
96    case TEnumerated :
97	length_primitive ("enumerated", name, variable);
98	break;
99    case TOctetString:
100	length_primitive ("octet_string", name, variable);
101	break;
102    case TBitString: {
103	if (ASN1_TAILQ_EMPTY(t->members))
104	    length_primitive("bit_string", name, variable);
105	else {
106	    if (!rfc1510_bitstring) {
107		Member *m;
108		int pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
109
110		fprintf(codefile,
111			"do {\n");
112		ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
113		    while (m->val / 8 < pos / 8) {
114			pos -= 8;
115		    }
116		    fprintf (codefile,
117			     "if((%s)->%s) { %s += %d; break; }\n",
118			     name, m->gen_name, variable, (pos + 8) / 8);
119		}
120		fprintf(codefile,
121			"} while(0);\n");
122		fprintf (codefile, "%s += 1;\n", variable);
123	    } else {
124		fprintf (codefile, "%s += 5;\n", variable);
125	    }
126	}
127	break;
128    }
129    case TSet:
130    case TSequence:
131    case TChoice: {
132	Member *m, *have_ellipsis = NULL;
133
134	if (t->members == NULL)
135	    break;
136
137	if(t->type == TChoice)
138	    fprintf (codefile, "switch((%s)->element) {\n", name);
139
140	ASN1_TAILQ_FOREACH(m, t->members, members) {
141	    char *s;
142
143	    if (m->ellipsis) {
144		have_ellipsis = m;
145		continue;
146	    }
147
148	    if(t->type == TChoice)
149		fprintf(codefile, "case %s:\n", m->label);
150
151	    asprintf (&s, "%s(%s)->%s%s",
152		      m->optional ? "" : "&", name,
153		      t->type == TChoice ? "u." : "", m->gen_name);
154	    if (s == NULL)
155		errx(1, "malloc");
156	    if (m->optional)
157		fprintf (codefile, "if(%s)", s);
158	    else if(m->defval)
159		gen_compare_defval(s + 1, m->defval);
160	    fprintf (codefile, "{\n"
161		     "size_t %s_oldret = %s;\n"
162		     "%s = 0;\n", tmpstr, variable, variable);
163	    length_type (s, m->type, "ret", m->gen_name);
164	    fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
165	    fprintf (codefile, "}\n");
166	    free (s);
167	    if(t->type == TChoice)
168		fprintf(codefile, "break;\n");
169	}
170	if(t->type == TChoice) {
171	    if (have_ellipsis)
172		fprintf(codefile,
173			"case %s:\n"
174			"ret += (%s)->u.%s.length;\n"
175			"break;\n",
176			have_ellipsis->label,
177			name,
178			have_ellipsis->gen_name);
179	    fprintf (codefile, "}\n"); /* switch */
180	}
181	break;
182    }
183    case TSetOf:
184    case TSequenceOf: {
185	char *n;
186	char *sname;
187
188	fprintf (codefile,
189		 "{\n"
190		 "int %s_oldret = %s;\n"
191		 "int i;\n"
192		 "%s = 0;\n",
193		 tmpstr, variable, variable);
194
195	fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name);
196	fprintf (codefile, "int %s_for_oldret = %s;\n"
197		 "%s = 0;\n", tmpstr, variable, variable);
198	asprintf (&n, "&(%s)->val[i]", name);
199	if (n == NULL)
200	    errx(1, "malloc");
201	asprintf (&sname, "%s_S_Of", tmpstr);
202	if (sname == NULL)
203	    errx(1, "malloc");
204	length_type(n, t->subtype, variable, sname);
205	fprintf (codefile, "%s += %s_for_oldret;\n",
206		 variable, tmpstr);
207	fprintf (codefile, "}\n");
208
209	fprintf (codefile,
210		 "%s += %s_oldret;\n"
211		 "}\n", variable, tmpstr);
212	free(n);
213	free(sname);
214	break;
215    }
216    case TGeneralizedTime:
217	length_primitive ("generalized_time", name, variable);
218	break;
219    case TGeneralString:
220	length_primitive ("general_string", name, variable);
221	break;
222    case TUTCTime:
223	length_primitive ("utctime", name, variable);
224	break;
225    case TUTF8String:
226	length_primitive ("utf8string", name, variable);
227	break;
228    case TPrintableString:
229	length_primitive ("printable_string", name, variable);
230	break;
231    case TIA5String:
232	length_primitive ("ia5_string", name, variable);
233	break;
234    case TBMPString:
235	length_primitive ("bmp_string", name, variable);
236	break;
237    case TUniversalString:
238	length_primitive ("universal_string", name, variable);
239	break;
240    case TVisibleString:
241	length_primitive ("visible_string", name, variable);
242	break;
243    case TNull:
244	fprintf (codefile, "/* NULL */\n");
245	break;
246    case TTag:{
247    	char *tname;
248	asprintf(&tname, "%s_tag", tmpstr);
249	if (tname == NULL)
250	    errx(1, "malloc");
251	length_type (name, t->subtype, variable, tname);
252	fprintf (codefile, "ret += %lu + der_length_len (ret);\n",
253		 (unsigned long)length_tag(t->tag.tagvalue));
254	free(tname);
255	break;
256    }
257    case TOID:
258	length_primitive ("oid", name, variable);
259	break;
260    default :
261	abort ();
262    }
263    return 0;
264}
265
266void
267generate_type_length (const Symbol *s)
268{
269    fprintf (headerfile,
270	     "size_t length_%s(const %s *);\n",
271	     s->gen_name, s->gen_name);
272
273    fprintf (codefile,
274	     "size_t\n"
275	     "length_%s(const %s *data)\n"
276	     "{\n"
277	     "size_t ret = 0;\n",
278	     s->gen_name, s->gen_name);
279
280    length_type ("data", s->type, "ret", "Top");
281    fprintf (codefile, "return ret;\n}\n\n");
282}
283
284