LCOV - code coverage report
Current view: top level - source4/heimdal/lib/asn1 - gen_free.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 94 99 94.9 %
Date: 2021-09-23 10:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       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        4122 : free_primitive (const char *typename, const char *name)
      40             : {
      41        4580 :     fprintf (codefile, "der_free_%s(%s);\n", typename, name);
      42        4122 : }
      43             : 
      44             : static void
      45       38684 : free_type (const char *name, const Type *t, int preserve)
      46             : {
      47       38684 :     switch (t->type) {
      48        8778 :     case TType:
      49             : #if 0
      50             :         free_type (name, t->symbol->type, preserve);
      51             : #endif
      52        8778 :         fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
      53        8316 :         break;
      54        1672 :     case TInteger:
      55        1672 :         if (t->range == NULL && t->members == NULL) {
      56         684 :             free_primitive ("heim_integer", name);
      57         684 :             break;
      58             :         }
      59             :     case TBoolean:
      60             :     case TEnumerated :
      61             :     case TNull:
      62             :     case TGeneralizedTime:
      63             :     case TUTCTime:
      64        1224 :         break;
      65         418 :     case TBitString:
      66         418 :         if (ASN1_TAILQ_EMPTY(t->members))
      67         216 :             free_primitive("bit_string", name);
      68         396 :         break;
      69        1824 :     case TOctetString:
      70        1728 :         free_primitive ("octet_string", name);
      71        1728 :         break;
      72        3553 :     case TChoice:
      73             :     case TSet:
      74             :     case TSequence: {
      75        3553 :         Member *m, *have_ellipsis = NULL;
      76             : 
      77        3553 :         if (t->members == NULL)
      78           0 :             break;
      79             : 
      80        3553 :         if ((t->type == TSequence || t->type == TChoice) && preserve)
      81         114 :             fprintf(codefile, "der_free_octet_string(&data->_save);\n");
      82             : 
      83        3553 :         if(t->type == TChoice)
      84         380 :             fprintf(codefile, "switch((%s)->element) {\n", name);
      85             : 
      86       16739 :         ASN1_TAILQ_FOREACH(m, t->members, members) {
      87             :             char *s;
      88             : 
      89       13186 :             if (m->ellipsis){
      90         608 :                 have_ellipsis = m;
      91         608 :                 continue;
      92             :             }
      93             : 
      94       12578 :             if(t->type == TChoice)
      95        1159 :                 fprintf(codefile, "case %s:\n", m->label);
      96       38396 :             if (asprintf (&s, "%s(%s)->%s%s",
      97       12578 :                           m->optional ? "" : "&", name,
      98       25156 :                           t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
      99           0 :                 errx(1, "malloc");
     100       12578 :             if(m->optional)
     101        4389 :                 fprintf(codefile, "if(%s) {\n", s);
     102       12578 :             free_type (s, m->type, FALSE);
     103       12578 :             if(m->optional)
     104        4389 :                 fprintf(codefile,
     105             :                         "free(%s);\n"
     106             :                         "%s = NULL;\n"
     107             :                         "}\n",s, s);
     108       12578 :             free (s);
     109       12578 :             if(t->type == TChoice)
     110        1159 :                 fprintf(codefile, "break;\n");
     111             :         }
     112             : 
     113        3553 :         if(t->type == TChoice) {
     114         380 :             if (have_ellipsis)
     115          95 :                 fprintf(codefile,
     116             :                         "case %s:\n"
     117             :                         "der_free_octet_string(&(%s)->u.%s);\n"
     118             :                         "break;",
     119             :                         have_ellipsis->label,
     120             :                         name, have_ellipsis->gen_name);
     121         380 :             fprintf(codefile, "}\n");
     122             :         }
     123        3366 :         break;
     124             :     }
     125        1368 :     case TSetOf:
     126             :     case TSequenceOf: {
     127             :         char *n;
     128             : 
     129        1440 :         fprintf (codefile, "while((%s)->len){\n", name);
     130        1368 :         if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)
     131           0 :             errx(1, "malloc");
     132        1368 :         free_type(n, t->subtype, FALSE);
     133        1440 :         fprintf(codefile,
     134             :                 "(%s)->len--;\n"
     135             :                 "}\n",
     136             :                 name);
     137        1440 :         fprintf(codefile,
     138             :                 "free((%s)->val);\n"
     139             :                 "(%s)->val = NULL;\n", name, name);
     140        1368 :         free(n);
     141        1296 :         break;
     142             :     }
     143         266 :     case TGeneralString:
     144         252 :         free_primitive ("general_string", name);
     145         252 :         break;
     146          19 :     case TTeletexString:
     147          18 :         free_primitive ("general_string", name);
     148          18 :         break;
     149         760 :     case TUTF8String:
     150         720 :         free_primitive ("utf8string", name);
     151         720 :         break;
     152          19 :     case TPrintableString:
     153          18 :         free_primitive ("printable_string", name);
     154          18 :         break;
     155          76 :     case TIA5String:
     156          72 :         free_primitive ("ia5_string", name);
     157          72 :         break;
     158          38 :     case TBMPString:
     159          36 :         free_primitive ("bmp_string", name);
     160          36 :         break;
     161          19 :     case TUniversalString:
     162          18 :         free_primitive ("universal_string", name);
     163          18 :         break;
     164          19 :     case TVisibleString:
     165          18 :         free_primitive ("visible_string", name);
     166          18 :         break;
     167       19152 :     case TTag:
     168       19152 :         free_type (name, t->subtype, preserve);
     169       19152 :         break;
     170         361 :     case TOID :
     171         342 :         free_primitive ("oid", name);
     172         342 :         break;
     173           0 :     default :
     174           0 :         abort ();
     175             :     }
     176       36648 : }
     177             : 
     178             : void
     179        5586 : generate_type_free (const Symbol *s)
     180             : {
     181        5586 :     int preserve = preserve_type(s->name) ? TRUE : FALSE;
     182             : 
     183        5880 :     fprintf (codefile, "void ASN1CALL\n"
     184             :              "free_%s(%s *data)\n"
     185             :              "{\n",
     186             :              s->gen_name, s->gen_name);
     187             : 
     188        5586 :     free_type ("data", s->type, preserve);
     189        5880 :     fprintf (codefile, "}\n\n");
     190        5586 : }
     191             : 

Generated by: LCOV version 1.13