1*0a6a1f1dSLionel Sambuc /* $NetBSD: gen_length.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2005 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
length_primitive(const char * typename,const char * name,const char * variable)41ebfedea0SLionel Sambuc length_primitive (const char *typename,
42ebfedea0SLionel Sambuc const char *name,
43ebfedea0SLionel Sambuc const char *variable)
44ebfedea0SLionel Sambuc {
45ebfedea0SLionel Sambuc fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name);
46ebfedea0SLionel Sambuc }
47ebfedea0SLionel Sambuc
48ebfedea0SLionel Sambuc /* XXX same as der_length_tag */
49ebfedea0SLionel Sambuc static size_t
length_tag(unsigned int tag)50ebfedea0SLionel Sambuc length_tag(unsigned int tag)
51ebfedea0SLionel Sambuc {
52ebfedea0SLionel Sambuc size_t len = 0;
53ebfedea0SLionel Sambuc
54ebfedea0SLionel Sambuc if(tag <= 30)
55ebfedea0SLionel Sambuc return 1;
56ebfedea0SLionel Sambuc while(tag) {
57ebfedea0SLionel Sambuc tag /= 128;
58ebfedea0SLionel Sambuc len++;
59ebfedea0SLionel Sambuc }
60ebfedea0SLionel Sambuc return len + 1;
61ebfedea0SLionel Sambuc }
62ebfedea0SLionel Sambuc
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc static int
length_type(const char * name,const Type * t,const char * variable,const char * tmpstr)65ebfedea0SLionel Sambuc length_type (const char *name, const Type *t,
66ebfedea0SLionel Sambuc const char *variable, const char *tmpstr)
67ebfedea0SLionel Sambuc {
68ebfedea0SLionel Sambuc switch (t->type) {
69ebfedea0SLionel Sambuc case TType:
70ebfedea0SLionel Sambuc #if 0
71ebfedea0SLionel Sambuc length_type (name, t->symbol->type);
72ebfedea0SLionel Sambuc #endif
73ebfedea0SLionel Sambuc fprintf (codefile, "%s += length_%s(%s);\n",
74ebfedea0SLionel Sambuc variable, t->symbol->gen_name, name);
75ebfedea0SLionel Sambuc break;
76ebfedea0SLionel Sambuc case TInteger:
77ebfedea0SLionel Sambuc if(t->members) {
78ebfedea0SLionel Sambuc fprintf(codefile,
79ebfedea0SLionel Sambuc "{\n"
80ebfedea0SLionel Sambuc "int enumint = *%s;\n", name);
81ebfedea0SLionel Sambuc length_primitive ("integer", "&enumint", variable);
82ebfedea0SLionel Sambuc fprintf(codefile, "}\n");
83ebfedea0SLionel Sambuc } else if (t->range == NULL) {
84ebfedea0SLionel Sambuc length_primitive ("heim_integer", name, variable);
85ebfedea0SLionel Sambuc } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
86ebfedea0SLionel Sambuc length_primitive ("integer", name, variable);
87ebfedea0SLionel Sambuc } else if (t->range->min == 0 && t->range->max == UINT_MAX) {
88ebfedea0SLionel Sambuc length_primitive ("unsigned", name, variable);
89ebfedea0SLionel Sambuc } else if (t->range->min == 0 && t->range->max == INT_MAX) {
90ebfedea0SLionel Sambuc length_primitive ("unsigned", name, variable);
91ebfedea0SLionel Sambuc } else
92ebfedea0SLionel Sambuc errx(1, "%s: unsupported range %d -> %d",
93ebfedea0SLionel Sambuc name, t->range->min, t->range->max);
94ebfedea0SLionel Sambuc
95ebfedea0SLionel Sambuc break;
96ebfedea0SLionel Sambuc case TBoolean:
97ebfedea0SLionel Sambuc fprintf (codefile, "%s += 1;\n", variable);
98ebfedea0SLionel Sambuc break;
99ebfedea0SLionel Sambuc case TEnumerated :
100ebfedea0SLionel Sambuc length_primitive ("enumerated", name, variable);
101ebfedea0SLionel Sambuc break;
102ebfedea0SLionel Sambuc case TOctetString:
103ebfedea0SLionel Sambuc length_primitive ("octet_string", name, variable);
104ebfedea0SLionel Sambuc break;
105ebfedea0SLionel Sambuc case TBitString: {
106ebfedea0SLionel Sambuc if (ASN1_TAILQ_EMPTY(t->members))
107ebfedea0SLionel Sambuc length_primitive("bit_string", name, variable);
108ebfedea0SLionel Sambuc else {
109ebfedea0SLionel Sambuc if (!rfc1510_bitstring) {
110ebfedea0SLionel Sambuc Member *m;
111ebfedea0SLionel Sambuc int pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
112ebfedea0SLionel Sambuc
113ebfedea0SLionel Sambuc fprintf(codefile,
114ebfedea0SLionel Sambuc "do {\n");
115ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
116ebfedea0SLionel Sambuc while (m->val / 8 < pos / 8) {
117ebfedea0SLionel Sambuc pos -= 8;
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc fprintf (codefile,
120ebfedea0SLionel Sambuc "if((%s)->%s) { %s += %d; break; }\n",
121ebfedea0SLionel Sambuc name, m->gen_name, variable, (pos + 8) / 8);
122ebfedea0SLionel Sambuc }
123ebfedea0SLionel Sambuc fprintf(codefile,
124ebfedea0SLionel Sambuc "} while(0);\n");
125ebfedea0SLionel Sambuc fprintf (codefile, "%s += 1;\n", variable);
126ebfedea0SLionel Sambuc } else {
127ebfedea0SLionel Sambuc fprintf (codefile, "%s += 5;\n", variable);
128ebfedea0SLionel Sambuc }
129ebfedea0SLionel Sambuc }
130ebfedea0SLionel Sambuc break;
131ebfedea0SLionel Sambuc }
132ebfedea0SLionel Sambuc case TSet:
133ebfedea0SLionel Sambuc case TSequence:
134ebfedea0SLionel Sambuc case TChoice: {
135ebfedea0SLionel Sambuc Member *m, *have_ellipsis = NULL;
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc if (t->members == NULL)
138ebfedea0SLionel Sambuc break;
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc if(t->type == TChoice)
141ebfedea0SLionel Sambuc fprintf (codefile, "switch((%s)->element) {\n", name);
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH(m, t->members, members) {
144ebfedea0SLionel Sambuc char *s;
145ebfedea0SLionel Sambuc
146ebfedea0SLionel Sambuc if (m->ellipsis) {
147ebfedea0SLionel Sambuc have_ellipsis = m;
148ebfedea0SLionel Sambuc continue;
149ebfedea0SLionel Sambuc }
150ebfedea0SLionel Sambuc
151ebfedea0SLionel Sambuc if(t->type == TChoice)
152ebfedea0SLionel Sambuc fprintf(codefile, "case %s:\n", m->label);
153ebfedea0SLionel Sambuc
154ebfedea0SLionel Sambuc if (asprintf (&s, "%s(%s)->%s%s",
155ebfedea0SLionel Sambuc m->optional ? "" : "&", name,
156ebfedea0SLionel Sambuc t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
157ebfedea0SLionel Sambuc errx(1, "malloc");
158ebfedea0SLionel Sambuc if (m->optional)
159ebfedea0SLionel Sambuc fprintf (codefile, "if(%s)", s);
160ebfedea0SLionel Sambuc else if(m->defval)
161ebfedea0SLionel Sambuc gen_compare_defval(s + 1, m->defval);
162ebfedea0SLionel Sambuc fprintf (codefile, "{\n"
163ebfedea0SLionel Sambuc "size_t %s_oldret = %s;\n"
164ebfedea0SLionel Sambuc "%s = 0;\n", tmpstr, variable, variable);
165ebfedea0SLionel Sambuc length_type (s, m->type, "ret", m->gen_name);
166ebfedea0SLionel Sambuc fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
167ebfedea0SLionel Sambuc fprintf (codefile, "}\n");
168ebfedea0SLionel Sambuc free (s);
169ebfedea0SLionel Sambuc if(t->type == TChoice)
170ebfedea0SLionel Sambuc fprintf(codefile, "break;\n");
171ebfedea0SLionel Sambuc }
172ebfedea0SLionel Sambuc if(t->type == TChoice) {
173ebfedea0SLionel Sambuc if (have_ellipsis)
174ebfedea0SLionel Sambuc fprintf(codefile,
175ebfedea0SLionel Sambuc "case %s:\n"
176ebfedea0SLionel Sambuc "ret += (%s)->u.%s.length;\n"
177ebfedea0SLionel Sambuc "break;\n",
178ebfedea0SLionel Sambuc have_ellipsis->label,
179ebfedea0SLionel Sambuc name,
180ebfedea0SLionel Sambuc have_ellipsis->gen_name);
181ebfedea0SLionel Sambuc fprintf (codefile, "}\n"); /* switch */
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc break;
184ebfedea0SLionel Sambuc }
185ebfedea0SLionel Sambuc case TSetOf:
186ebfedea0SLionel Sambuc case TSequenceOf: {
187ebfedea0SLionel Sambuc char *n = NULL;
188ebfedea0SLionel Sambuc char *sname = NULL;
189ebfedea0SLionel Sambuc
190ebfedea0SLionel Sambuc fprintf (codefile,
191ebfedea0SLionel Sambuc "{\n"
192ebfedea0SLionel Sambuc "size_t %s_oldret = %s;\n"
193ebfedea0SLionel Sambuc "int i;\n"
194ebfedea0SLionel Sambuc "%s = 0;\n",
195ebfedea0SLionel Sambuc tmpstr, variable, variable);
196ebfedea0SLionel Sambuc
197ebfedea0SLionel Sambuc fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name);
198ebfedea0SLionel Sambuc fprintf (codefile, "size_t %s_for_oldret = %s;\n"
199ebfedea0SLionel Sambuc "%s = 0;\n", tmpstr, variable, variable);
200ebfedea0SLionel Sambuc if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)
201ebfedea0SLionel Sambuc errx(1, "malloc");
202ebfedea0SLionel Sambuc if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
203ebfedea0SLionel Sambuc errx(1, "malloc");
204ebfedea0SLionel Sambuc length_type(n, t->subtype, variable, sname);
205ebfedea0SLionel Sambuc fprintf (codefile, "%s += %s_for_oldret;\n",
206ebfedea0SLionel Sambuc variable, tmpstr);
207ebfedea0SLionel Sambuc fprintf (codefile, "}\n");
208ebfedea0SLionel Sambuc
209ebfedea0SLionel Sambuc fprintf (codefile,
210ebfedea0SLionel Sambuc "%s += %s_oldret;\n"
211ebfedea0SLionel Sambuc "}\n", variable, tmpstr);
212ebfedea0SLionel Sambuc free(n);
213ebfedea0SLionel Sambuc free(sname);
214ebfedea0SLionel Sambuc break;
215ebfedea0SLionel Sambuc }
216ebfedea0SLionel Sambuc case TGeneralizedTime:
217ebfedea0SLionel Sambuc length_primitive ("generalized_time", name, variable);
218ebfedea0SLionel Sambuc break;
219ebfedea0SLionel Sambuc case TGeneralString:
220ebfedea0SLionel Sambuc length_primitive ("general_string", name, variable);
221ebfedea0SLionel Sambuc break;
222ebfedea0SLionel Sambuc case TTeletexString:
223ebfedea0SLionel Sambuc length_primitive ("general_string", name, variable);
224ebfedea0SLionel Sambuc break;
225ebfedea0SLionel Sambuc case TUTCTime:
226ebfedea0SLionel Sambuc length_primitive ("utctime", name, variable);
227ebfedea0SLionel Sambuc break;
228ebfedea0SLionel Sambuc case TUTF8String:
229ebfedea0SLionel Sambuc length_primitive ("utf8string", name, variable);
230ebfedea0SLionel Sambuc break;
231ebfedea0SLionel Sambuc case TPrintableString:
232ebfedea0SLionel Sambuc length_primitive ("printable_string", name, variable);
233ebfedea0SLionel Sambuc break;
234ebfedea0SLionel Sambuc case TIA5String:
235ebfedea0SLionel Sambuc length_primitive ("ia5_string", name, variable);
236ebfedea0SLionel Sambuc break;
237ebfedea0SLionel Sambuc case TBMPString:
238ebfedea0SLionel Sambuc length_primitive ("bmp_string", name, variable);
239ebfedea0SLionel Sambuc break;
240ebfedea0SLionel Sambuc case TUniversalString:
241ebfedea0SLionel Sambuc length_primitive ("universal_string", name, variable);
242ebfedea0SLionel Sambuc break;
243ebfedea0SLionel Sambuc case TVisibleString:
244ebfedea0SLionel Sambuc length_primitive ("visible_string", name, variable);
245ebfedea0SLionel Sambuc break;
246ebfedea0SLionel Sambuc case TNull:
247ebfedea0SLionel Sambuc fprintf (codefile, "/* NULL */\n");
248ebfedea0SLionel Sambuc break;
249ebfedea0SLionel Sambuc case TTag:{
250ebfedea0SLionel Sambuc char *tname = NULL;
251ebfedea0SLionel Sambuc if (asprintf(&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
252ebfedea0SLionel Sambuc errx(1, "malloc");
253ebfedea0SLionel Sambuc length_type (name, t->subtype, variable, tname);
254ebfedea0SLionel Sambuc fprintf (codefile, "ret += %lu + der_length_len (ret);\n",
255ebfedea0SLionel Sambuc (unsigned long)length_tag(t->tag.tagvalue));
256ebfedea0SLionel Sambuc free(tname);
257ebfedea0SLionel Sambuc break;
258ebfedea0SLionel Sambuc }
259ebfedea0SLionel Sambuc case TOID:
260ebfedea0SLionel Sambuc length_primitive ("oid", name, variable);
261ebfedea0SLionel Sambuc break;
262ebfedea0SLionel Sambuc default :
263ebfedea0SLionel Sambuc abort ();
264ebfedea0SLionel Sambuc }
265ebfedea0SLionel Sambuc return 0;
266ebfedea0SLionel Sambuc }
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc void
generate_type_length(const Symbol * s)269ebfedea0SLionel Sambuc generate_type_length (const Symbol *s)
270ebfedea0SLionel Sambuc {
271ebfedea0SLionel Sambuc fprintf (codefile,
272ebfedea0SLionel Sambuc "size_t ASN1CALL\n"
273ebfedea0SLionel Sambuc "length_%s(const %s *data)\n"
274ebfedea0SLionel Sambuc "{\n"
275ebfedea0SLionel Sambuc "size_t ret = 0;\n",
276ebfedea0SLionel Sambuc s->gen_name, s->gen_name);
277ebfedea0SLionel Sambuc
278ebfedea0SLionel Sambuc length_type ("data", s->type, "ret", "Top");
279ebfedea0SLionel Sambuc fprintf (codefile, "return ret;\n}\n\n");
280ebfedea0SLionel Sambuc }
281ebfedea0SLionel Sambuc
282