LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/asn1 - gen_length.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 135 178 75.8 %
Date: 2024-06-13 04:01:37 Functions: 4 4 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         728 : length_primitive (const char *typename,
      40             :                   const char *name,
      41             :                   const char *variable)
      42             : {
      43         728 :     fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name);
      44         728 : }
      45             : 
      46             : /* XXX same as der_length_tag */
      47             : static size_t
      48        2814 : length_tag(unsigned int tag)
      49             : {
      50        2814 :     size_t len = 0;
      51             : 
      52        2814 :     if(tag <= 30)
      53        2814 :         return 1;
      54           0 :     while(tag) {
      55           0 :         tag /= 128;
      56           0 :         len++;
      57             :     }
      58           0 :     return len + 1;
      59             : }
      60             : 
      61             : 
      62             : static int
      63        5516 : length_type (const char *name, const Type *t,
      64             :              const char *variable, const char *tmpstr)
      65             : {
      66        5516 :     switch (t->type) {
      67        1134 :     case TType:
      68             : #if 0
      69             :         length_type (name, t->symbol->type);
      70             : #endif
      71        1134 :         fprintf (codefile, "%s += length_%s(%s);\n",
      72        1134 :                  variable, t->symbol->gen_name, name);
      73        1134 :         break;
      74         308 :     case TInteger:
      75         308 :         if(t->members) {
      76          28 :             fprintf(codefile,
      77             :                     "{\n"
      78             :                     "int enumint = *%s;\n", name);
      79          28 :             length_primitive ("integer", "&enumint", variable);
      80          28 :             fprintf(codefile, "}\n");
      81         280 :         } else if (t->range == NULL) {
      82          56 :             length_primitive ("heim_integer", name, variable);
      83         238 :         } else if (t->range->min < 0 &&
      84          14 :                    (t->range->min < INT_MIN || t->range->max > INT_MAX)) {
      85          14 :             length_primitive ("integer64", name, variable);
      86         210 :         } else if (t->range->min < 0) {
      87           0 :             length_primitive ("integer", name, variable);
      88         210 :         } else if (t->range->max > UINT_MAX) {
      89           0 :             length_primitive ("unsigned64", name, variable);
      90             :         } else {
      91         210 :             length_primitive ("unsigned", name, variable);
      92             :         }
      93         308 :         break;
      94          28 :     case TBoolean:
      95          28 :         fprintf (codefile, "%s += 1;\n", variable);
      96          28 :         break;
      97           0 :     case TEnumerated :
      98           0 :         length_primitive ("enumerated", name, variable);
      99           0 :         break;
     100         196 :     case TOctetString:
     101         196 :         length_primitive ("octet_string", name, variable);
     102         196 :         break;
     103         140 :     case TBitString: {
     104         140 :         if (HEIM_TAILQ_EMPTY(t->members))
     105          98 :             length_primitive("bit_string", name, variable);
     106             :         else {
     107          42 :             if (!rfc1510_bitstring) {
     108             :                 Member *m;
     109          42 :                 int pos = HEIM_TAILQ_LAST(t->members, memhead)->val;
     110             : 
     111          42 :                 fprintf(codefile,
     112             :                         "do {\n");
     113         532 :                 HEIM_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
     114        1022 :                     while (m->val / 8 < pos / 8) {
     115          42 :                         pos -= 8;
     116             :                     }
     117         490 :                     fprintf (codefile,
     118             :                              "if((%s)->%s) { %s += %d; break; }\n",
     119         490 :                              name, m->gen_name, variable, (pos + 8) / 8);
     120             :                 }
     121          42 :                 fprintf(codefile,
     122             :                         "} while(0);\n");
     123          42 :                 fprintf (codefile, "%s += 1;\n", variable);
     124             :             } else {
     125           0 :                 fprintf (codefile, "%s += 5;\n", variable);
     126             :             }
     127             :         }
     128         140 :         break;
     129             :     }
     130         532 :     case TSet:
     131             :     case TSequence:
     132             :     case TChoice: {
     133         532 :         Member *m, *have_ellipsis = NULL;
     134             : 
     135         532 :         if (t->members == NULL)
     136           0 :             break;
     137             : 
     138         532 :         if(t->type == TChoice)
     139         126 :             fprintf (codefile, "switch((%s)->element) {\n", name);
     140             : 
     141        2422 :         HEIM_TAILQ_FOREACH(m, t->members, members) {
     142             :             char *s;
     143             : 
     144        1890 :             if (m->ellipsis) {
     145          84 :                 have_ellipsis = m;
     146          84 :                 continue;
     147             :             }
     148             : 
     149        1806 :             if(t->type == TChoice)
     150         462 :                 fprintf(codefile, "case %s:\n", m->label);
     151             : 
     152        5418 :             if (asprintf (&s, "%s(%s)->%s%s",
     153        1806 :                           m->optional ? "" : "&", name,
     154        3612 :                           t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
     155           0 :                 errx(1, "malloc");
     156        1806 :             if (m->optional)
     157         700 :                 fprintf (codefile, "if(%s)", s);
     158        1106 :             else if(m->defval)
     159           0 :                 gen_compare_defval(s + 1, m->defval);
     160        1806 :             fprintf (codefile, "{\n"
     161             :                      "size_t %s_oldret = %s;\n"
     162             :                      "%s = 0;\n", tmpstr, variable, variable);
     163        1806 :             length_type (s, m->type, "ret", m->gen_name);
     164        1806 :             fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
     165        1806 :             fprintf (codefile, "}\n");
     166        1806 :             free (s);
     167        1806 :             if(t->type == TChoice)
     168         462 :                 fprintf(codefile, "break;\n");
     169             :         }
     170         532 :         if(t->type == TChoice) {
     171         126 :             if (have_ellipsis)
     172          14 :                 fprintf(codefile,
     173             :                         "case %s:\n"
     174             :                         "ret += (%s)->u.%s.length;\n"
     175             :                         "break;\n",
     176             :                         have_ellipsis->label,
     177             :                         name,
     178             :                         have_ellipsis->gen_name);
     179         126 :             fprintf (codefile, "}\n"); /* switch */
     180             :         }
     181         532 :         break;
     182             :     }
     183         224 :     case TSetOf:
     184             :     case TSequenceOf: {
     185         224 :         char *n = NULL;
     186         224 :         char *sname = NULL;
     187             : 
     188         224 :         fprintf (codefile,
     189             :                  "{\n"
     190             :                  "size_t %s_oldret = %s;\n"
     191             :                  "unsigned int n_%s;\n"
     192             :                  "%s = 0;\n",
     193             :                  tmpstr, variable, tmpstr, variable);
     194             : 
     195         224 :         fprintf (codefile, "for(n_%s = (%s)->len; n_%s > 0; --n_%s){\n",
     196             :                  tmpstr, name, tmpstr, tmpstr);
     197         224 :         fprintf (codefile, "size_t %s_for_oldret = %s;\n"
     198             :                  "%s = 0;\n", tmpstr, variable, variable);
     199         224 :         if (asprintf (&n, "&(%s)->val[n_%s - 1]", name, tmpstr) < 0  || n == NULL)
     200           0 :             errx(1, "malloc");
     201         224 :         if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
     202           0 :             errx(1, "malloc");
     203         224 :         length_type(n, t->subtype, variable, sname);
     204         224 :         fprintf (codefile, "%s += %s_for_oldret;\n",
     205             :                  variable, tmpstr);
     206         224 :         fprintf (codefile, "}\n");
     207             : 
     208         224 :         fprintf (codefile,
     209             :                  "%s += %s_oldret;\n"
     210             :                  "}\n", variable, tmpstr);
     211         224 :         free(n);
     212         224 :         free(sname);
     213         224 :         break;
     214             :     }
     215           0 :     case TGeneralizedTime:
     216           0 :         length_primitive ("generalized_time", name, variable);
     217           0 :         break;
     218          14 :     case TGeneralString:
     219          14 :         length_primitive ("general_string", name, variable);
     220          14 :         break;
     221           0 :     case TTeletexString:
     222           0 :         length_primitive ("general_string", name, variable);
     223           0 :         break;
     224           0 :     case TUTCTime:
     225           0 :         length_primitive ("utctime", name, variable);
     226           0 :         break;
     227          56 :     case TUTF8String:
     228          56 :         length_primitive ("utf8string", name, variable);
     229          56 :         break;
     230           0 :     case TPrintableString:
     231           0 :         length_primitive ("printable_string", name, variable);
     232           0 :         break;
     233           0 :     case TIA5String:
     234           0 :         length_primitive ("ia5_string", name, variable);
     235           0 :         break;
     236           0 :     case TBMPString:
     237           0 :         length_primitive ("bmp_string", name, variable);
     238           0 :         break;
     239           0 :     case TUniversalString:
     240           0 :         length_primitive ("universal_string", name, variable);
     241           0 :         break;
     242           0 :     case TVisibleString:
     243           0 :         length_primitive ("visible_string", name, variable);
     244           0 :         break;
     245          14 :     case TNull:
     246          14 :         fprintf (codefile, "/* NULL */\n");
     247          14 :         break;
     248        2814 :     case TTag:{
     249        2814 :         char *tname = NULL;
     250        2814 :         int replace_tag = 0;
     251        7070 :         int prim = !(t->tag.tagclass != ASN1_C_UNIV &&
     252        4312 :                      t->tag.tagenv == TE_EXPLICIT) &&
     253        1498 :             is_primitive_type(t->subtype);
     254             : 
     255        2814 :         if (asprintf(&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
     256           0 :             errx(1, "malloc");
     257        2814 :         length_type (name, t->subtype, variable, tname);
     258             :         /* See the comments in encode_type() about IMPLICIT tags */
     259        2898 :         if (t->tag.tagenv == TE_IMPLICIT && !prim &&
     260         224 :             t->subtype->type != TSequenceOf && t->subtype->type != TSetOf &&
     261          70 :             t->subtype->type != TChoice) {
     262         182 :             if (t->subtype->symbol &&
     263          84 :                 (t->subtype->type == TSequence ||
     264          42 :                  t->subtype->type == TSet))
     265           0 :                 replace_tag = 1;
     266          70 :             else if (t->subtype->symbol && strcmp(t->subtype->symbol->name, "heim_any"))
     267          42 :                 replace_tag = 1;
     268        2744 :         } else if (t->tag.tagenv == TE_IMPLICIT && prim && t->subtype->symbol)
     269          14 :             replace_tag = is_tagged_type(t->subtype->symbol->type);
     270        2814 :         if (replace_tag)
     271             :             /*
     272             :              * We're replacing the tag of the underlying type.  If that type is
     273             :              * imported, then we don't know its tag, so we rely on the
     274             :              * asn1_tag_tag_<TypeName> enum value we generated for it, and we
     275             :              * use the asn1_tag_length_<TypeName> enum value to avoid having to
     276             :              * call der_length_tag() at run-time.
     277             :              */
     278         112 :             fprintf(codefile, "ret += %lu - asn1_tag_length_%s;\n",
     279          56 :                     (unsigned long)length_tag(t->tag.tagvalue),
     280          56 :                     t->subtype->symbol->gen_name);
     281             :         else
     282        2758 :             fprintf(codefile, "ret += %lu + der_length_len (ret);\n",
     283        2758 :                     (unsigned long)length_tag(t->tag.tagvalue));
     284        2814 :         free(tname);
     285        2814 :         break;
     286             :     }
     287          56 :     case TOID:
     288          56 :         length_primitive ("oid", name, variable);
     289          56 :         break;
     290           0 :     default :
     291           0 :         abort ();
     292             :     }
     293        5516 :     return 0;
     294             : }
     295             : 
     296             : void
     297         672 : generate_type_length (const Symbol *s)
     298             : {
     299         672 :     fprintf (codefile,
     300             :              "size_t ASN1CALL\n"
     301             :              "length_%s(const %s *data)\n"
     302             :              "{\n"
     303             :              "size_t ret = 0;\n",
     304             :              s->gen_name, s->gen_name);
     305             : 
     306         672 :     length_type ("data", s->type, "ret", "Top");
     307         672 :     fprintf (codefile, "return ret;\n}\n\n");
     308         672 : }
     309             : 

Generated by: LCOV version 1.13