2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
39 length_primitive (const char *typename
,
43 fprintf (codefile
, "%s += der_length_%s(%s);\n", variable
, typename
, name
);
46 /* XXX same as der_length_tag */
48 length_tag(unsigned int tag
)
63 length_type (const char *name
, const Type
*t
,
64 const char *variable
, const char *tmpstr
)
69 length_type (name
, t
->symbol
->type
);
71 fprintf (codefile
, "%s += length_%s(%s);\n",
72 variable
, t
->symbol
->gen_name
, name
);
78 "int enumint = *%s;\n", name
);
79 length_primitive ("integer", "&enumint", variable
);
80 fprintf(codefile
, "}\n");
81 } else if (t
->range
== NULL
) {
82 length_primitive ("heim_integer", name
, variable
);
83 } else if (t
->range
->min
< 0 &&
84 (t
->range
->min
< INT_MIN
|| t
->range
->max
> INT_MAX
)) {
85 length_primitive ("integer64", name
, variable
);
86 } else if (t
->range
->min
< 0) {
87 length_primitive ("integer", name
, variable
);
88 } else if (t
->range
->max
> UINT_MAX
) {
89 length_primitive ("unsigned64", name
, variable
);
91 length_primitive ("unsigned", name
, variable
);
95 fprintf (codefile
, "%s += 1;\n", variable
);
98 length_primitive ("enumerated", name
, variable
);
101 length_primitive ("octet_string", name
, variable
);
104 if (HEIM_TAILQ_EMPTY(t
->members
))
105 length_primitive("bit_string", name
, variable
);
107 if (!rfc1510_bitstring
) {
109 int pos
= HEIM_TAILQ_LAST(t
->members
, memhead
)->val
;
113 HEIM_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
114 while (m
->val
/ 8 < pos
/ 8) {
118 "if((%s)->%s) { %s += %d; break; }\n",
119 name
, m
->gen_name
, variable
, (pos
+ 8) / 8);
123 fprintf (codefile
, "%s += 1;\n", variable
);
125 fprintf (codefile
, "%s += 5;\n", variable
);
133 Member
*m
, *have_ellipsis
= NULL
;
135 if (t
->members
== NULL
)
138 if(t
->type
== TChoice
)
139 fprintf (codefile
, "switch((%s)->element) {\n", name
);
141 HEIM_TAILQ_FOREACH(m
, t
->members
, members
) {
149 if(t
->type
== TChoice
)
150 fprintf(codefile
, "case %s:\n", m
->label
);
152 if (asprintf (&s
, "%s(%s)->%s%s",
153 m
->optional
? "" : "&", name
,
154 t
->type
== TChoice
? "u." : "", m
->gen_name
) < 0 || s
== NULL
)
157 fprintf (codefile
, "if(%s)", s
);
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");
167 if(t
->type
== TChoice
)
168 fprintf(codefile
, "break;\n");
170 if(t
->type
== TChoice
) {
174 "ret += (%s)->u.%s.length;\n"
176 have_ellipsis
->label
,
178 have_ellipsis
->gen_name
);
179 fprintf (codefile
, "}\n"); /* switch */
190 "size_t %s_oldret = %s;\n"
191 "unsigned int n_%s;\n"
193 tmpstr
, variable
, tmpstr
, variable
);
195 fprintf (codefile
, "for(n_%s = (%s)->len; n_%s > 0; --n_%s){\n",
196 tmpstr
, name
, tmpstr
, tmpstr
);
197 fprintf (codefile
, "size_t %s_for_oldret = %s;\n"
198 "%s = 0;\n", tmpstr
, variable
, variable
);
199 if (asprintf (&n
, "&(%s)->val[n_%s - 1]", name
, tmpstr
) < 0 || n
== NULL
)
201 if (asprintf (&sname
, "%s_S_Of", tmpstr
) < 0 || sname
== NULL
)
203 length_type(n
, t
->subtype
, variable
, sname
);
204 fprintf (codefile
, "%s += %s_for_oldret;\n",
206 fprintf (codefile
, "}\n");
210 "}\n", variable
, tmpstr
);
215 case TGeneralizedTime
:
216 length_primitive ("generalized_time", name
, variable
);
219 length_primitive ("general_string", name
, variable
);
222 length_primitive ("general_string", name
, variable
);
225 length_primitive ("utctime", name
, variable
);
228 length_primitive ("utf8string", name
, variable
);
230 case TPrintableString
:
231 length_primitive ("printable_string", name
, variable
);
234 length_primitive ("ia5_string", name
, variable
);
237 length_primitive ("bmp_string", name
, variable
);
239 case TUniversalString
:
240 length_primitive ("universal_string", name
, variable
);
243 length_primitive ("visible_string", name
, variable
);
246 fprintf (codefile
, "/* NULL */\n");
251 int prim
= !(t
->tag
.tagclass
!= ASN1_C_UNIV
&&
252 t
->tag
.tagenv
== TE_EXPLICIT
) &&
253 is_primitive_type(t
->subtype
);
255 if (asprintf(&tname
, "%s_tag", tmpstr
) < 0 || tname
== NULL
)
257 length_type (name
, t
->subtype
, variable
, tname
);
258 /* See the comments in encode_type() about IMPLICIT tags */
259 if (t
->tag
.tagenv
== TE_IMPLICIT
&& !prim
&&
260 t
->subtype
->type
!= TSequenceOf
&& t
->subtype
->type
!= TSetOf
&&
261 t
->subtype
->type
!= TChoice
) {
262 if (t
->subtype
->symbol
&&
263 (t
->subtype
->type
== TSequence
||
264 t
->subtype
->type
== TSet
))
266 else if (t
->subtype
->symbol
&& strcmp(t
->subtype
->symbol
->name
, "heim_any"))
268 } else if (t
->tag
.tagenv
== TE_IMPLICIT
&& prim
&& t
->subtype
->symbol
)
269 replace_tag
= is_tagged_type(t
->subtype
->symbol
->type
);
272 * We're replacing the tag of the underlying type. If that type is
273 * imported, then we don't know its tag, so we rely on the
274 * asn1_tag_tag_<TypeName> enum value we generated for it, and we
275 * use the asn1_tag_length_<TypeName> enum value to avoid having to
276 * call der_length_tag() at run-time.
278 fprintf(codefile
, "ret += %lu - asn1_tag_length_%s;\n",
279 (unsigned long)length_tag(t
->tag
.tagvalue
),
280 t
->subtype
->symbol
->gen_name
);
282 fprintf(codefile
, "ret += %lu + der_length_len (ret);\n",
283 (unsigned long)length_tag(t
->tag
.tagvalue
));
288 length_primitive ("oid", name
, variable
);
297 generate_type_length (const Symbol
*s
)
301 "length_%s(const %s *data)\n"
304 s
->gen_name
, s
->gen_name
);
306 length_type ("data", s
->type
, "ret", "Top");
307 fprintf (codefile
, "return ret;\n}\n\n");