1*0a6a1f1dSLionel Sambuc /* $NetBSD: gen_free.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "gen_locl.h"
37ebfedea0SLionel Sambuc
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc static void
free_primitive(const char * typename,const char * name)41ebfedea0SLionel Sambuc free_primitive (const char *typename, const char *name)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc fprintf (codefile, "der_free_%s(%s);\n", typename, name);
44ebfedea0SLionel Sambuc }
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc static void
free_type(const char * name,const Type * t,int preserve)47ebfedea0SLionel Sambuc free_type (const char *name, const Type *t, int preserve)
48ebfedea0SLionel Sambuc {
49ebfedea0SLionel Sambuc switch (t->type) {
50ebfedea0SLionel Sambuc case TType:
51ebfedea0SLionel Sambuc #if 0
52ebfedea0SLionel Sambuc free_type (name, t->symbol->type, preserve);
53ebfedea0SLionel Sambuc #endif
54ebfedea0SLionel Sambuc fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
55ebfedea0SLionel Sambuc break;
56ebfedea0SLionel Sambuc case TInteger:
57ebfedea0SLionel Sambuc if (t->range == NULL && t->members == NULL) {
58ebfedea0SLionel Sambuc free_primitive ("heim_integer", name);
59ebfedea0SLionel Sambuc break;
60ebfedea0SLionel Sambuc }
61ebfedea0SLionel Sambuc case TBoolean:
62ebfedea0SLionel Sambuc case TEnumerated :
63ebfedea0SLionel Sambuc case TNull:
64ebfedea0SLionel Sambuc case TGeneralizedTime:
65ebfedea0SLionel Sambuc case TUTCTime:
66ebfedea0SLionel Sambuc break;
67ebfedea0SLionel Sambuc case TBitString:
68ebfedea0SLionel Sambuc if (ASN1_TAILQ_EMPTY(t->members))
69ebfedea0SLionel Sambuc free_primitive("bit_string", name);
70ebfedea0SLionel Sambuc break;
71ebfedea0SLionel Sambuc case TOctetString:
72ebfedea0SLionel Sambuc free_primitive ("octet_string", name);
73ebfedea0SLionel Sambuc break;
74ebfedea0SLionel Sambuc case TChoice:
75ebfedea0SLionel Sambuc case TSet:
76ebfedea0SLionel Sambuc case TSequence: {
77ebfedea0SLionel Sambuc Member *m, *have_ellipsis = NULL;
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc if (t->members == NULL)
80ebfedea0SLionel Sambuc break;
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc if ((t->type == TSequence || t->type == TChoice) && preserve)
83ebfedea0SLionel Sambuc fprintf(codefile, "der_free_octet_string(&data->_save);\n");
84ebfedea0SLionel Sambuc
85ebfedea0SLionel Sambuc if(t->type == TChoice)
86ebfedea0SLionel Sambuc fprintf(codefile, "switch((%s)->element) {\n", name);
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc ASN1_TAILQ_FOREACH(m, t->members, members) {
89ebfedea0SLionel Sambuc char *s;
90ebfedea0SLionel Sambuc
91ebfedea0SLionel Sambuc if (m->ellipsis){
92ebfedea0SLionel Sambuc have_ellipsis = m;
93ebfedea0SLionel Sambuc continue;
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc if(t->type == TChoice)
97ebfedea0SLionel Sambuc fprintf(codefile, "case %s:\n", m->label);
98ebfedea0SLionel Sambuc if (asprintf (&s, "%s(%s)->%s%s",
99ebfedea0SLionel Sambuc m->optional ? "" : "&", name,
100ebfedea0SLionel Sambuc t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
101ebfedea0SLionel Sambuc errx(1, "malloc");
102ebfedea0SLionel Sambuc if(m->optional)
103ebfedea0SLionel Sambuc fprintf(codefile, "if(%s) {\n", s);
104ebfedea0SLionel Sambuc free_type (s, m->type, FALSE);
105ebfedea0SLionel Sambuc if(m->optional)
106ebfedea0SLionel Sambuc fprintf(codefile,
107ebfedea0SLionel Sambuc "free(%s);\n"
108ebfedea0SLionel Sambuc "%s = NULL;\n"
109ebfedea0SLionel Sambuc "}\n",s, s);
110ebfedea0SLionel Sambuc free (s);
111ebfedea0SLionel Sambuc if(t->type == TChoice)
112ebfedea0SLionel Sambuc fprintf(codefile, "break;\n");
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc
115ebfedea0SLionel Sambuc if(t->type == TChoice) {
116ebfedea0SLionel Sambuc if (have_ellipsis)
117ebfedea0SLionel Sambuc fprintf(codefile,
118ebfedea0SLionel Sambuc "case %s:\n"
119ebfedea0SLionel Sambuc "der_free_octet_string(&(%s)->u.%s);\n"
120ebfedea0SLionel Sambuc "break;",
121ebfedea0SLionel Sambuc have_ellipsis->label,
122ebfedea0SLionel Sambuc name, have_ellipsis->gen_name);
123ebfedea0SLionel Sambuc fprintf(codefile, "}\n");
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc break;
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc case TSetOf:
128ebfedea0SLionel Sambuc case TSequenceOf: {
129ebfedea0SLionel Sambuc char *n;
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc fprintf (codefile, "while((%s)->len){\n", name);
132ebfedea0SLionel Sambuc if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)
133ebfedea0SLionel Sambuc errx(1, "malloc");
134ebfedea0SLionel Sambuc free_type(n, t->subtype, FALSE);
135ebfedea0SLionel Sambuc fprintf(codefile,
136ebfedea0SLionel Sambuc "(%s)->len--;\n"
137ebfedea0SLionel Sambuc "}\n",
138ebfedea0SLionel Sambuc name);
139ebfedea0SLionel Sambuc fprintf(codefile,
140ebfedea0SLionel Sambuc "free((%s)->val);\n"
141ebfedea0SLionel Sambuc "(%s)->val = NULL;\n", name, name);
142ebfedea0SLionel Sambuc free(n);
143ebfedea0SLionel Sambuc break;
144ebfedea0SLionel Sambuc }
145ebfedea0SLionel Sambuc case TGeneralString:
146ebfedea0SLionel Sambuc free_primitive ("general_string", name);
147ebfedea0SLionel Sambuc break;
148ebfedea0SLionel Sambuc case TTeletexString:
149ebfedea0SLionel Sambuc free_primitive ("general_string", name);
150ebfedea0SLionel Sambuc break;
151ebfedea0SLionel Sambuc case TUTF8String:
152ebfedea0SLionel Sambuc free_primitive ("utf8string", name);
153ebfedea0SLionel Sambuc break;
154ebfedea0SLionel Sambuc case TPrintableString:
155ebfedea0SLionel Sambuc free_primitive ("printable_string", name);
156ebfedea0SLionel Sambuc break;
157ebfedea0SLionel Sambuc case TIA5String:
158ebfedea0SLionel Sambuc free_primitive ("ia5_string", name);
159ebfedea0SLionel Sambuc break;
160ebfedea0SLionel Sambuc case TBMPString:
161ebfedea0SLionel Sambuc free_primitive ("bmp_string", name);
162ebfedea0SLionel Sambuc break;
163ebfedea0SLionel Sambuc case TUniversalString:
164ebfedea0SLionel Sambuc free_primitive ("universal_string", name);
165ebfedea0SLionel Sambuc break;
166ebfedea0SLionel Sambuc case TVisibleString:
167ebfedea0SLionel Sambuc free_primitive ("visible_string", name);
168ebfedea0SLionel Sambuc break;
169ebfedea0SLionel Sambuc case TTag:
170ebfedea0SLionel Sambuc free_type (name, t->subtype, preserve);
171ebfedea0SLionel Sambuc break;
172ebfedea0SLionel Sambuc case TOID :
173ebfedea0SLionel Sambuc free_primitive ("oid", name);
174ebfedea0SLionel Sambuc break;
175ebfedea0SLionel Sambuc default :
176ebfedea0SLionel Sambuc abort ();
177ebfedea0SLionel Sambuc }
178ebfedea0SLionel Sambuc }
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc void
generate_type_free(const Symbol * s)181ebfedea0SLionel Sambuc generate_type_free (const Symbol *s)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc int preserve = preserve_type(s->name) ? TRUE : FALSE;
184ebfedea0SLionel Sambuc
185ebfedea0SLionel Sambuc fprintf (codefile, "void ASN1CALL\n"
186ebfedea0SLionel Sambuc "free_%s(%s *data)\n"
187ebfedea0SLionel Sambuc "{\n",
188ebfedea0SLionel Sambuc s->gen_name, s->gen_name);
189ebfedea0SLionel Sambuc
190ebfedea0SLionel Sambuc free_type ("data", s->type, preserve);
191ebfedea0SLionel Sambuc fprintf (codefile, "}\n\n");
192ebfedea0SLionel Sambuc }
193ebfedea0SLionel Sambuc
194