1 /* $NetBSD: gen_free.c,v 1.3 2023/06/19 21:41:42 christos 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: gen_free.c,v 1.3 2023/06/19 21:41:42 christos Exp $"); 39 40 static void 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 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 /* fallthrough */ 62 case TBoolean: 63 case TEnumerated : 64 case TNull: 65 case TGeneralizedTime: 66 case TUTCTime: 67 /* 68 * This doesn't do much, but it leaves zeros where garbage might 69 * otherwise have been found. Gets us closer to having the equivalent 70 * of a memset()-to-zero data structure after calling the free 71 * functions. 72 */ 73 fprintf(codefile, "*%s = 0;\n", name); 74 break; 75 case TBitString: 76 if (ASN1_TAILQ_EMPTY(t->members)) 77 free_primitive("bit_string", name); 78 break; 79 case TOctetString: 80 free_primitive ("octet_string", name); 81 break; 82 case TChoice: 83 case TSet: 84 case TSequence: { 85 Member *m, *have_ellipsis = NULL; 86 87 if (t->members == NULL) 88 break; 89 90 if ((t->type == TSequence || t->type == TChoice) && preserve) 91 fprintf(codefile, "der_free_octet_string(&data->_save);\n"); 92 93 if(t->type == TChoice) 94 fprintf(codefile, "switch((%s)->element) {\n", name); 95 96 ASN1_TAILQ_FOREACH(m, t->members, members) { 97 char *s; 98 99 if (m->ellipsis){ 100 have_ellipsis = m; 101 continue; 102 } 103 104 if(t->type == TChoice) 105 fprintf(codefile, "case %s:\n", m->label); 106 if (asprintf (&s, "%s(%s)->%s%s", 107 m->optional ? "" : "&", name, 108 t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL) 109 errx(1, "malloc"); 110 if(m->optional) 111 fprintf(codefile, "if(%s) {\n", s); 112 free_type (s, m->type, FALSE); 113 if(m->optional) 114 fprintf(codefile, 115 "free(%s);\n" 116 "%s = NULL;\n" 117 "}\n",s, s); 118 free (s); 119 if(t->type == TChoice) 120 fprintf(codefile, "break;\n"); 121 } 122 123 if(t->type == TChoice) { 124 if (have_ellipsis) 125 fprintf(codefile, 126 "case %s:\n" 127 "der_free_octet_string(&(%s)->u.%s);\n" 128 "break;", 129 have_ellipsis->label, 130 name, have_ellipsis->gen_name); 131 fprintf(codefile, "}\n"); 132 } 133 break; 134 } 135 case TSetOf: 136 case TSequenceOf: { 137 char *n; 138 139 fprintf (codefile, "while((%s)->len){\n", name); 140 if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL) 141 errx(1, "malloc"); 142 free_type(n, t->subtype, FALSE); 143 fprintf(codefile, 144 "(%s)->len--;\n" 145 "}\n", 146 name); 147 fprintf(codefile, 148 "free((%s)->val);\n" 149 "(%s)->val = NULL;\n", name, name); 150 free(n); 151 break; 152 } 153 case TGeneralString: 154 free_primitive ("general_string", name); 155 break; 156 case TTeletexString: 157 free_primitive ("general_string", name); 158 break; 159 case TUTF8String: 160 free_primitive ("utf8string", name); 161 break; 162 case TPrintableString: 163 free_primitive ("printable_string", name); 164 break; 165 case TIA5String: 166 free_primitive ("ia5_string", name); 167 break; 168 case TBMPString: 169 free_primitive ("bmp_string", name); 170 break; 171 case TUniversalString: 172 free_primitive ("universal_string", name); 173 break; 174 case TVisibleString: 175 free_primitive ("visible_string", name); 176 break; 177 case TTag: 178 free_type (name, t->subtype, preserve); 179 break; 180 case TOID : 181 free_primitive ("oid", name); 182 break; 183 default : 184 abort (); 185 } 186 } 187 188 void 189 generate_type_free (const Symbol *s) 190 { 191 int preserve = preserve_type(s->name) ? TRUE : FALSE; 192 193 fprintf (codefile, "void ASN1CALL\n" 194 "free_%s(%s *data)\n" 195 "{\n", 196 s->gen_name, s->gen_name); 197 198 free_type ("data", s->type, preserve); 199 fprintf (codefile, "}\n\n"); 200 } 201 202