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