r19604: This is a massive commit, and I appologise in advance for it's size.
[Samba.git] / source / heimdal / lib / asn1 / gen_copy.c
blob9455f33c6f360a7ca1c88baae9da92bef3010a97
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("$Id: gen_copy.c,v 1.18 2006/10/14 05:34:19 lha Exp $");
38 static int used_fail;
40 static void
41 copy_primitive (const char *typename, const char *from, const char *to)
43 fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
44 typename, from, to);
45 used_fail++;
48 static void
49 copy_type (const char *from, const char *to, const Type *t, int preserve)
51 switch (t->type) {
52 case TType:
53 #if 0
54 copy_type (from, to, t->symbol->type, preserve);
55 #endif
56 fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
57 t->symbol->gen_name, from, to);
58 used_fail++;
59 break;
60 case TInteger:
61 if (t->range == NULL && t->members == NULL) {
62 copy_primitive ("heim_integer", from, to);
63 break;
65 case TBoolean:
66 case TEnumerated :
67 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
68 break;
69 case TOctetString:
70 copy_primitive ("octet_string", from, to);
71 break;
72 case TBitString:
73 if (ASN1_TAILQ_EMPTY(t->members))
74 copy_primitive ("bit_string", from, to);
75 else
76 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
77 break;
78 case TSet:
79 case TSequence:
80 case TChoice: {
81 Member *m, *have_ellipsis = NULL;
83 if(t->members == NULL)
84 break;
86 if ((t->type == TSequence || t->type == TChoice) && preserve) {
87 fprintf(codefile,
88 "{ int ret;\n"
89 "ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
90 "if (ret) goto fail;\n"
91 "}\n",
92 from, to);
93 used_fail++;
96 if(t->type == TChoice) {
97 fprintf(codefile, "(%s)->element = (%s)->element;\n", to, from);
98 fprintf(codefile, "switch((%s)->element) {\n", from);
101 ASN1_TAILQ_FOREACH(m, t->members, members) {
102 char *fs;
103 char *ts;
105 if (m->ellipsis) {
106 have_ellipsis = m;
107 continue;
110 if(t->type == TChoice)
111 fprintf(codefile, "case %s:\n", m->label);
113 asprintf (&fs, "%s(%s)->%s%s",
114 m->optional ? "" : "&", from,
115 t->type == TChoice ? "u." : "", m->gen_name);
116 if (fs == NULL)
117 errx(1, "malloc");
118 asprintf (&ts, "%s(%s)->%s%s",
119 m->optional ? "" : "&", to,
120 t->type == TChoice ? "u." : "", m->gen_name);
121 if (ts == NULL)
122 errx(1, "malloc");
123 if(m->optional){
124 fprintf(codefile, "if(%s) {\n", fs);
125 fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
126 fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
127 used_fail++;
129 copy_type (fs, ts, m->type, FALSE);
130 if(m->optional){
131 fprintf(codefile, "}else\n");
132 fprintf(codefile, "%s = NULL;\n", ts);
134 free (fs);
135 free (ts);
136 if(t->type == TChoice)
137 fprintf(codefile, "break;\n");
139 if(t->type == TChoice) {
140 if (have_ellipsis) {
141 fprintf(codefile, "case %s: {\n"
142 "int ret;\n"
143 "ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
144 "if (ret) goto fail;\n"
145 "break;\n"
146 "}\n",
147 have_ellipsis->label,
148 from, have_ellipsis->gen_name,
149 to, have_ellipsis->gen_name);
150 used_fail++;
152 fprintf(codefile, "}\n");
154 break;
156 case TSetOf:
157 case TSequenceOf: {
158 char *f;
159 char *T;
161 fprintf (codefile, "if(((%s)->val = "
162 "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
163 to, from, to, from);
164 fprintf (codefile, "goto fail;\n");
165 used_fail++;
166 fprintf(codefile,
167 "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
168 to, to, from, to);
169 asprintf(&f, "&(%s)->val[(%s)->len]", from, to);
170 if (f == NULL)
171 errx(1, "malloc");
172 asprintf(&T, "&(%s)->val[(%s)->len]", to, to);
173 if (T == NULL)
174 errx(1, "malloc");
175 copy_type(f, T, t->subtype, FALSE);
176 fprintf(codefile, "}\n");
177 free(f);
178 free(T);
179 break;
181 case TGeneralizedTime:
182 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
183 break;
184 case TGeneralString:
185 copy_primitive ("general_string", from, to);
186 break;
187 case TUTCTime:
188 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
189 break;
190 case TUTF8String:
191 copy_primitive ("utf8string", from, to);
192 break;
193 case TPrintableString:
194 copy_primitive ("printable_string", from, to);
195 break;
196 case TIA5String:
197 copy_primitive ("ia5_string", from, to);
198 break;
199 case TBMPString:
200 copy_primitive ("bmp_string", from, to);
201 break;
202 case TUniversalString:
203 copy_primitive ("universal_string", from, to);
204 break;
205 case TTag:
206 copy_type (from, to, t->subtype, preserve);
207 break;
208 case TOID:
209 copy_primitive ("oid", from, to);
210 break;
211 case TNull:
212 break;
213 default :
214 abort ();
218 void
219 generate_type_copy (const Symbol *s)
221 int preserve = preserve_type(s->name) ? TRUE : FALSE;
223 used_fail = 0;
225 fprintf (headerfile,
226 "int copy_%s (const %s *, %s *);\n",
227 s->gen_name, s->gen_name, s->gen_name);
229 fprintf (codefile, "int\n"
230 "copy_%s(const %s *from, %s *to)\n"
231 "{\n"
232 "memset(to, 0, sizeof(*to));\n",
233 s->gen_name, s->gen_name, s->gen_name);
234 copy_type ("from", "to", s->type, preserve);
235 fprintf (codefile, "return 0;\n");
237 if (used_fail)
238 fprintf (codefile, "fail:\n"
239 "free_%s(to);\n"
240 "return ENOMEM;\n",
241 s->gen_name);
243 fprintf(codefile,
244 "}\n\n");