Path: blob/main/crypto/heimdal/lib/asn1/gen_free.c
34878 views
/*1* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gen_locl.h"3435RCSID("$Id$");3637static void38free_primitive (const char *typename, const char *name)39{40fprintf (codefile, "der_free_%s(%s);\n", typename, name);41}4243static void44free_type (const char *name, const Type *t, int preserve)45{46switch (t->type) {47case TType:48#if 049free_type (name, t->symbol->type, preserve);50#endif51fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);52break;53case TInteger:54if (t->range == NULL && t->members == NULL) {55free_primitive ("heim_integer", name);56break;57}58case TBoolean:59case TEnumerated :60case TNull:61case TGeneralizedTime:62case TUTCTime:63break;64case TBitString:65if (ASN1_TAILQ_EMPTY(t->members))66free_primitive("bit_string", name);67break;68case TOctetString:69free_primitive ("octet_string", name);70break;71case TChoice:72case TSet:73case TSequence: {74Member *m, *have_ellipsis = NULL;7576if (t->members == NULL)77break;7879if ((t->type == TSequence || t->type == TChoice) && preserve)80fprintf(codefile, "der_free_octet_string(&data->_save);\n");8182if(t->type == TChoice)83fprintf(codefile, "switch((%s)->element) {\n", name);8485ASN1_TAILQ_FOREACH(m, t->members, members) {86char *s;8788if (m->ellipsis){89have_ellipsis = m;90continue;91}9293if(t->type == TChoice)94fprintf(codefile, "case %s:\n", m->label);95if (asprintf (&s, "%s(%s)->%s%s",96m->optional ? "" : "&", name,97t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)98errx(1, "malloc");99if(m->optional)100fprintf(codefile, "if(%s) {\n", s);101free_type (s, m->type, FALSE);102if(m->optional)103fprintf(codefile,104"free(%s);\n"105"%s = NULL;\n"106"}\n",s, s);107free (s);108if(t->type == TChoice)109fprintf(codefile, "break;\n");110}111112if(t->type == TChoice) {113if (have_ellipsis)114fprintf(codefile,115"case %s:\n"116"der_free_octet_string(&(%s)->u.%s);\n"117"break;",118have_ellipsis->label,119name, have_ellipsis->gen_name);120fprintf(codefile, "}\n");121}122break;123}124case TSetOf:125case TSequenceOf: {126char *n;127128fprintf (codefile, "while((%s)->len){\n", name);129if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)130errx(1, "malloc");131free_type(n, t->subtype, FALSE);132fprintf(codefile,133"(%s)->len--;\n"134"}\n",135name);136fprintf(codefile,137"free((%s)->val);\n"138"(%s)->val = NULL;\n", name, name);139free(n);140break;141}142case TGeneralString:143free_primitive ("general_string", name);144break;145case TTeletexString:146free_primitive ("general_string", name);147break;148case TUTF8String:149free_primitive ("utf8string", name);150break;151case TPrintableString:152free_primitive ("printable_string", name);153break;154case TIA5String:155free_primitive ("ia5_string", name);156break;157case TBMPString:158free_primitive ("bmp_string", name);159break;160case TUniversalString:161free_primitive ("universal_string", name);162break;163case TVisibleString:164free_primitive ("visible_string", name);165break;166case TTag:167free_type (name, t->subtype, preserve);168break;169case TOID :170free_primitive ("oid", name);171break;172default :173abort ();174}175}176177void178generate_type_free (const Symbol *s)179{180int preserve = preserve_type(s->name) ? TRUE : FALSE;181182fprintf (codefile, "void ASN1CALL\n"183"free_%s(%s *data)\n"184"{\n",185s->gen_name, s->gen_name);186187free_type ("data", s->type, preserve);188fprintf (codefile, "}\n\n");189}190191192193