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