1b528cefcSMark Murray /* 2ae771770SStanislav Sedov * Copyright (c) 1997 - 2005 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 36ae771770SStanislav Sedov RCSID("$Id$"); 37b528cefcSMark Murray 38b528cefcSMark Murray static void 39b528cefcSMark Murray length_primitive (const char *typename, 40b528cefcSMark Murray const char *name, 41b528cefcSMark Murray const char *variable) 42b528cefcSMark Murray { 43c19800e8SDoug Rabson fprintf (codefile, "%s += der_length_%s(%s);\n", variable, typename, name); 44b528cefcSMark Murray } 45b528cefcSMark Murray 46ae771770SStanislav Sedov /* XXX same as der_length_tag */ 47c19800e8SDoug Rabson static size_t 48c19800e8SDoug Rabson length_tag(unsigned int tag) 49c19800e8SDoug Rabson { 50c19800e8SDoug Rabson size_t len = 0; 51c19800e8SDoug Rabson 52c19800e8SDoug Rabson if(tag <= 30) 53c19800e8SDoug Rabson return 1; 54c19800e8SDoug Rabson while(tag) { 55c19800e8SDoug Rabson tag /= 128; 56c19800e8SDoug Rabson len++; 57c19800e8SDoug Rabson } 58c19800e8SDoug Rabson return len + 1; 59c19800e8SDoug Rabson } 60c19800e8SDoug Rabson 61c19800e8SDoug Rabson 62c19800e8SDoug Rabson static int 63c19800e8SDoug Rabson length_type (const char *name, const Type *t, 64c19800e8SDoug Rabson const char *variable, const char *tmpstr) 65b528cefcSMark Murray { 66b528cefcSMark Murray switch (t->type) { 67b528cefcSMark Murray case TType: 68b528cefcSMark Murray #if 0 69b528cefcSMark Murray length_type (name, t->symbol->type); 70b528cefcSMark Murray #endif 71b528cefcSMark Murray fprintf (codefile, "%s += length_%s(%s);\n", 72b528cefcSMark Murray variable, t->symbol->gen_name, name); 73b528cefcSMark Murray break; 74b528cefcSMark Murray case TInteger: 75c19800e8SDoug Rabson if(t->members) { 76c19800e8SDoug Rabson fprintf(codefile, 77c19800e8SDoug Rabson "{\n" 78c19800e8SDoug Rabson "int enumint = *%s;\n", name); 79c19800e8SDoug Rabson length_primitive ("integer", "&enumint", variable); 80c19800e8SDoug Rabson fprintf(codefile, "}\n"); 81c19800e8SDoug Rabson } else if (t->range == NULL) { 82c19800e8SDoug Rabson length_primitive ("heim_integer", name, variable); 83*1b748759SDimitry Andric } else if (t->range->min < INT_MIN && t->range->max <= INT64_MAX) { 84*1b748759SDimitry Andric length_primitive ("integer64", name, variable); 85*1b748759SDimitry Andric } else if (t->range->min >= 0 && t->range->max > UINT_MAX) { 86*1b748759SDimitry Andric length_primitive ("unsigned64", name, variable); 87*1b748759SDimitry Andric } else if (t->range->min >= INT_MIN && t->range->max <= INT_MAX) { 88b528cefcSMark Murray length_primitive ("integer", name, variable); 89*1b748759SDimitry Andric } else if (t->range->min >= 0 && t->range->max <= UINT_MAX) { 90c19800e8SDoug Rabson length_primitive ("unsigned", name, variable); 91c19800e8SDoug Rabson } else 92*1b748759SDimitry Andric errx(1, "%s: unsupported range %" PRId64 " -> %" PRId64, 93c19800e8SDoug Rabson name, t->range->min, t->range->max); 94c19800e8SDoug Rabson 95c19800e8SDoug Rabson break; 96c19800e8SDoug Rabson case TBoolean: 97c19800e8SDoug Rabson fprintf (codefile, "%s += 1;\n", variable); 98b528cefcSMark Murray break; 994137ff4cSJacques Vidrine case TEnumerated : 1004137ff4cSJacques Vidrine length_primitive ("enumerated", name, variable); 1014137ff4cSJacques Vidrine break; 102b528cefcSMark Murray case TOctetString: 103b528cefcSMark Murray length_primitive ("octet_string", name, variable); 104b528cefcSMark Murray break; 105b528cefcSMark Murray case TBitString: { 106c19800e8SDoug Rabson if (ASN1_TAILQ_EMPTY(t->members)) 107c19800e8SDoug Rabson length_primitive("bit_string", name, variable); 108c19800e8SDoug Rabson else { 109c19800e8SDoug Rabson if (!rfc1510_bitstring) { 110c19800e8SDoug Rabson Member *m; 111c19800e8SDoug Rabson int pos = ASN1_TAILQ_LAST(t->members, memhead)->val; 112c19800e8SDoug Rabson 113c19800e8SDoug Rabson fprintf(codefile, 114c19800e8SDoug Rabson "do {\n"); 115c19800e8SDoug Rabson ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) { 116c19800e8SDoug Rabson while (m->val / 8 < pos / 8) { 117c19800e8SDoug Rabson pos -= 8; 118c19800e8SDoug Rabson } 119c19800e8SDoug Rabson fprintf (codefile, 120c19800e8SDoug Rabson "if((%s)->%s) { %s += %d; break; }\n", 121c19800e8SDoug Rabson name, m->gen_name, variable, (pos + 8) / 8); 122c19800e8SDoug Rabson } 123c19800e8SDoug Rabson fprintf(codefile, 124c19800e8SDoug Rabson "} while(0);\n"); 125c19800e8SDoug Rabson fprintf (codefile, "%s += 1;\n", variable); 126c19800e8SDoug Rabson } else { 127c19800e8SDoug Rabson fprintf (codefile, "%s += 5;\n", variable); 128c19800e8SDoug Rabson } 129c19800e8SDoug Rabson } 130b528cefcSMark Murray break; 131b528cefcSMark Murray } 132c19800e8SDoug Rabson case TSet: 133c19800e8SDoug Rabson case TSequence: 134c19800e8SDoug Rabson case TChoice: { 135c19800e8SDoug Rabson Member *m, *have_ellipsis = NULL; 136b528cefcSMark Murray 137b528cefcSMark Murray if (t->members == NULL) 138b528cefcSMark Murray break; 139b528cefcSMark Murray 140c19800e8SDoug Rabson if(t->type == TChoice) 141c19800e8SDoug Rabson fprintf (codefile, "switch((%s)->element) {\n", name); 142c19800e8SDoug Rabson 143c19800e8SDoug Rabson ASN1_TAILQ_FOREACH(m, t->members, members) { 144b528cefcSMark Murray char *s; 145b528cefcSMark Murray 146c19800e8SDoug Rabson if (m->ellipsis) { 147c19800e8SDoug Rabson have_ellipsis = m; 148c19800e8SDoug Rabson continue; 149c19800e8SDoug Rabson } 150c19800e8SDoug Rabson 151c19800e8SDoug Rabson if(t->type == TChoice) 152c19800e8SDoug Rabson fprintf(codefile, "case %s:\n", m->label); 153c19800e8SDoug Rabson 154ae771770SStanislav Sedov if (asprintf (&s, "%s(%s)->%s%s", 155c19800e8SDoug Rabson m->optional ? "" : "&", name, 156ae771770SStanislav Sedov t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL) 157c19800e8SDoug Rabson errx(1, "malloc"); 158b528cefcSMark Murray if (m->optional) 159b528cefcSMark Murray fprintf (codefile, "if(%s)", s); 160c19800e8SDoug Rabson else if(m->defval) 161c19800e8SDoug Rabson gen_compare_defval(s + 1, m->defval); 162b528cefcSMark Murray fprintf (codefile, "{\n" 163c19800e8SDoug Rabson "size_t %s_oldret = %s;\n" 164c19800e8SDoug Rabson "%s = 0;\n", tmpstr, variable, variable); 165c19800e8SDoug Rabson length_type (s, m->type, "ret", m->gen_name); 166c19800e8SDoug Rabson fprintf (codefile, "ret += %s_oldret;\n", tmpstr); 167b528cefcSMark Murray fprintf (codefile, "}\n"); 168b528cefcSMark Murray free (s); 169c19800e8SDoug Rabson if(t->type == TChoice) 170c19800e8SDoug Rabson fprintf(codefile, "break;\n"); 171b528cefcSMark Murray } 172c19800e8SDoug Rabson if(t->type == TChoice) { 173c19800e8SDoug Rabson if (have_ellipsis) 174b528cefcSMark Murray fprintf(codefile, 175c19800e8SDoug Rabson "case %s:\n" 176c19800e8SDoug Rabson "ret += (%s)->u.%s.length;\n" 177c19800e8SDoug Rabson "break;\n", 178c19800e8SDoug Rabson have_ellipsis->label, 179c19800e8SDoug Rabson name, 180c19800e8SDoug Rabson have_ellipsis->gen_name); 181c19800e8SDoug Rabson fprintf (codefile, "}\n"); /* switch */ 182c19800e8SDoug Rabson } 183b528cefcSMark Murray break; 184b528cefcSMark Murray } 185c19800e8SDoug Rabson case TSetOf: 186b528cefcSMark Murray case TSequenceOf: { 187ae771770SStanislav Sedov char *n = NULL; 188ae771770SStanislav Sedov char *sname = NULL; 189b528cefcSMark Murray 190b528cefcSMark Murray fprintf (codefile, 191b528cefcSMark Murray "{\n" 192ae771770SStanislav Sedov "size_t %s_oldret = %s;\n" 193b528cefcSMark Murray "int i;\n" 194b528cefcSMark Murray "%s = 0;\n", 195c19800e8SDoug Rabson tmpstr, variable, variable); 196b528cefcSMark Murray 197b528cefcSMark Murray fprintf (codefile, "for(i = (%s)->len - 1; i >= 0; --i){\n", name); 198ae771770SStanislav Sedov fprintf (codefile, "size_t %s_for_oldret = %s;\n" 199c19800e8SDoug Rabson "%s = 0;\n", tmpstr, variable, variable); 200ae771770SStanislav Sedov if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL) 201c19800e8SDoug Rabson errx(1, "malloc"); 202ae771770SStanislav Sedov if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL) 203c19800e8SDoug Rabson errx(1, "malloc"); 204c19800e8SDoug Rabson length_type(n, t->subtype, variable, sname); 205c19800e8SDoug Rabson fprintf (codefile, "%s += %s_for_oldret;\n", 206c19800e8SDoug Rabson variable, tmpstr); 207b528cefcSMark Murray fprintf (codefile, "}\n"); 208b528cefcSMark Murray 209b528cefcSMark Murray fprintf (codefile, 210c19800e8SDoug Rabson "%s += %s_oldret;\n" 211c19800e8SDoug Rabson "}\n", variable, tmpstr); 212b528cefcSMark Murray free(n); 213c19800e8SDoug Rabson free(sname); 214b528cefcSMark Murray break; 215b528cefcSMark Murray } 216b528cefcSMark Murray case TGeneralizedTime: 217b528cefcSMark Murray length_primitive ("generalized_time", name, variable); 218b528cefcSMark Murray break; 219b528cefcSMark Murray case TGeneralString: 220b528cefcSMark Murray length_primitive ("general_string", name, variable); 221b528cefcSMark Murray break; 222ae771770SStanislav Sedov case TTeletexString: 223ae771770SStanislav Sedov length_primitive ("general_string", name, variable); 224ae771770SStanislav Sedov break; 225c19800e8SDoug Rabson case TUTCTime: 226c19800e8SDoug Rabson length_primitive ("utctime", name, variable); 227c19800e8SDoug Rabson break; 228c19800e8SDoug Rabson case TUTF8String: 229c19800e8SDoug Rabson length_primitive ("utf8string", name, variable); 230c19800e8SDoug Rabson break; 231c19800e8SDoug Rabson case TPrintableString: 232c19800e8SDoug Rabson length_primitive ("printable_string", name, variable); 233c19800e8SDoug Rabson break; 234c19800e8SDoug Rabson case TIA5String: 235c19800e8SDoug Rabson length_primitive ("ia5_string", name, variable); 236c19800e8SDoug Rabson break; 237c19800e8SDoug Rabson case TBMPString: 238c19800e8SDoug Rabson length_primitive ("bmp_string", name, variable); 239c19800e8SDoug Rabson break; 240c19800e8SDoug Rabson case TUniversalString: 241c19800e8SDoug Rabson length_primitive ("universal_string", name, variable); 242c19800e8SDoug Rabson break; 243c19800e8SDoug Rabson case TVisibleString: 244c19800e8SDoug Rabson length_primitive ("visible_string", name, variable); 245c19800e8SDoug Rabson break; 246c19800e8SDoug Rabson case TNull: 247c19800e8SDoug Rabson fprintf (codefile, "/* NULL */\n"); 248c19800e8SDoug Rabson break; 249c19800e8SDoug Rabson case TTag:{ 250ae771770SStanislav Sedov char *tname = NULL; 251ae771770SStanislav Sedov if (asprintf(&tname, "%s_tag", tmpstr) < 0 || tname == NULL) 252c19800e8SDoug Rabson errx(1, "malloc"); 253c19800e8SDoug Rabson length_type (name, t->subtype, variable, tname); 254c19800e8SDoug Rabson fprintf (codefile, "ret += %lu + der_length_len (ret);\n", 255c19800e8SDoug Rabson (unsigned long)length_tag(t->tag.tagvalue)); 256c19800e8SDoug Rabson free(tname); 257c19800e8SDoug Rabson break; 258c19800e8SDoug Rabson } 259c19800e8SDoug Rabson case TOID: 260c19800e8SDoug Rabson length_primitive ("oid", name, variable); 261b528cefcSMark Murray break; 262b528cefcSMark Murray default : 263b528cefcSMark Murray abort (); 264b528cefcSMark Murray } 265c19800e8SDoug Rabson return 0; 266b528cefcSMark Murray } 267b528cefcSMark Murray 268b528cefcSMark Murray void 269b528cefcSMark Murray generate_type_length (const Symbol *s) 270b528cefcSMark Murray { 271b528cefcSMark Murray fprintf (codefile, 272ae771770SStanislav Sedov "size_t ASN1CALL\n" 273b528cefcSMark Murray "length_%s(const %s *data)\n" 274b528cefcSMark Murray "{\n" 275b528cefcSMark Murray "size_t ret = 0;\n", 276b528cefcSMark Murray s->gen_name, s->gen_name); 277b528cefcSMark Murray 278c19800e8SDoug Rabson length_type ("data", s->type, "ret", "Top"); 279b528cefcSMark Murray fprintf (codefile, "return ret;\n}\n\n"); 280b528cefcSMark Murray } 281b528cefcSMark Murray 282