xref: /freebsd/crypto/heimdal/lib/asn1/gen_free.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*
2  * Copyright (c) 1997 - 2000 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: gen_free.c,v 1.8 2000/04/06 17:24:02 assar Exp $");
37 
38 static void
39 free_primitive (const char *typename, const char *name)
40 {
41     fprintf (codefile, "free_%s(%s);\n", typename, name);
42 }
43 
44 static void
45 free_type (const char *name, const Type *t)
46 {
47   switch (t->type) {
48   case TType:
49 #if 0
50       free_type (name, t->symbol->type);
51 #endif
52       fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
53       break;
54   case TInteger:
55   case TUInteger:
56       break;
57   case TOctetString:
58       free_primitive ("octet_string", name);
59       break;
60   case TBitString: {
61       break;
62   }
63   case TSequence: {
64       Member *m;
65       int tag = -1;
66 
67       if (t->members == NULL)
68 	  break;
69 
70       for (m = t->members; m && tag != m->val; m = m->next) {
71 	  char *s;
72 
73 	  asprintf (&s, "%s(%s)->%s",
74 		    m->optional ? "" : "&", name, m->gen_name);
75 	  if(m->optional)
76 	      fprintf(codefile, "if(%s) {\n", s);
77 	  free_type (s, m->type);
78 	  if(m->optional)
79 	      fprintf(codefile,
80 		      "free(%s);\n"
81 		      "}\n",s);
82 	  if (tag == -1)
83 	      tag = m->val;
84 	  free (s);
85       }
86       break;
87   }
88   case TSequenceOf: {
89       char *n;
90 
91       fprintf (codefile, "while((%s)->len){\n", name);
92       asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name);
93       free_type(n, t->subtype);
94       fprintf(codefile,
95 	      "(%s)->len--;\n"
96 	      "}\n",
97 	      name);
98       fprintf(codefile,
99 	      "free((%s)->val);\n", name);
100       free(n);
101       break;
102   }
103   case TGeneralizedTime:
104       break;
105   case TGeneralString:
106       free_primitive ("general_string", name);
107       break;
108   case TApplication:
109       free_type (name, t->subtype);
110       break;
111   default :
112       abort ();
113   }
114 }
115 
116 void
117 generate_type_free (const Symbol *s)
118 {
119   fprintf (headerfile,
120 	   "void   free_%s  (%s *);\n",
121 	   s->gen_name, s->gen_name);
122 
123   fprintf (codefile, "void\n"
124 	   "free_%s(%s *data)\n"
125 	   "{\n",
126 	   s->gen_name, s->gen_name);
127 
128   free_type ("data", s->type);
129   fprintf (codefile, "}\n\n");
130 }
131 
132