Path: blob/main/crypto/heimdal/lib/asn1/gen_glue.c
34879 views
/*1* Copyright (c) 1997, 1999, 2000, 2003 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "gen_locl.h"3637RCSID("$Id$");3839static void40generate_2int (const Type *t, const char *gen_name)41{42Member *m;4344fprintf (headerfile,45"unsigned %s2int(%s);\n",46gen_name, gen_name);4748fprintf (codefile,49"unsigned %s2int(%s f)\n"50"{\n"51"unsigned r = 0;\n",52gen_name, gen_name);5354ASN1_TAILQ_FOREACH(m, t->members, members) {55fprintf (codefile, "if(f.%s) r |= (1U << %d);\n",56m->gen_name, m->val);57}58fprintf (codefile, "return r;\n"59"}\n\n");60}6162static void63generate_int2 (const Type *t, const char *gen_name)64{65Member *m;6667fprintf (headerfile,68"%s int2%s(unsigned);\n",69gen_name, gen_name);7071fprintf (codefile,72"%s int2%s(unsigned n)\n"73"{\n"74"\t%s flags;\n\n"75"\tmemset(&flags, 0, sizeof(flags));\n\n",76gen_name, gen_name, gen_name);7778if(t->members) {79ASN1_TAILQ_FOREACH(m, t->members, members) {80fprintf (codefile, "\tflags.%s = (n >> %d) & 1;\n",81m->gen_name, m->val);82}83}84fprintf (codefile, "\treturn flags;\n"85"}\n\n");86}8788/*89* This depends on the bit string being declared in increasing order90*/9192static void93generate_units (const Type *t, const char *gen_name)94{95Member *m;9697if (template_flag) {98fprintf (headerfile,99"extern const struct units *asn1_%s_table_units;\n",100gen_name);101fprintf (headerfile, "#define asn1_%s_units() (asn1_%s_table_units)\n",102gen_name, gen_name);103} else {104fprintf (headerfile,105"const struct units * asn1_%s_units(void);\n",106gen_name);107}108109fprintf (codefile,110"static struct units %s_units[] = {\n",111gen_name);112113if(t->members) {114ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {115fprintf (codefile,116"\t{\"%s\",\t1U << %d},\n", m->name, m->val);117}118}119120fprintf (codefile,121"\t{NULL,\t0}\n"122"};\n\n");123124if (template_flag)125fprintf (codefile,126"const struct units * asn1_%s_table_units = %s_units;\n",127gen_name, gen_name);128else129fprintf (codefile,130"const struct units * asn1_%s_units(void){\n"131"return %s_units;\n"132"}\n\n",133gen_name, gen_name);134135136}137138void139generate_glue (const Type *t, const char *gen_name)140{141switch(t->type) {142case TTag:143generate_glue(t->subtype, gen_name);144break;145case TBitString :146if (!ASN1_TAILQ_EMPTY(t->members)) {147generate_2int (t, gen_name);148generate_int2 (t, gen_name);149generate_units (t, gen_name);150}151break;152default :153break;154}155}156157158