xref: /freebsd/crypto/heimdal/lib/asn1/gen_glue.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * Copyright (c) 1997, 1999, 2000, 2003 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "gen_locl.h"
37 
38 RCSID("$Id$");
39 
40 static void
41 generate_2int (const Type *t, const char *gen_name)
42 {
43     Member *m;
44 
45     fprintf (headerfile,
46 	     "unsigned %s2int(%s);\n",
47 	     gen_name, gen_name);
48 
49     fprintf (codefile,
50 	     "unsigned %s2int(%s f)\n"
51 	     "{\n"
52 	     "unsigned r = 0;\n",
53 	     gen_name, gen_name);
54 
55     ASN1_TAILQ_FOREACH(m, t->members, members) {
56 	fprintf (codefile, "if(f.%s) r |= (1U << %d);\n",
57 		 m->gen_name, m->val);
58     }
59     fprintf (codefile, "return r;\n"
60 	     "}\n\n");
61 }
62 
63 static void
64 generate_int2 (const Type *t, const char *gen_name)
65 {
66     Member *m;
67 
68     fprintf (headerfile,
69 	     "%s int2%s(unsigned);\n",
70 	     gen_name, gen_name);
71 
72     fprintf (codefile,
73 	     "%s int2%s(unsigned n)\n"
74 	     "{\n"
75 	     "\t%s flags;\n\n"
76 	     "\tmemset(&flags, 0, sizeof(flags));\n\n",
77 	     gen_name, gen_name, gen_name);
78 
79     if(t->members) {
80 	ASN1_TAILQ_FOREACH(m, t->members, members) {
81 	    fprintf (codefile, "\tflags.%s = (n >> %d) & 1;\n",
82 		     m->gen_name, m->val);
83 	}
84     }
85     fprintf (codefile, "\treturn flags;\n"
86 	     "}\n\n");
87 }
88 
89 /*
90  * This depends on the bit string being declared in increasing order
91  */
92 
93 static void
94 generate_units (const Type *t, const char *gen_name)
95 {
96     Member *m;
97 
98     if (template_flag) {
99 	fprintf (headerfile,
100 		 "extern const struct units *asn1_%s_table_units;\n",
101 		 gen_name);
102 	fprintf (headerfile, "#define asn1_%s_units() (asn1_%s_table_units)\n",
103 		 gen_name, gen_name);
104     } else {
105 	fprintf (headerfile,
106 		 "const struct units * asn1_%s_units(void);\n",
107 		 gen_name);
108     }
109 
110     fprintf (codefile,
111 	     "static struct units %s_units[] = {\n",
112 	     gen_name);
113 
114     if(t->members) {
115 	ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
116 	    fprintf (codefile,
117 		     "\t{\"%s\",\t1U << %d},\n", m->name, m->val);
118 	}
119     }
120 
121     fprintf (codefile,
122 	     "\t{NULL,\t0}\n"
123 	     "};\n\n");
124 
125     if (template_flag)
126 	fprintf (codefile,
127 		 "const struct units * asn1_%s_table_units = %s_units;\n",
128 		 gen_name, gen_name);
129     else
130 	fprintf (codefile,
131 		 "const struct units * asn1_%s_units(void){\n"
132 		 "return %s_units;\n"
133 		 "}\n\n",
134 		 gen_name, gen_name);
135 
136 
137 }
138 
139 void
140 generate_glue (const Type *t, const char *gen_name)
141 {
142     switch(t->type) {
143     case TTag:
144 	generate_glue(t->subtype, gen_name);
145 	break;
146     case TBitString :
147 	if (!ASN1_TAILQ_EMPTY(t->members)) {
148 	    generate_2int (t, gen_name);
149 	    generate_int2 (t, gen_name);
150 	    generate_units (t, gen_name);
151 	}
152 	break;
153     default :
154 	break;
155     }
156 }
157