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