1*afab4e30Schristos /* $NetBSD: gen_copy.c,v 1.3 2023/06/19 21:41:42 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include "gen_locl.h"
37ca1c9b0cSelric
38*afab4e30Schristos __RCSID("$NetBSD: gen_copy.c,v 1.3 2023/06/19 21:41:42 christos Exp $");
39ca1c9b0cSelric
40ca1c9b0cSelric static int used_fail;
41ca1c9b0cSelric
42ca1c9b0cSelric static void
copy_primitive(const char * typename,const char * from,const char * to)43ca1c9b0cSelric copy_primitive (const char *typename, const char *from, const char *to)
44ca1c9b0cSelric {
45ca1c9b0cSelric fprintf (codefile, "if(der_copy_%s(%s, %s)) goto fail;\n",
46ca1c9b0cSelric typename, from, to);
47ca1c9b0cSelric used_fail++;
48ca1c9b0cSelric }
49ca1c9b0cSelric
50ca1c9b0cSelric static void
copy_type(const char * from,const char * to,const Type * t,int preserve)51ca1c9b0cSelric copy_type (const char *from, const char *to, const Type *t, int preserve)
52ca1c9b0cSelric {
53ca1c9b0cSelric switch (t->type) {
54ca1c9b0cSelric case TType:
55ca1c9b0cSelric #if 0
56ca1c9b0cSelric copy_type (from, to, t->symbol->type, preserve);
57ca1c9b0cSelric #endif
58ca1c9b0cSelric fprintf (codefile, "if(copy_%s(%s, %s)) goto fail;\n",
59ca1c9b0cSelric t->symbol->gen_name, from, to);
60ca1c9b0cSelric used_fail++;
61ca1c9b0cSelric break;
62ca1c9b0cSelric case TInteger:
63ca1c9b0cSelric if (t->range == NULL && t->members == NULL) {
64ca1c9b0cSelric copy_primitive ("heim_integer", from, to);
65ca1c9b0cSelric break;
66ca1c9b0cSelric }
67*afab4e30Schristos /* fallthrough */
68ca1c9b0cSelric case TBoolean:
69ca1c9b0cSelric case TEnumerated :
70ca1c9b0cSelric fprintf(codefile, "*(%s) = *(%s);\n", to, from);
71ca1c9b0cSelric break;
72ca1c9b0cSelric case TOctetString:
73ca1c9b0cSelric copy_primitive ("octet_string", from, to);
74ca1c9b0cSelric break;
75ca1c9b0cSelric case TBitString:
76ca1c9b0cSelric if (ASN1_TAILQ_EMPTY(t->members))
77ca1c9b0cSelric copy_primitive ("bit_string", from, to);
78ca1c9b0cSelric else
79ca1c9b0cSelric fprintf(codefile, "*(%s) = *(%s);\n", to, from);
80ca1c9b0cSelric break;
81ca1c9b0cSelric case TSet:
82ca1c9b0cSelric case TSequence:
83ca1c9b0cSelric case TChoice: {
84ca1c9b0cSelric Member *m, *have_ellipsis = NULL;
85ca1c9b0cSelric
86ca1c9b0cSelric if(t->members == NULL)
87ca1c9b0cSelric break;
88ca1c9b0cSelric
89ca1c9b0cSelric if ((t->type == TSequence || t->type == TChoice) && preserve) {
90ca1c9b0cSelric fprintf(codefile,
91ca1c9b0cSelric "{ int ret;\n"
92ca1c9b0cSelric "ret = der_copy_octet_string(&(%s)->_save, &(%s)->_save);\n"
93ca1c9b0cSelric "if (ret) goto fail;\n"
94ca1c9b0cSelric "}\n",
95ca1c9b0cSelric from, to);
96ca1c9b0cSelric used_fail++;
97ca1c9b0cSelric }
98ca1c9b0cSelric
99ca1c9b0cSelric if(t->type == TChoice) {
100ca1c9b0cSelric fprintf(codefile, "(%s)->element = (%s)->element;\n", to, from);
101ca1c9b0cSelric fprintf(codefile, "switch((%s)->element) {\n", from);
102ca1c9b0cSelric }
103ca1c9b0cSelric
104ca1c9b0cSelric ASN1_TAILQ_FOREACH(m, t->members, members) {
105ca1c9b0cSelric char *fs;
106ca1c9b0cSelric char *ts;
107ca1c9b0cSelric
108ca1c9b0cSelric if (m->ellipsis) {
109ca1c9b0cSelric have_ellipsis = m;
110ca1c9b0cSelric continue;
111ca1c9b0cSelric }
112ca1c9b0cSelric
113ca1c9b0cSelric if(t->type == TChoice)
114ca1c9b0cSelric fprintf(codefile, "case %s:\n", m->label);
115ca1c9b0cSelric
116ca1c9b0cSelric if (asprintf (&fs, "%s(%s)->%s%s",
117ca1c9b0cSelric m->optional ? "" : "&", from,
118ca1c9b0cSelric t->type == TChoice ? "u." : "", m->gen_name) < 0)
119ca1c9b0cSelric errx(1, "malloc");
120ca1c9b0cSelric if (fs == NULL)
121ca1c9b0cSelric errx(1, "malloc");
122ca1c9b0cSelric if (asprintf (&ts, "%s(%s)->%s%s",
123ca1c9b0cSelric m->optional ? "" : "&", to,
124ca1c9b0cSelric t->type == TChoice ? "u." : "", m->gen_name) < 0)
125ca1c9b0cSelric errx(1, "malloc");
126ca1c9b0cSelric if (ts == NULL)
127ca1c9b0cSelric errx(1, "malloc");
128ca1c9b0cSelric if(m->optional){
129ca1c9b0cSelric fprintf(codefile, "if(%s) {\n", fs);
130ca1c9b0cSelric fprintf(codefile, "%s = malloc(sizeof(*%s));\n", ts, ts);
131ca1c9b0cSelric fprintf(codefile, "if(%s == NULL) goto fail;\n", ts);
132ca1c9b0cSelric used_fail++;
133ca1c9b0cSelric }
134ca1c9b0cSelric copy_type (fs, ts, m->type, FALSE);
135ca1c9b0cSelric if(m->optional){
136ca1c9b0cSelric fprintf(codefile, "}else\n");
137ca1c9b0cSelric fprintf(codefile, "%s = NULL;\n", ts);
138ca1c9b0cSelric }
139ca1c9b0cSelric free (fs);
140ca1c9b0cSelric free (ts);
141ca1c9b0cSelric if(t->type == TChoice)
142ca1c9b0cSelric fprintf(codefile, "break;\n");
143ca1c9b0cSelric }
144ca1c9b0cSelric if(t->type == TChoice) {
145ca1c9b0cSelric if (have_ellipsis) {
146ca1c9b0cSelric fprintf(codefile, "case %s: {\n"
147ca1c9b0cSelric "int ret;\n"
148ca1c9b0cSelric "ret=der_copy_octet_string(&(%s)->u.%s, &(%s)->u.%s);\n"
149ca1c9b0cSelric "if (ret) goto fail;\n"
150ca1c9b0cSelric "break;\n"
151ca1c9b0cSelric "}\n",
152ca1c9b0cSelric have_ellipsis->label,
153ca1c9b0cSelric from, have_ellipsis->gen_name,
154ca1c9b0cSelric to, have_ellipsis->gen_name);
155ca1c9b0cSelric used_fail++;
156ca1c9b0cSelric }
157ca1c9b0cSelric fprintf(codefile, "}\n");
158ca1c9b0cSelric }
159ca1c9b0cSelric break;
160ca1c9b0cSelric }
161ca1c9b0cSelric case TSetOf:
162ca1c9b0cSelric case TSequenceOf: {
163ca1c9b0cSelric char *f = NULL, *T = NULL;
164ca1c9b0cSelric
165ca1c9b0cSelric fprintf (codefile, "if(((%s)->val = "
166ca1c9b0cSelric "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n",
167ca1c9b0cSelric to, from, to, from);
168ca1c9b0cSelric fprintf (codefile, "goto fail;\n");
169ca1c9b0cSelric used_fail++;
170ca1c9b0cSelric fprintf(codefile,
171ca1c9b0cSelric "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n",
172ca1c9b0cSelric to, to, from, to);
173ca1c9b0cSelric if (asprintf(&f, "&(%s)->val[(%s)->len]", from, to) < 0)
174ca1c9b0cSelric errx(1, "malloc");
175ca1c9b0cSelric if (f == NULL)
176ca1c9b0cSelric errx(1, "malloc");
177ca1c9b0cSelric if (asprintf(&T, "&(%s)->val[(%s)->len]", to, to) < 0)
178ca1c9b0cSelric errx(1, "malloc");
179ca1c9b0cSelric if (T == NULL)
180ca1c9b0cSelric errx(1, "malloc");
181ca1c9b0cSelric copy_type(f, T, t->subtype, FALSE);
182ca1c9b0cSelric fprintf(codefile, "}\n");
183ca1c9b0cSelric free(f);
184ca1c9b0cSelric free(T);
185ca1c9b0cSelric break;
186ca1c9b0cSelric }
187ca1c9b0cSelric case TGeneralizedTime:
188ca1c9b0cSelric fprintf(codefile, "*(%s) = *(%s);\n", to, from);
189ca1c9b0cSelric break;
190ca1c9b0cSelric case TGeneralString:
191ca1c9b0cSelric copy_primitive ("general_string", from, to);
192ca1c9b0cSelric break;
193ca1c9b0cSelric case TTeletexString:
194ca1c9b0cSelric copy_primitive ("general_string", from, to);
195ca1c9b0cSelric break;
196ca1c9b0cSelric case TUTCTime:
197ca1c9b0cSelric fprintf(codefile, "*(%s) = *(%s);\n", to, from);
198ca1c9b0cSelric break;
199ca1c9b0cSelric case TUTF8String:
200ca1c9b0cSelric copy_primitive ("utf8string", from, to);
201ca1c9b0cSelric break;
202ca1c9b0cSelric case TPrintableString:
203ca1c9b0cSelric copy_primitive ("printable_string", from, to);
204ca1c9b0cSelric break;
205ca1c9b0cSelric case TIA5String:
206ca1c9b0cSelric copy_primitive ("ia5_string", from, to);
207ca1c9b0cSelric break;
208ca1c9b0cSelric case TBMPString:
209ca1c9b0cSelric copy_primitive ("bmp_string", from, to);
210ca1c9b0cSelric break;
211ca1c9b0cSelric case TUniversalString:
212ca1c9b0cSelric copy_primitive ("universal_string", from, to);
213ca1c9b0cSelric break;
214ca1c9b0cSelric case TVisibleString:
215ca1c9b0cSelric copy_primitive ("visible_string", from, to);
216ca1c9b0cSelric break;
217ca1c9b0cSelric case TTag:
218ca1c9b0cSelric copy_type (from, to, t->subtype, preserve);
219ca1c9b0cSelric break;
220ca1c9b0cSelric case TOID:
221ca1c9b0cSelric copy_primitive ("oid", from, to);
222ca1c9b0cSelric break;
223ca1c9b0cSelric case TNull:
224ca1c9b0cSelric break;
225ca1c9b0cSelric default :
226ca1c9b0cSelric abort ();
227ca1c9b0cSelric }
228ca1c9b0cSelric }
229ca1c9b0cSelric
230ca1c9b0cSelric void
generate_type_copy(const Symbol * s)231ca1c9b0cSelric generate_type_copy (const Symbol *s)
232ca1c9b0cSelric {
233ca1c9b0cSelric int preserve = preserve_type(s->name) ? TRUE : FALSE;
234ca1c9b0cSelric
235ca1c9b0cSelric used_fail = 0;
236ca1c9b0cSelric
237ca1c9b0cSelric fprintf (codefile, "int ASN1CALL\n"
238ca1c9b0cSelric "copy_%s(const %s *from, %s *to)\n"
239ca1c9b0cSelric "{\n"
240ca1c9b0cSelric "memset(to, 0, sizeof(*to));\n",
241ca1c9b0cSelric s->gen_name, s->gen_name, s->gen_name);
242ca1c9b0cSelric copy_type ("from", "to", s->type, preserve);
243ca1c9b0cSelric fprintf (codefile, "return 0;\n");
244ca1c9b0cSelric
245ca1c9b0cSelric if (used_fail)
246ca1c9b0cSelric fprintf (codefile, "fail:\n"
247ca1c9b0cSelric "free_%s(to);\n"
248ca1c9b0cSelric "return ENOMEM;\n",
249ca1c9b0cSelric s->gen_name);
250ca1c9b0cSelric
251ca1c9b0cSelric fprintf(codefile,
252ca1c9b0cSelric "}\n\n");
253ca1c9b0cSelric }
254ca1c9b0cSelric
255