2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
41 copy_primitive (const char *typename, const char *from, const char *to)
43 fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
49 copy_type (const char *from, const char *to, const Type *t, int preserve)
54 copy_type (from, to, t->symbol->type, preserve);
56 fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
57 t->symbol->gen_name, from, to);
61 if (t->range == NULL && t->members == NULL) {
62 copy_primitive ("heim_integer", from, to);
67 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
70 copy_primitive ("octet_string", from, to);
73 if (ASN1_TAILQ_EMPTY(t->members))
74 copy_primitive ("bit_string", from, to);
76 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
81 Member *m, *have_ellipsis = NULL;
83 if(t->members == NULL)
86 if ((t->type == TSequence || t->type == TChoice) && preserve) {
89 "ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
90 "if (ret) goto fail;\n"
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) {
110 if(t->type == TChoice)
111 fprintf(codefile, "case %s:\n", m->label);
113 if (asprintf (&fs, "%s(%s)->%s%s",
114 m->optional ? "" : "&", from,
115 t->type == TChoice ? "u." : "", m->gen_name) < 0)
119 if (asprintf (&ts, "%s(%s)->%s%s",
120 m->optional ? "" : "&", to,
121 t->type == TChoice ? "u." : "", m->gen_name) < 0)
126 fprintf(codefile, "if(%s) {\n", fs);
127 fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
128 fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
131 copy_type (fs, ts, m->type, FALSE);
133 fprintf(codefile, "}else\n");
134 fprintf(codefile, "%s = NULL;\n", ts);
138 if(t->type == TChoice)
139 fprintf(codefile, "break;\n");
141 if(t->type == TChoice) {
143 fprintf(codefile, "case %s: {\n"
145 "ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
146 "if (ret) goto fail;\n"
149 have_ellipsis->label,
150 from, have_ellipsis->gen_name,
151 to, have_ellipsis->gen_name);
154 fprintf(codefile, "}\n");
160 char *f = NULL, *T = NULL;
162 fprintf (codefile, "if(((%s)->val = "
163 "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
165 fprintf (codefile, "goto fail;\n");
168 "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
170 if (asprintf(&f, "&(%s)->val[(%s)->len]", from, to) < 0)
174 if (asprintf(&T, "&(%s)->val[(%s)->len]", to, to) < 0)
178 copy_type(f, T, t->subtype, FALSE);
179 fprintf(codefile, "}\n");
184 case TGeneralizedTime:
185 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
188 copy_primitive ("general_string", from, to);
191 copy_primitive ("general_string", from, to);
194 fprintf(codefile, "*(%s) = *(%s);\n", to, from);
197 copy_primitive ("utf8string", from, to);
199 case TPrintableString:
200 copy_primitive ("printable_string", from, to);
203 copy_primitive ("ia5_string", from, to);
206 copy_primitive ("bmp_string", from, to);
208 case TUniversalString:
209 copy_primitive ("universal_string", from, to);
212 copy_primitive ("visible_string", from, to);
215 copy_type (from, to, t->subtype, preserve);
218 copy_primitive ("oid", from, to);
228 generate_type_copy (const Symbol *s)
230 int preserve = preserve_type(s->name) ? TRUE : FALSE;
234 fprintf (codefile, "int ASN1CALL\n"
235 "copy_%s(const %s *from, %s *to)\n"
237 "memset(to, 0, sizeof(*to));\n",
238 s->gen_name, s->gen_name, s->gen_name);
239 copy_type ("from", "to", s->type, preserve);
240 fprintf (codefile, "return 0;\n");
243 fprintf (codefile, "fail:\n"