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_encode.c,v 1.9 1999/12/02 17:05:02 joda Exp $"); 37b528cefcSMark Murray 38b528cefcSMark Murray static void 39b528cefcSMark Murray encode_primitive (const char *typename, const char *name) 40b528cefcSMark Murray { 41b528cefcSMark Murray fprintf (codefile, 42b528cefcSMark Murray "e = encode_%s(p, len, %s, &l);\n" 43b528cefcSMark Murray "BACK;\n", 44b528cefcSMark Murray typename, 45b528cefcSMark Murray name); 46b528cefcSMark Murray } 47b528cefcSMark Murray 48b528cefcSMark Murray static void 49b528cefcSMark Murray encode_type (const char *name, const Type *t) 50b528cefcSMark Murray { 51b528cefcSMark Murray switch (t->type) { 52b528cefcSMark Murray case TType: 53b528cefcSMark Murray #if 0 54b528cefcSMark Murray encode_type (name, t->symbol->type); 55b528cefcSMark Murray #endif 56b528cefcSMark Murray fprintf (codefile, 57b528cefcSMark Murray "e = encode_%s(p, len, %s, &l);\n" 58b528cefcSMark Murray "BACK;\n", 59b528cefcSMark Murray t->symbol->gen_name, name); 60b528cefcSMark Murray break; 61b528cefcSMark Murray case TInteger: 62b528cefcSMark Murray encode_primitive ("integer", name); 63b528cefcSMark Murray break; 64b528cefcSMark Murray case TOctetString: 65b528cefcSMark Murray encode_primitive ("octet_string", name); 66b528cefcSMark Murray break; 67b528cefcSMark Murray case TBitString: { 68b528cefcSMark Murray Member *m; 69b528cefcSMark Murray int pos; 70b528cefcSMark Murray int rest; 71b528cefcSMark Murray int tag = -1; 72b528cefcSMark Murray 73b528cefcSMark Murray if (t->members == NULL) 74b528cefcSMark Murray break; 75b528cefcSMark Murray 76b528cefcSMark Murray fprintf (codefile, "{\n" 77b528cefcSMark Murray "unsigned char c = 0;\n"); 78b528cefcSMark Murray pos = t->members->prev->val; 79b528cefcSMark Murray /* fix for buggy MIT (and OSF?) code */ 80b528cefcSMark Murray if (pos > 31) 81b528cefcSMark Murray abort (); 82b528cefcSMark Murray /* 83b528cefcSMark Murray * It seems that if we do not always set pos to 31 here, the MIT 84b528cefcSMark Murray * code will do the wrong thing. 85b528cefcSMark Murray * 86b528cefcSMark Murray * I hate ASN.1 (and DER), but I hate it even more when everybody 87b528cefcSMark Murray * has to screw it up differently. 88b528cefcSMark Murray */ 89b528cefcSMark Murray pos = 31; 90b528cefcSMark Murray rest = 7 - (pos % 8); 91b528cefcSMark Murray 92b528cefcSMark Murray for (m = t->members->prev; m && tag != m->val; m = m->prev) { 93b528cefcSMark Murray while (m->val / 8 < pos / 8) { 94b528cefcSMark Murray fprintf (codefile, 95b528cefcSMark Murray "*p-- = c; len--; ret++;\n" 96b528cefcSMark Murray "c = 0;\n"); 97b528cefcSMark Murray pos -= 8; 98b528cefcSMark Murray } 99b528cefcSMark Murray fprintf (codefile, 100b528cefcSMark Murray "if(%s->%s) c |= 1<<%d;\n", name, m->gen_name, 101b528cefcSMark Murray 7 - m->val % 8); 102b528cefcSMark Murray 103b528cefcSMark Murray if (tag == -1) 104b528cefcSMark Murray tag = m->val; 105b528cefcSMark Murray } 106b528cefcSMark Murray 107b528cefcSMark Murray fprintf (codefile, 108b528cefcSMark Murray "*p-- = c;\n" 109b528cefcSMark Murray "*p-- = %d;\n" 110b528cefcSMark Murray "len -= 2;\n" 111b528cefcSMark Murray "ret += 2;\n" 112b528cefcSMark Murray "}\n\n" 113b528cefcSMark Murray "e = der_put_length_and_tag (p, len, ret, UNIV, PRIM," 114b528cefcSMark Murray "UT_BitString, &l);\n" 115b528cefcSMark Murray "BACK;\n", 116b528cefcSMark Murray rest); 117b528cefcSMark Murray break; 118b528cefcSMark Murray } 119b528cefcSMark Murray case TSequence: { 120b528cefcSMark Murray Member *m; 121b528cefcSMark Murray int tag = -1; 122b528cefcSMark Murray 123b528cefcSMark Murray if (t->members == NULL) 124b528cefcSMark Murray break; 125b528cefcSMark Murray 126b528cefcSMark Murray for (m = t->members->prev; m && tag != m->val; m = m->prev) { 127b528cefcSMark Murray char *s; 128b528cefcSMark Murray 129b528cefcSMark Murray asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name); 130b528cefcSMark Murray if (m->optional) 131b528cefcSMark Murray fprintf (codefile, 132b528cefcSMark Murray "if(%s)\n", 133b528cefcSMark Murray s); 134b528cefcSMark Murray #if 1 135b528cefcSMark Murray fprintf (codefile, "{\n" 136b528cefcSMark Murray "int oldret = ret;\n" 137b528cefcSMark Murray "ret = 0;\n"); 138b528cefcSMark Murray #endif 139b528cefcSMark Murray encode_type (s, m->type); 140b528cefcSMark Murray fprintf (codefile, 141b528cefcSMark Murray "e = der_put_length_and_tag (p, len, ret, CONTEXT, CONS, " 142b528cefcSMark Murray "%d, &l);\n" 143b528cefcSMark Murray "BACK;\n", 144b528cefcSMark Murray m->val); 145b528cefcSMark Murray #if 1 146b528cefcSMark Murray fprintf (codefile, 147b528cefcSMark Murray "ret += oldret;\n" 148b528cefcSMark Murray "}\n"); 149b528cefcSMark Murray #endif 150b528cefcSMark Murray if (tag == -1) 151b528cefcSMark Murray tag = m->val; 152b528cefcSMark Murray free (s); 153b528cefcSMark Murray } 154b528cefcSMark Murray fprintf (codefile, 155b528cefcSMark Murray "e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n" 156b528cefcSMark Murray "BACK;\n"); 157b528cefcSMark Murray break; 158b528cefcSMark Murray } 159b528cefcSMark Murray case TSequenceOf: { 160b528cefcSMark Murray char *n; 161b528cefcSMark Murray 162b528cefcSMark Murray fprintf (codefile, 163b528cefcSMark Murray "for(i = (%s)->len - 1; i >= 0; --i) {\n" 164b528cefcSMark Murray #if 1 165b528cefcSMark Murray "int oldret = ret;\n" 166b528cefcSMark Murray "ret = 0;\n", 167b528cefcSMark Murray #else 168b528cefcSMark Murray , 169b528cefcSMark Murray #endif 170b528cefcSMark Murray name); 171b528cefcSMark Murray asprintf (&n, "&(%s)->val[i]", name); 172b528cefcSMark Murray encode_type (n, t->subtype); 173b528cefcSMark Murray fprintf (codefile, 174b528cefcSMark Murray #if 1 175b528cefcSMark Murray "ret += oldret;\n" 176b528cefcSMark Murray #endif 177b528cefcSMark Murray "}\n" 178b528cefcSMark Murray "e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n" 179b528cefcSMark Murray "BACK;\n"); 180b528cefcSMark Murray free (n); 181b528cefcSMark Murray break; 182b528cefcSMark Murray } 183b528cefcSMark Murray case TGeneralizedTime: 184b528cefcSMark Murray encode_primitive ("generalized_time", name); 185b528cefcSMark Murray break; 186b528cefcSMark Murray case TGeneralString: 187b528cefcSMark Murray encode_primitive ("general_string", name); 188b528cefcSMark Murray break; 189b528cefcSMark Murray case TApplication: 190b528cefcSMark Murray encode_type (name, t->subtype); 191b528cefcSMark Murray fprintf (codefile, 192b528cefcSMark Murray "e = der_put_length_and_tag (p, len, ret, APPL, CONS, %d, &l);\n" 193b528cefcSMark Murray "BACK;\n", 194b528cefcSMark Murray t->application); 195b528cefcSMark Murray break; 196b528cefcSMark Murray default: 197b528cefcSMark Murray abort (); 198b528cefcSMark Murray } 199b528cefcSMark Murray } 200b528cefcSMark Murray 201b528cefcSMark Murray void 202b528cefcSMark Murray generate_type_encode (const Symbol *s) 203b528cefcSMark Murray { 204b528cefcSMark Murray fprintf (headerfile, 205b528cefcSMark Murray "int " 206b528cefcSMark Murray "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n", 207b528cefcSMark Murray s->gen_name, s->gen_name); 208b528cefcSMark Murray 209b528cefcSMark Murray fprintf (codefile, "#define BACK if (e) return e; p -= l; len -= l; ret += l\n\n"); 210b528cefcSMark Murray 211b528cefcSMark Murray 212b528cefcSMark Murray fprintf (codefile, "int\n" 213b528cefcSMark Murray "encode_%s(unsigned char *p, size_t len," 214b528cefcSMark Murray " const %s *data, size_t *size)\n" 215b528cefcSMark Murray "{\n", 216b528cefcSMark Murray s->gen_name, s->gen_name); 217b528cefcSMark Murray 218b528cefcSMark Murray switch (s->type->type) { 219b528cefcSMark Murray case TInteger: 220b528cefcSMark Murray fprintf (codefile, "return encode_integer (p, len, data, size);\n"); 221b528cefcSMark Murray break; 222b528cefcSMark Murray case TOctetString: 223b528cefcSMark Murray fprintf (codefile, "return encode_octet_string (p, len, data, size);\n"); 224b528cefcSMark Murray break; 225b528cefcSMark Murray case TGeneralizedTime: 226b528cefcSMark Murray fprintf (codefile, "return encode_generalized_time (p, len, data, size);\n"); 227b528cefcSMark Murray break; 228b528cefcSMark Murray case TGeneralString: 229b528cefcSMark Murray fprintf (codefile, "return encode_general_string (p, len, data, size);\n"); 230b528cefcSMark Murray break; 231b528cefcSMark Murray case TBitString: 232b528cefcSMark Murray case TSequence: 233b528cefcSMark Murray case TSequenceOf: 234b528cefcSMark Murray case TApplication: 235b528cefcSMark Murray case TType: 236b528cefcSMark Murray fprintf (codefile, 237b528cefcSMark Murray "size_t ret = 0;\n" 238b528cefcSMark Murray "size_t l;\n" 239b528cefcSMark Murray "int i, e;\n\n"); 240b528cefcSMark Murray fprintf(codefile, "i = 0;\n"); /* hack to avoid `unused variable' */ 241b528cefcSMark Murray 242b528cefcSMark Murray encode_type ("data", s->type); 243b528cefcSMark Murray fprintf (codefile, "*size = ret;\n" 244b528cefcSMark Murray "return 0;\n"); 245b528cefcSMark Murray break; 246b528cefcSMark Murray default: 247b528cefcSMark Murray abort (); 248b528cefcSMark Murray } 249b528cefcSMark Murray fprintf (codefile, "}\n\n"); 250b528cefcSMark Murray } 251