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