155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "gen_locl.h"
3555682Smarkm
36233294SstasRCSID("$Id$");
3755682Smarkm
38178825Sdfrstatic int used_fail;
39178825Sdfr
4055682Smarkmstatic void
4155682Smarkmcopy_primitive (const char *typename, const char *from, const char *to)
4255682Smarkm{
43233294Sstas    fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
4455682Smarkm	     typename, from, to);
45178825Sdfr    used_fail++;
4655682Smarkm}
4755682Smarkm
4855682Smarkmstatic void
49178825Sdfrcopy_type (const char *from, const char *to, const Type *t, int preserve)
5055682Smarkm{
51178825Sdfr    switch (t->type) {
52178825Sdfr    case TType:
5355682Smarkm#if 0
54178825Sdfr	copy_type (from, to, t->symbol->type, preserve);
5555682Smarkm#endif
56233294Sstas	fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
57178825Sdfr		 t->symbol->gen_name, from, to);
58178825Sdfr	used_fail++;
59178825Sdfr	break;
60178825Sdfr    case TInteger:
61178825Sdfr	if (t->range == NULL && t->members == NULL) {
62178825Sdfr	    copy_primitive ("heim_integer", from, to);
63178825Sdfr	    break;
64178825Sdfr	}
65178825Sdfr    case TBoolean:
66178825Sdfr    case TEnumerated :
67178825Sdfr	fprintf(codefile, "*(%s) = *(%s);\n", to, from);
68178825Sdfr	break;
69178825Sdfr    case TOctetString:
70178825Sdfr	copy_primitive ("octet_string", from, to);
71178825Sdfr	break;
72178825Sdfr    case TBitString:
73178825Sdfr	if (ASN1_TAILQ_EMPTY(t->members))
74178825Sdfr	    copy_primitive ("bit_string", from, to);
75178825Sdfr	else
76178825Sdfr	    fprintf(codefile, "*(%s) = *(%s);\n", to, from);
77178825Sdfr	break;
78178825Sdfr    case TSet:
79178825Sdfr    case TSequence:
80178825Sdfr    case TChoice: {
81178825Sdfr	Member *m, *have_ellipsis = NULL;
8255682Smarkm
83178825Sdfr	if(t->members == NULL)
84178825Sdfr	    break;
85233294Sstas
86178825Sdfr	if ((t->type == TSequence || t->type == TChoice) && preserve) {
87178825Sdfr	    fprintf(codefile,
88178825Sdfr		    "{ int ret;\n"
89178825Sdfr		    "ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
90178825Sdfr		    "if (ret) goto fail;\n"
91178825Sdfr		    "}\n",
92178825Sdfr		    from, to);
93178825Sdfr	    used_fail++;
94178825Sdfr	}
9555682Smarkm
96178825Sdfr	if(t->type == TChoice) {
97178825Sdfr	    fprintf(codefile, "(%s)->element = (%s)->element;\n", to, from);
98178825Sdfr	    fprintf(codefile, "switch((%s)->element) {\n", from);
99178825Sdfr	}
10055682Smarkm
101178825Sdfr	ASN1_TAILQ_FOREACH(m, t->members, members) {
102178825Sdfr	    char *fs;
103178825Sdfr	    char *ts;
104178825Sdfr
105178825Sdfr	    if (m->ellipsis) {
106178825Sdfr		have_ellipsis = m;
107178825Sdfr		continue;
108178825Sdfr	    }
109178825Sdfr
110178825Sdfr	    if(t->type == TChoice)
111178825Sdfr		fprintf(codefile, "case %s:\n", m->label);
112178825Sdfr
113233294Sstas	    if (asprintf (&fs, "%s(%s)->%s%s",
114233294Sstas			  m->optional ? "" : "&", from,
115233294Sstas			  t->type == TChoice ? "u." : "", m->gen_name) < 0)
116233294Sstas		errx(1, "malloc");
117178825Sdfr	    if (fs == NULL)
118178825Sdfr		errx(1, "malloc");
119233294Sstas	    if (asprintf (&ts, "%s(%s)->%s%s",
120233294Sstas			  m->optional ? "" : "&", to,
121233294Sstas			  t->type == TChoice ? "u." : "", m->gen_name) < 0)
122233294Sstas		errx(1, "malloc");
123178825Sdfr	    if (ts == NULL)
124178825Sdfr		errx(1, "malloc");
125178825Sdfr	    if(m->optional){
126178825Sdfr		fprintf(codefile, "if(%s) {\n", fs);
127178825Sdfr		fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
128178825Sdfr		fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
129178825Sdfr		used_fail++;
130178825Sdfr	    }
131178825Sdfr	    copy_type (fs, ts, m->type, FALSE);
132178825Sdfr	    if(m->optional){
133178825Sdfr		fprintf(codefile, "}else\n");
134178825Sdfr		fprintf(codefile, "%s = NULL;\n", ts);
135178825Sdfr	    }
136178825Sdfr	    free (fs);
137178825Sdfr	    free (ts);
138178825Sdfr	    if(t->type == TChoice)
139178825Sdfr		fprintf(codefile, "break;\n");
140178825Sdfr	}
141178825Sdfr	if(t->type == TChoice) {
142178825Sdfr	    if (have_ellipsis) {
143178825Sdfr		fprintf(codefile, "case %s: {\n"
144178825Sdfr			"int ret;\n"
145178825Sdfr			"ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
146178825Sdfr			"if (ret) goto fail;\n"
147178825Sdfr			"break;\n"
148178825Sdfr			"}\n",
149178825Sdfr			have_ellipsis->label,
150233294Sstas			from, have_ellipsis->gen_name,
151178825Sdfr			to, have_ellipsis->gen_name);
152178825Sdfr		used_fail++;
153178825Sdfr	    }
154233294Sstas	    fprintf(codefile, "}\n");
155178825Sdfr	}
156178825Sdfr	break;
157178825Sdfr    }
158178825Sdfr    case TSetOf:
159178825Sdfr    case TSequenceOf: {
160233294Sstas	char *f = NULL, *T = NULL;
161178825Sdfr
162178825Sdfr	fprintf (codefile, "if(((%s)->val = "
163233294Sstas		 "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
164178825Sdfr		 to, from, to, from);
165178825Sdfr	fprintf (codefile, "goto fail;\n");
166178825Sdfr	used_fail++;
167178825Sdfr	fprintf(codefile,
168178825Sdfr		"for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
169178825Sdfr		to, to, from, to);
170233294Sstas	if (asprintf(&f, "&(%s)->val[(%s)->len]", from, to) < 0)
171233294Sstas	    errx(1, "malloc");
172178825Sdfr	if (f == NULL)
173178825Sdfr	    errx(1, "malloc");
174233294Sstas	if (asprintf(&T, "&(%s)->val[(%s)->len]", to, to) < 0)
175233294Sstas	    errx(1, "malloc");
176178825Sdfr	if (T == NULL)
177178825Sdfr	    errx(1, "malloc");
178178825Sdfr	copy_type(f, T, t->subtype, FALSE);
179178825Sdfr	fprintf(codefile, "}\n");
180178825Sdfr	free(f);
181178825Sdfr	free(T);
182178825Sdfr	break;
183178825Sdfr    }
184178825Sdfr    case TGeneralizedTime:
185178825Sdfr	fprintf(codefile, "*(%s) = *(%s);\n", to, from);
186178825Sdfr	break;
187178825Sdfr    case TGeneralString:
188178825Sdfr	copy_primitive ("general_string", from, to);
189178825Sdfr	break;
190233294Sstas    case TTeletexString:
191233294Sstas	copy_primitive ("general_string", from, to);
192233294Sstas	break;
193178825Sdfr    case TUTCTime:
194178825Sdfr	fprintf(codefile, "*(%s) = *(%s);\n", to, from);
195178825Sdfr	break;
196178825Sdfr    case TUTF8String:
197178825Sdfr	copy_primitive ("utf8string", from, to);
198178825Sdfr	break;
199178825Sdfr    case TPrintableString:
200178825Sdfr	copy_primitive ("printable_string", from, to);
201178825Sdfr	break;
202178825Sdfr    case TIA5String:
203178825Sdfr	copy_primitive ("ia5_string", from, to);
204178825Sdfr	break;
205178825Sdfr    case TBMPString:
206178825Sdfr	copy_primitive ("bmp_string", from, to);
207178825Sdfr	break;
208178825Sdfr    case TUniversalString:
209178825Sdfr	copy_primitive ("universal_string", from, to);
210178825Sdfr	break;
211178825Sdfr    case TVisibleString:
212178825Sdfr	copy_primitive ("visible_string", from, to);
213178825Sdfr	break;
214178825Sdfr    case TTag:
215178825Sdfr	copy_type (from, to, t->subtype, preserve);
216178825Sdfr	break;
217178825Sdfr    case TOID:
218178825Sdfr	copy_primitive ("oid", from, to);
219178825Sdfr	break;
220178825Sdfr    case TNull:
221178825Sdfr	break;
222178825Sdfr    default :
223178825Sdfr	abort ();
224178825Sdfr    }
22555682Smarkm}
22655682Smarkm
22755682Smarkmvoid
22855682Smarkmgenerate_type_copy (const Symbol *s)
22955682Smarkm{
230178825Sdfr  int preserve = preserve_type(s->name) ? TRUE : FALSE;
231178825Sdfr
232178825Sdfr  used_fail = 0;
233178825Sdfr
234233294Sstas  fprintf (codefile, "int ASN1CALL\n"
23555682Smarkm	   "copy_%s(const %s *from, %s *to)\n"
236178825Sdfr	   "{\n"
237178825Sdfr	   "memset(to, 0, sizeof(*to));\n",
23855682Smarkm	   s->gen_name, s->gen_name, s->gen_name);
239178825Sdfr  copy_type ("from", "to", s->type, preserve);
240178825Sdfr  fprintf (codefile, "return 0;\n");
24155682Smarkm
242178825Sdfr  if (used_fail)
243178825Sdfr      fprintf (codefile, "fail:\n"
244178825Sdfr	       "free_%s(to);\n"
245178825Sdfr	       "return ENOMEM;\n",
246178825Sdfr	       s->gen_name);
247178825Sdfr
248178825Sdfr  fprintf(codefile,
249178825Sdfr	  "}\n\n");
25055682Smarkm}
25155682Smarkm
252