1 /*	$NetBSD: gen_free.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include "gen_locl.h"
37 
38 __RCSID("NetBSD");
39 
40 static void
free_primitive(const char * typename,const char * name)41 free_primitive (const char *typename, const char *name)
42 {
43     fprintf (codefile, "der_free_%s(%s);\n", typename, name);
44 }
45 
46 static void
free_type(const char * name,const Type * t,int preserve)47 free_type (const char *name, const Type *t, int preserve)
48 {
49     switch (t->type) {
50     case TType:
51 #if 0
52 	free_type (name, t->symbol->type, preserve);
53 #endif
54 	fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
55 	break;
56     case TInteger:
57 	if (t->range == NULL && t->members == NULL) {
58 	    free_primitive ("heim_integer", name);
59 	    break;
60 	}
61     case TBoolean:
62     case TEnumerated :
63     case TNull:
64     case TGeneralizedTime:
65     case TUTCTime:
66 	break;
67     case TBitString:
68 	if (ASN1_TAILQ_EMPTY(t->members))
69 	    free_primitive("bit_string", name);
70 	break;
71     case TOctetString:
72 	free_primitive ("octet_string", name);
73 	break;
74     case TChoice:
75     case TSet:
76     case TSequence: {
77 	Member *m, *have_ellipsis = NULL;
78 
79 	if (t->members == NULL)
80 	    break;
81 
82 	if ((t->type == TSequence || t->type == TChoice) && preserve)
83 	    fprintf(codefile, "der_free_octet_string(&data->_save);\n");
84 
85 	if(t->type == TChoice)
86 	    fprintf(codefile, "switch((%s)->element) {\n", name);
87 
88 	ASN1_TAILQ_FOREACH(m, t->members, members) {
89 	    char *s;
90 
91 	    if (m->ellipsis){
92 		have_ellipsis = m;
93 		continue;
94 	    }
95 
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)
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");
113 	}
114 
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");
124 	}
125 	break;
126     }
127     case TSetOf:
128     case TSequenceOf: {
129 	char *n;
130 
131 	fprintf (codefile, "while((%s)->len){\n", name);
132 	if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)
133 	    errx(1, "malloc");
134 	free_type(n, t->subtype, FALSE);
135 	fprintf(codefile,
136 		"(%s)->len--;\n"
137 		"}\n",
138 		name);
139 	fprintf(codefile,
140 		"free((%s)->val);\n"
141 		"(%s)->val = NULL;\n", name, name);
142 	free(n);
143 	break;
144     }
145     case TGeneralString:
146 	free_primitive ("general_string", name);
147 	break;
148     case TTeletexString:
149 	free_primitive ("general_string", name);
150 	break;
151     case TUTF8String:
152 	free_primitive ("utf8string", name);
153 	break;
154     case TPrintableString:
155 	free_primitive ("printable_string", name);
156 	break;
157     case TIA5String:
158 	free_primitive ("ia5_string", name);
159 	break;
160     case TBMPString:
161 	free_primitive ("bmp_string", name);
162 	break;
163     case TUniversalString:
164 	free_primitive ("universal_string", name);
165 	break;
166     case TVisibleString:
167 	free_primitive ("visible_string", name);
168 	break;
169     case TTag:
170 	free_type (name, t->subtype, preserve);
171 	break;
172     case TOID :
173 	free_primitive ("oid", name);
174 	break;
175     default :
176 	abort ();
177     }
178 }
179 
180 void
generate_type_free(const Symbol * s)181 generate_type_free (const Symbol *s)
182 {
183     int preserve = preserve_type(s->name) ? TRUE : FALSE;
184 
185     fprintf (codefile, "void ASN1CALL\n"
186 	     "free_%s(%s *data)\n"
187 	     "{\n",
188 	     s->gen_name, s->gen_name);
189 
190     free_type ("data", s->type, preserve);
191     fprintf (codefile, "}\n\n");
192 }
193 
194