Path: blob/main/crypto/heimdal/lib/asn1/gen_length.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 void38length_primitive (const char *typename,39const char *name,40const char *variable)41{42fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name);43}4445/* XXX same as der_length_tag */46static size_t47length_tag(unsigned int tag)48{49size_t len = 0;5051if(tag <= 30)52return 1;53while(tag) {54tag /= 128;55len++;56}57return len + 1;58}596061static int62length_type (const char *name, const Type *t,63const char *variable, const char *tmpstr)64{65switch (t->type) {66case TType:67#if 068length_type (name, t->symbol->type);69#endif70fprintf (codefile, "%s += length_%s(%s);\n",71variable, t->symbol->gen_name, name);72break;73case TInteger:74if(t->members) {75fprintf(codefile,76"{\n"77"int enumint = *%s;\n", name);78length_primitive ("integer", "&enumint", variable);79fprintf(codefile, "}\n");80} else if (t->range == NULL) {81length_primitive ("heim_integer", name, variable);82} else if (t->range->min < INT_MIN && t->range->max <= INT64_MAX) {83length_primitive ("integer64", name, variable);84} else if (t->range->min >= 0 && t->range->max > UINT_MAX) {85length_primitive ("unsigned64", name, variable);86} else if (t->range->min >= INT_MIN && t->range->max <= INT_MAX) {87length_primitive ("integer", name, variable);88} else if (t->range->min >= 0 && t->range->max <= UINT_MAX) {89length_primitive ("unsigned", name, variable);90} else91errx(1, "%s: unsupported range %" PRId64 " -> %" PRId64,92name, t->range->min, t->range->max);9394break;95case TBoolean:96fprintf (codefile, "%s += 1;\n", variable);97break;98case TEnumerated :99length_primitive ("enumerated", name, variable);100break;101case TOctetString:102length_primitive ("octet_string", name, variable);103break;104case TBitString: {105if (ASN1_TAILQ_EMPTY(t->members))106length_primitive("bit_string", name, variable);107else {108if (!rfc1510_bitstring) {109Member *m;110int pos = ASN1_TAILQ_LAST(t->members, memhead)->val;111112fprintf(codefile,113"do {\n");114ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {115while (m->val / 8 < pos / 8) {116pos -= 8;117}118fprintf (codefile,119"if((%s)->%s) { %s += %d; break; }\n",120name, m->gen_name, variable, (pos + 8) / 8);121}122fprintf(codefile,123"} while(0);\n");124fprintf (codefile, "%s += 1;\n", variable);125} else {126fprintf (codefile, "%s += 5;\n", variable);127}128}129break;130}131case TSet:132case TSequence:133case TChoice: {134Member *m, *have_ellipsis = NULL;135136if (t->members == NULL)137break;138139if(t->type == TChoice)140fprintf (codefile, "switch((%s)->element) {\n", name);141142ASN1_TAILQ_FOREACH(m, t->members, members) {143char *s;144145if (m->ellipsis) {146have_ellipsis = m;147continue;148}149150if(t->type == TChoice)151fprintf(codefile, "case %s:\n", m->label);152153if (asprintf (&s, "%s(%s)->%s%s",154m->optional ? "" : "&", name,155t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)156errx(1, "malloc");157if (m->optional)158fprintf (codefile, "if(%s)", s);159else if(m->defval)160gen_compare_defval(s + 1, m->defval);161fprintf (codefile, "{\n"162"size_t %s_oldret = %s;\n"163"%s = 0;\n", tmpstr, variable, variable);164length_type (s, m->type, "ret", m->gen_name);165fprintf (codefile, "ret += %s_oldret;\n", tmpstr);166fprintf (codefile, "}\n");167free (s);168if(t->type == TChoice)169fprintf(codefile, "break;\n");170}171if(t->type == TChoice) {172if (have_ellipsis)173fprintf(codefile,174"case %s:\n"175"ret += (%s)->u.%s.length;\n"176"break;\n",177have_ellipsis->label,178name,179have_ellipsis->gen_name);180fprintf (codefile, "}\n"); /* switch */181}182break;183}184case TSetOf:185case TSequenceOf: {186char *n = NULL;187char *sname = NULL;188189fprintf (codefile,190"{\n"191"size_t %s_oldret = %s;\n"192"int i;\n"193"%s = 0;\n",194tmpstr, variable, variable);195196fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name);197fprintf (codefile, "size_t %s_for_oldret = %s;\n"198"%s = 0;\n", tmpstr, variable, variable);199if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)200errx(1, "malloc");201if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)202errx(1, "malloc");203length_type(n, t->subtype, variable, sname);204fprintf (codefile, "%s += %s_for_oldret;\n",205variable, tmpstr);206fprintf (codefile, "}\n");207208fprintf (codefile,209"%s += %s_oldret;\n"210"}\n", variable, tmpstr);211free(n);212free(sname);213break;214}215case TGeneralizedTime:216length_primitive ("generalized_time", name, variable);217break;218case TGeneralString:219length_primitive ("general_string", name, variable);220break;221case TTeletexString:222length_primitive ("general_string", name, variable);223break;224case TUTCTime:225length_primitive ("utctime", name, variable);226break;227case TUTF8String:228length_primitive ("utf8string", name, variable);229break;230case TPrintableString:231length_primitive ("printable_string", name, variable);232break;233case TIA5String:234length_primitive ("ia5_string", name, variable);235break;236case TBMPString:237length_primitive ("bmp_string", name, variable);238break;239case TUniversalString:240length_primitive ("universal_string", name, variable);241break;242case TVisibleString:243length_primitive ("visible_string", name, variable);244break;245case TNull:246fprintf (codefile, "/* NULL */\n");247break;248case TTag:{249char *tname = NULL;250if (asprintf(&tname, "%s_tag", tmpstr) < 0 || tname == NULL)251errx(1, "malloc");252length_type (name, t->subtype, variable, tname);253fprintf (codefile, "ret += %lu + der_length_len (ret);\n",254(unsigned long)length_tag(t->tag.tagvalue));255free(tname);256break;257}258case TOID:259length_primitive ("oid", name, variable);260break;261default :262abort ();263}264return 0;265}266267void268generate_type_length (const Symbol *s)269{270fprintf (codefile,271"size_t ASN1CALL\n"272"length_%s(const %s *data)\n"273"{\n"274"size_t ret = 0;\n",275s->gen_name, s->gen_name);276277length_type ("data", s->type, "ret", "Top");278fprintf (codefile, "return ret;\n}\n\n");279}280281282283