No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / gen_copy.c
blobfb2e07979dcd6b713496aa1f5921eafef9c1fded
1 /*
2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "gen_locl.h"
36 __RCSID("$Heimdal: gen_copy.c 19539 2006-12-28 17:15:05Z lha $"
37 "$NetBSD$");
39 static int used_fail;
41 static void
42 copy_primitive (const char *typename, const char *from, const char *to)
44 fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
45 typename, from, to);
46 used_fail++;
49 static void
50 copy_type (const char *from, const char *to, const Type *t, int preserve)
52 switch (t->type) {
53 case TType:
54 #if 0
55 copy_type (from, to, t->symbol->type, preserve);
56 #endif
57 fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
58 t->symbol->gen_name, from, to);
59 used_fail++;
60 break;
61 case TInteger:
62 if (t->range == NULL && t->members == NULL) {
63 copy_primitive ("heim_integer", from, to);
64 break;
66 case TBoolean:
67 case TEnumerated :
68 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
69 break;
70 case TOctetString:
71 copy_primitive ("octet_string", from, to);
72 break;
73 case TBitString:
74 if (ASN1_TAILQ_EMPTY(t->members))
75 copy_primitive ("bit_string", from, to);
76 else
77 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
78 break;
79 case TSet:
80 case TSequence:
81 case TChoice: {
82 Member *m, *have_ellipsis = NULL;
84 if(t->members == NULL)
85 break;
87 if ((t->type == TSequence || t->type == TChoice) && preserve) {
88 fprintf(codefile,
89 "{ int ret;\n"
90 "ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
91 "if (ret) goto fail;\n"
92 "}\n",
93 from, to);
94 used_fail++;
97 if(t->type == TChoice) {
98 fprintf(codefile, "(%s)->element = (%s)->element;\n", to, from);
99 fprintf(codefile, "switch((%s)->element) {\n", from);
102 ASN1_TAILQ_FOREACH(m, t->members, members) {
103 char *fs;
104 char *ts;
106 if (m->ellipsis) {
107 have_ellipsis = m;
108 continue;
111 if(t->type == TChoice)
112 fprintf(codefile, "case %s:\n", m->label);
114 asprintf (&fs, "%s(%s)->%s%s",
115 m->optional ? "" : "&", from,
116 t->type == TChoice ? "u." : "", m->gen_name);
117 if (fs == NULL)
118 errx(1, "malloc");
119 asprintf (&ts, "%s(%s)->%s%s",
120 m->optional ? "" : "&", to,
121 t->type == TChoice ? "u." : "", m->gen_name);
122 if (ts == NULL)
123 errx(1, "malloc");
124 if(m->optional){
125 fprintf(codefile, "if(%s) {\n", fs);
126 fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
127 fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
128 used_fail++;
130 copy_type (fs, ts, m->type, FALSE);
131 if(m->optional){
132 fprintf(codefile, "}else\n");
133 fprintf(codefile, "%s = NULL;\n", ts);
135 free (fs);
136 free (ts);
137 if(t->type == TChoice)
138 fprintf(codefile, "break;\n");
140 if(t->type == TChoice) {
141 if (have_ellipsis) {
142 fprintf(codefile, "case %s: {\n"
143 "int ret;\n"
144 "ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
145 "if (ret) goto fail;\n"
146 "break;\n"
147 "}\n",
148 have_ellipsis->label,
149 from, have_ellipsis->gen_name,
150 to, have_ellipsis->gen_name);
151 used_fail++;
153 fprintf(codefile, "}\n");
155 break;
157 case TSetOf:
158 case TSequenceOf: {
159 char *f;
160 char *T;
162 fprintf (codefile, "if(((%s)->val = "
163 "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
164 to, from, to, from);
165 fprintf (codefile, "goto fail;\n");
166 used_fail++;
167 fprintf(codefile,
168 "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
169 to, to, from, to);
170 asprintf(&f, "&(%s)->val[(%s)->len]", from, to);
171 if (f == NULL)
172 errx(1, "malloc");
173 asprintf(&T, "&(%s)->val[(%s)->len]", to, to);
174 if (T == NULL)
175 errx(1, "malloc");
176 copy_type(f, T, t->subtype, FALSE);
177 fprintf(codefile, "}\n");
178 free(f);
179 free(T);
180 break;
182 case TGeneralizedTime:
183 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
184 break;
185 case TGeneralString:
186 copy_primitive ("general_string", from, to);
187 break;
188 case TUTCTime:
189 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
190 break;
191 case TUTF8String:
192 copy_primitive ("utf8string", from, to);
193 break;
194 case TPrintableString:
195 copy_primitive ("printable_string", from, to);
196 break;
197 case TIA5String:
198 copy_primitive ("ia5_string", from, to);
199 break;
200 case TBMPString:
201 copy_primitive ("bmp_string", from, to);
202 break;
203 case TUniversalString:
204 copy_primitive ("universal_string", from, to);
205 break;
206 case TVisibleString:
207 copy_primitive ("visible_string", from, to);
208 break;
209 case TTag:
210 copy_type (from, to, t->subtype, preserve);
211 break;
212 case TOID:
213 copy_primitive ("oid", from, to);
214 break;
215 case TNull:
216 break;
217 default :
218 abort ();
222 void
223 generate_type_copy (const Symbol *s)
225 int preserve = preserve_type(s->name) ? TRUE : FALSE;
227 used_fail = 0;
229 fprintf (headerfile,
230 "int copy_%s (const %s *, %s *);\n",
231 s->gen_name, s->gen_name, s->gen_name);
233 fprintf (codefile, "int\n"
234 "copy_%s(const %s *from, %s *to)\n"
235 "{\n"
236 "memset(to, 0, sizeof(*to));\n",
237 s->gen_name, s->gen_name, s->gen_name);
238 copy_type ("from", "to", s->type, preserve);
239 fprintf (codefile, "return 0;\n");
241 if (used_fail)
242 fprintf (codefile, "fail:\n"
243 "free_%s(to);\n"
244 "return ENOMEM;\n",
245 s->gen_name);
247 fprintf(codefile,
248 "}\n\n");