1/* 2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 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. 16 * 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34#include "gen_locl.h" 35#include "lex.h" 36 37RCSID("$Id$"); 38 39static void 40decode_primitive (const char *typename, const char *name, const char *forwstr) 41{ 42#if 0 43 fprintf (codefile, 44 "e = decode_%s(p, len, %s, &l);\n" 45 "%s;\n", 46 typename, 47 name, 48 forwstr); 49#else 50 fprintf (codefile, 51 "e = der_get_%s(p, len, %s, &l);\n" 52 "if(e) %s;\np += l; len -= l; ret += l;\n", 53 typename, 54 name, 55 forwstr); 56#endif 57} 58 59static void 60find_tag (const Type *t, 61 Der_class *cl, Der_type *ty, unsigned *tag) 62{ 63 switch (t->type) { 64 case TBitString: 65 *cl = ASN1_C_UNIV; 66 *ty = PRIM; 67 *tag = UT_BitString; 68 break; 69 case TBoolean: 70 *cl = ASN1_C_UNIV; 71 *ty = PRIM; 72 *tag = UT_Boolean; 73 break; 74 case TChoice: 75 errx(1, "Cannot have recursive CHOICE"); 76 case TEnumerated: 77 *cl = ASN1_C_UNIV; 78 *ty = PRIM; 79 *tag = UT_Enumerated; 80 break; 81 case TGeneralString: 82 *cl = ASN1_C_UNIV; 83 *ty = PRIM; 84 *tag = UT_GeneralString; 85 break; 86 case TTeletexString: 87 *cl = ASN1_C_UNIV; 88 *ty = PRIM; 89 *tag = UT_TeletexString; 90 break; 91 case TGeneralizedTime: 92 *cl = ASN1_C_UNIV; 93 *ty = PRIM; 94 *tag = UT_GeneralizedTime; 95 break; 96 case TIA5String: 97 *cl = ASN1_C_UNIV; 98 *ty = PRIM; 99 *tag = UT_IA5String; 100 break; 101 case TInteger: 102 *cl = ASN1_C_UNIV; 103 *ty = PRIM; 104 *tag = UT_Integer; 105 break; 106 case TNull: 107 *cl = ASN1_C_UNIV; 108 *ty = PRIM; 109 *tag = UT_Null; 110 break; 111 case TOID: 112 *cl = ASN1_C_UNIV; 113 *ty = PRIM; 114 *tag = UT_OID; 115 break; 116 case TOctetString: 117 *cl = ASN1_C_UNIV; 118 *ty = PRIM; 119 *tag = UT_OctetString; 120 break; 121 case TPrintableString: 122 *cl = ASN1_C_UNIV; 123 *ty = PRIM; 124 *tag = UT_PrintableString; 125 break; 126 case TSequence: 127 case TSequenceOf: 128 *cl = ASN1_C_UNIV; 129 *ty = CONS; 130 *tag = UT_Sequence; 131 break; 132 case TSet: 133 case TSetOf: 134 *cl = ASN1_C_UNIV; 135 *ty = CONS; 136 *tag = UT_Set; 137 break; 138 case TTag: 139 *cl = t->tag.tagclass; 140 *ty = is_primitive_type(t->subtype->type) ? PRIM : CONS; 141 *tag = t->tag.tagvalue; 142 break; 143 case TType: 144 if ((t->symbol->stype == Stype && t->symbol->type == NULL) 145 || t->symbol->stype == SUndefined) { 146 lex_error_message("%s is imported or still undefined, " 147 " can't generate tag checking data in CHOICE " 148 "without this information", 149 t->symbol->name); 150 exit(1); 151 } 152 find_tag(t->symbol->type, cl, ty, tag); 153 return; 154 case TUTCTime: 155 *cl = ASN1_C_UNIV; 156 *ty = PRIM; 157 *tag = UT_UTCTime; 158 break; 159 case TUTF8String: 160 *cl = ASN1_C_UNIV; 161 *ty = PRIM; 162 *tag = UT_UTF8String; 163 break; 164 case TBMPString: 165 *cl = ASN1_C_UNIV; 166 *ty = PRIM; 167 *tag = UT_BMPString; 168 break; 169 case TUniversalString: 170 *cl = ASN1_C_UNIV; 171 *ty = PRIM; 172 *tag = UT_UniversalString; 173 break; 174 case TVisibleString: 175 *cl = ASN1_C_UNIV; 176 *ty = PRIM; 177 *tag = UT_VisibleString; 178 break; 179 default: 180 abort(); 181 } 182} 183 184static void 185range_check(const char *name, 186 const char *length, 187 const char *forwstr, 188 struct range *r) 189{ 190 if (r->min == r->max + 2 || r->min < r->max) 191 fprintf (codefile, 192 "if ((%s)->%s > %d) {\n" 193 "e = ASN1_MAX_CONSTRAINT; %s;\n" 194 "}\n", 195 name, length, r->max, forwstr); 196 if (r->min - 1 == r->max || r->min < r->max) 197 fprintf (codefile, 198 "if ((%s)->%s < %d) {\n" 199 "e = ASN1_MIN_CONSTRAINT; %s;\n" 200 "}\n", 201 name, length, r->min, forwstr); 202 if (r->max == r->min) 203 fprintf (codefile, 204 "if ((%s)->%s != %d) {\n" 205 "e = ASN1_EXACT_CONSTRAINT; %s;\n" 206 "}\n", 207 name, length, r->min, forwstr); 208} 209 210static int 211decode_type (const char *name, const Type *t, int optional, 212 const char *forwstr, const char *tmpstr, const char *dertype, 213 unsigned int depth) 214{ 215 switch (t->type) { 216 case TType: { 217 if (optional) 218 fprintf(codefile, 219 "%s = calloc(1, sizeof(*%s));\n" 220 "if (%s == NULL) %s;\n", 221 name, name, name, forwstr); 222 fprintf (codefile, 223 "e = decode_%s(p, len, %s, &l);\n", 224 t->symbol->gen_name, name); 225 if (optional) { 226 fprintf (codefile, 227 "if(e) {\n" 228 "free(%s);\n" 229 "%s = NULL;\n" 230 "} else {\n" 231 "p += l; len -= l; ret += l;\n" 232 "}\n", 233 name, name); 234 } else { 235 fprintf (codefile, 236 "if(e) %s;\n", 237 forwstr); 238 fprintf (codefile, 239 "p += l; len -= l; ret += l;\n"); 240 } 241 break; 242 } 243 case TInteger: 244 if(t->members) { 245 /* 246 * This will produce a worning, how its hard to fix since: 247 * if its enum to an NameType, we can add appriate 248 * type-cast. If its not though, we have to figure out if 249 * there is negative enum enum and use appropriate 250 * signness and size on the intertype we cast the result 251 * too. 252 */ 253 fprintf(codefile, 254 "{\n" 255 "int enumint;\n"); 256 decode_primitive ("integer", "&enumint", forwstr); 257 fprintf(codefile, 258 "*%s = enumint;\n" 259 "}\n", 260 name); 261 } else if (t->range == NULL) { 262 decode_primitive ("heim_integer", name, forwstr); 263 } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) { 264 decode_primitive ("integer", name, forwstr); 265 } else if (t->range->min == 0 && t->range->max == UINT_MAX) { 266 decode_primitive ("unsigned", name, forwstr); 267 } else if (t->range->min == 0 && t->range->max == INT_MAX) { 268 decode_primitive ("unsigned", name, forwstr); 269 } else 270 errx(1, "%s: unsupported range %d -> %d", 271 name, t->range->min, t->range->max); 272 break; 273 case TBoolean: 274 decode_primitive ("boolean", name, forwstr); 275 break; 276 case TEnumerated: 277 decode_primitive ("enumerated", name, forwstr); 278 break; 279 case TOctetString: 280 if (dertype) { 281 fprintf(codefile, 282 "if (%s == CONS) {\n", 283 dertype); 284 decode_primitive("octet_string_ber", name, forwstr); 285 fprintf(codefile, 286 "} else {\n"); 287 } 288 decode_primitive ("octet_string", name, forwstr); 289 if (dertype) 290 fprintf(codefile, "}\n"); 291 if (t->range) 292 range_check(name, "length", forwstr, t->range); 293 break; 294 case TBitString: { 295 Member *m; 296 int pos = 0; 297 298 if (ASN1_TAILQ_EMPTY(t->members)) { 299 decode_primitive ("bit_string", name, forwstr); 300 break; 301 } 302 fprintf(codefile, 303 "if (len < 1) return ASN1_OVERRUN;\n" 304 "p++; len--; ret++;\n"); 305 fprintf(codefile, 306 "do {\n" 307 "if (len < 1) break;\n"); 308 ASN1_TAILQ_FOREACH(m, t->members, members) { 309 while (m->val / 8 > pos / 8) { 310 fprintf (codefile, 311 "p++; len--; ret++;\n" 312 "if (len < 1) break;\n"); 313 pos += 8; 314 } 315 fprintf (codefile, 316 "(%s)->%s = (*p >> %d) & 1;\n", 317 name, m->gen_name, 7 - m->val % 8); 318 } 319 fprintf(codefile, 320 "} while(0);\n"); 321 fprintf (codefile, 322 "p += len; ret += len;\n"); 323 break; 324 } 325 case TSequence: { 326 Member *m; 327 328 if (t->members == NULL) 329 break; 330 331 ASN1_TAILQ_FOREACH(m, t->members, members) { 332 char *s = NULL; 333 334 if (m->ellipsis) 335 continue; 336 337 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", 338 name, m->gen_name) < 0 || s == NULL) 339 errx(1, "malloc"); 340 decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL, 341 depth + 1); 342 free (s); 343 } 344 345 break; 346 } 347 case TSet: { 348 Member *m; 349 unsigned int memno; 350 351 if(t->members == NULL) 352 break; 353 354 fprintf(codefile, "{\n"); 355 fprintf(codefile, "unsigned int members = 0;\n"); 356 fprintf(codefile, "while(len > 0) {\n"); 357 fprintf(codefile, 358 "Der_class class;\n" 359 "Der_type type;\n" 360 "int tag;\n" 361 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n" 362 "if(e) %s;\n", forwstr); 363 fprintf(codefile, "switch (MAKE_TAG(class, type, tag)) {\n"); 364 memno = 0; 365 ASN1_TAILQ_FOREACH(m, t->members, members) { 366 char *s; 367 368 assert(m->type->type == TTag); 369 370 fprintf(codefile, "case MAKE_TAG(%s, %s, %s):\n", 371 classname(m->type->tag.tagclass), 372 is_primitive_type(m->type->subtype->type) ? "PRIM" : "CONS", 373 valuename(m->type->tag.tagclass, m->type->tag.tagvalue)); 374 375 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL) 376 errx(1, "malloc"); 377 if(m->optional) 378 fprintf(codefile, 379 "%s = calloc(1, sizeof(*%s));\n" 380 "if (%s == NULL) { e = ENOMEM; %s; }\n", 381 s, s, s, forwstr); 382 decode_type (s, m->type, 0, forwstr, m->gen_name, NULL, depth + 1); 383 free (s); 384 385 fprintf(codefile, "members |= (1 << %d);\n", memno); 386 memno++; 387 fprintf(codefile, "break;\n"); 388 } 389 fprintf(codefile, 390 "default:\n" 391 "return ASN1_MISPLACED_FIELD;\n" 392 "break;\n"); 393 fprintf(codefile, "}\n"); 394 fprintf(codefile, "}\n"); 395 memno = 0; 396 ASN1_TAILQ_FOREACH(m, t->members, members) { 397 char *s; 398 399 if (asprintf (&s, "%s->%s", name, m->gen_name) < 0 || s == NULL) 400 errx(1, "malloc"); 401 fprintf(codefile, "if((members & (1 << %d)) == 0)\n", memno); 402 if(m->optional) 403 fprintf(codefile, "%s = NULL;\n", s); 404 else if(m->defval) 405 gen_assign_defval(s, m->defval); 406 else 407 fprintf(codefile, "return ASN1_MISSING_FIELD;\n"); 408 free(s); 409 memno++; 410 } 411 fprintf(codefile, "}\n"); 412 break; 413 } 414 case TSetOf: 415 case TSequenceOf: { 416 char *n = NULL; 417 char *sname = NULL; 418 419 fprintf (codefile, 420 "{\n" 421 "size_t %s_origlen = len;\n" 422 "size_t %s_oldret = ret;\n" 423 "size_t %s_olen = 0;\n" 424 "void *%s_tmp;\n" 425 "ret = 0;\n" 426 "(%s)->len = 0;\n" 427 "(%s)->val = NULL;\n", 428 tmpstr, 429 tmpstr, 430 tmpstr, 431 tmpstr, 432 name, 433 name); 434 435 fprintf (codefile, 436 "while(ret < %s_origlen) {\n" 437 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n" 438 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n" 439 "%s_olen = %s_nlen;\n" 440 "%s_tmp = realloc((%s)->val, %s_olen);\n" 441 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n" 442 "(%s)->val = %s_tmp;\n", 443 tmpstr, 444 tmpstr, tmpstr, name, 445 tmpstr, tmpstr, forwstr, 446 tmpstr, tmpstr, 447 tmpstr, name, tmpstr, 448 tmpstr, forwstr, 449 name, tmpstr); 450 451 if (asprintf (&n, "&(%s)->val[(%s)->len]", name, name) < 0 || n == NULL) 452 errx(1, "malloc"); 453 if (asprintf (&sname, "%s_s_of", tmpstr) < 0 || sname == NULL) 454 errx(1, "malloc"); 455 decode_type (n, t->subtype, 0, forwstr, sname, NULL, depth + 1); 456 fprintf (codefile, 457 "(%s)->len++;\n" 458 "len = %s_origlen - ret;\n" 459 "}\n" 460 "ret += %s_oldret;\n" 461 "}\n", 462 name, 463 tmpstr, tmpstr); 464 if (t->range) 465 range_check(name, "len", forwstr, t->range); 466 free (n); 467 free (sname); 468 break; 469 } 470 case TGeneralizedTime: 471 decode_primitive ("generalized_time", name, forwstr); 472 break; 473 case TGeneralString: 474 decode_primitive ("general_string", name, forwstr); 475 break; 476 case TTeletexString: 477 decode_primitive ("general_string", name, forwstr); 478 break; 479 case TTag:{ 480 char *tname = NULL, *typestring = NULL; 481 char *ide = NULL; 482 483 if (asprintf(&typestring, "%s_type", tmpstr) < 0 || typestring == NULL) 484 errx(1, "malloc"); 485 486 fprintf(codefile, 487 "{\n" 488 "size_t %s_datalen, %s_oldlen;\n" 489 "Der_type %s;\n", 490 tmpstr, tmpstr, typestring); 491 if(support_ber) 492 fprintf(codefile, 493 "int is_indefinite%u;\n", depth); 494 495 fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, &%s, %s, " 496 "&%s_datalen, &l);\n", 497 classname(t->tag.tagclass), 498 typestring, 499 valuename(t->tag.tagclass, t->tag.tagvalue), 500 tmpstr); 501 502 /* XXX hardcode for now */ 503 if (support_ber && t->subtype->type == TOctetString) { 504 ide = typestring; 505 } else { 506 fprintf(codefile, 507 "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n", 508 typestring, 509 is_primitive_type(t->subtype->type) ? "PRIM" : "CONS"); 510 } 511 512 if(optional) { 513 fprintf(codefile, 514 "if(e) {\n" 515 "%s = NULL;\n" 516 "} else {\n" 517 "%s = calloc(1, sizeof(*%s));\n" 518 "if (%s == NULL) { e = ENOMEM; %s; }\n", 519 name, name, name, name, forwstr); 520 } else { 521 fprintf(codefile, "if(e) %s;\n", forwstr); 522 } 523 fprintf (codefile, 524 "p += l; len -= l; ret += l;\n" 525 "%s_oldlen = len;\n", 526 tmpstr); 527 if(support_ber) 528 fprintf (codefile, 529 "if((is_indefinite%u = _heim_fix_dce(%s_datalen, &len)) < 0)\n" 530 "{ e = ASN1_BAD_FORMAT; %s; }\n" 531 "if (is_indefinite%u) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }", 532 depth, tmpstr, forwstr, depth, forwstr); 533 else 534 fprintf(codefile, 535 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n" 536 "len = %s_datalen;\n", tmpstr, forwstr, tmpstr); 537 if (asprintf (&tname, "%s_Tag", tmpstr) < 0 || tname == NULL) 538 errx(1, "malloc"); 539 decode_type (name, t->subtype, 0, forwstr, tname, ide, depth + 1); 540 if(support_ber) 541 fprintf(codefile, 542 "if(is_indefinite%u){\n" 543 "len += 2;\n" 544 "e = der_match_tag_and_length(p, len, " 545 "(Der_class)0, &%s, UT_EndOfContent, " 546 "&%s_datalen, &l);\n" 547 "if(e) %s;\n" 548 "p += l; len -= l; ret += l;\n" 549 "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n" 550 "} else \n", 551 depth, 552 typestring, 553 tmpstr, 554 forwstr, 555 typestring, forwstr); 556 fprintf(codefile, 557 "len = %s_oldlen - %s_datalen;\n", 558 tmpstr, tmpstr); 559 if(optional) 560 fprintf(codefile, 561 "}\n"); 562 fprintf(codefile, 563 "}\n"); 564 free(tname); 565 free(typestring); 566 break; 567 } 568 case TChoice: { 569 Member *m, *have_ellipsis = NULL; 570 const char *els = ""; 571 572 if (t->members == NULL) 573 break; 574 575 ASN1_TAILQ_FOREACH(m, t->members, members) { 576 const Type *tt = m->type; 577 char *s = NULL; 578 Der_class cl; 579 Der_type ty; 580 unsigned tag; 581 582 if (m->ellipsis) { 583 have_ellipsis = m; 584 continue; 585 } 586 587 find_tag(tt, &cl, &ty, &tag); 588 589 fprintf(codefile, 590 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n", 591 els, 592 classname(cl), 593 ty ? "CONS" : "PRIM", 594 valuename(cl, tag)); 595 if (asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&", 596 name, m->gen_name) < 0 || s == NULL) 597 errx(1, "malloc"); 598 decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL, 599 depth + 1); 600 fprintf(codefile, 601 "(%s)->element = %s;\n", 602 name, m->label); 603 free(s); 604 fprintf(codefile, 605 "}\n"); 606 els = "else "; 607 } 608 if (have_ellipsis) { 609 fprintf(codefile, 610 "else {\n" 611 "(%s)->u.%s.data = calloc(1, len);\n" 612 "if ((%s)->u.%s.data == NULL) {\n" 613 "e = ENOMEM; %s;\n" 614 "}\n" 615 "(%s)->u.%s.length = len;\n" 616 "memcpy((%s)->u.%s.data, p, len);\n" 617 "(%s)->element = %s;\n" 618 "p += len;\n" 619 "ret += len;\n" 620 "len = 0;\n" 621 "}\n", 622 name, have_ellipsis->gen_name, 623 name, have_ellipsis->gen_name, 624 forwstr, 625 name, have_ellipsis->gen_name, 626 name, have_ellipsis->gen_name, 627 name, have_ellipsis->label); 628 } else { 629 fprintf(codefile, 630 "else {\n" 631 "(%s)->element = ASN1_CHOICE_INVALID;\n" 632 "e = ASN1_PARSE_ERROR;\n" 633 "%s;\n" 634 "}\n", 635 name, forwstr); 636 } 637 break; 638 } 639 case TUTCTime: 640 decode_primitive ("utctime", name, forwstr); 641 break; 642 case TUTF8String: 643 decode_primitive ("utf8string", name, forwstr); 644 break; 645 case TPrintableString: 646 decode_primitive ("printable_string", name, forwstr); 647 break; 648 case TIA5String: 649 decode_primitive ("ia5_string", name, forwstr); 650 break; 651 case TBMPString: 652 decode_primitive ("bmp_string", name, forwstr); 653 break; 654 case TUniversalString: 655 decode_primitive ("universal_string", name, forwstr); 656 break; 657 case TVisibleString: 658 decode_primitive ("visible_string", name, forwstr); 659 break; 660 case TNull: 661 fprintf (codefile, "/* NULL */\n"); 662 break; 663 case TOID: 664 decode_primitive ("oid", name, forwstr); 665 break; 666 default : 667 abort (); 668 } 669 return 0; 670} 671 672void 673generate_type_decode (const Symbol *s) 674{ 675 int preserve = preserve_type(s->name) ? TRUE : FALSE; 676 677 fprintf (codefile, "int ASN1CALL\n" 678 "decode_%s(const unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE," 679 " size_t len HEIMDAL_UNUSED_ATTRIBUTE, %s *data, size_t *size)\n" 680 "{\n", 681 s->gen_name, s->gen_name); 682 683 switch (s->type->type) { 684 case TInteger: 685 case TBoolean: 686 case TOctetString: 687 case TOID: 688 case TGeneralizedTime: 689 case TGeneralString: 690 case TTeletexString: 691 case TUTF8String: 692 case TPrintableString: 693 case TIA5String: 694 case TBMPString: 695 case TUniversalString: 696 case TVisibleString: 697 case TUTCTime: 698 case TNull: 699 case TEnumerated: 700 case TBitString: 701 case TSequence: 702 case TSequenceOf: 703 case TSet: 704 case TSetOf: 705 case TTag: 706 case TType: 707 case TChoice: 708 fprintf (codefile, 709 "size_t ret = 0;\n" 710 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n" 711 "int e HEIMDAL_UNUSED_ATTRIBUTE;\n"); 712 if (preserve) 713 fprintf (codefile, "const unsigned char *begin = p;\n"); 714 715 fprintf (codefile, "\n"); 716 fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */ 717 718 decode_type ("data", s->type, 0, "goto fail", "Top", NULL, 1); 719 if (preserve) 720 fprintf (codefile, 721 "data->_save.data = calloc(1, ret);\n" 722 "if (data->_save.data == NULL) { \n" 723 "e = ENOMEM; goto fail; \n" 724 "}\n" 725 "data->_save.length = ret;\n" 726 "memcpy(data->_save.data, begin, ret);\n"); 727 fprintf (codefile, 728 "if(size) *size = ret;\n" 729 "return 0;\n"); 730 fprintf (codefile, 731 "fail:\n" 732 "free_%s(data);\n" 733 "return e;\n", 734 s->gen_name); 735 break; 736 default: 737 abort (); 738 } 739 fprintf (codefile, "}\n\n"); 740} 741