1b528cefcSMark Murray /* 2b528cefcSMark Murray * Copyright (c) 1997 - 1999 Kungliga Tekniska H�gskolan 3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden). 4b528cefcSMark Murray * All rights reserved. 5b528cefcSMark Murray * 6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without 7b528cefcSMark Murray * modification, are permitted provided that the following conditions 8b528cefcSMark Murray * are met: 9b528cefcSMark Murray * 10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright 11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer. 12b528cefcSMark Murray * 13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright 14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the 15b528cefcSMark Murray * documentation and/or other materials provided with the distribution. 16b528cefcSMark Murray * 17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors 18b528cefcSMark Murray * may be used to endorse or promote products derived from this software 19b528cefcSMark Murray * without specific prior written permission. 20b528cefcSMark Murray * 21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31b528cefcSMark Murray * SUCH DAMAGE. 32b528cefcSMark Murray */ 33b528cefcSMark Murray 34b528cefcSMark Murray #include "gen_locl.h" 35b528cefcSMark Murray 36b528cefcSMark Murray RCSID("$Id: gen_copy.c,v 1.10 1999/12/02 17:05:02 joda Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray static void 39b528cefcSMark Murray copy_primitive (const char *typename, const char *from, const char *to) 40b528cefcSMark Murray { 41b528cefcSMark Murray fprintf (codefile, "if(copy_%s(%s, %s)) return ENOMEM;\n", 42b528cefcSMark Murray typename, from, to); 43b528cefcSMark Murray } 44b528cefcSMark Murray 45b528cefcSMark Murray static void 46b528cefcSMark Murray copy_type (const char *from, const char *to, const Type *t) 47b528cefcSMark Murray { 48b528cefcSMark Murray switch (t->type) { 49b528cefcSMark Murray case TType: 50b528cefcSMark Murray #if 0 51b528cefcSMark Murray copy_type (from, to, t->symbol->type); 52b528cefcSMark Murray #endif 53b528cefcSMark Murray fprintf (codefile, "if(copy_%s(%s, %s)) return ENOMEM;\n", 54b528cefcSMark Murray t->symbol->gen_name, from, to); 55b528cefcSMark Murray break; 56b528cefcSMark Murray case TInteger: 57b528cefcSMark Murray fprintf(codefile, "*(%s) = *(%s);\n", to, from); 58b528cefcSMark Murray break; 59b528cefcSMark Murray case TOctetString: 60b528cefcSMark Murray copy_primitive ("octet_string", from, to); 61b528cefcSMark Murray break; 62b528cefcSMark Murray case TBitString: { 63b528cefcSMark Murray fprintf(codefile, "*(%s) = *(%s);\n", to, from); 64b528cefcSMark Murray break; 65b528cefcSMark Murray } 66b528cefcSMark Murray case TSequence: { 67b528cefcSMark Murray Member *m; 68b528cefcSMark Murray int tag = -1; 69b528cefcSMark Murray 70b528cefcSMark Murray if (t->members == NULL) 71b528cefcSMark Murray break; 72b528cefcSMark Murray 73b528cefcSMark Murray for (m = t->members; m && tag != m->val; m = m->next) { 74b528cefcSMark Murray char *f; 75b528cefcSMark Murray char *t; 76b528cefcSMark Murray 77b528cefcSMark Murray asprintf (&f, "%s(%s)->%s", 78b528cefcSMark Murray m->optional ? "" : "&", from, m->gen_name); 79b528cefcSMark Murray asprintf (&t, "%s(%s)->%s", 80b528cefcSMark Murray m->optional ? "" : "&", to, m->gen_name); 81b528cefcSMark Murray if(m->optional){ 82b528cefcSMark Murray fprintf(codefile, "if(%s) {\n", f); 83b528cefcSMark Murray fprintf(codefile, "%s = malloc(sizeof(*%s));\n", t, t); 84b528cefcSMark Murray fprintf(codefile, "if(%s == NULL) return ENOMEM;\n", t); 85b528cefcSMark Murray } 86b528cefcSMark Murray copy_type (f, t, m->type); 87b528cefcSMark Murray if(m->optional){ 88b528cefcSMark Murray fprintf(codefile, "}else\n"); 89b528cefcSMark Murray fprintf(codefile, "%s = NULL;\n", t); 90b528cefcSMark Murray } 91b528cefcSMark Murray if (tag == -1) 92b528cefcSMark Murray tag = m->val; 93b528cefcSMark Murray free (f); 94b528cefcSMark Murray free (t); 95b528cefcSMark Murray } 96b528cefcSMark Murray break; 97b528cefcSMark Murray } 98b528cefcSMark Murray case TSequenceOf: { 99b528cefcSMark Murray char *f; 100b528cefcSMark Murray char *T; 101b528cefcSMark Murray 102b528cefcSMark Murray fprintf (codefile, "if(((%s)->val = " 103b528cefcSMark Murray "malloc((%s)->len * sizeof(*(%s)->val))) == NULL && (%s)->len != 0)\n", 104b528cefcSMark Murray to, from, to, from); 105b528cefcSMark Murray fprintf (codefile, "return ENOMEM;\n"); 106b528cefcSMark Murray fprintf(codefile, 107b528cefcSMark Murray "for((%s)->len = 0; (%s)->len < (%s)->len; (%s)->len++){\n", 108b528cefcSMark Murray to, to, from, to); 109b528cefcSMark Murray asprintf(&f, "&(%s)->val[(%s)->len]", from, to); 110b528cefcSMark Murray asprintf(&T, "&(%s)->val[(%s)->len]", to, to); 111b528cefcSMark Murray copy_type(f, T, t->subtype); 112b528cefcSMark Murray fprintf(codefile, "}\n"); 113b528cefcSMark Murray free(f); 114b528cefcSMark Murray free(T); 115b528cefcSMark Murray break; 116b528cefcSMark Murray } 117b528cefcSMark Murray case TGeneralizedTime: 118b528cefcSMark Murray fprintf(codefile, "*(%s) = *(%s);\n", to, from); 119b528cefcSMark Murray break; 120b528cefcSMark Murray case TGeneralString: 121b528cefcSMark Murray copy_primitive ("general_string", from, to); 122b528cefcSMark Murray break; 123b528cefcSMark Murray case TApplication: 124b528cefcSMark Murray copy_type (from, to, t->subtype); 125b528cefcSMark Murray break; 126b528cefcSMark Murray default : 127b528cefcSMark Murray abort (); 128b528cefcSMark Murray } 129b528cefcSMark Murray } 130b528cefcSMark Murray 131b528cefcSMark Murray void 132b528cefcSMark Murray generate_type_copy (const Symbol *s) 133b528cefcSMark Murray { 134b528cefcSMark Murray fprintf (headerfile, 135b528cefcSMark Murray "int copy_%s (const %s *, %s *);\n", 136b528cefcSMark Murray s->gen_name, s->gen_name, s->gen_name); 137b528cefcSMark Murray 138b528cefcSMark Murray fprintf (codefile, "int\n" 139b528cefcSMark Murray "copy_%s(const %s *from, %s *to)\n" 140b528cefcSMark Murray "{\n", 141b528cefcSMark Murray s->gen_name, s->gen_name, s->gen_name); 142b528cefcSMark Murray 143b528cefcSMark Murray copy_type ("from", "to", s->type); 144b528cefcSMark Murray fprintf (codefile, "return 0;\n}\n\n"); 145b528cefcSMark Murray } 146b528cefcSMark Murray 147