2000-01-09 20:58:00 +00:00
|
|
|
|
/*
|
2002-02-19 15:46:56 +00:00
|
|
|
|
* Copyright (c) 1997 - 2001 Kungliga Tekniska H<EFBFBD>gskolan
|
2000-01-09 20:58:00 +00:00
|
|
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "gen_locl.h"
|
|
|
|
|
|
2002-02-19 15:46:56 +00:00
|
|
|
|
RCSID("$Id: gen_encode.c,v 1.12 2001/09/25 13:39:26 assar Exp $");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
encode_primitive (const char *typename, const char *name)
|
|
|
|
|
{
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"e = encode_%s(p, len, %s, &l);\n"
|
|
|
|
|
"BACK;\n",
|
|
|
|
|
typename,
|
|
|
|
|
name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
encode_type (const char *name, const Type *t)
|
|
|
|
|
{
|
2001-02-13 16:46:19 +00:00
|
|
|
|
switch (t->type) {
|
|
|
|
|
case TType:
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#if 0
|
2001-02-13 16:46:19 +00:00
|
|
|
|
encode_type (name, t->symbol->type);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
|
|
|
|
fprintf (codefile,
|
2001-02-13 16:46:19 +00:00
|
|
|
|
"e = encode_%s(p, len, %s, &l);\n"
|
|
|
|
|
"BACK;\n",
|
|
|
|
|
t->symbol->gen_name, name);
|
|
|
|
|
break;
|
|
|
|
|
case TInteger:
|
|
|
|
|
if(t->members == NULL)
|
|
|
|
|
encode_primitive ("integer", name);
|
|
|
|
|
else {
|
|
|
|
|
char *s;
|
|
|
|
|
asprintf(&s, "(const int*)%s", name);
|
|
|
|
|
if(s == NULL)
|
|
|
|
|
errx(1, "out of memory");
|
|
|
|
|
encode_primitive ("integer", s);
|
|
|
|
|
free(s);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case TUInteger:
|
|
|
|
|
encode_primitive ("unsigned", name);
|
|
|
|
|
break;
|
|
|
|
|
case TOctetString:
|
|
|
|
|
encode_primitive ("octet_string", name);
|
|
|
|
|
break;
|
2002-02-19 15:46:56 +00:00
|
|
|
|
case TOID :
|
|
|
|
|
encode_primitive ("oid", name);
|
|
|
|
|
break;
|
2001-02-13 16:46:19 +00:00
|
|
|
|
case TBitString: {
|
|
|
|
|
Member *m;
|
|
|
|
|
int pos;
|
|
|
|
|
int rest;
|
|
|
|
|
int tag = -1;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
if (t->members == NULL)
|
|
|
|
|
break;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
fprintf (codefile, "{\n"
|
|
|
|
|
"unsigned char c = 0;\n");
|
|
|
|
|
pos = t->members->prev->val;
|
|
|
|
|
/* fix for buggy MIT (and OSF?) code */
|
|
|
|
|
if (pos > 31)
|
|
|
|
|
abort ();
|
|
|
|
|
/*
|
|
|
|
|
* It seems that if we do not always set pos to 31 here, the MIT
|
|
|
|
|
* code will do the wrong thing.
|
|
|
|
|
*
|
|
|
|
|
* I hate ASN.1 (and DER), but I hate it even more when everybody
|
|
|
|
|
* has to screw it up differently.
|
|
|
|
|
*/
|
|
|
|
|
pos = 31;
|
|
|
|
|
rest = 7 - (pos % 8);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
for (m = t->members->prev; m && tag != m->val; m = m->prev) {
|
|
|
|
|
while (m->val / 8 < pos / 8) {
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"*p-- = c; len--; ret++;\n"
|
|
|
|
|
"c = 0;\n");
|
|
|
|
|
pos -= 8;
|
|
|
|
|
}
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"if(%s->%s) c |= 1<<%d;\n", name, m->gen_name,
|
|
|
|
|
7 - m->val % 8);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
if (tag == -1)
|
|
|
|
|
tag = m->val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"*p-- = c;\n"
|
|
|
|
|
"*p-- = %d;\n"
|
|
|
|
|
"len -= 2;\n"
|
|
|
|
|
"ret += 2;\n"
|
|
|
|
|
"}\n\n"
|
|
|
|
|
"e = der_put_length_and_tag (p, len, ret, UNIV, PRIM,"
|
|
|
|
|
"UT_BitString, &l);\n"
|
|
|
|
|
"BACK;\n",
|
|
|
|
|
rest);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-02-19 15:46:56 +00:00
|
|
|
|
case TEnumerated : {
|
|
|
|
|
encode_primitive ("enumerated", name);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-02-13 16:46:19 +00:00
|
|
|
|
case TSequence: {
|
|
|
|
|
Member *m;
|
|
|
|
|
int tag = -1;
|
|
|
|
|
|
|
|
|
|
if (t->members == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (m = t->members->prev; m && tag != m->val; m = m->prev) {
|
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
|
|
asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
|
|
|
|
|
if (m->optional)
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"if(%s)\n",
|
|
|
|
|
s);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#if 1
|
2001-02-13 16:46:19 +00:00
|
|
|
|
fprintf (codefile, "{\n"
|
|
|
|
|
"int oldret = ret;\n"
|
|
|
|
|
"ret = 0;\n");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2001-02-13 16:46:19 +00:00
|
|
|
|
encode_type (s, m->type);
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"e = der_put_length_and_tag (p, len, ret, CONTEXT, CONS, "
|
|
|
|
|
"%d, &l);\n"
|
|
|
|
|
"BACK;\n",
|
|
|
|
|
m->val);
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#if 1
|
2001-02-13 16:46:19 +00:00
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"ret += oldret;\n"
|
|
|
|
|
"}\n");
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2001-02-13 16:46:19 +00:00
|
|
|
|
if (tag == -1)
|
|
|
|
|
tag = m->val;
|
|
|
|
|
free (s);
|
|
|
|
|
}
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n"
|
|
|
|
|
"BACK;\n");
|
|
|
|
|
break;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
2001-02-13 16:46:19 +00:00
|
|
|
|
case TSequenceOf: {
|
|
|
|
|
char *n;
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"for(i = (%s)->len - 1; i >= 0; --i) {\n"
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#if 1
|
2001-02-13 16:46:19 +00:00
|
|
|
|
"int oldret = ret;\n"
|
|
|
|
|
"ret = 0;\n",
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#else
|
2001-02-13 16:46:19 +00:00
|
|
|
|
,
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2001-02-13 16:46:19 +00:00
|
|
|
|
name);
|
|
|
|
|
asprintf (&n, "&(%s)->val[i]", name);
|
|
|
|
|
encode_type (n, t->subtype);
|
|
|
|
|
fprintf (codefile,
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#if 1
|
2001-02-13 16:46:19 +00:00
|
|
|
|
"ret += oldret;\n"
|
2000-01-09 20:58:00 +00:00
|
|
|
|
#endif
|
2001-02-13 16:46:19 +00:00
|
|
|
|
"}\n"
|
|
|
|
|
"e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n"
|
|
|
|
|
"BACK;\n");
|
|
|
|
|
free (n);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case TGeneralizedTime:
|
|
|
|
|
encode_primitive ("generalized_time", name);
|
|
|
|
|
break;
|
|
|
|
|
case TGeneralString:
|
|
|
|
|
encode_primitive ("general_string", name);
|
|
|
|
|
break;
|
|
|
|
|
case TApplication:
|
|
|
|
|
encode_type (name, t->subtype);
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"e = der_put_length_and_tag (p, len, ret, APPL, CONS, %d, &l);\n"
|
|
|
|
|
"BACK;\n",
|
|
|
|
|
t->application);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
generate_type_encode (const Symbol *s)
|
|
|
|
|
{
|
|
|
|
|
fprintf (headerfile,
|
|
|
|
|
"int "
|
|
|
|
|
"encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
|
|
|
|
|
s->gen_name, s->gen_name);
|
|
|
|
|
|
|
|
|
|
fprintf (codefile, "#define BACK if (e) return e; p -= l; len -= l; ret += l\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fprintf (codefile, "int\n"
|
|
|
|
|
"encode_%s(unsigned char *p, size_t len,"
|
|
|
|
|
" const %s *data, size_t *size)\n"
|
|
|
|
|
"{\n",
|
|
|
|
|
s->gen_name, s->gen_name);
|
|
|
|
|
|
|
|
|
|
switch (s->type->type) {
|
|
|
|
|
case TInteger:
|
2001-02-13 16:46:19 +00:00
|
|
|
|
case TUInteger:
|
2000-01-09 20:58:00 +00:00
|
|
|
|
case TOctetString:
|
|
|
|
|
case TGeneralizedTime:
|
|
|
|
|
case TGeneralString:
|
|
|
|
|
case TBitString:
|
2002-02-19 15:46:56 +00:00
|
|
|
|
case TEnumerated:
|
|
|
|
|
case TOID:
|
2000-01-09 20:58:00 +00:00
|
|
|
|
case TSequence:
|
|
|
|
|
case TSequenceOf:
|
|
|
|
|
case TApplication:
|
|
|
|
|
case TType:
|
|
|
|
|
fprintf (codefile,
|
|
|
|
|
"size_t ret = 0;\n"
|
|
|
|
|
"size_t l;\n"
|
|
|
|
|
"int i, e;\n\n");
|
|
|
|
|
fprintf(codefile, "i = 0;\n"); /* hack to avoid `unused variable' */
|
|
|
|
|
|
2001-02-13 16:46:19 +00:00
|
|
|
|
encode_type("data", s->type);
|
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
|
fprintf (codefile, "*size = ret;\n"
|
|
|
|
|
"return 0;\n");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
|
|
|
|
fprintf (codefile, "}\n\n");
|
|
|
|
|
}
|