2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 __RCSID("$Heimdal: gen_encode.c 22429 2008-01-13 10:25:50Z lha $"
40 encode_primitive (const char *typename
, const char *name
)
43 "e = der_put_%s(p, len, %s, &l);\n"
44 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
50 classname(Der_class
class)
52 const char *cn
[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
53 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
54 if(class < ASN1_C_UNIV
|| class > ASN1_C_PRIVATE
)
61 valuename(Der_class
class, int value
)
68 #define X(Y) { Y, #Y }
76 X(UT_GeneralizedTime
),
83 X(UT_ObjectDescriptor
),
85 X(UT_PrintableString
),
93 X(UT_UniversalString
),
99 if(class == ASN1_C_UNIV
) {
100 for(p
= values
; p
->value
!= -1; p
++)
101 if(p
->value
== value
)
104 snprintf(s
, sizeof(s
), "%d", value
);
109 encode_type (const char *name
, const Type
*t
, const char *tmpstr
)
116 encode_type (name
, t
->symbol
->type
);
119 "e = encode_%s(p, len, %s, &l);\n"
120 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
121 t
->symbol
->gen_name
, name
);
127 "int enumint = (int)*%s;\n",
129 encode_primitive ("integer", "&enumint");
130 fprintf(codefile
, "}\n;");
131 } else if (t
->range
== NULL
) {
132 encode_primitive ("heim_integer", name
);
133 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
134 encode_primitive ("integer", name
);
135 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
136 encode_primitive ("unsigned", name
);
137 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
138 encode_primitive ("unsigned", name
);
140 errx(1, "%s: unsupported range %d -> %d",
141 name
, t
->range
->min
, t
->range
->max
);
145 encode_primitive ("boolean", name
);
149 encode_primitive ("octet_string", name
);
156 if (ASN1_TAILQ_EMPTY(t
->members
)) {
157 encode_primitive("bit_string", name
);
162 fprintf (codefile
, "{\n"
163 "unsigned char c = 0;\n");
164 if (!rfc1510_bitstring
)
167 "int bit_set = 0;\n");
169 pos
= t
->members
->prev
->val
;
170 /* fix for buggy MIT (and OSF?) code */
175 * It seems that if we do not always set pos to 31 here, the MIT
176 * code will do the wrong thing.
178 * I hate ASN.1 (and DER), but I hate it even more when everybody
179 * has to screw it up differently.
181 pos
= ASN1_TAILQ_LAST(t
->members
, memhead
)->val
;
182 if (rfc1510_bitstring
) {
187 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
188 while (m
->val
/ 8 < pos
/ 8) {
189 if (!rfc1510_bitstring
)
191 "if (c != 0 || bit_set) {\n");
193 "if (len < 1) return ASN1_OVERFLOW;\n"
194 "*p-- = c; len--; ret++;\n");
195 if (!rfc1510_bitstring
)
200 "if (c & 1) break;\n"
214 name
, m
->gen_name
, 7 - m
->val
% 8);
219 if (!rfc1510_bitstring
)
221 "if (c != 0 || bit_set) {\n");
223 "if (len < 1) return ASN1_OVERFLOW;\n"
224 "*p-- = c; len--; ret++;\n");
225 if (!rfc1510_bitstring
)
231 "if (c & 1) break;\n"
240 "if (len < 1) return ASN1_OVERFLOW;\n"
245 rfc1510_bitstring
? "0" : "rest");
250 encode_primitive ("enumerated", name
);
259 if (t
->members
== NULL
)
262 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
268 asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
);
271 fprintf(codefile
, "/* %s */\n", m
->name
);
277 gen_compare_defval(s
+ 1, m
->defval
);
278 fprintf (codefile
, "{\n");
279 fprintf (codefile
, "size_t %s_oldret = ret;\n", tmpstr
);
280 fprintf (codefile
, "ret = 0;\n");
281 encode_type (s
, m
->type
, m
->gen_name
);
282 fprintf (codefile
, "ret += %s_oldret;\n", tmpstr
);
283 fprintf (codefile
, "}\n");
292 "struct heim_octet_string *val;\n"
293 "size_t elen, totallen = 0;\n"
297 "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
302 "val = malloc(sizeof(val[0]) * (%s)->len);\n"
303 "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
307 "for(i = 0; i < (%s)->len; i++) {\n",
311 "ASN1_MALLOC_ENCODE(%s, val[i].data, "
312 "val[i].length, &(%s)->val[i], &elen, eret);\n",
313 t
->subtype
->symbol
->gen_name
,
320 "free(val[i].data);\n"
326 "totallen += elen;\n"
330 "if (totallen > len) {\n"
331 "for (i = 0; i < (%s)->len; i++) {\n"
332 "free(val[i].data);\n"
335 "return ASN1_OVERFLOW;\n"
340 "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
344 "for(i = (%s)->len - 1; i >= 0; --i) {\n"
345 "p -= val[i].length;\n"
346 "ret += val[i].length;\n"
347 "memcpy(p + 1, val[i].data, val[i].length);\n"
348 "free(val[i].data);\n"
360 "for(i = (%s)->len - 1; i >= 0; --i) {\n"
361 "size_t %s_for_oldret = ret;\n"
364 asprintf (&n
, "&(%s)->val[i]", name
);
367 asprintf (&sname
, "%s_S_Of", tmpstr
);
370 encode_type (n
, t
->subtype
, sname
);
372 "ret += %s_for_oldret;\n"
379 case TGeneralizedTime
:
380 encode_primitive ("generalized_time", name
);
384 encode_primitive ("general_string", name
);
390 asprintf (&tname
, "%s_tag", tmpstr
);
393 c
= encode_type (name
, t
->subtype
, tname
);
395 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
396 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
397 classname(t
->tag
.tagclass
),
399 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
));
404 Member
*m
, *have_ellipsis
= NULL
;
407 if (t
->members
== NULL
)
410 fprintf(codefile
, "\n");
412 asprintf (&s
, "(%s)", name
);
415 fprintf(codefile
, "switch(%s->element) {\n", s
);
417 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
425 fprintf (codefile
, "case %s: {", m
->label
);
426 asprintf(&s2
, "%s(%s)->u.%s", m
->optional
? "" : "&",
431 fprintf (codefile
, "if(%s) {\n", s2
);
432 fprintf (codefile
, "size_t %s_oldret = ret;\n", tmpstr
);
433 fprintf (codefile
, "ret = 0;\n");
434 constructed
= encode_type (s2
, m
->type
, m
->gen_name
);
435 fprintf (codefile
, "ret += %s_oldret;\n", tmpstr
);
437 fprintf (codefile
, "}\n");
438 fprintf(codefile
, "break;\n");
439 fprintf(codefile
, "}\n");
446 "if (len < (%s)->u.%s.length)\n"
447 "return ASN1_OVERFLOW;\n"
448 "p -= (%s)->u.%s.length;\n"
449 "ret += (%s)->u.%s.length;\n"
450 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
453 have_ellipsis
->label
,
454 name
, have_ellipsis
->gen_name
,
455 name
, have_ellipsis
->gen_name
,
456 name
, have_ellipsis
->gen_name
,
457 name
, have_ellipsis
->gen_name
,
458 name
, have_ellipsis
->gen_name
);
460 fprintf(codefile
, "};\n");
464 encode_primitive ("oid", name
);
468 encode_primitive ("utctime", name
);
472 encode_primitive ("utf8string", name
);
475 case TPrintableString
:
476 encode_primitive ("printable_string", name
);
480 encode_primitive ("ia5_string", name
);
484 encode_primitive ("bmp_string", name
);
487 case TUniversalString
:
488 encode_primitive ("universal_string", name
);
492 encode_primitive ("visible_string", name
);
496 fprintf (codefile
, "/* NULL */\n");
506 generate_type_encode (const Symbol
*s
)
510 "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
511 s
->gen_name
, s
->gen_name
);
513 fprintf (codefile
, "int\n"
514 "encode_%s(unsigned char *p, size_t len,"
515 " const %s *data, size_t *size)\n"
517 s
->gen_name
, s
->gen_name
);
519 switch (s
->type
->type
) {
523 case TGeneralizedTime
:
527 case TPrintableString
:
530 case TUniversalString
:
547 fprintf(codefile
, "i = 0;\n"); /* hack to avoid `unused variable' */
549 encode_type("data", s
->type
, "Top");
551 fprintf (codefile
, "*size = ret;\n"
557 fprintf (codefile
, "}\n\n");