/*1* Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gen_locl.h"3435RCSID("$Id$");3637void38generate_type_seq (const Symbol *s)39{40char *subname;41Type *type;4243if (!seq_type(s->name))44return;45type = s->type;46while(type->type == TTag)47type = type->subtype;4849if (type->type != TSequenceOf && type->type != TSetOf) {50fprintf(stderr, "%s not seq of %d\n", s->name, (int)type->type);51return;52}5354/*55* Require the subtype to be a type so we can name it and use56* copy_/free_57*/5859if (type->subtype->type != TType) {60fprintf(stderr, "%s subtype is not a type, can't generate "61"sequence code for this case: %d\n",62s->name, (int)type->subtype->type);63exit(1);64}6566subname = type->subtype->symbol->gen_name;6768fprintf (headerfile,69"ASN1EXP int ASN1CALL add_%s (%s *, const %s *);\n"70"ASN1EXP int ASN1CALL remove_%s (%s *, unsigned int);\n",71s->gen_name, s->gen_name, subname,72s->gen_name, s->gen_name);7374fprintf (codefile, "int ASN1CALL\n"75"add_%s(%s *data, const %s *element)\n"76"{\n",77s->gen_name, s->gen_name, subname);7879fprintf (codefile,80"int ret;\n"81"void *ptr;\n"82"\n"83"ptr = realloc(data->val, \n"84"\t(data->len + 1) * sizeof(data->val[0]));\n"85"if (ptr == NULL) return ENOMEM;\n"86"data->val = ptr;\n\n"87"ret = copy_%s(element, &data->val[data->len]);\n"88"if (ret) return ret;\n"89"data->len++;\n"90"return 0;\n",91subname);9293fprintf (codefile, "}\n\n");9495fprintf (codefile, "int ASN1CALL\n"96"remove_%s(%s *data, unsigned int element)\n"97"{\n",98s->gen_name, s->gen_name);99100fprintf (codefile,101"void *ptr;\n"102"\n"103"if (data->len == 0 || element >= data->len)\n"104"\treturn ASN1_OVERRUN;\n"105"free_%s(&data->val[element]);\n"106"data->len--;\n"107/* don't move if its the last element */108"if (element < data->len)\n"109"\tmemmove(&data->val[element], &data->val[element + 1], \n"110"\t\tsizeof(data->val[0]) * (data->len - element));\n"111/* resize but don't care about failures since it doesn't matter */112"ptr = realloc(data->val, data->len * sizeof(data->val[0]));\n"113"if (ptr != NULL || data->len == 0) data->val = ptr;\n"114"return 0;\n",115subname);116117fprintf (codefile, "}\n\n");118}119120121