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