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 free_primitive (const char *typename
, const char *name
)
41 fprintf (codefile
, "der_free_%s(%s);\n", typename
, name
);
45 free_type (const char *name
, const Type
*t
, int preserve
)
50 free_type (name
, t
->symbol
->type
, preserve
);
52 fprintf (codefile
, "free_%s(%s);\n", t
->symbol
->gen_name
, name
);
55 if (t
->range
== NULL
&& t
->members
== NULL
) {
56 free_primitive ("heim_integer", name
);
63 case TGeneralizedTime
:
66 * This doesn't do much, but it leaves zeros where garbage might
67 * otherwise have been found. Gets us closer to having the equivalent
68 * of a memset()-to-zero data structure after calling the free
71 fprintf(codefile
, "*%s = 0;\n", name
);
74 if (HEIM_TAILQ_EMPTY(t
->members
))
75 free_primitive("bit_string", name
);
78 free_primitive ("octet_string", name
);
83 Member
*m
, *have_ellipsis
= NULL
;
85 if (t
->members
== NULL
)
88 if ((t
->type
== TSequence
|| t
->type
== TChoice
) && preserve
)
89 fprintf(codefile
, "der_free_octet_string(&data->_save);\n");
91 if(t
->type
== TChoice
)
92 fprintf(codefile
, "switch((%s)->element) {\n", name
);
94 HEIM_TAILQ_FOREACH(m
, t
->members
, members
) {
102 if(t
->type
== TChoice
)
103 fprintf(codefile
, "case %s:\n", m
->label
);
104 if (asprintf (&s
, "%s(%s)->%s%s",
105 m
->optional
? "" : "&", name
,
106 t
->type
== TChoice
? "u." : "", m
->gen_name
) < 0 || s
== NULL
)
109 fprintf(codefile
, "if(%s) {\n", s
);
110 free_type (s
, m
->type
, FALSE
);
117 if(t
->type
== TChoice
)
118 fprintf(codefile
, "break;\n");
121 if(t
->type
== TChoice
) {
125 "der_free_octet_string(&(%s)->u.%s);\n"
127 have_ellipsis
->label
,
128 name
, have_ellipsis
->gen_name
);
129 fprintf(codefile
, "}\n");
137 fprintf (codefile
, "if ((%s)->val)\nwhile((%s)->len){\n", name
, name
);
138 if (asprintf (&n
, "&(%s)->val[(%s)->len-1]", name
, name
) < 0 || n
== NULL
)
140 free_type(n
, t
->subtype
, FALSE
);
143 "} else (%s)->len = 0;\n",
147 "(%s)->val = NULL;\n", name
, name
);
152 free_primitive ("general_string", name
);
155 free_primitive ("general_string", name
);
158 free_primitive ("utf8string", name
);
160 case TPrintableString
:
161 free_primitive ("printable_string", name
);
164 free_primitive ("ia5_string", name
);
167 free_primitive ("bmp_string", name
);
169 case TUniversalString
:
170 free_primitive ("universal_string", name
);
173 free_primitive ("visible_string", name
);
176 free_type (name
, t
->subtype
, preserve
);
179 free_primitive ("oid", name
);
187 generate_type_free (const Symbol
*s
)
189 struct decoration deco
;
190 ssize_t more_deco
= -1;
191 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
193 fprintf (codefile
, "void ASN1CALL\n"
194 "free_%s(%s *data)\n"
196 s
->gen_name
, s
->gen_name
);
198 free_type ("data", s
->type
, preserve
);
199 while (decorate_type(s
->gen_name
, &deco
, &more_deco
)) {
200 if (deco
.ext
&& deco
.free_function_name
== NULL
) {
201 /* Decorated with field of external type but no free function */
203 fprintf(codefile
, "(data)->%s = 0;\n", deco
.field_name
);
206 "memset(&(data)->%s, 0, sizeof((data)->%s));\n",
207 deco
.field_name
, deco
.field_name
);
208 } else if (deco
.ext
) {
209 /* Decorated with field of external type w/ free function */
211 fprintf(codefile
, "if ((data)->%s) {\n", deco
.field_name
);
212 fprintf(codefile
, "%s((data)->%s);\n",
213 deco
.free_function_name
, deco
.field_name
);
214 fprintf(codefile
, "(data)->%s = 0;\n", deco
.field_name
);
215 fprintf(codefile
, "}\n");
217 fprintf(codefile
, "%s(&(data)->%s);\n",
218 deco
.free_function_name
, deco
.field_name
);
220 "memset(&(data)->%s, 0, sizeof((data)->%s));\n",
221 deco
.field_name
, deco
.field_name
);
223 } else if (deco
.opt
) {
224 /* Decorated with optional field of ASN.1 type */
225 fprintf(codefile
, "if ((data)->%s) {\n", deco
.field_name
);
226 fprintf(codefile
, "free_%s((data)->%s);\n",
227 deco
.field_type
, deco
.field_name
);
228 fprintf(codefile
, "free((data)->%s);\n", deco
.field_name
);
229 fprintf(codefile
, "(data)->%s = NULL;\n", deco
.field_name
);
230 fprintf(codefile
, "}\n");
232 /* Decorated with required field of ASN.1 type */
233 fprintf(codefile
, "free_%s(&(data)->%s);\n",
234 deco
.field_type
, deco
.field_name
);
236 free(deco
.field_type
);
238 fprintf (codefile
, "}\n\n");