1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
|
/*
* compiler/core/normalize.c
*
* 1. swap COMPONENTS OF for actual types
* - do this since save lots of special case handling in
* code generation
*
* 2. change SEQUENCE OF/SET OF (type def (not ref))
* to SEQUENCE OF/SEQ OF (type ref)
* and add type def for orig.
* - do this since OF type are AsnList
*
* 3. change CHOICE defs within other constructed types
* into CHOICE refs
* - makes code production easier. can be changed
* with some work
*
* 4. change SEQUENCE/SET defs within other constructed types
* into SEQUENCE/SET refs
* - makes code production easier. can be changed
* with some work (allocation in decode is wrong
* - isPtr set incorrectly)
*
* 5. change SELECTION types to the actual field from the choice
*
* 6. convert Linked oid's with value refs into a ENC_OID's
* so values can be easily defined in C/C++. MS 92/03/01
*
* 7. if IMPLICIT-TAGS is specified, mark type references
* as implicit, if the ref'd type is not CHOICE or ANY.
* (Extra tags on primitives (ie not references) are already
* removed in the parsing step (asn1.yacc)).
*
* 8. SET OF/SEQ OF defs nested in other SETs/SEQ/CHOICEs/SET OF/SEQ OF
* types are moved to separate type defs - added 08/92 to support
* C++ lists more easily.
*
* 9. INTEGERs with named elmts and ENUM defs nested in other
* SETs/SEQ/CHOICEs/SET OF/SEQ OF types are moved to separate type
* defs - added 08/92 to support C++ class hierarchy better.
*
* ******** 10 is no longer done - in fact it was stupid for ******
* ******** ANY DEFINED BY types MS 09/92 ******
* 10. Move ANY and ANY DEFINED BY type defs nested in SET/SEQ/CHOICE/SET OF
* /SEQ OF to a separate definition - this should make fixing the
* produced code simpler.
*
* Mike Sample
* 91/12/12
*
* Copyright (C) 1991, 1992 Michael Sample
* and the University of British Columbia
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* $Header: /usr/app/odstb/CVS/snacc/compiler/core/normalize.c,v 1.3 1995/07/25 19:41:40 rj Exp $
* $Log: normalize.c,v $
* Revision 1.3 1995/07/25 19:41:40 rj
* changed `_' to `-' in file names.
*
* Revision 1.2 1994/09/01 00:40:56 rj
* snacc_config.h removed.
*
* Revision 1.1 1994/08/28 09:49:23 rj
* first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
*
*/
#include <stdio.h>
#include "asn-incl.h"
#include "mem.h"
#include "asn1module.h"
#include "oid.h"
#include "lib-types.h"
#include "snacc-util.h"
#include "normalize.h"
#define LIST_ELMT_SUFFIX "ListElmt"
#define CHOICE_SUFFIX "Choice"
#define SET_SUFFIX "Set"
#define SEQ_SUFFIX "Seq"
#define SETOF_SUFFIX "SetOf"
#define SEQOF_SUFFIX "SeqOf"
#define INT_SUFFIX "Int"
#define ENUM_SUFFIX "Enum"
#define BITS_SUFFIX "Bits"
#define ANY_SUFFIX "Any"
long int oidRecursionCountG = 0;
void NormalizeTypeDef PROTO ((Module *m, TypeDef *td));
void NormalizeType PROTO ((Module *m, TypeDef *td, Type *parent, NamedTypeList *e, Type *t));
void NormalizeElmtTypes PROTO ((Module *m, TypeDef *td, Type *parent, NamedTypeList *e));
void NormalizeBasicType PROTO ((Module *m, TypeDef *td, Type *parent, NamedTypeList *e, Type *type, BasicType *bt));
TypeDef *AddListElmtTypeDef PROTO ((Module *m, TypeDef *td, Type *t, BasicType *bt));
TypeDef *AddConsTypeDef PROTO ((Module *m, TypeDef *td, Type *t, BasicType *bt, char *suffix));
void NormalizeValueDef PROTO ((Module *m, ValueDef *vd));
int FlattenLinkedOid PROTO ((OID *o, char *asn1FileName, AsnInt lineNo, int quiet));
/*
* looks through the given module and performs the operations
* mentioned above
*/
void
NormalizeModule PARAMS ((m),
Module *m)
{
TypeDef *td;
ValueDef *vd;
/*
* go through each type in typeList
*/
FOR_EACH_LIST_ELMT (td, m->typeDefs)
{
NormalizeTypeDef (m, td);
}
/*
* go through each value for types?
*/
FOR_EACH_LIST_ELMT (vd, m->valueDefs)
{
NormalizeValueDef (m, vd);
}
} /* NormalizeModule */
void
NormalizeTypeDef PARAMS ((m, td),
Module *m _AND_
TypeDef *td)
{
if (td == NULL)
return;
NormalizeType (m, td, NULL, NULL, td->type);
} /* NormalizeTypeDef */
void
NormalizeType PARAMS ((m, td, parent, e, t),
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
NamedTypeList *e _AND_
Type *t)
{
enum BasicTypeChoiceId typeId;
Type *tmpType;
Tag *lastTag;
if (t == NULL)
return;
NormalizeBasicType (m, td, parent, e, t, t->basicType);
/*
* make type refs implicit if IMPLICIT-TAGS specified and
* ref'd type is OK for implicit tagging.
* Tag removal work is done in parsing (yacc).
*/
if ((m->tagDefault == IMPLICIT_TAGS))
{
tmpType = ParanoidGetType (t);
typeId = tmpType->basicType->choiceId;
if ((t->tags != NULL) && (!LIST_EMPTY (t->tags)))
lastTag = (Tag*)LAST_LIST_ELMT (t->tags);
else
lastTag = NULL;
/*
* only mark as implicit if
* 1. This type has a tag in it's taglist
* 2. This type is a reference to another type
* 3. the referenced type is not an untagged CHOICE, ANY or
* ANY DEFINED BY (just need to check that it has
* tags since all other types have tags)
*/
if (((lastTag != NULL) && !(lastTag->explicit)) &&
((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
(t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF)) &&
(CountTags (t->basicType->a.localTypeRef->link->type) != 0))
{
t->implicit = TRUE;
}
}
} /* NormalizeType */
void
NormalizeElmtTypes PARAMS ((m, td, parent, e),
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
NamedTypeList *e)
{
NamedType *nt;
FOR_EACH_LIST_ELMT (nt, e)
{
NormalizeType (m, td, parent, e, nt->type);
}
} /* NormalizeElmtTypes */
/*
* this is where most of the action happens
* assumes that "e"'s curr ptr is namedtype that holds "type"
*/
void
NormalizeBasicType PARAMS ((m, td, parent, e, type, bt),
Module *m _AND_
TypeDef *td _AND_
Type *parent _AND_
NamedTypeList *e _AND_
Type *type _AND_
BasicType *bt)
{
int i, numElmtsAdded;
NamedType *newElmt;
NamedType **newElmtHndl;
NamedType *nt;
NamedTypeList *elmts;
NamedType *origNext;
Type *compType;
Type *parentType;
TypeDef *newDef;
BasicType *tmpBasicType;
TagList *tags;
Tag *tag;
Tag **tagHndl;
if (bt == NULL)
return;
switch (bt->choiceId)
{
case BASICTYPE_COMPONENTSOF:
/*
* copy elmts of COMPONENTS OF type into this type
*/
if (parent == NULL)
{
PrintErrLoc (m->asn1SrcFileName, type->lineNo);
fprintf (stderr,"ERROR - COMPONENTS OF must be a SET or SEQUENCE element\n");
m->status = MOD_ERROR;
return;
}
compType = ParanoidGetType (bt->a.componentsOf);
parentType = ParanoidGetType (parent);
/* COMPONENTS OF must be nested in a SET or SEQUENCE type */
if ((parentType->basicType->choiceId != BASICTYPE_SET) &&
(parentType->basicType->choiceId != BASICTYPE_SEQUENCE))
{
PrintErrLoc (m->asn1SrcFileName, type->lineNo);
fprintf (stderr,"ERROR - COMPONENTS OF must be a SET or SEQUENCE element\n");
m->status = MOD_ERROR;
return;
}
/* COMPONENTS OF in a SET must ref a SET and vice versa for SEQ */
if (((parentType->basicType->choiceId == BASICTYPE_SET) &&
(compType->basicType->choiceId != BASICTYPE_SET)) ||
((parentType->basicType->choiceId == BASICTYPE_SEQUENCE) &&
(compType->basicType->choiceId != BASICTYPE_SEQUENCE)))
{
PrintErrLoc (m->asn1SrcFileName, type->lineNo);
fprintf (stderr,"ERROR - COMPONENTS OF in a SET must reference a SET type and COMPONENTS OF in SEQUENCE must reference a SEQUENCE type\n");
type->basicType = compType->basicType;
m->status = MOD_ERROR;
return;
}
/*
* replace "COMPONENTS OF" with elmts from ref'd set
*/
elmts = compType->basicType->a.set;
if (elmts == NULL)
break;
/*
* add new list elmts that point to elmts
* of type ref'd by COMPONENTS OF
*/
FOR_EACH_LIST_ELMT (nt, elmts)
{
newElmtHndl = (NamedType**)AsnListAdd (e);
*newElmtHndl = nt;
}
/*
* Set e list's curr ptr to first of of the
* newly added components.
* Do this so NormalizeElmtTypes will do the
* newly added ones as well
*/
numElmtsAdded = AsnListCount (elmts);
for (i = 0; i < numElmtsAdded; i++)
AsnListPrev (e);
/* remove the componets of ref since elmts copied in */
AsnListRemove (e);
break;
case BASICTYPE_SELECTION:
/*
* first normalize the CHOICE that is selected from
* - this will be done twice to the CHOICE but nothing
* bad should happen. The main reason for 'normalizing'
* the CHOICE first is to strip tags from the choice elmts
* if IMPLICIT-TAGS is set.
* NOTE: this call assumes that import/local type refs
* both use the 'TypeRef' struct and that a selection references
* a CHOICE by name (not definition)
*/
NormalizeType (m, type->basicType->a.selection->typeRef->basicType->a.localTypeRef->link, NULL, NULL, type->basicType->a.selection->typeRef->basicType->a.localTypeRef->link->type);
/*
* use SELECTION field name if this is an elmt type with no
* field name.
*/
if ((e != NULL) &&
(((NamedType*) e->curr->data)->fieldName == NULL))
((NamedType*) e->curr->data)->fieldName =
type->basicType->a.selection->link->fieldName;
/*
* replace SELECTION type with refd type.
* must append the named CHOICE field's tags to
* any existing tags on this SELECTION type.
*/
tmpBasicType = type->basicType->a.selection->link->type->basicType;
tags = type->basicType->a.selection->link->type->tags;
FOR_EACH_LIST_ELMT (tag, tags)
{
if (!(((m->tagDefault == IMPLICIT_TAGS) || (type->implicit)) &&
(tag == (Tag*)FIRST_LIST_ELMT (tags))))
{
tagHndl = (Tag**) AsnListAppend (type->tags);
*tagHndl = tag;
}
type->implicit = FALSE;
}
if (type->basicType->a.selection->link->type->implicit)
type->implicit = TRUE;
Free (type->basicType->a.selection->fieldName);
Free (type->basicType->a.selection->typeRef->basicType);
Free (type->basicType->a.selection->typeRef);
type->basicType = tmpBasicType;
break;
case BASICTYPE_SEQUENCEOF:
case BASICTYPE_SETOF:
/* convert def inside other type into a ref */
if (td->type != type)
{
if (bt->choiceId == BASICTYPE_SETOF)
newDef = AddConsTypeDef (m, td, type, bt, SETOF_SUFFIX);
else
newDef = AddConsTypeDef (m, td, type, bt, SEQOF_SUFFIX);
NormalizeType (m, newDef, NULL, NULL, newDef->type);
}
else
NormalizeType (m, td, type, NULL, type->basicType->a.setOf);
break;
/* NOT NEEDED ANY MORE
* convert typdef after SET OF/SEQ OF to type REFS
switch (bt->a.setOf->basicType->choiceId)
{
case BASICTYPE_SEQUENCE:
case BASICTYPE_SET:
case BASICTYPE_CHOICE:
case BASICTYPE_SEQUENCEOF:
case BASICTYPE_SETOF:
case BASICTYPE_COMPONENTSOF:
newDef = AddListElmtTypeDef (m, td, type, bt);
NormalizeType (m, newDef, NULL, NULL, newDef->type);
break;
default:
NormalizeType (m, td, NULL, NULL, bt->a.setOf);
break;
}
*/
break;
case BASICTYPE_CHOICE:
/*
* change CHOICE defs embedded in other types
* into type refs
*/
if (td->type != type)
{
newDef = AddConsTypeDef (m, td, type, bt, CHOICE_SUFFIX);
NormalizeType (m, newDef, NULL, NULL, newDef->type);
}
else
NormalizeElmtTypes (m, td, type, bt->a.set);
break;
case BASICTYPE_SEQUENCE:
/*
* change SEQ defs embedded in other types
* into type refs
*/
if (td->type != type)
{
newDef = AddConsTypeDef (m, td, type, bt, SEQ_SUFFIX);
NormalizeType (m, newDef, NULL, NULL, newDef->type);
}
else
NormalizeElmtTypes (m, td, type, bt->a.sequence);
break;
case BASICTYPE_SET:
/*
* change SET defs embedded in other types
* into type refs
*/
if (td->type != type)
{
newDef = AddConsTypeDef (m, td, type, bt, SET_SUFFIX);
NormalizeType (m, newDef, NULL, NULL, newDef->type);
}
else
NormalizeElmtTypes (m, td, type, bt->a.set);
break;
case BASICTYPE_INTEGER:
/* if they have named elements convert this def into a ref */
if ((td->type != type) && (bt->a.integer != NULL) &&
(!LIST_EMPTY (bt->a.integer)))
{
newDef = AddConsTypeDef (m, td, type, bt, INT_SUFFIX);
}
break;
case BASICTYPE_ENUMERATED:
/* if they have named elements convert this def into a ref */
if ((td->type != type) && (bt->a.enumerated != NULL) &&
(!LIST_EMPTY (bt->a.enumerated)))
{
newDef = AddConsTypeDef (m, td, type, bt, ENUM_SUFFIX);
}
break;
case BASICTYPE_BITSTRING:
/* if they have named elements convert this def into a ref */
if ((td->type != type) && (bt->a.bitString != NULL) &&
(!LIST_EMPTY (bt->a.bitString)))
{
newDef = AddConsTypeDef (m, td, type, bt, BITS_SUFFIX);
}
break;
case BASICTYPE_ANY:
case BASICTYPE_ANYDEFINEDBY:
m->hasAnys = TRUE;
/* NO LONGER DONE
* change ANY defs embedded in other types
* into type refs
if (td->type != type)
newDef = AddConsTypeDef (m, td, type, bt, ANY_SUFFIX);
*/
break;
default:
/* the rest are not processed */
break;
}
} /* NormalizeBasicType */
/*
* given a set of/seq of type t within typedef td, change the
* set of /seq of elmt type def into a type ref and
* add a type def for the elmt at the top level.
*/
TypeDef*
AddListElmtTypeDef PARAMS ((m, td, t, bt),
Module *m _AND_
TypeDef *td _AND_
Type *t _AND_
BasicType *bt)
{
TypeDef *newDef;
TypeDef **typeDefHndl;
int end;
int digit;
/*
* make new type def
*/
newDef = (TypeDef*)Malloc (sizeof (TypeDef));
newDef->exported = FALSE;
newDef->type = bt->a.setOf;
/*
* make name for new type
* Foo ::= SET OF SEQUENCE {...}
* -->
* FooListElmt ::= SEQUENCE {...}
* Foo ::= SET OF FooListElmt
*/
newDef->definedName =
Malloc (strlen (td->definedName) +
strlen (LIST_ELMT_SUFFIX) + 4);
strcpy (newDef->definedName, td->definedName);
strcat (newDef->definedName, LIST_ELMT_SUFFIX);
end = strlen (newDef->definedName);
digit = 1;
while (LookupType (m->typeDefs, newDef->definedName) != NULL)
{
newDef->definedName[end] = '\0';
AppendDigit (newDef->definedName, digit++);
}
/*
* now put new type at head of list
*/
typeDefHndl = (TypeDef**)AsnListPrepend (m->typeDefs);
*typeDefHndl = newDef;
/*
* replace SET OF/SEQ OF body with type ref
*/
bt->a.setOf = (Type*)Malloc (sizeof (Type));
bt->a.setOf->optional = FALSE;
bt->a.setOf->implicit = FALSE;
bt->a.setOf->lineNo = t->lineNo;
bt->a.setOf->basicType = (BasicType*)Malloc (sizeof (BasicType));
bt->a.setOf->basicType->choiceId = BASICTYPE_LOCALTYPEREF;
bt->a.setOf->basicType->a.localTypeRef = (TypeRef*)Malloc (sizeof (TypeRef));
bt->a.setOf->basicType->a.localTypeRef->link = newDef;
bt->a.setOf->basicType->a.localTypeRef->typeName =
newDef->definedName;
bt->a.setOf->basicType->a.localTypeRef->moduleName = NULL;
return newDef;
} /* AddListElmtTypeDefs */
/*
* given a CHOICE/SET/SEQ/etc type t within typedef td, make t into a ref
* to a new top level typdef of the CHOICE/SET/SEQ
*/
TypeDef*
AddConsTypeDef PARAMS ((m, td, t, bt, suffix),
Module *m _AND_
TypeDef *td _AND_
Type *t _AND_
BasicType *bt _AND_
char *suffix)
{
TypeDef *newDef;
TypeDef **typeDefHndl;
Tag **tmpPtr;
Tag *lastTag;
int end;
int digit;
/*
* make new type def
*/
newDef = (TypeDef*)Malloc (sizeof (TypeDef));
newDef->exported = FALSE;
newDef->recursive = FALSE;
newDef->localRefCount = 1;
newDef->type = (Type*)Malloc (sizeof (Type));
newDef->type->optional = FALSE;
newDef->type->lineNo = t->lineNo;
newDef->type->basicType = bt;
/*
* make name for new choice/SET/SEQ
* Foo ::= SEQUENCE { .., bar CHOICE { ...}, ..}
* -->
* FooChoice ::= CHOICE { ...}
* Foo ::= SEQUENCE { .., bar FooChoice, .. }
*/
newDef->definedName =
Malloc (strlen (td->definedName) +
strlen (suffix) + 4);
strcpy (newDef->definedName, td->definedName);
strcat (newDef->definedName, suffix);
end = strlen (newDef->definedName);
digit = 1;
/* keep name unique */
while (LookupType (m->typeDefs, newDef->definedName) != NULL)
{
newDef->definedName[end] = '\0';
AppendDigit (newDef->definedName, digit++);
}
/*
* now put new type at head of list
*/
typeDefHndl = (TypeDef**)AsnListPrepend (m->typeDefs);
*typeDefHndl = newDef;
/*
* what to do with tags? Use default universal type on
* newly defined type and adjust (new) reference's tags
* appropriately
*
* NOTE: may be simpler just to move all the tags to the
* new def.
*/
newDef->type->tags = (TagList*)AsnListNew (sizeof (void*));
if (LIBTYPE_GET_UNIV_TAG_CODE ((newDef->type->basicType->choiceId))
!= NO_TAG_CODE)
{
tmpPtr = (Tag**)AsnListAppend (newDef->type->tags);
*tmpPtr = (Tag*)Malloc (sizeof (Tag));
(*tmpPtr)->tclass = UNIV;
(*tmpPtr)->code = LIBTYPE_GET_UNIV_TAG_CODE ((newDef->type->basicType->choiceId));
/* adjust tags of new ref to new def */
if ((t->tags != NULL) && (!LIST_EMPTY (t->tags)))
{
lastTag = (Tag*)LAST_LIST_ELMT (t->tags);
if ((lastTag->tclass == UNIV) &&
(lastTag->code ==
LIBTYPE_GET_UNIV_TAG_CODE ((newDef->type->basicType->choiceId))))
{
/* zap it since same as default universal tag */
SET_CURR_LIST_NODE (t->tags, LAST_LIST_NODE (t->tags));
AsnListRemove (t->tags);
t->implicit = FALSE;
}
else
{
t->implicit = TRUE; /* this will probably already be true */
}
}
}
/*
* replace embeded CHOICE/SET/SEQ def with ref to newly defined type
*/
t->basicType = (BasicType*)Malloc (sizeof (BasicType));
t->basicType->choiceId = BASICTYPE_LOCALTYPEREF;
t->basicType->a.localTypeRef = (TypeRef*)Malloc (sizeof (TypeRef));
t->basicType->a.localTypeRef->link = newDef;
t->basicType->a.localTypeRef->typeName =
newDef->definedName;
t->basicType->a.localTypeRef->moduleName = NULL;
return newDef;
} /* AddConsTypeDef */
void
NormalizeValueDef PARAMS ((m, vd),
Module *m _AND_
ValueDef *vd)
{
NormalizeValue (m, vd, vd->value, FALSE);
}
void
NormalizeValue PARAMS ((m, vd, v, quiet),
Module *m _AND_
ValueDef *vd _AND_
Value *v _AND_
int quiet)
{
AsnOid *eoid;
OID *o;
OID *tmp;
int eLen;
/*
* convert linked oids into ENC_OID's
*/
if (v->basicValue->choiceId == BASICVALUE_LINKEDOID)
{
if (!FlattenLinkedOid (v->basicValue->a.linkedOid, m->asn1SrcFileName, v->lineNo, quiet))
return;
eLen = EncodedOidLen (v->basicValue->a.linkedOid);
eoid = MT (AsnOid);
eoid->octetLen = eLen;
eoid->octs = (char*)Malloc (eLen);
BuildEncodedOid (v->basicValue->a.linkedOid, eoid);
/* free linked oid */
for (o = v->basicValue->a.linkedOid; o != NULL; )
{
tmp = o->next;
Free (o);
o = tmp;
}
v->basicValue->choiceId = BASICVALUE_OID;
v->basicValue->a.oid = eoid;
}
}
/*
* replaces value refs with the value's number if poss
* returns TRUE if successfully done.
* returns FALSE if a value ref could not be traced
* (false should not happen if the value link succeeded)
* "quiet" parameter allows err msg to be turned off
* which prevents cascading errors by other oid's that
* reference a bad oid.
*/
int
FlattenLinkedOid PARAMS ((o, asn1FileName, lineNo, quiet),
OID *o _AND_
char *asn1FileName _AND_
AsnInt lineNo _AND_
int quiet)
{
OID *firstElmt;
OID *refdOid;
OID *tmpOid;
OID **nextOid;
Value *val;
Value *valRef;
if (oidRecursionCountG > 100)
{
PrintErrLoc (asn1FileName, lineNo);
fprintf (stderr,"ERROR - recursive OBJECT IDENTIFIER value.\n");
return FALSE;
}
firstElmt = o;
for (; o != NULL; o = o->next)
{
valRef = o->valueRef;
if ((valRef == NULL) || (o->arcNum != NULL_OID_ARCNUM))
continue; /* no linking nec for this one */
val = GetValue (o->valueRef);
/*
* if the very first component is an oid val ref
* then insert that value
*/
if ((o == firstElmt) && (val->basicValue->choiceId == BASICVALUE_OID))
{
UnbuildEncodedOid (val->basicValue->a.oid, &refdOid);
for (tmpOid = refdOid; tmpOid->next != NULL; tmpOid = tmpOid->next)
;
tmpOid->next = o->next;
memcpy (firstElmt, refdOid, sizeof (OID));
Free (refdOid); /* free first component of OID since copied */
}
else if ((o == firstElmt) && (val->basicValue->choiceId == BASICVALUE_LINKEDOID))
{
oidRecursionCountG++;
if (!FlattenLinkedOid (val->basicValue->a.linkedOid, asn1FileName, lineNo, TRUE))
{
oidRecursionCountG--;
return FALSE;
}
oidRecursionCountG--;
nextOid = &refdOid;
for (tmpOid = val->basicValue->a.linkedOid;
tmpOid != NULL; tmpOid = tmpOid->next)
{
*nextOid = (OID*)Malloc (sizeof (OID));
(*nextOid)->arcNum = tmpOid->arcNum;
nextOid = &(*nextOid)->next;
}
(*nextOid) = o->next;
memcpy (firstElmt, refdOid, sizeof (OID));
Free (refdOid); /* since copied into firstElmt */
}
else if ((val->basicValue->choiceId == BASICVALUE_INTEGER))
{
o->arcNum = val->basicValue->a.integer;
if ((o->arcNum < 0) && !quiet)
{
PrintErrLoc (asn1FileName, lineNo);
fprintf (stderr,"ERROR - OBJECT IDENTIFIER arc values cannot be negative.\n");
}
}
else /* bad arc value type */
{
if (!quiet)
{
PrintErrLoc (asn1FileName, lineNo);
fprintf (stderr,"ERROR - type mismatch for an arc value. Values ref'd from an OBJECT IDENTIFIER value must be either an OBJECT IDENTIFIER (first oid elmt only) or INTEGER value (this may be reported twice!)\n");
}
return FALSE;
}
/* free mem assoc with value ref */
Free (valRef->basicValue->a.localValueRef->valueName);
Free (valRef->basicValue->a.localValueRef);
Free (valRef->basicValue);
Free (valRef);
o->valueRef = NULL;
}
return TRUE;
} /* FlattenLinkedOid */
|