No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / gen_free.c
blob072e06245f20a8fcbd91f2ec8a0fedc921e60763
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "gen_locl.h"
36 __RCSID("$Heimdal: gen_free.c 19539 2006-12-28 17:15:05Z lha $"
37 "$NetBSD$");
39 static void
40 free_primitive (const char *typename, const char *name)
42 fprintf (codefile, "der_free_%s(%s);\n", typename, name);
45 static void
46 free_type (const char *name, const Type *t, int preserve)
48 switch (t->type) {
49 case TType:
50 #if 0
51 free_type (name, t->symbol->type, preserve);
52 #endif
53 fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
54 break;
55 case TInteger:
56 if (t->range == NULL && t->members == NULL) {
57 free_primitive ("heim_integer", name);
58 break;
60 case TBoolean:
61 case TEnumerated :
62 case TNull:
63 case TGeneralizedTime:
64 case TUTCTime:
65 break;
66 case TBitString:
67 if (ASN1_TAILQ_EMPTY(t->members))
68 free_primitive("bit_string", name);
69 break;
70 case TOctetString:
71 free_primitive ("octet_string", name);
72 break;
73 case TChoice:
74 case TSet:
75 case TSequence: {
76 Member *m, *have_ellipsis = NULL;
78 if (t->members == NULL)
79 break;
81 if ((t->type == TSequence || t->type == TChoice) && preserve)
82 fprintf(codefile, "der_free_octet_string(&data->_save);\n");
84 if(t->type == TChoice)
85 fprintf(codefile, "switch((%s)->element) {\n", name);
87 ASN1_TAILQ_FOREACH(m, t->members, members) {
88 char *s;
90 if (m->ellipsis){
91 have_ellipsis = m;
92 continue;
95 if(t->type == TChoice)
96 fprintf(codefile, "case %s:\n", m->label);
97 asprintf (&s, "%s(%s)->%s%s",
98 m->optional ? "" : "&", name,
99 t->type == TChoice ? "u." : "", m->gen_name);
100 if (s == NULL)
101 errx(1, "malloc");
102 if(m->optional)
103 fprintf(codefile, "if(%s) {\n", s);
104 free_type (s, m->type, FALSE);
105 if(m->optional)
106 fprintf(codefile,
107 "free(%s);\n"
108 "%s = NULL;\n"
109 "}\n",s, s);
110 free (s);
111 if(t->type == TChoice)
112 fprintf(codefile, "break;\n");
115 if(t->type == TChoice) {
116 if (have_ellipsis)
117 fprintf(codefile,
118 "case %s:\n"
119 "der_free_octet_string(&(%s)->u.%s);\n"
120 "break;",
121 have_ellipsis->label,
122 name, have_ellipsis->gen_name);
123 fprintf(codefile, "}\n");
125 break;
127 case TSetOf:
128 case TSequenceOf: {
129 char *n;
131 fprintf (codefile, "while((%s)->len){\n", name);
132 asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name);
133 if (n == NULL)
134 errx(1, "malloc");
135 free_type(n, t->subtype, FALSE);
136 fprintf(codefile,
137 "(%s)->len--;\n"
138 "}\n",
139 name);
140 fprintf(codefile,
141 "free((%s)->val);\n"
142 "(%s)->val = NULL;\n", name, name);
143 free(n);
144 break;
146 case TGeneralString:
147 free_primitive ("general_string", name);
148 break;
149 case TUTF8String:
150 free_primitive ("utf8string", name);
151 break;
152 case TPrintableString:
153 free_primitive ("printable_string", name);
154 break;
155 case TIA5String:
156 free_primitive ("ia5_string", name);
157 break;
158 case TBMPString:
159 free_primitive ("bmp_string", name);
160 break;
161 case TUniversalString:
162 free_primitive ("universal_string", name);
163 break;
164 case TVisibleString:
165 free_primitive ("visible_string", name);
166 break;
167 case TTag:
168 free_type (name, t->subtype, preserve);
169 break;
170 case TOID :
171 free_primitive ("oid", name);
172 break;
173 default :
174 abort ();
178 void
179 generate_type_free (const Symbol *s)
181 int preserve = preserve_type(s->name) ? TRUE : FALSE;
183 fprintf (headerfile,
184 "void free_%s (%s *);\n",
185 s->gen_name, s->gen_name);
187 fprintf (codefile, "void\n"
188 "free_%s(%s *data)\n"
189 "{\n",
190 s->gen_name, s->gen_name);
192 free_type ("data", s->type, preserve);
193 fprintf (codefile, "}\n\n");