File: snacc-util.c

package info (click to toggle)
esnacc 1.8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,452 kB
  • sloc: ansic: 42,340; cpp: 13,880; yacc: 2,682; tcl: 1,587; lex: 688; sh: 573; makefile: 111
file content (1697 lines) | stat: -rw-r--r-- 43,256 bytes parent folder | download | duplicates (5)
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
/*
 * compiler/core/snacc_util.c
 *
 * utilities for dealing with the Module data structure
 *
 * AUTHOR: Mike Sample
 * DATE:   91/09/02
 *
 * 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: /baseline/SNACC/compiler/core/snacc-util.c,v 1.15 2004/04/06 15:13:41 gronej Exp $
 * $Log: snacc-util.c,v $
 * Revision 1.15  2004/04/06 15:13:41  gronej
 * *** empty log message ***
 *
 * Revision 1.14  2004/03/25 19:20:17  gronej
 * fixed some linux warnings
 *
 * Revision 1.13  2004/01/14 19:07:53  gronej
 * Updated Compiler to accept and process relative-oid's
 *
 * Revision 1.12  2003/07/28 11:11:24  colestor
 * Changes to complete handing of the "--snacc namespace" compiler directive.
 * Also, updates to handle ASN.1 constant integer tag designations for C++/C.
 *
 * Revision 1.11  2003/07/07 14:50:14  nicholar
 * Eliminated headers and cleaned up include references
 *
 * Revision 1.10  2003/04/29 21:07:14  leonberp
 * integerated Deepak's changes for IOB support
 *
 * Revision 1.9  2003/01/27 16:50:45  leonberp
 * added GetLastNamedNumberValue()
 *
 * Revision 1.8  2002/10/28 20:50:58  leonberp
 * Ok.. I think this is the final fix for OCTET/BIT STRING CONTAINING
 *
 * Revision 1.7  2002/10/24 21:07:22  leonberp
 * latest fixing for OCTET CONTAINING
 *
 * Revision 1.6  2002/10/21 17:15:19  mcphersc
 * fixed long int
 *
 * Revision 1.5  2002/09/16 16:50:27  mcphersc
 * Fixed warnings
 *
 * Revision 1.4  2002/09/04 18:31:39  vracarl
 * got rid of c++ comments
 *
 * Revision 1.3  2002/05/15 17:01:01  leonberp
 * added support for new basicTypes to compiler
 *
 * Revision 1.2  2000/10/24 14:54:55  rwc
 * Updated to remove high-level warnings (level 4 on MSVC++) for an easier build.
 * SOME warnings persist due to difficulty in modifying the SNACC compiler to
 * properly build clean source; also some files are built by Lex/Yacc.
 *
 * Revision 1.1.1.1  2000/08/21 20:36:01  leonberp
 * First CVS Version of SNACC.
 *
 * Revision 1.3  1995/07/25 19:41:44  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.2  1994/09/01  00:45:09  rj
 * snacc_config.h removed.
 *
 * Revision 1.1  1994/08/28  09:49:39  rj
 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
 *
 */

#include <string.h>
#include "asn-incl.h"
#include "asn1module.h"
#include "lib-types.h"
#include "define.h"
#include "snacc-util.h"


extern FILE* errFileG;		// Defined in snacc.c


/*
 * Allocates and initializes a type and it's basicType info
 * used extensively by asn1.yacc
 * (was a macro)
 */
void
SetupType PARAMS ((t, typeId, lineNum),
    Type **t _AND_
    enum BasicTypeChoiceId typeId _AND_
    unsigned long lineNum)
{
    Tag **tmpPtr;

    (*t) = (Type*)Malloc (sizeof (Type));
    (*t)->lineNo = lineNum;
    (*t)->basicType = (BasicType*)Malloc (sizeof (BasicType));
    (*t)->basicType->choiceId = typeId;
    (*t)->tags = (TagList*)AsnListNew (sizeof (void*));
    if (LIBTYPE_GET_UNIV_TAG_CODE ((typeId)) != NO_TAG_CODE)
    {
         tmpPtr = (Tag**)AsnListAppend ((AsnList*)(*t)->tags);
         *tmpPtr = (Tag*)Malloc (sizeof (Tag));
         (*tmpPtr)->tclass = UNIV;
         (*tmpPtr)->code = LIBTYPE_GET_UNIV_TAG_CODE ((typeId));
    }
} /* SetupType */


/*
 * Allocates and initializes a type and it's basicType to MACROTYPE
 * and sets the MACROTYPE type to the given macrotype
 */
void
SetupMacroType PARAMS ((t, macroTypeId, lineNum),
    Type **t _AND_
    enum MacroTypeChoiceId macroTypeId _AND_
    unsigned long lineNum)
{

    (*t) = MT (Type);
    (*t)->lineNo = lineNum;
    (*t)->basicType = MT (BasicType);
    (*t)->basicType->choiceId = BASICTYPE_MACROTYPE;
    (*t)->tags = (TagList*)AsnListNew (sizeof (void*));
    (*t)->basicType->a.macroType = MT (MacroType);
    (*t)->basicType->a.macroType->choiceId = macroTypeId;
} /* SetupMacroType */


/*
 * similar to SetupType but for values instead
 */
void
SetupValue PARAMS ((v, valId, lineNum),
    Value **v _AND_
    enum BasicValueChoiceId valId _AND_
    unsigned long lineNum)
{
    *v = (Value*)Malloc (sizeof (Value));
    (*v)->basicValue = (BasicValue*)Malloc (sizeof (BasicValue));
    (*v)->basicValue->choiceId = valId;
    (*v)->lineNo = lineNum;
} /* SetupValue */


/*
 * adds elmt with given name to module m's import list from
 * the module with name refdModuleName.  If module m does not
 * have an import list from that module one is created.
 * The import element is given the private scope implied
 * by the ASN.1 modname.typ-or-val-name reference format
 * The passed in strings (name, refdModuleName) are copied.
 */
void
AddPrivateImportElmt PARAMS ((m, name, refdModuleName, lineNo),
    Module *m _AND_
    char *name _AND_
    char *refdModuleName _AND_
    long  lineNo)
{
    ImportElmt   *newElmt;
    ImportElmt   *ie;
    ImportModule *impMod;


    /* see if module m already imports something from "refdModule" */
    if ((impMod = LookupImportModule (m, refdModuleName)) == NULL)
    {
        impMod = MT (ImportModule);
        impMod->modId = MT (ModuleId);
        impMod->modId->name = Malloc (strlen (refdModuleName)+1);
        strcpy (impMod->modId->name, refdModuleName);

        newElmt = MT (ImportElmt);
        newElmt->name = Malloc (strlen (name)+1);
        strcpy (newElmt->name, name);
        newElmt->privateScope = TRUE;

        APPEND (newElmt, impMod->importElmts);
        APPEND (impMod, m->imports);
    }
    else  /* module "refdModule is already imported from */
    {
        ie = LookupImportElmtInImportElmtList (impMod->importElmts, name);

        if (ie == NULL)
        {
            newElmt  = MT (ImportElmt);
            newElmt->name = Malloc (strlen (name)+1);
            strcpy (newElmt->name, name);
            APPEND (newElmt, impMod->importElmts);
        }
        else if (!ie->privateScope)
        {
            PrintErrLoc (m->asn1SrcFileName, (long)lineNo);
            fprintf (errFileG, "WARNING - \"%s.%s\" type/value reference refers to a type/value already in the import list that does not have private scope.\n",
				refdModuleName, name);
        }
    }
} /* AddPrivateImportElmt */

/*
 *   looks for the named import type/value in all of the IMPORT lists of the
 *   given module.
 *   RETURNS a ptr to the import elmt if found, NULL if it was not found.
 *   If the item was found (ptr returned) the foundImportModule addr
 *   parameter will be set to the module's importModule that holds
 *   the found elmt.
 *
 *   returns NULL if the named import name was not found
 *
 */
ImportElmt*
LookupImportElmtInModule PARAMS ((m, name, foundImportModule),
    Module *m _AND_
    char *name _AND_
    ImportModule **foundImportModule)
{
     ImportModule *importMod;
     ImportElmt *importElmt;
     ImportElmt *retVal;
     void *tmp;

     if (m->imports == NULL)
         return NULL;

     tmp = (void*)CURR_LIST_NODE (m->imports);
     retVal = NULL;
     FOR_EACH_LIST_ELMT (importMod, m->imports)
     {
         importElmt = LookupImportElmtInImportElmtList (importMod->importElmts, name);
         if (importElmt != NULL)
         {
             *foundImportModule = importMod;
             retVal = importElmt;
             break;
         }
     }

     SET_CURR_LIST_NODE (m->imports, tmp); /* restore orig loc */
     return retVal;

}  /* LookupImportElmtInModule */


/*
 * given a list of import elmts, returns ptr to the elmt with
 * the matching name. NULL if not found
 */
ImportElmt*
LookupImportElmtInImportElmtList PARAMS ((impElmtList, name),
    ImportElmtList *impElmtList _AND_
    char *name)
    
{
     ImportElmt *impElmt;
     ImportElmt *retVal;
     void *tmp;

     if (impElmtList == NULL)
         return NULL;

     tmp = (void*) CURR_LIST_NODE (impElmtList);
     retVal = NULL;
     FOR_EACH_LIST_ELMT (impElmt, impElmtList)
     {
         if (strcmp (impElmt->name, name) == 0)
         {
             retVal = impElmt;
             break;
         }
     }

     SET_CURR_LIST_NODE (impElmtList, tmp);
     return retVal;

}  /* LookupImportElmtInImportElmtList */




/*
 *   looks for an import list that imports from "importModuleName"
 *   module in the given module.
 *
 *   returns a ptr to the ImportList if found
 *   returns NULL if not found
 */
ImportModule*
LookupImportModule PARAMS ((m, importModuleName),
    Module *m _AND_
    char *importModuleName)
{
    ImportModule *importModule;
    ImportModule *retVal;
    void *tmp;

    if (m->imports == NULL)
        return NULL;

    tmp = (void*)CURR_LIST_NODE (m->imports);
    retVal = NULL;
    FOR_EACH_LIST_ELMT (importModule, m->imports)
    {
        if (strcmp (importModule->modId->name, importModuleName) == 0)
        {
            retVal= importModule;
            break;
        }
    }

    SET_CURR_LIST_NODE (m->imports, tmp);
    return retVal;

}  /* LookupImportModule */




/*
 *  Looks for the type with name matching typeName (null terminated char*)
 *  in the given the TypeDef list
 *
 *  RETURNS: ptr to the TypeDef with the matching typeName (if any)
 *           NULL if no match was made
 */
TypeDef*
LookupType PARAMS ((typeDefList, typeName),
    TypeDefList *typeDefList _AND_
    char *typeName)
{
    TypeDef *td;
    TypeDef *retVal;
    void *tmp;

    if (typeDefList == NULL)
        return NULL;

    if (typeName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    tmp = (void*)CURR_LIST_NODE (typeDefList); /* remember curr list spot */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (td, typeDefList)
    {
        if (strcmp (typeName, td->definedName) == 0)
        {
            retVal = td;
            break;
        }
    }

    SET_CURR_LIST_NODE (typeDefList,tmp); /* restore curr location */

    return retVal;

}  /* LookupType */




/*	Deepak: 04/Feb/2003
 *  Looks for the type with name matching typeName (null terminated char*)
 *  in the given NamedTypeList
 *
 *  RETURNS: ptr to the NamedType with the matching typeName (if any)
 *           NULL if no match was made
 */
NamedType*
LookupObjectClassFieldType PARAMS ((namedTypeList, typeName),
    NamedTypeList *namedTypeList _AND_
    char *typeName)
{
    NamedType *nt;
    NamedType *retVal;
    void *tmp;

    if (namedTypeList == NULL)
        return NULL;

    if (typeName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupObjectClassFieldType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    tmp = (void*)CURR_LIST_NODE (namedTypeList); /* remember curr list spot */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (nt, namedTypeList)
    {
        if (strcmp (typeName, nt->fieldName) == 0)
        {
            retVal = nt;
            break;
        }
    }

    SET_CURR_LIST_NODE (namedTypeList,tmp); /* restore curr location */

    return retVal;

}  /* LookupObjectClassFieldType */




/*	Deepak: 05/Mar/2003
 *  Looks for the type with name matching typeName according to With Syntax (null terminated char*)
 *  in the given NamedTypeList
 *
 *  RETURNS: ptr to the WithSyntax with the matching typeName (if any)
 *           NULL if no match was made
 */
WithSyntax*
LookupObjectClassFieldTypeWithSyntax PARAMS ((withSyntaxList, typeName, bError),
    WithSyntaxList *withSyntaxList _AND_
    char *typeName _AND_
	char* bError)
{
    WithSyntax *ws;
    WithSyntax *retVal;
    void *tmp;

    if (withSyntaxList == NULL)
        return NULL;

    if (typeName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupObjectClassFieldType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    tmp = (void*)CURR_LIST_NODE (withSyntaxList); /* remember curr list spot */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (ws, withSyntaxList)
    {
        if (strcmp (typeName, ws->definedName) == 0)
        {
            retVal = ws;
			*bError = FALSE;
            break;
        }
        else if (strcmp (typeName, ws->typeName) == 0)
        {
            retVal = ws;
			*bError = TRUE;
            break;
        }
    }

    SET_CURR_LIST_NODE (withSyntaxList,tmp); /* restore curr location */

    return retVal;

}  /* LookupObjectClassFieldTypeWithSyntax */


/*	Deepak: 05/Mar/2003
 *  Looks for the object with name matching objectName (null terminated char*)
 *  in the given objAssignmentList
 *
 *  RETURNS: ptr to the ObjectAssignment with the matching objectName (if any)
 *           NULL if no match was made
 */
ObjectAssignment*
LookupObjectClassObjectAssignment PARAMS ((objAssignmentList, objName),
    ObjectAssignmentList *objAssignmentList _AND_
    char *objName)
{
    ObjectAssignment *oa;
    ObjectAssignment *retVal;
    void *tmp;

    if (objAssignmentList == NULL)
        return NULL;

    if (objName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupObjectClassFieldType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    tmp = (void*)CURR_LIST_NODE (objAssignmentList); /* remember curr list spot */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (oa, objAssignmentList)
    {
        if (strcmp (objName, oa->objectName) == 0)
        {
            retVal = oa;
            break;
        }
    }

    SET_CURR_LIST_NODE (objAssignmentList,tmp); /* restore curr location */

    return retVal;

}  /* LookupObjectClassObjectAssignment */


/*	Deepak: 11/Mar/2003
 *  Looks for the ObjectSet with name matching objectName (null terminated char*)
 *  in the given objSetAssignmentList
 *
 *  RETURNS: ptr to the ObjectSetAssignment with the matching objectName (if any)
 *           NULL if no match was made
 */
ObjectSetAssignment*
LookupObjectClassObjectSetAssignment PARAMS ((objSetAssignmentList, objSetName),
    ObjectSetAssignmentList *objSetAssignmentList _AND_
    char *objSetName)
{
    ObjectSetAssignment *osa;
    ObjectSetAssignment *retVal;
    void *tmp;

    if (objSetAssignmentList == NULL)
        return NULL;

    if (objSetName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupObjectClassFieldType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    tmp = (void*)CURR_LIST_NODE (objSetAssignmentList); /* remember curr list spot */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (osa, objSetAssignmentList)
    {
        if (strcmp (objSetName, osa->objectSetName) == 0)
        {
            retVal = osa;
            break;
        }
    }

    SET_CURR_LIST_NODE (objSetAssignmentList,tmp); /* restore curr location */

    return retVal;

}  /* LookupObjectClassObjectSetAssignment */



/*
 * Returns ptr to module that has matching name or OID
 * if oid is not null, lookup done only by oid
 *
 * returns NULL if no match was found
 */
Module*
LookupModule PARAMS ((moduleList, modName, oid),
    ModuleList *moduleList _AND_
    char *modName _AND_
    OID *oid)
{
    Module *currMod;
    Module *retVal;
    void *tmp;

    if ((moduleList == NULL) || ((modName == NULL) && (oid == NULL)))
        return NULL;

    tmp = (void*)CURR_LIST_NODE (moduleList); /* remember orig loc */
    retVal = NULL;
    FOR_EACH_LIST_ELMT (currMod, moduleList)
    {

        /*
         *  may fail due to unresolved int or oid value ref
         *  so try name match anyway.
         *  This is not standard (CCITT) if the oids were resolved
         *  but different, in which case the match should
         *  fail regardless of the name match. oh well, ts.
         */
        if (CompareOids (oid, currMod->modId->oid))
        {
            retVal = currMod;
            break; /* exit for loop */
        }

        else if ((modName != NULL) &&
            (strcmp (modName, currMod->modId->name) == 0))
        {
            retVal = currMod;
            break; /* exit for loop */
        }
    }

    SET_CURR_LIST_NODE (moduleList, tmp);
    return retVal;

}  /* LookupModule */




/*
 * Given a constructed type, it returns the component of that
 * type with the matching field name.  Returns NULL if teh
 * given type does not have the named field or is not
 * a type that has fields.
 */
NamedType*
LookupFieldInType PARAMS ((tRef, fieldName),
    Type *tRef _AND_
    char *fieldName)
{
    NamedType *e;
    NamedType *retVal;
    Type *t;
    void *tmp;

    t = ParanoidGetType (tRef); /* skip any references etc */

    if ((t->basicType->choiceId != BASICTYPE_SET) &&
        (t->basicType->choiceId != BASICTYPE_SEQUENCE) &&
        (t->basicType->choiceId != BASICTYPE_CHOICE))
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupFieldInType: ERROR - attempt to look for field in a non SET/SEQ/CHOICE type\n");
#endif
        return NULL;
    }

    /* return if null list */
    if (t->basicType->a.set == NULL)
        return NULL;

    /* remember set's original curr elmt */
    tmp = (void*)CURR_LIST_NODE (t->basicType->a.set);
    retVal = NULL;
    FOR_EACH_LIST_ELMT (e, t->basicType->a.set)
    {
        /* remember fieldname is optional so it can be null */
        if ((e->fieldName != NULL) && (strcmp (e->fieldName, fieldName) == 0))
        {
            retVal = e;
            break; /* exit  for loop */
        }
    }
    SET_CURR_LIST_NODE (t->basicType->a.set, tmp);
    return retVal;

} /* LookupFieldInType */



/*
 *  Goes through typerefs (if any) to get to actual
 *  ASN1 type. Returns the found "defining" type.
 *  May return the given type t, if it's not a typeref
 *  or if it is an unlinked type ref
 */
Type*
GetType PARAMS ((type),
    Type *type)
{
    TypeDef *td;
    Type *t;

    t = type;
    if (t == NULL)
        return NULL;

    while (1)
    {
        switch (t->basicType->choiceId)
        {
            case BASICTYPE_LOCALTYPEREF:
            case BASICTYPE_IMPORTTYPEREF:
                td = t->basicType->a.localTypeRef->link;
                if (td == NULL)
                    return type;
                else
                    t = td->type;
                break;

            default:
                return t;
        }
    }
}  /* GetType */


/*
 * like GetType ie, skips type references to return the defining type.
 * This is a paranoid version - it checks for circular type errors.
 *  eg: A ::= B
 *      B ::= A
 * would make the normal GetType recurse forever (until no stk mem)
 */
Type*
ParanoidGetType PARAMS ((type),
    Type *type)
{
    TypeDef *td;
    Type *t;
    DefinedObj *l;

    t = type;
    if (t == NULL)
        return NULL;

    l = NewObjList();
    while (1)
    {
        switch (t->basicType->choiceId)
        {
            case BASICTYPE_LOCALTYPEREF:
            case BASICTYPE_IMPORTTYPEREF:
                td = t->basicType->a.localTypeRef->link;
                if ((td == NULL) || (ObjIsDefined (l, td->type, ObjPtrCmp)))
                {
                    return type;
                }
                else
                {
                    t = td->type;
                    DefineObj (&l, t);
                }
                break;

            default:
                FreeDefinedObjs (&l);
                return t;
        }
    }

}  /* ParnoidGetType */


/*
 *  Goes through typerefs (if any) to get to actual
 *  ASN1 basic type (eg int, bool, seq, seq of, set,
 *  set of, choice, any, etc.
 *  Returns the typeId of that type, otherwise -1.
 */
enum BasicTypeChoiceId
GetBuiltinType PARAMS ((t),
    Type *t)
{
    Type *definingType;

    definingType = GetType (t);
    if (definingType != NULL)
        return definingType->basicType->choiceId;
    else
        return -1;

}  /* GetBuiltinType */


/*  Paranoid version of GetBuiltinType
 *  goes through typerefs (if any) to get to actual
 *  ASN1 basic type (eg int, bool, seq, seq of, set,
 *  set of, choice, any, etc.
 *  Returns the typeId of that type, otherwise -1.
 */
enum BasicTypeChoiceId
ParanoidGetBuiltinType PARAMS ((t),
    Type *t)
{
    Type *definingType;

    definingType = ParanoidGetType (t);
    if (definingType != NULL)
        return definingType->basicType->choiceId;
    else
        return -1;

}  /* GetBuiltinType */



/*
 * Goes through typerefs (if any) to get to
 * the namedElmts (if any) associated with the
 * given type (INTEGER, ENUMERATED, BITSTRING or
 * LOCAL/IMPORT REFS to these types).
 * Returns NULL if there are no associated Named Elmts
 */
NamedNumberList*
GetNamedElmts PARAMS ((t),
    Type *t)
{
    Type *definingType;

    if (t == NULL)
        return NULL;

    definingType = ParanoidGetType (t);

    if (definingType == NULL)
        return NULL;

    switch (definingType->basicType->choiceId)
    {
        case BASICTYPE_INTEGER:
        case BASICTYPE_ENUMERATED:
        case BASICTYPE_BITSTRING:
            return definingType->basicType->a.integer;

        /*
         * for non-named elmt types
         * just return NULL
         */
        default:
            return NULL;
    }

    /* not reached */
}  /* GetNamedElmts */


/*
 * [Same as GetNamedElmts except goes through CHOICEs as well &
 *  REQUIRES you to deallocate the list (but not its members).]
 *  This is nec. for CHOICEs that contain INTs etc. with named #'s]
 *  This is used for value linking.
 *
 * Goes through typerefs (if any) to get to
 * the namedElmts (if any) associated with the
 * given type (INTEGER, ENUMERATED, BITSTRING or
 * LOCAL/IMPORT REFS to these types).  Also returns
 * a named element list for CHOICE types that contain
 * named elemnts
 * Returns an empty list if there are no associated Named Elmts.
 * you are responsible  for freeing this list.  Do not free the list
 * elmts - they are part of the types.
 */
NamedNumberList*
GetAllNamedElmts PARAMS ((t),
    Type *t)
{
    Type *definingType;
    NamedType *nt;
    NamedNumberList *retVal;
    NamedNumberList *ntElmtList;
    ValueDef *nn;  /* named number is a valuedef */
    ValueDef **nnHndl;

    retVal = AsnListNew (sizeof (void*));

    if (t == NULL)
        return retVal;

    definingType = ParanoidGetType (t);

    if (definingType == NULL)
        return retVal;


    switch (definingType->basicType->choiceId)
    {
        case BASICTYPE_INTEGER:
        case BASICTYPE_ENUMERATED:
        case BASICTYPE_BITSTRING:
            /*
             * add the named elmts (if any) to the new list
             */
            FOR_EACH_LIST_ELMT (nn, definingType->basicType->a.integer)
            {
                nnHndl = (ValueDef**)AsnListAppend (retVal);
                *nnHndl = nn;
            }
            break;

        /*
         * for choices must group all named elmts from choice components
         * and return in a list.
         */
        case BASICTYPE_CHOICE:
            FOR_EACH_LIST_ELMT (nt, definingType->basicType->a.choice)
            {
                ntElmtList = GetAllNamedElmts (nt->type);
                retVal = AsnListConcat (retVal, ntElmtList);
                Free (ntElmtList); /* zap now unused list head */
            }
            break;
    default:
      break;
    }
    return retVal;
}  /* GetAllNamedElmts */


/*
 * Recursively does pseudo breadth first search from the given ancestor
 * looking for the given child node.  Returns the direct parent Type
 * of the child if found, NULL otherwise.  This routine does not follow
 * type references.
 */
Type*
GetParentS PARAMS ((ancestor, child),
    Type *ancestor _AND_
    Type *child)
{
    NamedType *e;
    Type *parent;
    void *tmp;

    if ((ancestor->basicType->choiceId != BASICTYPE_SET) &&
        (ancestor->basicType->choiceId != BASICTYPE_SEQUENCE) &&
        (ancestor->basicType->choiceId != BASICTYPE_CHOICE) &&
        (ancestor->basicType->choiceId != BASICTYPE_SETOF) &&
        (ancestor->basicType->choiceId != BASICTYPE_SEQUENCEOF) &&
        (ancestor->basicType->choiceId != BASICTYPE_OCTETCONTAINING) &&
        (ancestor->basicType->choiceId != BASICTYPE_BITCONTAINING))
    {
        return NULL;
    }

    if (ancestor->basicType->a.set == NULL)
        return NULL;

    if ((ancestor->basicType->choiceId == BASICTYPE_SETOF) ||
        (ancestor->basicType->choiceId == BASICTYPE_SEQUENCEOF))
    {
        if (child == ancestor->basicType->a.setOf)
            return ancestor;
        else
            return GetParentS (ancestor->basicType->a.setOf, child);
    }

    tmp = (void*)CURR_LIST_NODE (ancestor->basicType->a.set);
    /*
     * look through direct children of ancestor first
     */
    FOR_EACH_LIST_ELMT (e,  ancestor->basicType->a.set)
    {
        if ( ((e->type->basicType->choiceId == BASICTYPE_OCTETCONTAINING) ||
              (e->type->basicType->choiceId == BASICTYPE_BITCONTAINING)) &&
             (child == e->type->basicType->a.stringContaining) )
		  {
            SET_CURR_LIST_NODE (ancestor->basicType->a.set, tmp);
            return ancestor;
		  }
		  else if (child == e->type)
        {
            SET_CURR_LIST_NODE (ancestor->basicType->a.set, tmp);
            return ancestor;
        }
    }


    /*
     * look through grandchildren if not in children
     */
    FOR_EACH_LIST_ELMT (e,  ancestor->basicType->a.set)
    {
        if ((parent = GetParentS (e->type, child)) != NULL)
        {
            SET_CURR_LIST_NODE (ancestor->basicType->a.set, tmp);
            return parent;
        }
    }

    SET_CURR_LIST_NODE (ancestor->basicType->a.set, tmp);
    return NULL;
}  /* GetParent */



/*
 *  Looks for the value with the given valueName (null term char*) in the
 *  given list of ValueDefs
 *  RETURNS: ptr to ValueDef with matching key (if any)
 *           NULL if no match was made
 */

ValueDef*
LookupValue PARAMS ((valueList, valueName),
    ValueDefList *valueList _AND_
    char *valueName)
{
    ValueDef *v;
    ValueDef *retVal;
    void *tmp;

    if (valueName == NULL)
    {
#ifdef DEBUG
        fprintf (errFileG, "LookupType: warning - failure due to NULL key\n");
#endif
        return NULL;
    }

    if (valueList == NULL)
        return NULL;

    tmp = (void*)CURR_LIST_NODE (valueList);
    retVal = NULL;
    FOR_EACH_LIST_ELMT (v, valueList)
    {
        if (strcmp (valueName, v->definedName) == 0)
        {
            retVal = v;
            break; /* exit for loop */
        }
    }

    SET_CURR_LIST_NODE (valueList, tmp);
    return retVal;

}  /* LookupValue */

BasicValue *
GetLastNamedNumberValue PARAMS ((valueList),
    NamedNumberList *valueList)
{
    ValueDef *v;
    BasicValue *retVal;
    void *tmp;

    if (valueList == NULL)
        return NULL;

    tmp = (void*)CURR_LIST_NODE (valueList);
    
    retVal = NULL;

    FOR_EACH_LIST_ELMT_RVS (v, valueList)
    {
        if (v->value->basicValue->choiceId == BASICVALUE_INTEGER)
        {
            retVal = v->value->basicValue;
            break; /* exit for loop */
        }
    }

    SET_CURR_LIST_NODE (valueList, tmp);
    return retVal;
}  /* LookupValue */



/*
 *  Goes through valuerefs (if any) to get to actual
 *  ASN1 value.  Analogous to GetType.
 */
Value*
GetValue PARAMS ((v),
    Value *v)
{
    ValueDef *vd;

    while (v != NULL)
    {
        switch (v->basicValue->choiceId)
        {
            case BASICVALUE_LOCALVALUEREF:
            case BASICVALUE_IMPORTVALUEREF:
                vd = v->basicValue->a.localValueRef->link;
                if (vd == NULL)
                    v = NULL;
                else
                    v = vd->value;
                break;

            default:
                return v;
        }
    }
    fprintf (errFileG, "GetValue: ERROR - cannot get value for unlinked local/import value refs\n");
    return NULL;

}  /* GetValue */


/*
 * Returns TRUE if oid1 and oid2 are identical otherwise FALSE
 */
int
CompareOids PARAMS ((oid1, oid2),
    OID *oid1 _AND_
    OID *oid2)
{
    if ((oid1 == NULL) && (oid2 == NULL))
        return FALSE;

    for (; (oid1 != NULL) && (oid2 != NULL); oid1 = oid1->next, oid2 = oid2->next)
    {
        /*
         *  fail if value refs have not been resolved or
         *   no match between arcnums
         */
        if ((oid1->arcNum == NULL_OID_ARCNUM) ||
             (oid2->arcNum == NULL_OID_ARCNUM) ||
             (oid1->arcNum != oid2->arcNum))
            return FALSE;

        /*
         * could check ref'd values for same name
         * incase value ref has not been resolved
         * and put in arcNum
         */
    }

    if ((oid1 == NULL) && (oid2 == NULL))
        return TRUE;
    else
        return FALSE;

}  /*  CompareOids */


/*
 *  Returns TRUE if the given type is INTEGER, ENUMERATED or
 *  BIT STRING and it has named elements
 *  ie Foo ::= INTEGER { one (1), two (2) } would return TRUE
 */
int
HasNamedElmts PARAMS ((t),
    Type *t)
{
    return ((t->basicType->choiceId == BASICTYPE_INTEGER) ||
             (t->basicType->choiceId == BASICTYPE_ENUMERATED) ||
             (t->basicType->choiceId == BASICTYPE_BITSTRING)) &&
           (t->basicType->a.integer != NULL) &&
           !LIST_EMPTY (t->basicType->a.integer);
} /* HasNamedElmts */


/*
 * Returns true if the given tag lists are the same
 * (assumes value refs have be resolved)
 */
int
TagsAreIdentical PARAMS ((t1, t2),
    TagList *t1 _AND_
    TagList *t2)
{
    Tag *tag1;
    Tag *tag2;

    /* both lists are empty */
    if (((t1 == NULL) || LIST_EMPTY (t1)) &&
        ((t2 == NULL) || LIST_EMPTY (t2)))
        return TRUE;

    else if ((t1 == NULL) || (t2 == NULL))
        return FALSE;

    else if (LIST_COUNT (t1) == LIST_COUNT (t2))
    {
        SET_CURR_LIST_NODE (t2, FIRST_LIST_NODE (t2));
        FOR_EACH_LIST_ELMT (tag1, t1)
        {
            tag2 = (Tag*) CURR_LIST_ELMT (t2);
            if ((tag1->tclass != tag2->tclass) || (tag1->code == tag2->code))
                return FALSE;
            SET_CURR_LIST_NODE (t2, NEXT_LIST_NODE (t2));
        }
        return TRUE;
    }
    else
        return FALSE;

} /* TagsAreIdentical */



/*
 * Returns TRUE if the tag currently on the given type has the default
 * tag specified in the type tbl. otherwise returns FALSE.
 */
int
HasDefaultTag PARAMS ((t),
    Type *t)
{
    Tag *firstTag = NULL;
    int dfltCode;
    int dfltClass;

    dfltClass = UNIV;
    dfltCode = LIBTYPE_GET_UNIV_TAG_CODE (t->basicType->choiceId);
    if ((t->tags != NULL) && !LIST_EMPTY (t->tags))
        firstTag = (Tag*)FIRST_LIST_ELMT (t->tags);

    return ((firstTag != NULL) && (LIST_COUNT (t->tags) == 1) &&
             (firstTag->tclass == dfltClass) && (firstTag->code == dfltCode)) ||
            ((firstTag == NULL) && (dfltCode == NO_TAG_CODE));

} /* HasDefaultTag */


/*
 * Returns TRUE if t is a primitive type or if it is
 * defined by a reference to a primitive type
 */
int
IsPrimitiveByDefOrRef PARAMS ((t),
    Type *t)
{
    Type *definingType;

    definingType = GetType (t);

    if (definingType == NULL)
        return FALSE; /* bad error handling */

    return IsPrimitiveByDef (definingType);
}  /* IsPrimitiveByDefOrRef */


/*
 * Returns TRUE if the given type is a primitive type. Does NOT
 * follow type references - type refs are not considered primitive.
 * The following types are considered primitive:
 *  BOOLEAN
 *  INTEGER/BIG_INT
 *  BITSTRING
 *  OCTETSTRING
 *  NULL
 *  OID
 *  REAL
 *  ENUMERATED
 *	NumericString, PrintableString, IA5String, BMPString, 
 *	UniversalString, UTF8String, and TeletexString (T61String)
 */
int
IsPrimitiveByDef PARAMS ((t),
    Type *t)
{
    switch (t->basicType->choiceId)
    {
        case BASICTYPE_LOCALTYPEREF:
        case BASICTYPE_IMPORTTYPEREF:
		case BASICTYPE_SEQUENCET:	// Deepak: 29/Nov/2002 
		case BASICTYPE_OBJECTCLASS:	// Deepak: 05/Feb/2003
        case BASICTYPE_OBJECTCLASSFIELDTYPE:	// Deepak: 05/Feb/2003
        case BASICTYPE_SEQUENCE:
        case BASICTYPE_SET:
        case BASICTYPE_CHOICE:
        case BASICTYPE_SEQUENCEOF:
        case BASICTYPE_SETOF:
        case BASICTYPE_COMPONENTSOF:
        case BASICTYPE_ANYDEFINEDBY:
        case BASICTYPE_ANY:
            return FALSE;
            break;


        case BASICTYPE_SELECTION:
            if (t->basicType->a.selection->link != NULL)
            return IsPrimitiveByDef (t->basicType->a.selection->link->type);
                   break;

        case BASICTYPE_BOOLEAN:
        case BASICTYPE_INTEGER:
        case BASICTYPE_BITSTRING:
        case BASICTYPE_OCTETSTRING:
        case BASICTYPE_NULL:
        case BASICTYPE_OID:
        case BASICTYPE_RELATIVE_OID:
        case BASICTYPE_REAL:
        case BASICTYPE_ENUMERATED:
		  case BASICTYPE_NUMERIC_STR:
		  case BASICTYPE_PRINTABLE_STR:
		  case BASICTYPE_IA5_STR:
		  case BASICTYPE_BMP_STR:
		  case BASICTYPE_UNIVERSAL_STR:
		  case BASICTYPE_UTF8_STR:
		  case BASICTYPE_T61_STR:
        case BASICTYPE_GENERALIZEDTIME:
        case BASICTYPE_UTCTIME:
        case BASICTYPE_OBJECTDESCRIPTOR:
        case BASICTYPE_VISIBLE_STR:
        case BASICTYPE_GRAPHIC_STR:
        case BASICTYPE_VIDEOTEX_STR:
        case BASICTYPE_GENERAL_STR:

            return TRUE;
            break;

        case BASICTYPE_UNKNOWN:
        case BASICTYPE_MACROTYPE:
        case BASICTYPE_MACRODEF:
        case BASICTYPE_EXTERNAL:
		    return FALSE;
            break;

        default:
            fprintf (errFileG, "IsPrimitiveByDef: ERROR - unknown type id ?!");
    }
    return FALSE;
}  /* IsPrimitiveByDef */


/*
 * Returns TRUE if the given type is a local type reference or an
 * import type ref.
 * e.g.
 *
 * Gumby ::= P1.ORName   --> isTypeRef returns TRUE P1.ORName
 * Bar ::= INTEGER       --> isTypeRef returns FALSE for INTEGER
 * Foo ::= Bar           --> isTypeRef returns TRUE for Bar
 */
int
IsTypeRef PARAMS ((t),
    Type *t)
{
    if ((t->basicType->choiceId == BASICTYPE_LOCALTYPEREF) ||
        (t->basicType->choiceId == BASICTYPE_IMPORTTYPEREF))
        return TRUE;
    else
        return FALSE;
}  /* IsTypeRef */



/*
 * Returns TRUE if the given type is defined
 * by a library type such as OCTET STRING.
 * Does NOT follow type refs - type refs return FALSE.
 *
 * NOTE - some possibly non-primitive types are defined by
 *        library types (ANY, ANY DEFINED BY)
 *
 * types defined by type refs or structured defs
 * cause FALSE to be returned. i.e.
 *  Foo ::= Bar  -> FALSE for Bar
 *  Bell ::= SEQUENCE { .. } -> False for SEQ...
 *
 * useful types are considered as type references and hence
 * return FALSE.
 */
int
IsDefinedByLibraryType PARAMS ((t),
    Type *t)
{
    int retVal;

    if (t == NULL)
        retVal = FALSE;

    else if (IsPrimitiveByDef (t))
        retVal = TRUE;

    /*
     * check for non-primitive types that
     * are defined by a library type
     */
    else
        switch (t->basicType->choiceId)
        {
            case BASICTYPE_ANYDEFINEDBY:
            case BASICTYPE_ANY:
                retVal = TRUE;
            break;

           default:
                retVal = FALSE;
    }
    return retVal;

} /* IsDefinedByLibraryType*/


/*
 * Returns FALSE if type t is
 *    a. a library type with default universal tags and no named elements
 *       OR
 *    b. a reference to a type with no extra tagging
 *
 * otherwise returns true, indicating that is is a new type derived
 * by tagging or adding named elmts to another type.
 *
 * eg  INTEGER  --> FALSE   (same as lib type)
 *     [APPLICATION 2] INTEGER --> TRUE (re-tagged lib type)
 *     INTEGER { one (1), two (2) } --> TRUE (lib type with named elmts)
 *     Bar2 --> FALSE (simple type ref)
 */
int
IsNewType PARAMS ((t),
    Type *t)
{
    /*
     * Type = [re-tagging] DefiningType [namedelmts]
     * identical: no retagging and no named elements
     */
    if (IsDefinedByLibraryType (t) && HasDefaultTag (t) && ! HasNamedElmts (t))
        return FALSE;

    else if (IsTypeRef (t) && ((t->tags == NULL) || (LIST_EMPTY (t->tags))))
        return FALSE;

    else
        return TRUE;

} /* IsNewType */


/*
 * Returns TRUE if elmts including curr list elmt
 * onward are all optional otherwise returns FALSE.
 * (note: this relies on the 'curr' ptr in the list)
 * if the list is null or the curr elmt is null
 * then returns TRUE
 */
int
IsTailOptional PARAMS ((e),
    NamedTypeList *e)
{
    NamedType *elmt;
    void *tmp;
    int retVal;

    if (e == NULL)
        return TRUE;

    tmp = (void*)CURR_LIST_NODE (e);
    if (tmp == NULL)
        return TRUE;

    retVal = TRUE;
    FOR_REST_LIST_ELMT (elmt, e) {
        if ((!elmt->type->optional) && (elmt->type->defaultVal == NULL) &&
            (!elmt->type->extensionAddition)) {
            retVal = FALSE;
            break;
        }
    }
    SET_CURR_LIST_NODE (e, tmp); /* reset list to orig loc */
    return retVal;
} /* IsTailOptional */



/*
 * Returns TRUE if all elmts after but not including the curr list elmt
 * are optional otherwise returns FALSE.
 * (note: this relies on the 'curr' ptr in the list)
 * if the list is null or the curr elmt is null
 * then returns TRUE. if there are no elmts after the curr elmt
 * returns TRUE.
 */
int
NextIsTailOptional PARAMS ((e),
    NamedTypeList *e)
{
    NamedType *elmt;
    void *tmp;
    void *tmp2;
    int retVal;

    if ((e == NULL) || (LIST_EMPTY (e)))
        return TRUE;

    tmp = (void*)CURR_LIST_NODE (e);
    if (tmp == NULL)
        return TRUE;

    tmp2 = (void*)NEXT_LIST_NODE (e);
    if (tmp2 == NULL)
        return TRUE;

    SET_CURR_LIST_NODE (e, tmp2);

    retVal = TRUE;
    FOR_REST_LIST_ELMT (elmt, e) {
        if ((!elmt->type->optional) && (elmt->type->defaultVal == NULL) &&
            (!elmt->type->extensionAddition)) {
            retVal = FALSE;
            break;
        }
    }
    SET_CURR_LIST_NODE (e, tmp); /* reset list to orig loc */
    return retVal;
} /* NextIsTailOptional */


/*
 * Returns TRUE if all elmts of the curr list are optional
 * or have default values.  Useful with SET and SEQ elements.
 */
int
AllElmtsOptional PARAMS ((e),
    NamedTypeList *e)
{
    NamedType *elmt;
    void *tmp;
    int retVal;

    if ((e == NULL) || LIST_EMPTY (e))
        return TRUE;

    tmp = (void*)CURR_LIST_NODE (e);
    SET_CURR_LIST_NODE (e, FIRST_LIST_NODE (e));

    retVal = TRUE;
    FOR_REST_LIST_ELMT (elmt, e) {
        if ((!elmt->type->optional) && (elmt->type->defaultVal == NULL) &&
            (!elmt->type->extensionAddition)) {
            retVal = FALSE;
            break;
        }
    }
    SET_CURR_LIST_NODE (e, tmp); /* reset list to orig loc */
    return retVal;
} /* AllElmtsOptional */





/*
 * Follows single levely of type ref or library type and returns a
 * handle to its AnyRefList.  Typically used in do_macros.c to
 * add a hash key for the type that t is or refs.  Need to get
 * to the type def of type t to give the AnyRefListHndl.
 */
AnyRefList**
GetAnyRefListHndl PARAMS ((t),
    Type *t)
{
    TypeDef *td;

    if (IsDefinedByLibraryType (t))
        return LIBTYPE_GET_ANY_REFS_HNDL (t->basicType->choiceId);
    else
    {
        if (!IsTypeRef (t))
            return NULL;
        else
        {
            td = t->basicType->a.localTypeRef->link;
            return &td->anyRefs;
        }
    }
} /* GetAnyRefListHndl */


/*
 * Given a subtype list s (possibly empty *s == NULL) it tacks on
 * the newSubtype in a appropriate fashion, possible chaning *s.
 * Op can be SUBTYPE_AND or SUBTYPE_OR.
 *
 * e.g. Foo ::= INTEGER ((1..100) | 200)
 *
 * Add the subtypes by
 *   AppendSubtype (&t->subtypes, (1..100), SUBTYPE_AND)
 *   AppendSubtype (&t->subtypes, 200, SUBTYPE_OR)
 *
 * op is meaningless if s is empty
 */
void
AppendSubtype PARAMS ((s, newSubtype, op),
    Subtype **s _AND_
    Subtype *newSubtype _AND_
    enum SubtypeChoiceId op)
{
    void **tmpPtr;
    Subtype *sPtr;

    if (*s == NULL)
        *s = newSubtype;

    else if (op == SUBTYPE_AND)
    {
        if ((*s)->choiceId == SUBTYPE_AND)
        {
            tmpPtr = (void**)AsnListAppend ((*s)->a.and);
            *tmpPtr = (void*)newSubtype;
        }
        else
        {
            sPtr = (Subtype*)Malloc (sizeof (Subtype));
            sPtr->choiceId  = SUBTYPE_AND;
            sPtr->a.and = NEWLIST();
            tmpPtr = (void**)AsnListAppend (sPtr->a.and);
            *tmpPtr = (void*)*s;
            tmpPtr = (void**)AsnListAppend (sPtr->a.and);
            *tmpPtr = (void*)newSubtype;
            *s = sPtr;
        }
    }
    else if (op == SUBTYPE_OR)
    {
        if ((*s)->choiceId == SUBTYPE_OR)
        {
            tmpPtr = (void**)AsnListAppend ((*s)->a.or);
            *tmpPtr = (void*)newSubtype;
        }
        else
        {
            sPtr = (Subtype*)Malloc (sizeof (Subtype));
            sPtr->choiceId  = SUBTYPE_OR;
            sPtr->a.or = NEWLIST();
            tmpPtr = (void**)AsnListAppend (sPtr->a.or);
            *tmpPtr = (void*)*s;
            tmpPtr = (void**)AsnListAppend (sPtr->a.or);
            *tmpPtr = (void*)newSubtype;
            *s = sPtr;
        }
    }
    else
        /* NOT not supported here */
        fprintf (errFileG, "AppendSubtype - unknown operation\n");

}  /* AppendSubtype */

static int
TagCodeFound(AsnList *tags, AsnInt startingTag)
{
    Tag *tag;
    FOR_EACH_LIST_ELMT(tag, tags) {
        if (tag->code == startingTag)
            return 1;
    }
    return 0;
}

void
AutomaticTagNamed PARAMS ((l, all),
    NamedTypeList *l)
{
    AsnList *used_tag;
    NamedType *e;
    void *lcurr;
    AsnInt startingTag = 0;

    if (l == NULL) {
        fprintf(errFileG, "AutomaticTagNamed - NULL tag list.\n");
    }

    /* Two passes */
    used_tag = AsnListNew(sizeof(void*));
    /* First pass, identify all the tags, and all the elements which
       need tags */
    lcurr = l->curr;
    FOR_EACH_LIST_ELMT(e, l) {
        if (e->type->tags != NULL) {
            Tag *t;
            void *curr = e->type->tags->curr;
            FOR_EACH_LIST_ELMT(t, e->type->tags) {
                if (t->tclass == CNTX) {
                    APPEND(t, used_tag);
                    break;
                }
            }
            e->type->tags->curr = curr;
        }
    }
    FOR_EACH_LIST_ELMT(e, l) {
        int found = 0;
        Tag *t;
        FOR_EACH_LIST_ELMT(t, e->type->tags) {
            if (t->tclass == CNTX) {
                fprintf(errFileG, "CNTX tag exists\n");
                found = 1;
            }
        }
        if (!found) {
            Tag *newTag = Malloc(sizeof(Tag));
            newTag->tclass = CNTX;
            newTag->form = PRIM;
            newTag->valueRef = NULL;
            /* It seems strange to only use context tagging; however, after
             * some brief interop testing, that seems like the right thing to
             * do */
            if (!(e->type->tags))
                AsnListFree(e->type->tags);
            e->type->tags = AsnListNew(sizeof(void *));
            while(TagCodeFound(used_tag, startingTag))
                startingTag++;
            newTag->code = startingTag++;
            newTag->explicit = FALSE;
            PREPEND(newTag, e->type->tags);
        }
    }
    l->curr = lcurr;

    AsnListFree(used_tag);
}