2 * Copyright (c) 1997 - 2005 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
37 RCSID("$Id: gen_decode.c,v 1.30 2006/09/24 09:13:12 lha Exp $");
40 decode_primitive (const char *typename
, const char *name
, const char *forwstr
)
44 "e = decode_%s(p, len, %s, &l);\n"
51 "e = der_get_%s(p, len, %s, &l);\n"
52 "if(e) %s;\np += l; len -= l; ret += l;\n",
60 is_primitive_type(int type
)
68 case TGeneralizedTime
:
73 case TPrintableString
:
76 case TUniversalString
:
85 find_tag (const Type
*t
,
86 Der_class
*cl
, Der_type
*ty
, unsigned *tag
)
100 errx(1, "Cannot have recursive CHOICE");
104 *tag
= UT_Enumerated
;
109 *tag
= UT_GeneralString
;
111 case TGeneralizedTime
:
114 *tag
= UT_GeneralizedTime
;
139 *tag
= UT_OctetString
;
141 case TPrintableString
:
144 *tag
= UT_PrintableString
;
159 *cl
= t
->tag
.tagclass
;
160 *ty
= is_primitive_type(t
->subtype
->type
) ? PRIM
: CONS
;
161 *tag
= t
->tag
.tagvalue
;
164 if ((t
->symbol
->stype
== Stype
&& t
->symbol
->type
== NULL
)
165 || t
->symbol
->stype
== SUndefined
) {
166 error_message("%s is imported or still undefined, "
167 " can't generate tag checking data in CHOICE "
168 "without this information",
172 find_tag(t
->symbol
->type
, cl
, ty
, tag
);
182 *tag
= UT_UTF8String
;
189 case TUniversalString
:
192 *tag
= UT_UniversalString
;
200 decode_type (const char *name
, const Type
*t
, int optional
,
201 const char *forwstr
, const char *tmpstr
)
207 "%s = calloc(1, sizeof(*%s));\n"
208 "if (%s == NULL) %s;\n",
209 name
, name
, name
, forwstr
);
211 "e = decode_%s(p, len, %s, &l);\n",
212 t
->symbol
->gen_name
, name
);
219 "p += l; len -= l; ret += l;\n"
227 "p += l; len -= l; ret += l;\n");
234 asprintf(&s
, "(int*)%s", name
);
236 errx (1, "out of memory");
237 decode_primitive ("integer", s
, forwstr
);
239 } else if (t
->range
== NULL
) {
240 decode_primitive ("heim_integer", name
, forwstr
);
241 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
242 decode_primitive ("integer", name
, forwstr
);
243 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
244 decode_primitive ("unsigned", name
, forwstr
);
245 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
246 decode_primitive ("unsigned", name
, forwstr
);
248 errx(1, "%s: unsupported range %d -> %d",
249 name
, t
->range
->min
, t
->range
->max
);
252 decode_primitive ("boolean", name
, forwstr
);
255 decode_primitive ("enumerated", name
, forwstr
);
258 decode_primitive ("octet_string", name
, forwstr
);
264 if (ASN1_TAILQ_EMPTY(t
->members
)) {
265 decode_primitive ("bit_string", name
, forwstr
);
269 "if (len < 1) return ASN1_OVERRUN;\n"
270 "p++; len--; ret++;\n");
273 "if (len < 1) break;\n");
274 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
275 while (m
->val
/ 8 > pos
/ 8) {
277 "p++; len--; ret++;\n"
278 "if (len < 1) break;\n");
282 "(%s)->%s = (*p >> %d) & 1;\n",
283 name
, m
->gen_name
, 7 - m
->val
% 8);
288 "p += len; ret += len;\n");
294 if (t
->members
== NULL
)
297 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
303 asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&",
307 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
);
317 if(t
->members
== NULL
)
320 fprintf(codefile
, "{\n");
321 fprintf(codefile
, "unsigned int members = 0;\n");
322 fprintf(codefile
, "while(len > 0) {\n");
327 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
328 "if(e) %s;\n", forwstr
);
329 fprintf(codefile
, "switch (MAKE_TAG(class, type, tag)) {\n");
331 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
334 assert(m
->type
->type
== TTag
);
336 fprintf(codefile
, "case MAKE_TAG(%s, %s, %s):\n",
337 classname(m
->type
->tag
.tagclass
),
338 is_primitive_type(m
->type
->subtype
->type
) ? "PRIM" : "CONS",
339 valuename(m
->type
->tag
.tagclass
, m
->type
->tag
.tagvalue
));
341 asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
);
346 "%s = calloc(1, sizeof(*%s));\n"
347 "if (%s == NULL) { e = ENOMEM; %s; }\n",
349 decode_type (s
, m
->type
, 0, forwstr
, m
->gen_name
);
352 fprintf(codefile
, "members |= (1 << %d);\n", memno
);
354 fprintf(codefile
, "break;\n");
358 "return ASN1_MISPLACED_FIELD;\n"
360 fprintf(codefile
, "}\n");
361 fprintf(codefile
, "}\n");
363 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
366 asprintf (&s
, "%s->%s", name
, m
->gen_name
);
369 fprintf(codefile
, "if((members & (1 << %d)) == 0)\n", memno
);
371 fprintf(codefile
, "%s = NULL;\n", s
);
373 gen_assign_defval(s
, m
->defval
);
375 fprintf(codefile
, "return ASN1_MISSING_FIELD;\n");
379 fprintf(codefile
, "}\n");
389 "size_t %s_origlen = len;\n"
390 "size_t %s_oldret = ret;\n"
394 "(%s)->val = NULL;\n"
395 "while(ret < %s_origlen) {\n"
396 "%s_tmp = realloc((%s)->val, "
397 " sizeof(*((%s)->val)) * ((%s)->len + 1));\n"
398 "if (%s_tmp == NULL) { %s; }\n"
399 "(%s)->val = %s_tmp;\n",
400 tmpstr
, tmpstr
, tmpstr
,
407 asprintf (&n
, "&(%s)->val[(%s)->len]", name
, name
);
410 asprintf (&sname
, "%s_s_of", tmpstr
);
413 decode_type (n
, t
->subtype
, 0, forwstr
, sname
);
416 "len = %s_origlen - ret;\n"
418 "ret += %s_oldret;\n"
426 case TGeneralizedTime
:
427 decode_primitive ("generalized_time", name
, forwstr
);
430 decode_primitive ("general_string", name
, forwstr
);
437 "size_t %s_datalen, %s_oldlen;\n",
442 fprintf(codefile
, "e = der_match_tag_and_length(p, len, %s, %s, %s, "
443 "&%s_datalen, &l);\n",
444 classname(t
->tag
.tagclass
),
445 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS",
446 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
453 "%s = calloc(1, sizeof(*%s));\n"
454 "if (%s == NULL) { e = ENOMEM; %s; }\n",
455 name
, name
, name
, name
, forwstr
);
457 fprintf(codefile
, "if(e) %s;\n", forwstr
);
460 "p += l; len -= l; ret += l;\n"
461 "%s_oldlen = len;\n",
465 "if((dce_fix = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
466 "{ e = ASN1_BAD_FORMAT; %s; }\n",
470 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
471 "len = %s_datalen;\n", tmpstr
, forwstr
, tmpstr
);
472 asprintf (&tname
, "%s_Tag", tmpstr
);
475 decode_type (name
, t
->subtype
, 0, forwstr
, tname
);
479 "e = der_match_tag_and_length (p, len, "
480 "(Der_class)0,(Der_type)0, UT_EndOfContent, "
481 "&%s_datalen, &l);\n"
482 "if(e) %s;\np += l; len -= l; ret += l;\n"
483 "} else \n", tmpstr
, forwstr
);
485 "len = %s_oldlen - %s_datalen;\n",
496 Member
*m
, *have_ellipsis
= NULL
;
497 const char *els
= "";
499 if (t
->members
== NULL
)
502 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
503 const Type
*tt
= m
->type
;
514 find_tag(tt
, &cl
, &ty
, &tag
);
517 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
520 ty
? "CONS" : "PRIM",
522 asprintf (&s
, "%s(%s)->u.%s", m
->optional
? "" : "&",
526 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
);
528 "(%s)->element = %s;\n",
538 "(%s)->u.%s.data = calloc(1, len);\n"
539 "if ((%s)->u.%s.data == NULL) {\n"
542 "(%s)->u.%s.length = len;\n"
543 "memcpy((%s)->u.%s.data, p, len);\n"
544 "(%s)->element = %s;\n"
549 name
, have_ellipsis
->gen_name
,
550 name
, have_ellipsis
->gen_name
,
552 name
, have_ellipsis
->gen_name
,
553 name
, have_ellipsis
->gen_name
,
554 name
, have_ellipsis
->label
);
558 "e = ASN1_PARSE_ERROR;\n"
566 decode_primitive ("utctime", name
, forwstr
);
569 decode_primitive ("utf8string", name
, forwstr
);
571 case TPrintableString
:
572 decode_primitive ("printable_string", name
, forwstr
);
575 decode_primitive ("ia5_string", name
, forwstr
);
578 decode_primitive ("bmp_string", name
, forwstr
);
580 case TUniversalString
:
581 decode_primitive ("universal_string", name
, forwstr
);
584 fprintf (codefile
, "/* NULL */\n");
587 decode_primitive ("oid", name
, forwstr
);
596 generate_type_decode (const Symbol
*s
)
598 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
602 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
603 s
->gen_name
, s
->gen_name
);
605 fprintf (codefile
, "int\n"
606 "decode_%s(const unsigned char *p,"
607 " size_t len, %s *data, size_t *size)\n"
609 s
->gen_name
, s
->gen_name
);
611 switch (s
->type
->type
) {
616 case TGeneralizedTime
:
619 case TPrintableString
:
622 case TUniversalString
:
639 fprintf (codefile
, "const unsigned char *begin = p;\n");
641 fprintf (codefile
, "\n");
642 fprintf (codefile
, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
644 decode_type ("data", s
->type
, 0, "goto fail", "Top");
647 "data->_save.data = calloc(1, ret);\n"
648 "if (data->_save.data == NULL) { \n"
649 "e = ENOMEM; goto fail; \n"
651 "data->_save.length = ret;\n"
652 "memcpy(data->_save.data, begin, ret);\n");
654 "if(size) *size = ret;\n"
665 fprintf (codefile
, "}\n\n");