1 /* $NetBSD: gen_free.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 free_primitive (const char *typename
, const char *name
)
43 fprintf (codefile
, "der_free_%s(%s);\n", typename
, name
);
47 free_type (const char *name
, const Type
*t
, int preserve
)
52 free_type (name
, t
->symbol
->type
, preserve
);
54 fprintf (codefile
, "free_%s(%s);\n", t
->symbol
->gen_name
, name
);
57 if (t
->range
== NULL
&& t
->members
== NULL
) {
58 free_primitive ("heim_integer", name
);
64 case TGeneralizedTime
:
68 if (ASN1_TAILQ_EMPTY(t
->members
))
69 free_primitive("bit_string", name
);
72 free_primitive ("octet_string", name
);
77 Member
*m
, *have_ellipsis
= NULL
;
79 if (t
->members
== NULL
)
82 if ((t
->type
== TSequence
|| t
->type
== TChoice
) && preserve
)
83 fprintf(codefile
, "der_free_octet_string(&data->_save);\n");
85 if(t
->type
== TChoice
)
86 fprintf(codefile
, "switch((%s)->element) {\n", name
);
88 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
96 if(t
->type
== TChoice
)
97 fprintf(codefile
, "case %s:\n", m
->label
);
98 if (asprintf (&s
, "%s(%s)->%s%s",
99 m
->optional
? "" : "&", name
,
100 t
->type
== TChoice
? "u." : "", m
->gen_name
) < 0 || s
== NULL
)
103 fprintf(codefile
, "if(%s) {\n", s
);
104 free_type (s
, m
->type
, FALSE
);
111 if(t
->type
== TChoice
)
112 fprintf(codefile
, "break;\n");
115 if(t
->type
== TChoice
) {
119 "der_free_octet_string(&(%s)->u.%s);\n"
121 have_ellipsis
->label
,
122 name
, have_ellipsis
->gen_name
);
123 fprintf(codefile
, "}\n");
131 fprintf (codefile
, "while((%s)->len){\n", name
);
132 if (asprintf (&n
, "&(%s)->val[(%s)->len-1]", name
, name
) < 0 || n
== NULL
)
134 free_type(n
, t
->subtype
, FALSE
);
141 "(%s)->val = NULL;\n", name
, name
);
146 free_primitive ("general_string", name
);
149 free_primitive ("general_string", name
);
152 free_primitive ("utf8string", name
);
154 case TPrintableString
:
155 free_primitive ("printable_string", name
);
158 free_primitive ("ia5_string", name
);
161 free_primitive ("bmp_string", name
);
163 case TUniversalString
:
164 free_primitive ("universal_string", name
);
167 free_primitive ("visible_string", name
);
170 free_type (name
, t
->subtype
, preserve
);
173 free_primitive ("oid", name
);
181 generate_type_free (const Symbol
*s
)
183 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
185 fprintf (codefile
, "void ASN1CALL\n"
186 "free_%s(%s *data)\n"
188 s
->gen_name
, s
->gen_name
);
190 free_type ("data", s
->type
, preserve
);
191 fprintf (codefile
, "}\n\n");