]> git.saurik.com Git - apple/security.git/blame - SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-dec.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / gen-dec.c
CommitLineData
bac41a7b
A
1/*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19/*
20 * compiler/back-ends/c-gen/gen-dec.c - routines for printing C decoders from type trees
21 *
22 * The type tree has already been run through the c type generator
23 * (type-info.c). Types that the type generator didn't know how
24 * to handle (or didn't want/need to handle eg macros) get the
25 * C_NO_TYPE label and are ignored for code generation.
26 *
27 * NOTE: this is a real rats nest - it sort of evolved. It was
28 * written assuming SETs/SEQ/CHOICE etc could be nested
29 * hence all the crap about 'levels'.
30 *
31 * Mike Sample
32 * 91/10/23
33 * Copyright (C) 1991, 1992 Michael Sample
34 * and the University of British Columbia
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
5a719ac8 41 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-dec.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
bac41a7b
A
42 * $Log: gen-dec.c,v $
43 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
44 * Move from private repository to open source repository
45 *
46 * Revision 1.2 2001/05/05 00:59:28 rmurphy
47 * Adding darwin license headers
48 *
49 * Revision 1.1.1.1 1999/03/16 18:06:41 aram
50 * Originals from SMIME Free Library.
51 *
52 * Revision 1.4 1997/02/28 13:39:54 wan
53 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
54 *
55 * Revision 1.3 1995/07/25 18:41:01 rj
56 * file name has been shortened for redundant part: c-gen/gen-c-dec -> c-gen/gen-dec.
57 *
58 * changed `_' to `-' in file names.
59 *
60 * Revision 1.2 1994/09/01 00:22:06 rj
61 * snacc_config.h and other superfluous .h files removed.
62 *
63 * Revision 1.1 1994/08/28 09:48:20 rj
64 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
65 *
66 */
67
68#include <stdio.h>
69
70#include "asn-incl.h"
71#include "asn1module.h"
72#include "mem.h"
73#include "define.h"
74#include "lib-types.h"
75#include "rules.h"
76#include "type-info.h"
77#include "str-util.h"
78#include "snacc-util.h"
79#include "util.h"
80#include "tag-util.h"
81#include "gen-dec.h"
82
83
84static CRules *genDecCRulesG;
85char *valueArgNameG = "v";
86static long int *longJmpValG;
87static char *decodedLenVarNameG = "totalElmtsLen";
88static char *itemLenVarNameG = "elmtLen";
89static char *mecVarNameG = "mandatoryElmtCount";
90static char *tagIdVarNameG = "tagId";
91char *bufTypeNameG = "BUF_TYPE";
92char *lenTypeNameG = "AsnLen";
93char *tagTypeNameG = "AsnTag";
94char *envTypeNameG = "ENV_TYPE";
95
96
97
98/* non-exported prototypes */
99
100static void PrintCBerDecoderPrototype PROTO ((FILE *hdr, TypeDef *td));
101static void PrintCBerDecoderDeclaration PROTO ((FILE *src, TypeDef *td));
102static void PrintCBerDecoderDefine PROTO ((FILE *src, TypeDef *td));
103
104static int RecCountVariableLevels PROTO ((Type *t));
105static int CountVariableLevels PROTO ((Type *t));
106static void PrintCBerDecoderLocals PROTO ((FILE *src, TypeDef *td));
107static void PrintCBerListDecoderLocals PROTO ((FILE *src));
108
109static void PrintCBerSetDecodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *e, int elmtLevel, int totalLevel, int tagLevel, char *varName));
110
111static void PrintCBerSeqDecodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, NamedTypeList *e, int elmtLevel, int totalLevel, int tagLevel, char *varName));
112
113static void PrintCBerListDecoderCode PROTO ((FILE *src, TypeDef *td, Type *t, int elmtLevel, int totalLevel, int tagLevel, char *varName));
114
115static void PrintCBerChoiceDecodeCode PROTO ((FILE *src, TypeDef *td, Type *t, int elmtLevel, int totalLevel, int tagLevel, char *varName));
116
117static void PrintCLenDecodingCode PROTO ((FILE *f));
118
119static void PrintCBerDecoderIncludes PROTO ((FILE *src, Module *m, ModuleList *mods));
120
121static void PrintCBerElmtDecodeCode PROTO ((FILE *src, TypeDef *td, Type *parent, Type *t, int elmtLevel, int totalLevel, int tagLevel, char *parnetVarName, char *elmtVarName, int stoleChoiceTags));
122
123
124void
125PrintCBerDecoder PARAMS ((src, hdr, r, m, td, longJmpVal),
126 FILE *src _AND_
127 FILE *hdr _AND_
128 CRules *r _AND_
129 Module *m _AND_
130 TypeDef *td _AND_
131 long int *longJmpVal)
132{
133 int i;
134 enum BasicTypeChoiceId typeId;
135 int elmtLevel;
136 CTDI *ctdi;
137 Tag *tag;
138 char *classStr;
139 char *formStr;
140 int stoleChoiceTags;
141 TagList *tags;
142
143 ctdi = td->cTypeDefInfo;
144 if (!ctdi->genDecodeRoutine)
145 return;
146
147 /*
148 * if is type that refs another pdu type or lib type
149 * without generating a new type via tagging or named elmts
150 * print define to the hdr file
151 * (a type is a pdu by default if it is ref'd by an ANY)
152 */
153 if (!IsNewType (td->type) &&
154 (!IsTypeRef (td->type) ||
155 (IsTypeRef (td->type) &&
156 (td->type->basicType->a.localTypeRef->link->cTypeDefInfo->isPdu ||
157 ((td->type->basicType->a.localTypeRef->link->anyRefs != NULL) &&
158 !LIST_EMPTY (td->type->basicType->a.localTypeRef->link->anyRefs))))))
159 {
160 fprintf(hdr,"#define B%s B%s\n", td->cTypeDefInfo->decodeRoutineName, td->type->cTypeRefInfo->decodeRoutineName);
161/*
162 fprintf(hdr,"#define B%s(b, v, bytesDecoded, env) B%s(b, v, bytesDecoded, env)\n", td->cTypeDefInfo->decodeRoutineName, td->type->cTypeRefInfo->decodeRoutineName);
163*/
164 return;
165 }
166
167
168
169 typeId = GetBuiltinType (td->type);
170
171 /* print proto type to hdr file */
172 fprintf (hdr, "void B%s PROTO ((%s b, %s *result, %s *bytesDecoded, %s env));\n", ctdi->decodeRoutineName, bufTypeNameG, ctdi->cTypeName, lenTypeNameG, envTypeNameG);
173
174 /* print routine in src */
175 fprintf (src,"void B%s PARAMS ((b, result, bytesDecoded, env),\n", ctdi->decodeRoutineName);
176 fprintf (src,"%s b _AND_\n", bufTypeNameG);
177 fprintf (src,"%s *result _AND_\n", ctdi->cTypeName);
178 fprintf (src,"%s *bytesDecoded _AND_\n", lenTypeNameG);
179 fprintf (src,"%s env)\n", envTypeNameG);
180 fprintf (src,"{\n");
181 fprintf (src," %s tag;\n", tagTypeNameG);
182
183 /* print extra locals for redundant lengths */
184 tags = GetTags (td->type, &stoleChoiceTags);
185 for (i = 1; !stoleChoiceTags && (i <= LIST_COUNT (tags)); i++)
186 fprintf (src," %s elmtLen%d;\n", lenTypeNameG, i);
187
188 /* add extra len for choice */
189 if (typeId == BASICTYPE_CHOICE)
190 fprintf (src," %s elmtLen%d;\n", lenTypeNameG, i);
191
192 fprintf (src,"\n");
193
194 /* decode tag/length pairs */
195 elmtLevel = 0;
196 if (!stoleChoiceTags)
197 {
198 FOR_EACH_LIST_ELMT (tag, tags)
199 {
200 classStr = Class2ClassStr (tag->tclass);
201 if (tag->form == ANY_FORM)
202 formStr = Form2FormStr (PRIM);
203 else
204 formStr = Form2FormStr (tag->form);
205
206 fprintf (src," if (((tag = BDecTag (b, bytesDecoded, env)) != \n");
207
208 if (tag->tclass == UNIV)
209 {
210 fprintf (src,"MAKE_TAG_ID (%s, %s, %s))", classStr, formStr, Code2UnivCodeStr (tag->code));
211 if (tag->form == ANY_FORM)
212 fprintf (src,"&&\n (tag != MAKE_TAG_ID (%s, %s, %s)))\n", classStr, Form2FormStr (CONS), Code2UnivCodeStr (tag->code));
213 else
214 fprintf (src,")\n");
215 }
216 else
217 {
218 fprintf (src,"MAKE_TAG_ID (%s, %s, %d))", classStr, formStr, tag->code);
219 if (tag->form == ANY_FORM)
220 fprintf (src,"&&\n (tag != MAKE_TAG_ID (%s, %s, %d)))\n", classStr, Form2FormStr (CONS), tag->code);
221 else
222 fprintf (src,")\n");
223
224 }
225 fprintf (src," {\n");
226 fprintf (src," Asn1Error (\"B%s: ERROR - wrong tag\\n\");\n", ctdi->decodeRoutineName);
227 fprintf (src," longjmp (env, %d);\n", (*longJmpVal)--);
228 fprintf (src," }\n");
229
230 fprintf (src," elmtLen%d = BDecLen (b, bytesDecoded, env);\n", ++elmtLevel);
231 }
232 }
233
234 /* for choices always decode first tag of the choice's content */
235 if (typeId == BASICTYPE_CHOICE)
236 {
237 fprintf (src," tag = BDecTag (b, bytesDecoded, env);\n");
238 fprintf (src," elmtLen%d = BDecLen (b, bytesDecoded, env);\n", ++elmtLevel);
239 }
240
241 if ((typeId != BASICTYPE_ANY) && (typeId != BASICTYPE_ANYDEFINEDBY))
242 fprintf (src," B%sContent (b, tag, elmtLen%d, result, bytesDecoded, env);\n", ctdi->decodeRoutineName, elmtLevel);
243 else
244 fprintf (src," B%s (b, result, bytesDecoded, env);\n", ctdi->decodeRoutineName, elmtLevel);
245
246
247 /* grab any EOCs that match redundant, indef lengths */
248 for (i = elmtLevel-1; i > 0; i--)
249 {
250 fprintf (src," if (elmtLen%d == INDEFINITE_LEN)\n", i);
251 fprintf (src," BDecEoc (b, bytesDecoded, env);\n");
252 }
253
254
255 fprintf (src,"} /* B%s */\n\n", ctdi->decodeRoutineName);
256
257 FreeTags (tags);
258} /* PrintCBerDecoder */
259
260
261void
262PrintCBerContentDecoder PARAMS ((src, hdr, r, m, td, longJmpVal),
263 FILE *src _AND_
264 FILE *hdr _AND_
265 CRules *r _AND_
266 Module *m _AND_
267 TypeDef *td _AND_
268 long int *longJmpVal)
269{
270 NamedType *e;
271 CTDI *ctdi;
272 CTypeId rhsTypeId; /* cTypeId of the type that defined this typedef */
273 Type *t;
274 BER_FORM form;
275
276 longJmpValG = longJmpVal;
277
278 genDecCRulesG = r;
279
280 ctdi = td->cTypeDefInfo;
281 if ((ctdi == NULL) || (td->type->cTypeRefInfo == NULL))
282 {
283 fprintf (stderr,"PrintCBerDecoder: ERROR - no type info\n");
284 return;
285 }
286
287 if (!ctdi->genDecodeRoutine)
288 return;
289
290 rhsTypeId = td->type->cTypeRefInfo->cTypeId;
291 switch (rhsTypeId)
292 {
293 /*
294 * type refs or primitive types are
295 * defined as calls to the referenced type
296 */
297 case C_ANY:
298 fprintf (hdr, "/* ANY - Fix Me! */\n");
299 case C_ANYDEFINEDBY:
300 fprintf(hdr, "#define B%s B%s\n", td->cTypeDefInfo->decodeRoutineName, td->type->cTypeRefInfo->decodeRoutineName);
301
302/*
303 fprintf(hdr, "#define B%s( b, tagId, elmtLen, v, bytesDecoded, env) ", td->cTypeDefInfo->decodeRoutineName);
304 fprintf (hdr, "B%s (b, tagId, elmtLen, v, bytesDecoded, env)", td->type->cTypeRefInfo->decodeRoutineName);
305*/
306 fprintf (hdr,"\n\n");
307 break;
308
309 case C_LIB:
310 case C_TYPEREF:
311 PrintCBerDecoderDefine (hdr, td);
312 fprintf (hdr,"\n\n");
313 break;
314
315
316 case C_CHOICE:
317 PrintCBerDecoderPrototype (hdr, td);
318 fprintf (hdr,"\n\n");
319 PrintCBerDecoderDeclaration (src, td);
320 fprintf (src,"{\n");
321 PrintCBerDecoderLocals (src, td);
322 fprintf (src,"\n\n");
323 PrintCBerChoiceDecodeCode (src, td, td->type, FIRST_LEVEL-1, FIRST_LEVEL,FIRST_LEVEL-1, valueArgNameG);
324
325 fprintf (src, " (*bytesDecoded) += totalElmtsLen1;\n");
326 fprintf (src,"} /* B%sContent */",td->cTypeDefInfo->decodeRoutineName);
327 fprintf (src,"\n\n");
328 break;
329
330 case C_STRUCT:
331 PrintCBerDecoderPrototype (hdr, td);
332 fprintf (hdr,"\n\n");
333 PrintCBerDecoderDeclaration (src, td);
334 fprintf (src,"{\n");
335 PrintCBerDecoderLocals (src, td);
336 fprintf (src,"\n\n");
337 if (td->type->basicType->choiceId == BASICTYPE_SET)
338 PrintCBerSetDecodeCode (src, td, td->type, td->type->basicType->a.set, FIRST_LEVEL-1, FIRST_LEVEL, FIRST_LEVEL-1, valueArgNameG);
339 else
340 PrintCBerSeqDecodeCode (src, td, td->type, td->type->basicType->a.sequence, FIRST_LEVEL-1, FIRST_LEVEL, FIRST_LEVEL-1, valueArgNameG);
341
342 fprintf (src, " (*bytesDecoded) += totalElmtsLen1;\n");
343 fprintf (src,"} /* B%sContent */",td->cTypeDefInfo->decodeRoutineName);
344 fprintf (src,"\n\n");
345 break;
346
347
348 case C_LIST:
349 PrintCBerDecoderPrototype (hdr, td);
350 fprintf (hdr,"\n\n");
351
352 PrintCBerDecoderDeclaration (src, td);
353 fprintf (src,"{\n");
354 PrintCBerDecoderLocals (src, td);
355 fprintf (src,"\n\n");
356 PrintCBerListDecoderCode (src, td, td->type, FIRST_LEVEL-1, FIRST_LEVEL, FIRST_LEVEL-1, valueArgNameG);
357
358 fprintf (src, " (*bytesDecoded) += totalElmtsLen1;\n");
359 fprintf (src,"} /* B%sContent */",td->cTypeDefInfo->decodeRoutineName);
360 fprintf (src,"\n\n");
361 break;
362
363 case C_NO_TYPE:
364/* fprintf (src,"< sorry, unsupported type >\n\n"); */
365 return; /* dont' print newlines */
366 break;
367
368 default:
369 fprintf (stderr,"PrintCBerContentDecoder: ERROR - unknown c type id\n");
370 return;
371 break;
372 }
373
374} /* PrintCBerContentDecoder */
375
376
377
378
379/*
380 * Prints prototype for decode routine in hdr file
381 */
382
383static void
384PrintCBerDecoderPrototype PARAMS ((hdr, td),
385 FILE *hdr _AND_
386 TypeDef *td)
387{
388 CTDI *ctdi;
389
390 ctdi = td->cTypeDefInfo;
391 fprintf (hdr,"void B%sContent PROTO ((%s b, %s tagId%d, %s elmtLen%d, %s *v, %s *bytesDecoded, %s env));\n", ctdi->decodeRoutineName, bufTypeNameG, tagTypeNameG, FIRST_LEVEL-1, lenTypeNameG, FIRST_LEVEL-1, ctdi->cTypeName,lenTypeNameG, envTypeNameG);
392
393} /* PrintCBerDecoderPrototype */
394
395
396
397/*
398 * Prints declarations of decode routine for the given type def
399 */
400static void
401PrintCBerDecoderDeclaration PARAMS ((src,td),
402 FILE *src _AND_
403 TypeDef *td)
404{
405 CTDI *ctdi;
406
407 ctdi = td->cTypeDefInfo;
408 fprintf (src,"void\n");
409 fprintf (src,"B%sContent PARAMS ((b, tagId%d, elmtLen%d, v, bytesDecoded, env),\n", ctdi->decodeRoutineName, FIRST_LEVEL -1, FIRST_LEVEL -1);
410 fprintf (src,"%s b _AND_\n", bufTypeNameG);
411 fprintf (src,"%s tagId%d _AND_\n", tagTypeNameG, FIRST_LEVEL -1);
412 fprintf (src,"%s elmtLen%d _AND_\n", lenTypeNameG, FIRST_LEVEL -1);
413 fprintf (src,"%s *v _AND_\n", ctdi->cTypeName);
414 fprintf (src,"%s *bytesDecoded _AND_\n", lenTypeNameG);
415 fprintf (src,"%s env)\n", envTypeNameG);
416
417} /* PrintCBerDecoderDeclaration */
418
419
420
421/*
422 * makes a define for type refs or primitive type renaming
423 * EG:
424 * TypeX ::= INTEGER --> #define BerDecodeTypeX(b,v) BerDecodeInteger(b,v)
425 * TypeX ::= TypeY --> #define BerDecodeTypeX(b,v) BerDecodeTypeY(b,v)
426 */
427static void
428PrintCBerDecoderDefine PARAMS ((hdr, td),
429 FILE *hdr _AND_
430 TypeDef *td)
431{
432 fprintf(hdr, "#define B%sContent B%sContent", td->cTypeDefInfo->decodeRoutineName, td->type->cTypeRefInfo->decodeRoutineName);
433
434/*
435 fprintf(hdr, "#define B%sContent( b, tagId, elmtLen, v, bytesDecoded, env) ", td->cTypeDefInfo->decodeRoutineName);
436 fprintf (hdr, "B%sContent (b, tagId, elmtLen, v, bytesDecoded, env)", td->type->cTypeRefInfo->decodeRoutineName);
437*/
438} /* PrintCBerDecoderDefine */
439
440
441
442/*
443 * used to figure out local variables to declare
444 */
445static int
446RecCountVariableLevels PARAMS ((t),
447 Type *t)
448{
449 CTRI *ctri;
450 int maxLevels = 0;
451 NamedType *e;
452 TagList *tl;
453 int tagCount;
454 int typeCount;
455 void *tmp;
456 enum BasicTypeChoiceId typeId;
457
458 ctri = t->cTypeRefInfo;
459 typeId = GetBuiltinType (t);
460
461 /* embedded struct/choices aren't really an issue any more */
462 if ((ctri->cTypeId == C_STRUCT) ||
463 (ctri->cTypeId == C_CHOICE))
464 {
465 maxLevels = 1;
466
467 tagCount = CountTags (t);
468
469 tmp = (void*)CURR_LIST_NODE (t->basicType->a.set);
470 FOR_EACH_LIST_ELMT (e, t->basicType->a.set)
471 {
472 if ((e->type == NULL) || (e->type->cTypeRefInfo == NULL))
473 continue;
474
475 typeCount = RecCountVariableLevels (e->type);
476
477 if (typeCount > maxLevels)
478 maxLevels = typeCount;
479 }
480 SET_CURR_LIST_NODE (t->basicType->a.set, tmp);
481 return maxLevels + tagCount;
482 }
483 else if (ctri->cTypeId == C_LIST)
484 {
485 return CountTags (t) +RecCountVariableLevels (t->basicType->a.setOf);
486 }
487 else if (typeId == BASICTYPE_CHOICE)
488 return CountTags (t) +1;
489 else if ((typeId == BASICTYPE_ANY) || (typeId == BASICTYPE_ANYDEFINEDBY))
490 return CountTags (t) +1;
491 else
492 return CountTags (t);
493
494} /* RecCountVariableLevels */
495
496
497
498/*
499 * returns the number of variable contexts needed for
500 * decoding the contents of this type. Does not consider tags on this type.
501 */
502static int
503CountVariableLevels PARAMS ((t),
504 Type *t)
505{
506 CTRI *ctri;
507 int maxLevels = 0;
508 NamedType *e;
509 TagList *tl;
510 int tagCount;
511 int typeCount;
512 void *tmp;
513
514 ctri = t->cTypeRefInfo;
515
516 if ((ctri->cTypeId == C_STRUCT) ||
517 (ctri->cTypeId == C_CHOICE))
518 {
519 maxLevels = 1;
520 tmp = (void*)CURR_LIST_NODE (t->basicType->a.set);
521 FOR_EACH_LIST_ELMT (e, t->basicType->a.set)
522 {
523 if ((e->type == NULL) || (e->type->cTypeRefInfo == NULL))
524 continue;
525
526 typeCount = RecCountVariableLevels (e->type);
527
528 /* add extra level since must decode key tag in choice */
529 if (GetBuiltinType (e->type) == BASICTYPE_CHOICE)
530 typeCount++;
531
532 if (typeCount > maxLevels)
533 maxLevels = typeCount;
534 }
535 SET_CURR_LIST_NODE (t->basicType->a.set, tmp);
536 return maxLevels;
537 }
538 else if (ctri->cTypeId == C_LIST)
539 return RecCountVariableLevels (t->basicType->a.setOf);
540 else if ((ctri->cTypeId == C_ANY) ||
541 (ctri->cTypeId == C_ANYDEFINEDBY))
542 return 1;
543 else
544 return 0;
545} /* CountVariableLevels */
546
547
548
549/*
550 * prints local vars for constructed types (set/seq/choice)
551 */
552static void
553PrintCBerDecoderLocals PARAMS ((src,td),
554 FILE *src _AND_
555 TypeDef *td)
556{
557 int levels;
558 int i;
559
560 levels = CountVariableLevels (td->type);
561
562 fprintf (src, " int seqDone = FALSE;\n");
563
564 for (i = 0; i < levels; i++)
565 {
566 fprintf (src, " %s totalElmtsLen%d = 0;\n", lenTypeNameG, i + FIRST_LEVEL);
567 fprintf (src, " %s elmtLen%d;\n", lenTypeNameG, i + FIRST_LEVEL);
568 fprintf (src, " %s tagId%d;\n", tagTypeNameG, i + FIRST_LEVEL);
569 if (i == 0)
570 fprintf (src, " int mandatoryElmtCount%d = 0;\n", i + FIRST_LEVEL);
571 }
572
573} /* PrintCBerDecoderLocals */
574
575
576/*
577 * given the Type *(t) of an elmt in a set/seq/choice/list,
578 * prints decoding code.
579 * elmtVarName is string ptr ref to field being decoded
580 * eg "(&personnelRecord.name)"
581 * stoleChoiceTags is as returned by GetTags
582 *
583 * elmtLevel - last elmtLen# var that is valid/used (has a len)
584 * totalLevel - totalElmtsLen# to be used for running total of dec bytes
585 * tagIdLevel - last tagId# var that is valid/used (contains a tag)
586 */
587static void
588PrintCBerElmtDecodeCode PARAMS ((src, td, parent, t, elmtLevel, totalLevel, tagLevel, parentVarName, elmtVarName, stoleChoiceTags),
589 FILE *src _AND_
590 TypeDef *td _AND_
591 Type *parent _AND_
592 Type *t _AND_
593 int elmtLevel _AND_
594 int totalLevel _AND_
595 int tagLevel _AND_
596 char *parentVarName _AND_
597 char *elmtVarName _AND_
598 int stoleChoiceTags)
599{
600 CTRI *ctri;
601 Type *tmpType;
602 char idVarRef[MAX_VAR_REF];
603 NamedType *idNamedType;
604 enum BasicTypeChoiceId tmpTypeId;
605
606 ctri = t->cTypeRefInfo;
607
608 /* check if meant to be encoded */
609 if (!ctri->isEncDec)
610 return;
611
612 tmpType = GetType (t);
613
614 if (tmpType->basicType->choiceId == BASICTYPE_ANY)
615 {
616 fprintf (src,"/* ANY - Fix Me ! */\n");
617 fprintf (src," SetAnyTypeBy???(%s, ???);\n", elmtVarName);
618 fprintf (src," B%s (b, %s, &%s%d, env);\n", ctri->decodeRoutineName, elmtVarName, decodedLenVarNameG, totalLevel);
619 }
620 else if (tmpType->basicType->choiceId == BASICTYPE_ANYDEFINEDBY)
621 {
622 /* get type of 'defining' field (int/enum/oid)*/
623 idNamedType = t->basicType->a.anyDefinedBy->link;
624 tmpTypeId = GetBuiltinType (idNamedType->type);
625
626 if (tmpTypeId == BASICTYPE_OID)
627 {
628 MakeVarPtrRef (genDecCRulesG, td, parent, idNamedType->type, parentVarName, idVarRef);
629 fprintf (src, " SetAnyTypeByOid (%s, %s);\n", elmtVarName, idVarRef);
630 }
631 else
632 {
633 /* want to ref int by value not ptr */
634 MakeVarValueRef (genDecCRulesG, td, parent, idNamedType->type, parentVarName, idVarRef);
635 fprintf (src, " SetAnyTypeByInt (%s, %s);\n", elmtVarName, idVarRef);
636 }
637 fprintf (src," B%s (b, %s, &%s%d, env);\n", ctri->decodeRoutineName, elmtVarName, decodedLenVarNameG, totalLevel);
638 }
639 else switch (ctri->cTypeId)
640 {
641 case C_LIB:
642 case C_TYPEREF:
643 /*
644 * choices and octet/bit str types need tagId argument
645 */
646 if ((tmpType->basicType->choiceId == BASICTYPE_CHOICE) &&
647 !stoleChoiceTags)
648 {
649 /*
650 * strip off top tag of choice in not already done
651 * since choice decoders assume you are passing in
652 * their top tag
653 */
654 fprintf (src, " %s%d = BDecTag (b, &%s%d, env);\n", tagIdVarNameG, ++tagLevel, decodedLenVarNameG, totalLevel);
655 fprintf (src, " %s%d = BDecLen (b, &%s%d, env);\n", itemLenVarNameG, ++elmtLevel, decodedLenVarNameG, totalLevel);
656 }
657 fprintf (src," B%sContent (b, %s%d, %s%d, %s, &%s%d, env);\n", ctri->decodeRoutineName, tagIdVarNameG, tagLevel, itemLenVarNameG, elmtLevel, elmtVarName, decodedLenVarNameG, totalLevel);
658
659 /* From ftp://ftp.cs.ubc.ca/pub/local/src/snacc/bugs-in-1.1 */
660 if ((tmpType->basicType->choiceId == BASICTYPE_CHOICE)
661 && !stoleChoiceTags)
662 {
663 fprintf(src," if (elmtLen%d == INDEFINITE_LEN)\n", elmtLevel-1);
664 fprintf(src," BDecEoc(b, &totalElmtsLen%d, env);\n", totalLevel);
665 }
666
667 break;
668
669
670 /*
671 * NOTE: the CHOICE, STRUCT and LIST switch clauses won't
672 * fire due to the current 'normalization'
673 * (see normalize.c)
674 */
675
676 case C_CHOICE:
677 /*
678 * strip off top tag of choice in not already done
679 * since choice decoders assume you are passing in
680 * their top tag
681 */
682 if (!stoleChoiceTags)
683 {
684 fprintf (src, " %s%d = BDecTag (b, &%s%d, env);\n\n", tagIdVarNameG, ++tagLevel, decodedLenVarNameG, totalLevel);
685
686 fprintf (src, " %s%d = BDecLen (b, &%s%d, env);\n", itemLenVarNameG, ++elmtLevel, decodedLenVarNameG, totalLevel);
687 }
688 PrintCBerChoiceDecodeCode (src, td, t, elmtLevel, totalLevel+1, tagLevel, elmtVarName);
689 break;
690
691
692 case C_STRUCT:
693 if (t->basicType->choiceId == BASICTYPE_SET)
694 PrintCBerSetDecodeCode (src, td, t, t->basicType->a.set, elmtLevel, totalLevel+1, tagLevel, elmtVarName);
695 else
696 {
697 PrintCBerSeqDecodeCode (src, td, t, t->basicType->a.sequence, elmtLevel,totalLevel+1, tagLevel, elmtVarName);
698 fprintf (src," seqDone = FALSE;\n");
699 }
700 fprintf (src," %s%d += %s%d;\n", decodedLenVarNameG, totalLevel, decodedLenVarNameG, totalLevel+1);
701 break;
702
703
704 case C_LIST:
705 PrintCBerListDecoderCode (src, td, t, elmtLevel, totalLevel+1, tagLevel, elmtVarName);
706 fprintf (src,"\n\n");
707 fprintf (src," %s%d += %s%d;\n", decodedLenVarNameG, totalLevel, decodedLenVarNameG, totalLevel+1);
708 break;
709
710
711 case C_NO_TYPE:
712 break;
713
714 default:
715 fprintf (stderr,"PrintCBerElmtDecodeCode: ERROR - unknown c type id\n");
716 break;
717 }
718
719} /* PrintCBerElmtDecodeCode */
720
721
722/*
723 * Prints code for decoding the elmts of SET
724 */
725static void
726PrintCBerSetDecodeCode PARAMS ((src, td, parent, elmts, elmtLevel, totalLevel, tagLevel, varName),
727 FILE *src _AND_
728 TypeDef *td _AND_
729 Type *parent _AND_
730 NamedTypeList *elmts _AND_
731 int elmtLevel _AND_
732 int totalLevel _AND_
733 int tagLevel _AND_
734 char *varName)
735{
736 NamedType *e;
737 CTRI *ctri;
738 TagList *tags;
739 Tag *tag;
740 TagList *tl;
741 enum BasicTypeChoiceId builtinType;
742 char *classStr;
743 char *formStr;
744 char *codeStr;
745 int mandatoryCount = 0;
746 int i;
747 char tmpVarName[MAX_VAR_REF];
748 int stoleChoiceTags;
749 char *routineName;
750 int initialTagLevel;
751 int initialElmtLevel;
752
753
754 initialTagLevel = tagLevel;
755 initialElmtLevel = elmtLevel;
756
757
758 routineName = td->cTypeDefInfo->decodeRoutineName;
759
760 if ((elmts == NULL) || LIST_EMPTY (elmts)) /* empty set */
761 {
762 fprintf (src," if (elmtLen%d == INDEFINITE_LEN)\n", elmtLevel);
763 fprintf (src," {\n");
764 fprintf (src," BDecEoc (b, &totalElmtsLen%d, env);\n", totalLevel);
765 fprintf (src," }\n");
766 fprintf (src," else if (elmtLen%d != 0)\n", elmtLevel);
767 fprintf (src," {\n");
768 fprintf (src," Asn1Error (\"Expected an empty SET\\n\");\n");
769 fprintf (src," longjmp (env, %d);\n",(*longJmpValG)--);
770
771 fprintf (src," }\n");
772
773/* forget about possible extension types for now
774 fprintf (src," if (elmtLen%d == INDEFINITE_LEN)\n", elmtLevel);
775 fprintf (src," {\n");
776 fprintf (src," tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", ++tagLevel, totalLevel);
777
778 fprintf (src," if (tagId%d == EOC_TAG_ID)\n", tagLevel);
779 fprintf (src," BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
780 fprintf (src," else\n");
781 fprintf (src," BerDiscardElmt (b, &totalElmtsLen%d, env);\n\n",totalLevel);
782 fprintf (src," }\n");
783 fprintf (src," else\n");
784 fprintf (src," {\n");
785 fprintf (src," BufSkip (b, elmtLen%d);\n", elmtLevel);
786 fprintf (src," totalElmtsLen%d += elmtLen%d;\n", totalLevel, elmtLevel);
787 fprintf (src," }\n");
788*/
789 return;
790 }
791
792
793 fprintf (src, "for ( ; (totalElmtsLen%d < elmtLen%d) || (elmtLen%d == INDEFINITE_LEN);)\n", totalLevel, elmtLevel, elmtLevel);
794 fprintf (src, "{\n");
795 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", ++tagLevel, totalLevel);
796 fprintf (src, " if ((tagId%d == EOC_TAG_ID) && (elmtLen%d == INDEFINITE_LEN))\n", tagLevel, elmtLevel);
797 fprintf (src, " {\n");
798 fprintf (src, " BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
799 fprintf (src, " break; /* got EOC so can exit this SET's for loop*/\n");
800 fprintf (src, " }\n");
801
802 fprintf (src, " elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
803
804 fprintf (src, " switch (tagId%d)\n", tagLevel);
805 fprintf (src, " {\n");
806
807 FOR_EACH_LIST_ELMT (e, elmts)
808 {
809
810 elmtLevel = initialElmtLevel+1;
811 tagLevel = initialTagLevel+1;
812 if ((e->type == NULL) || (e->type->cTypeRefInfo == NULL))
813 {
814 fprintf (src, "< ERROR - no c type information - prob unsuported type>\n");
815 continue;
816 }
817
818 ctri = e->type->cTypeRefInfo;
819
820 /* check if meant to be encoded */
821 if (!ctri->isEncDec)
822 continue;
823
824 tags = GetTags (e->type, &stoleChoiceTags);
825 builtinType = GetBuiltinType (e->type);
826
827 if ((tags == NULL) || LIST_EMPTY (tags))
828 {
829 if ((builtinType != BASICTYPE_ANY) &&
830 (builtinType != BASICTYPE_ANYDEFINEDBY))
831 fprintf (src, "<What? no tag on a SetElmt?>\n");
832 else
833 {
834 fprintf (src," /* ANY - Fix Me ! */\n");
835 fprintf (src," case MAKE_TAG_ID (?,?,?):\n");
836 }
837 }
838 else
839 {
840 tag = (Tag*)FIRST_LIST_ELMT (tags);
841 classStr = Class2ClassStr (tag->tclass);
842 codeStr = Code2UnivCodeStr (tag->code);
843 formStr = Form2FormStr (tag->form);
844
845 if (tag->tclass == UNIV)
846 {
847 if (tag->form == ANY_FORM)
848 {
849 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (CONS), codeStr);
850 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (PRIM), codeStr);
851 }
852 else
853 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, formStr, codeStr);
854 }
855 else
856 {
857 if (tag->form == ANY_FORM)
858 {
859 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (CONS), tag->code);
860
861 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (PRIM), tag->code);
862 }
863 else
864 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, formStr, tag->code);
865 }
866
867 AsnListFirst (tags);
868 AsnListNext (tags); /* set curr to 2nd tag */
869 FOR_REST_LIST_ELMT (tag, tags)
870 {
871
872 codeStr = Code2UnivCodeStr (tag->code);
873 classStr = Class2ClassStr (tag->tclass);
874 formStr = Form2FormStr (tag->form);
875
876 if (stoleChoiceTags)
877 {
878 if (tag->tclass == UNIV)
879 {
880 if (tag->form == ANY_FORM)
881 {
882 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (CONS), tag->code);
883
884 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (PRIM), tag->code);
885 }
886 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, formStr, codeStr);
887 }
888 else
889 {
890 if (tag->form == ANY_FORM)
891 {
892 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (CONS), tag->code);
893
894 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (PRIM), tag->code);
895 }
896 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, formStr, tag->code);
897 }
898 }
899 else
900 {
901 tagLevel = initialTagLevel+2;
902 if (tag->form == ANY_FORM)
903 {
904 fprintf (src," tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n", tagLevel, totalLevel);
905 if (tag->tclass == UNIV)
906 {
907 fprintf (src,"if ((tagId%d != MAKE_TAG_ID (%s, %s, %s)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
908 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %s)))\n", tagLevel, classStr, Form2FormStr (CONS), codeStr);
909 }
910 else
911 {
912 fprintf (src,"if ((tagId%d != MAKE_TAG_ID (%s, %s, %d)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
913 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %d)))\n", tagLevel, classStr, Form2FormStr (CONS), tag->code);
914 }
915
916 }
917 else
918 {
919 if (tag->tclass == UNIV)
920 fprintf (src,"if (BDecTag (b, &totalElmtsLen%d, env) != MAKE_TAG_ID (%s, %s, %s))\n", totalLevel, classStr, formStr, codeStr);
921 else
922 fprintf (src,"if (BDecTag (b, &totalElmtsLen%d, env) != MAKE_TAG_ID (%s, %s, %d))\n", totalLevel, classStr, formStr, tag->code);
923 }
924
925 fprintf (src," {\n");
926 fprintf (src," Asn1Error (\"Unexpected Tag\\n\");\n");
927 fprintf (src," longjmp (env, %d);\n", (*longJmpValG)--);
928 fprintf (src," }\n\n");
929 fprintf (src,"elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
930 }
931 }
932 }
933
934 MakeVarPtrRef (genDecCRulesG, td, parent, e->type, varName, tmpVarName);
935
936 /*
937 * allocate mem for decoding result
938 */
939 PrintElmtAllocCode (src, e->type, tmpVarName);
940
941 PrintCBerElmtDecodeCode (src, td, parent, e->type, elmtLevel, totalLevel, tagLevel, varName, tmpVarName, stoleChoiceTags);
942
943 /*
944 * must check for another EOC for ANYs
945 * Since the any decode routines
946 * decode their own first tag/len pair
947 */
948 if ((builtinType == BASICTYPE_ANY) ||
949 (builtinType == BASICTYPE_ANYDEFINEDBY))
950 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
951 /*
952 * must check for another EOC for tagged CHOICEs
953 * since the choice decoder routines do not check
954 * for an EOC on the choice's overall length -
955 * they are only passed the tag/len of the choice's
956 * component.
957 */
958 else if ((builtinType == BASICTYPE_CHOICE) && !(stoleChoiceTags) &&
959 ((tags != NULL) && !LIST_EMPTY (tags)))
960 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
961
962 else
963 PrintEocDecoders (src, elmtLevel-1, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
964
965 if ((!e->type->optional) && (e->type->defaultVal == NULL))
966 {
967 mandatoryCount++;
968 fprintf (src, " mandatoryElmtCount%d++;\n", totalLevel);
969 }
970
971 FreeTags (tags);
972
973 fprintf (src," break;\n\n");
974 } /* end for */
975
976 fprintf (src, " default:\n");
977 fprintf (src, " Asn1Error (\"B%sContent: ERROR - Unexpected tag in SET\\n\");\n", routineName);
978 fprintf (src, " longjmp (env, %d);\n",(*longJmpValG)--);
979 fprintf (src, " break;\n");
980
981/*
982 fprintf (src, " Asn1Warning (\"B%sContent: Warning - unexpected tag in SET, discarding elmt\\n\");\n", routineName);
983 fprintf (src, " BerDiscardElmt (b, &totalElmtsLen%d, env);\n\n", totalLevel);
984*/
985
986 fprintf (src, " } /* end switch */\n");
987 fprintf (src, " } /* end for */\n");
988
989 fprintf (src, " if (mandatoryElmtCount%d != %d)\n", totalLevel, mandatoryCount);
990
991 fprintf (src, " {\n");
992 fprintf (src, " Asn1Error (\"B%sContent: ERROR - non-optional elmt missing from SET\\n\");\n", routineName);
993 fprintf (src, " longjmp (env, %d);\n",(*longJmpValG)--);
994 fprintf (src, " }\n");
995
996} /* PrintCBerSetDecodeCode */
997
998
999
1000
1001/*
1002 * Prints code for decoding the elmts of a SEQUENCE
1003 */
1004static void
1005PrintCBerSeqDecodeCode PARAMS ((src, td, parent, elmts, elmtLevel, totalLevel, tagLevel, varName),
1006 FILE *src _AND_
1007 TypeDef *td _AND_
1008 Type *parent _AND_
1009 NamedTypeList *elmts _AND_
1010 int elmtLevel _AND_
1011 int totalLevel _AND_
1012 int tagLevel _AND_
1013 char *varName)
1014{
1015 CTRI *ctri;
1016 CTDI *ctdi;
1017 NamedType *e;
1018 NamedType *tmpElmt;
1019 NamedType *last;
1020 TagList *tags;
1021 Tag *tag;
1022 Tag *lastTag;
1023 enum BasicTypeChoiceId builtinType;
1024 enum BasicTypeChoiceId tmpTypeId;
1025 char *classStr;
1026 BER_FORM form;
1027 char *formStr;
1028 char *codeStr;
1029 int i;
1030 char tmpVarName[MAX_VAR_REF];
1031 int stoleChoiceTags;
1032 char *routineName;
1033 int inTailOptElmts = FALSE;
1034 int initialElmtLevel;
1035 int initialTagLevel;
1036
1037
1038 initialTagLevel = tagLevel;
1039 initialElmtLevel = elmtLevel;
1040
1041
1042 routineName = td->cTypeDefInfo->decodeRoutineName;
1043
1044 if ((elmts == NULL) || LIST_EMPTY (elmts)) /* empty seq */
1045 {
1046 fprintf (src," if (elmtLen%d == INDEFINITE_LEN)\n", elmtLevel);
1047 fprintf (src," {\n");
1048 fprintf (src," BDecEoc (b, &totalElmtsLen%d, env);\n", totalLevel);
1049 fprintf (src," }\n");
1050 fprintf (src," else if (elmtLen%d != 0)\n", elmtLevel);
1051 fprintf (src," {\n");
1052 fprintf (src," Asn1Error (\"Expected an empty SEQUENCE\\n\");\n");
1053 fprintf (src," longjmp (env, %d);\n",(*longJmpValG)--);
1054
1055 fprintf (src," }\n");
1056
1057/*
1058 forget about extended types for now
1059 fprintf (src," tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", tagLevel+1, totalLevel);
1060 fprintf (src," {\n");
1061 fprintf (src," if (tagId%d == EOC_TAG_ID)\n", tagLevel+1);
1062 fprintf (src," BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
1063 fprintf (src," else\n");
1064 fprintf (src," BerDiscardElmt (b, &totalElmtsLen%d, env);\n\n",totalLevel);
1065 fprintf (src," }\n");
1066 fprintf (src," else \n");
1067 fprintf (src," {\n");
1068 fprintf (src," BufSkip (b, elmtLen%d);\n", elmtLevel);
1069 fprintf (src," totalElmtsLen%d += elmtLen%d\n", totalLevel, elmtLevel);
1070 fprintf (src," }\n");
1071*/
1072 return;
1073 }
1074
1075 /*
1076 * must set list curr since IsTailOptional checks from curr pt
1077 * onward
1078 */
1079 AsnListFirst (elmts);
1080 inTailOptElmts = IsTailOptional (elmts);
1081 e = (NamedType*)FIRST_LIST_ELMT (elmts);
1082 tmpTypeId = GetBuiltinType (e->type);
1083
1084 /*
1085 * print code to decode the first tag
1086 */
1087 tagLevel++;
1088 if (!inTailOptElmts)
1089 {
1090 if (((tmpTypeId == BASICTYPE_ANY) ||
1091 (tmpTypeId == BASICTYPE_ANYDEFINEDBY)) &&
1092 (CountTags (e->type) == 0))
1093 {
1094 if ((e->type->optional) && (e != (NamedType*)LAST_LIST_ELMT (elmts)))
1095 {
1096 /* let this cause a compile error in the generated code */
1097 fprintf (src,"<untagged optional ANY - you must fix this>\n");
1098 }
1099 }
1100 else
1101 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", tagLevel, totalLevel);
1102 }
1103 else
1104 {
1105 fprintf (src, " if ((elmtLen%d != INDEFINITE_LEN) && (totalElmtsLen%d == elmtLen%d))\n", elmtLevel, totalLevel, elmtLevel);
1106 fprintf (src, " seqDone = TRUE;\n");
1107 fprintf (src, " else\n");
1108 fprintf (src, " {\n");
1109
1110 if (((tmpTypeId == BASICTYPE_ANY) ||
1111 (tmpTypeId == BASICTYPE_ANYDEFINEDBY)) &&
1112 (CountTags (e->type) == 0))
1113 {
1114 if ((e->type->optional) && (e != (NamedType*)LAST_LIST_ELMT (elmts)))
1115 {
1116 /* let this cause a compile error in the generated code */
1117 fprintf (src,"<untagged optional ANY - you must fix this>\n");
1118 }
1119 }
1120 else
1121 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", tagLevel, totalLevel);
1122 fprintf (src," if ((elmtLen%d == INDEFINITE_LEN) && (tagId%d == EOC_TAG_ID))\n", elmtLevel, tagLevel);
1123 fprintf (src, " {\n");
1124 fprintf (src, " BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
1125 fprintf (src, " seqDone = TRUE;\n");
1126 fprintf (src, " }\n");
1127 fprintf (src, " }\n\n");
1128 }
1129
1130 last = (NamedType*)LAST_LIST_ELMT (elmts);
1131 FOR_EACH_LIST_ELMT (e, elmts)
1132 {
1133 elmtLevel = initialElmtLevel;
1134 tagLevel = initialTagLevel+1;
1135
1136 if ((e->type == NULL) || (e->type->cTypeRefInfo == NULL))
1137 {
1138 fprintf (src, "< ERROR - no c type information - prob unsuported type>\n");
1139 continue;
1140 }
1141
1142 ctri = e->type->cTypeRefInfo;
1143
1144 /* check if meant to be encoded */
1145 if (!ctri->isEncDec)
1146 continue;
1147
1148 tags = GetTags (e->type, &stoleChoiceTags);
1149 builtinType = GetBuiltinType (e->type);
1150
1151
1152 if ((tags == NULL) || LIST_EMPTY (tags))
1153 {
1154 if ((builtinType != BASICTYPE_ANY) &&
1155 (builtinType != BASICTYPE_ANYDEFINEDBY))
1156 fprintf (src, "<What? no tag on a SetElmt?>\n");
1157
1158 if (inTailOptElmts)
1159 {
1160 fprintf (src," if (!seqDone)");
1161 }
1162 /* always enclose elmt decoder in block */
1163 fprintf (src," {\n");
1164
1165/*
1166 else
1167 {
1168 fprintf (src," if (tagId%d == MAKE_TAG_ID (?, ?, ?))\n", tagLevel);
1169 fprintf (src," {\n");
1170 }
1171*/
1172 }
1173 else /* has tags */
1174 {
1175 tag = (Tag*)FIRST_LIST_ELMT (tags);
1176
1177 classStr = Class2ClassStr (tag->tclass);
1178 codeStr = Code2UnivCodeStr (tag->code);
1179 formStr = Form2FormStr (tag->form);
1180
1181
1182
1183 if (inTailOptElmts)
1184 fprintf (src," if ((!seqDone) && (");
1185 else
1186 fprintf (src," if ((");
1187
1188 if (tag->tclass == UNIV)
1189 {
1190 if (tag->form == ANY_FORM)
1191 {
1192 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %s)) ||\n", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1193 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1194 }
1195 else
1196 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, formStr, codeStr);
1197 }
1198 else
1199 {
1200 if (tag->form == ANY_FORM)
1201 {
1202 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %d)) ||\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1203 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1204 }
1205 else
1206 fprintf (src,"(tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, formStr, tag->code);
1207 }
1208
1209 if (!stoleChoiceTags)
1210 {
1211 fprintf (src,"))\n");
1212 fprintf (src, " {\n");
1213 fprintf (src," elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1214 }
1215
1216 AsnListFirst (tags);
1217 AsnListNext (tags);
1218
1219 FOR_REST_LIST_ELMT (tag, tags)
1220 {
1221 classStr = Class2ClassStr (tag->tclass);
1222 codeStr = Code2UnivCodeStr (tag->code);
1223 formStr = Form2FormStr (tag->form);
1224
1225
1226 if (stoleChoiceTags)
1227 {
1228 fprintf (src," ||\n");
1229 if (tag->tclass == UNIV)
1230 {
1231 if (tag->form == ANY_FORM)
1232 {
1233 fprintf (src," (tagId%d ==MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1234 fprintf (src,"||\n (tagId%d == MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1235 }
1236 else
1237 fprintf (src," (tagId%d ==MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, formStr, codeStr);
1238 }
1239 else
1240 {
1241 if (tag->form == ANY_FORM)
1242 {
1243 fprintf (src," (tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1244 fprintf (src,"||\n (tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1245 }
1246 else
1247 fprintf (src," (tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, formStr, tag->code);
1248 }
1249 }
1250 else
1251 {
1252
1253 tagLevel = initialTagLevel + 2;
1254 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", tagLevel, totalLevel);
1255 if (tag->tclass == UNIV)
1256 {
1257 if (tag->form == ANY_FORM)
1258 {
1259 fprintf (src," if ((tagId%d != MAKE_TAG_ID (%s, %s, %s)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1260 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %s)))\n", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1261 }
1262 else
1263 fprintf (src," if (tagId%d != MAKE_TAG_ID (%s, %s, %s))\n", tagLevel, classStr, formStr, codeStr);
1264 }
1265 else
1266 {
1267 if (tag->form == ANY_FORM)
1268 {
1269 fprintf (src," if ((tagId%d != MAKE_TAG_ID (%s, %s, %d)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1270 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %d)))\n", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1271 }
1272 else
1273 fprintf (src," if (tagId%d != MAKE_TAG_ID (%s, %s, %d))\n", tagLevel, classStr, formStr, tag->code);
1274 }
1275
1276
1277 fprintf (src," {\n");
1278 fprintf (src," Asn1Error (\"Unexpected Tag\\n\");\n");
1279 fprintf (src," longjmp (env, %d);\n",(*longJmpValG)--);
1280 fprintf (src," }\n\n");
1281 fprintf (src," elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1282 }
1283 } /* end tag list for */
1284
1285 if (stoleChoiceTags)
1286 {
1287 fprintf (src,"))\n");
1288 fprintf (src, " {\n");
1289 fprintf (src, " elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1290 }
1291 }
1292
1293
1294 MakeVarPtrRef (genDecCRulesG, td, parent, e->type, varName, tmpVarName);
1295
1296 /*
1297 * allocate mem for decoding result
1298 */
1299 PrintElmtAllocCode (src, e->type, tmpVarName);
1300
1301 PrintCBerElmtDecodeCode (src, td, parent, e->type, elmtLevel, totalLevel, tagLevel, varName, tmpVarName, stoleChoiceTags);
1302
1303 /*
1304 * must check for another EOC for ANYs
1305 * Since the any decode routines
1306 * decode their own first tag/len pair
1307 */
1308 if ((builtinType == BASICTYPE_ANY) ||
1309 (builtinType == BASICTYPE_ANYDEFINEDBY))
1310 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1311 /*
1312 * must check for another EOC for tagged CHOICEs
1313 * since the choice decoder routines do not check
1314 * for an EOC on the choice's overall length -
1315 * they are only passed the tag/len of the choice's
1316 * component.
1317 */
1318 else if ((builtinType == BASICTYPE_CHOICE) && (!stoleChoiceTags) &&
1319 ((tags != NULL) && !LIST_EMPTY (tags)))
1320 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1321
1322 else
1323 PrintEocDecoders (src, elmtLevel-1, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1324
1325
1326 /* could check cons len vs decode len here */
1327
1328 if (!inTailOptElmts)
1329 {
1330 /*
1331 * determine whether next elmt in Seq is start
1332 * of tailing optionals
1333 */
1334 AsnListNext (elmts);
1335 inTailOptElmts = IsTailOptional (elmts);
1336 AsnListPrev (elmts);
1337 }
1338
1339 /*
1340 * print code for getting the next tag
1341 */
1342 tmpTypeId = GetBuiltinType (e->type);
1343
1344 if (e != last)
1345 {
1346 tmpElmt = (NamedType*)NEXT_LIST_ELMT (elmts);
1347 tmpTypeId = GetBuiltinType (tmpElmt->type);
1348 if (!inTailOptElmts)
1349 {
1350 if (((tmpTypeId == BASICTYPE_ANY) ||
1351 (tmpTypeId == BASICTYPE_ANYDEFINEDBY)) &&
1352 (CountTags (tmpElmt->type) == 0))
1353 {
1354 if ((e->type->optional) ||
1355 ((tmpElmt->type->optional) && (tmpElmt != last)))
1356 {
1357 /* let this cause a compile error in the gen'd code */
1358 fprintf (src," <problems with untagged ANY that is optional or follows an optional sequence element - you must fix this>\n");
1359 }
1360 /* don't get a tag since ANY's decode their own */
1361 }
1362 else
1363 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n", initialTagLevel+1, totalLevel);
1364 }
1365 else
1366 {
1367 fprintf (src, " if ((elmtLen%d != INDEFINITE_LEN) && (totalElmtsLen%d == elmtLen%d))\n", initialElmtLevel, totalLevel, initialElmtLevel);
1368 fprintf (src, " seqDone = TRUE;\n");
1369 fprintf (src, " else\n");
1370 fprintf (src, " {\n");
1371 if (((tmpTypeId == BASICTYPE_ANY) ||
1372 (tmpTypeId == BASICTYPE_ANYDEFINEDBY)) &&
1373 (CountTags (tmpElmt->type) == 0))
1374 {
1375 if ((e->type->optional) ||
1376 ((tmpElmt->type->optional) && (tmpElmt != last)))
1377 {
1378 /* let this cause a compile error in the gen'd code */
1379 fprintf (src," <problems with untagged ANY that is optional or follows an optional sequence element - you must fix this>\n");
1380
1381 }
1382
1383 /* peek ahead for first octet of eoc */
1384 fprintf (src," tagId%d = BufPeekByte (b);\n", initialTagLevel+1);
1385 fprintf (src," if ((elmtLen%d == INDEFINITE_LEN) && (tagId%d == EOC_TAG_ID))\n", initialElmtLevel, initialTagLevel+1);
1386 fprintf (src, " {\n");
1387 fprintf (src, " BDecEoc (b, &totalElmtsLen%d, env);\n", totalLevel);
1388 fprintf (src, " seqDone = TRUE;\n");
1389 fprintf (src, " }\n");
1390 }
1391 else
1392 {
1393 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", initialTagLevel+1, totalLevel);
1394 fprintf (src," if ((elmtLen%d == INDEFINITE_LEN) && (tagId%d == EOC_TAG_ID))\n", initialElmtLevel, initialTagLevel+1);
1395 fprintf (src, " {\n");
1396 fprintf (src, " BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
1397 fprintf (src, " seqDone = TRUE;\n");
1398 fprintf (src, " }\n");
1399 }
1400 fprintf (src, " }\n");
1401 }
1402 }
1403 else /* for last elmt only */
1404 {
1405 fprintf (src," seqDone = TRUE;\n");
1406 fprintf (src," if (elmtLen%d == INDEFINITE_LEN)\n", initialElmtLevel);
1407 fprintf (src," BDecEoc (b, &totalElmtsLen%d, env);\n", totalLevel);
1408 fprintf (src," else if (totalElmtsLen%d != elmtLen%d)\n", totalLevel, initialElmtLevel);
1409 fprintf (src," longjmp (env, %d);\n",(*longJmpValG)--);
1410 }
1411
1412 /*
1413 * close (tag check/seqDone test) if block and
1414 * print else clause to handle missing non-optional elmt
1415 * errors
1416 */
1417 tmpTypeId = GetBuiltinType (e->type);
1418 if (((tmpTypeId == BASICTYPE_ANYDEFINEDBY) ||
1419 (tmpTypeId == BASICTYPE_ANY)) &&
1420 (CountTags (e->type) == 0))
1421 {
1422 /* close if stmt block */
1423 fprintf (src," }\n");
1424 }
1425 else if (!e->type->optional && (e->type->defaultVal == NULL))
1426 {
1427
1428 fprintf (src, " }\n"); /* end of tag check if */
1429 fprintf (src, " else\n");
1430 fprintf (src, " longjmp (env, %d);\n", (*longJmpValG)--);
1431 }
1432 else
1433 {
1434 fprintf (src, " }\n"); /* end of tag check if */
1435 }
1436
1437 fprintf (src,"\n\n");
1438 FreeTags (tags);
1439 }
1440
1441
1442 /*
1443 * print code to make sure that truly finished with sequence
1444 */
1445
1446 fprintf (src," if (!seqDone)\n");
1447 fprintf (src, " longjmp (env, %d);\n\n", (*longJmpValG)--);
1448
1449} /* PrintCBerSeqDecodeCode */
1450
1451
1452/*
1453 * Generates code for internally defined lists
1454 * eg:
1455 * TypeX = SET { foo INTEGER, bar SEQUENCE OF INTEGER } -->
1456 * BerDecodeTypeX (b, len, v, bytesDecoded, env)
1457 * {
1458 * ...
1459 * listLen1 = BerDecodeLen (b, &totalElmtsLen, env);
1460 * retVal->bar = NewList();
1461 * for ( ; totalElmtsLen1 < listLen1 || listLen1== INDEFINITE_LEN;)
1462 * {
1463 * tagId1 = BerDecodeTag (b, &totalElmtsLen1, env);
1464 * check for EOC
1465 * elmtLen1 = BerDecodeLen (b, &totalElmtsLen1, env)
1466 * tmpInt = Asn1Alloc (sizeof (int));
1467 * BerDecodeInteger (b, elmtLen1, tmpInt, &totalElmtsLen1, env);
1468 * AppendList (retVal->bar, tmpInt);
1469 * }
1470 * totalElmtsLen += totalElmtsLen1;
1471 * ...
1472 * }
1473 */
1474static void
1475PrintCBerListDecoderCode PARAMS ((src, td, list, elmtLevel, totalLevel, tagLevel, varName),
1476 FILE *src _AND_
1477 TypeDef *td _AND_
1478 Type *list _AND_
1479 int elmtLevel _AND_
1480 int totalLevel _AND_
1481 int tagLevel _AND_
1482 char *varName)
1483{
1484 CTRI *ctri;
1485 TagList *tags;
1486 Tag *tag;
1487 Tag *lastTag;
1488 enum BasicTypeChoiceId builtinType;
1489 char *classStr;
1490 BER_FORM form;
1491 char *formStr;
1492 char *codeStr;
1493 int mandatoryCount = 0;
1494 int i;
1495 char tmpVarName[MAX_VAR_REF];
1496 int stoleChoiceTags;
1497 char *routineName;
1498 int initialTagLevel;
1499 int initialElmtLevel;
1500 int taglessAny;
1501
1502 initialTagLevel = tagLevel;
1503 initialElmtLevel = elmtLevel;
1504
1505
1506 routineName = td->cTypeDefInfo->decodeRoutineName;
1507 ctri = list->basicType->a.setOf->cTypeRefInfo;
1508 tags = GetTags (list->basicType->a.setOf, &stoleChoiceTags);
1509 builtinType = GetBuiltinType (list->basicType->a.setOf);
1510
1511 taglessAny = (((tags == NULL) || LIST_EMPTY (tags)) &&
1512 ((builtinType == BASICTYPE_ANY) ||
1513 (builtinType == BASICTYPE_ANYDEFINEDBY)));
1514
1515 fprintf (src, " for (totalElmtsLen%d = 0; (totalElmtsLen%d < elmtLen%d) || (elmtLen%d == INDEFINITE_LEN);)\n", totalLevel, totalLevel, elmtLevel, elmtLevel);
1516 fprintf (src, " {\n");
1517 fprintf (src," %s **tmpVar;\n", ctri->cTypeName);
1518
1519 if (taglessAny)
1520 {
1521 fprintf (src, " tagId%d = BufPeekByte (b);\n\n", ++tagLevel);
1522 fprintf (src, " if ((tagId%d == EOC_TAG_ID) && (elmtLen%d == INDEFINITE_LEN))\n", tagLevel, elmtLevel);
1523 fprintf (src, " {\n");
1524 fprintf (src, " BDecEoc (b, &totalElmtsLen%d, env);\n", totalLevel);
1525 fprintf (src, " break; /* got EOC so can exit this SET OF/SEQ OF's for loop*/\n");
1526 fprintf (src, " }\n");
1527 }
1528 else
1529 {
1530 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", ++tagLevel, totalLevel);
1531 fprintf (src, " if ((tagId%d == EOC_TAG_ID) && (elmtLen%d == INDEFINITE_LEN))\n", tagLevel, elmtLevel);
1532 fprintf (src, " {\n");
1533 fprintf (src, " BDEC_2ND_EOC_OCTET (b, &totalElmtsLen%d, env)\n", totalLevel);
1534 fprintf (src, " break; /* got EOC so can exit this SET OF/SEQ OF's for loop*/\n");
1535 fprintf (src, " }\n");
1536 }
1537
1538
1539 if ((tags == NULL) || LIST_EMPTY (tags))
1540 {
1541 if (!taglessAny)
1542 fprintf (src, "<What? no tag on a SET OF/SEQ OF Elmt?>\n");
1543/*
1544 else
1545 {
1546 fprintf (src," if (tagId%d == MAKE_TAG_ID (?, ?, ?))",tagLevel);
1547 fprintf (src," {\n");
1548 }
1549*/
1550
1551 }
1552 else if (!stoleChoiceTags) /* choice decoder will check tag */
1553 {
1554 tag = (Tag*)FIRST_LIST_ELMT (tags);
1555 classStr = Class2ClassStr (tag->tclass);
1556 codeStr = Code2UnivCodeStr (tag->code);
1557 formStr = Form2FormStr (tag->form);
1558
1559 if (tag->tclass == UNIV)
1560 {
1561 if (tag->form == ANY_FORM)
1562 {
1563 fprintf (src," if ((tagId%d == MAKE_TAG_ID (%s, %s, %s)) ||", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1564
1565 fprintf (src," (tagId%d == MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1566 }
1567 else
1568 fprintf (src," if ((tagId%d == MAKE_TAG_ID (%s, %s, %s))", tagLevel, classStr, formStr, codeStr);
1569 }
1570 else
1571 {
1572 if (tag->form == ANY_FORM)
1573 {
1574 fprintf (src," if ((tagId%d == MAKE_TAG_ID (%s, %s, %d)) ||\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1575 fprintf (src," (tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1576 }
1577 else
1578 fprintf (src," if ((tagId%d == MAKE_TAG_ID (%s, %s, %d))", tagLevel, classStr, formStr, tag->code);
1579 }
1580
1581 fprintf (src,")\n");
1582 fprintf (src, " {\n");
1583 fprintf (src, " elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1584
1585 AsnListFirst (tags);
1586 AsnListNext (tags);
1587 FOR_REST_LIST_ELMT (tag, tags)
1588 {
1589 tagLevel = initialTagLevel+2;
1590 fprintf (src, " tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n\n", tagLevel, totalLevel);
1591 classStr = Class2ClassStr (tag->tclass);
1592 codeStr = Code2UnivCodeStr (tag->code);
1593 formStr = Form2FormStr (tag->form);
1594
1595 if (tag->tclass == UNIV)
1596 {
1597 if (tag->form == ANY_FORM)
1598 {
1599 fprintf (src," if ((tagId%d != MAKE_TAG_ID (%s, %s, %s)) ||\n", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1600 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %s)))\n", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1601 }
1602 else
1603 fprintf (src," if (tagId%d != MAKE_TAG_ID (%s, %s, %s))\n", tagLevel, classStr, formStr, codeStr);
1604 }
1605 else
1606 {
1607 if (tag->form == ANY_FORM)
1608 {
1609 fprintf (src," if ((tagId%d != MAKE_TAG_ID (%s, %s, %d)) ||\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1610 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %d)))\n", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1611
1612 }
1613 else
1614 fprintf (src," if (tagId%d != MAKE_TAG_ID (%s, %s, %d))\n", tagLevel, classStr, formStr, tag->code);
1615 }
1616
1617
1618 fprintf (src," {\n");
1619 fprintf (src," Asn1Error (\"Unexpected Tag\\n\");\n");
1620 fprintf (src," longjmp (env, %d);\n", (*longJmpValG)--);
1621 fprintf (src," }\n\n");
1622 fprintf (src," elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1623 }
1624 }
1625 if (stoleChoiceTags)
1626 {
1627 fprintf (src, " elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1628 }
1629
1630
1631
1632 strcpy (tmpVarName, "(*tmpVar)");
1633 fprintf (src," tmpVar = (%s**) AsnListAppend (%s);\n", ctri->cTypeName, varName);
1634 fprintf (src, " %s = (%s*) Asn1Alloc (sizeof (%s));\n", tmpVarName, ctri->cTypeName, ctri->cTypeName);
1635
1636 fprintf (src," CheckAsn1Alloc (%s, env);\n", tmpVarName);
1637 PrintCBerElmtDecodeCode (src, td, list, list->basicType->a.setOf, elmtLevel, totalLevel, tagLevel, varName, tmpVarName, stoleChoiceTags);
1638
1639 /*
1640 * must check for another EOC for ANYs
1641 * Since the any decode routines
1642 * decode their own first tag/len pair
1643 */
1644 if ((builtinType == BASICTYPE_ANY) ||
1645 (builtinType == BASICTYPE_ANYDEFINEDBY))
1646 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1647 /*
1648 * must check for another EOC for tagged CHOICEs
1649 * since the choice decoder routines do not check
1650 * for an EOC on the choice's overall length -
1651 * they are only passed the tag/len of the choice's
1652 * component.
1653 */
1654 else if ((builtinType == BASICTYPE_CHOICE) && (!stoleChoiceTags) &&
1655 ((tags != NULL) && !LIST_EMPTY (tags)))
1656 PrintEocDecoders (src, elmtLevel, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1657
1658 else
1659 PrintEocDecoders (src, elmtLevel-1, initialElmtLevel, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1660
1661
1662 if ((!stoleChoiceTags) && (!taglessAny))
1663 {
1664 fprintf (src, " } /* end of tag check if */\n");
1665 fprintf (src, " else /* wrong tag */\n");
1666 fprintf (src," {\n");
1667 fprintf (src," Asn1Error (\"Unexpected Tag\\n\");\n");
1668 fprintf (src," longjmp (env, %d);\n", (*longJmpValG)--);
1669 fprintf (src," }\n");
1670 }
1671 fprintf (src, " } /* end of for */\n\n");
1672
1673 FreeTags (tags);
1674
1675} /* PrintCBerListDecodeCode */
1676
1677
1678
1679/*
1680 * t is the choice type pointer
1681 */
1682static void
1683PrintCBerChoiceDecodeCode PARAMS ((src, td, t, elmtLevel, totalLevel, tagLevel, varName),
1684 FILE *src _AND_
1685 TypeDef *td _AND_
1686 Type *t _AND_
1687 int elmtLevel _AND_
1688 int totalLevel _AND_
1689 int tagLevel _AND_
1690 char *varName)
1691{
1692 NamedType *e;
1693 CTRI *ctri;
1694 TagList *tags;
1695 Tag *tag;
1696 Tag *lastTag;
1697 enum BasicTypeChoiceId builtinType;
1698 char *classStr;
1699 BER_FORM form;
1700 char *formStr;
1701 char *codeStr;
1702 int mandatoryCount = 0;
1703 int i;
1704 char tmpVarName[MAX_VAR_REF];
1705 char choiceIdVarName[MAX_VAR_REF];
1706 CTRI *parentCtri;
1707 int stoleChoiceTags;
1708 void *tmp;
1709 int initialTagLevel;
1710 int initialElmtLevel;
1711
1712 initialTagLevel = tagLevel;
1713 initialElmtLevel = elmtLevel;
1714
1715 parentCtri = t->cTypeRefInfo;
1716
1717
1718 fprintf (src, " switch (tagId%d)\n", tagLevel);
1719 fprintf (src, " {\n");
1720
1721
1722 FOR_EACH_LIST_ELMT (e, t->basicType->a.choice)
1723 {
1724 /* hack ! remember curr loc cause called routine hacks it */
1725 tmp = (void*)CURR_LIST_NODE (t->basicType->a.choice);
1726
1727 tagLevel = initialTagLevel;
1728 elmtLevel = initialElmtLevel;
1729
1730 if ((e->type == NULL) || (e->type->cTypeRefInfo == NULL))
1731 {
1732 fprintf (src, "< ERROR - no c type information - prob unsuported type>\n");
1733 continue;
1734 }
1735
1736 ctri = e->type->cTypeRefInfo;
1737
1738 tags = GetTags (e->type, &stoleChoiceTags);
1739 builtinType = GetBuiltinType (e->type);
1740
1741 if ((tags == NULL) || LIST_EMPTY (tags))
1742 {
1743 if ((builtinType != BASICTYPE_ANY) &&
1744 (builtinType != BASICTYPE_ANYDEFINEDBY))
1745 fprintf (src, "<What? no tag on a CHOICE elmt?>\n");
1746 else
1747 {
1748 fprintf (src, " /* You must hand code ANY type refs */\n");
1749 fprintf (src," case MAKE_TAG_ID (?, ?, ?):\n");
1750
1751 }
1752
1753 }
1754 else
1755 {
1756 tag = (Tag*)FIRST_LIST_ELMT (tags);
1757 classStr = Class2ClassStr (tag->tclass);
1758 codeStr = Code2UnivCodeStr (tag->code);
1759 formStr = Form2FormStr (tag->form);
1760
1761 if (tag->tclass == UNIV)
1762 {
1763 if (tag->form == ANY_FORM)
1764 {
1765 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (PRIM), codeStr);
1766 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (CONS), codeStr);
1767 }
1768 else
1769 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, formStr, codeStr);
1770 }
1771 else
1772 {
1773 if (tag->form == ANY_FORM)
1774 {
1775 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (PRIM), tag->code);
1776 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (CONS), tag->code);
1777 }
1778 else
1779 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, formStr, tag->code);
1780 }
1781
1782
1783 AsnListFirst (tags);
1784 AsnListNext (tags); /* set curr ptr to 2nd elmt */
1785 FOR_REST_LIST_ELMT (tag, tags)
1786 {
1787 classStr = Class2ClassStr (tag->tclass);
1788 codeStr = Code2UnivCodeStr (tag->code);
1789 formStr = Form2FormStr (tag->form);
1790
1791
1792 if (stoleChoiceTags)
1793 {
1794 if (tag->tclass == UNIV)
1795 {
1796 if (tag->form == ANY_FORM)
1797 {
1798 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (PRIM), codeStr);
1799 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, Form2FormStr (CONS), codeStr);
1800 }
1801 else
1802 fprintf (src," case MAKE_TAG_ID (%s, %s, %s):\n", classStr, formStr, codeStr);
1803 }
1804 else
1805 {
1806 if (tag->form == ANY_FORM)
1807 {
1808 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (PRIM), tag->code);
1809 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, Form2FormStr (CONS), tag->code);
1810 }
1811 else
1812 fprintf (src," case MAKE_TAG_ID (%s, %s, %d):\n", classStr, formStr, tag->code);
1813 }
1814 }
1815 else
1816 {
1817 tagLevel = initialTagLevel +1;
1818 if (tag->form == ANY_FORM)
1819 {
1820 fprintf (src," tagId%d = BDecTag (b, &totalElmtsLen%d, env);\n", tagLevel, totalLevel);
1821 if (tag->tclass == UNIV)
1822 {
1823 fprintf (src,"if ((tagId%d != MAKE_TAG_ID (%s, %s, %s)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), codeStr);
1824 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %s)))\n", tagLevel, classStr, Form2FormStr (CONS), codeStr);
1825 }
1826 else
1827 {
1828 fprintf (src,"if ((tagId%d != MAKE_TAG_ID (%s, %s, %d)) &&\n", tagLevel, classStr, Form2FormStr (PRIM), tag->code);
1829 fprintf (src," (tagId%d != MAKE_TAG_ID (%s, %s, %d)))\n", tagLevel, classStr, Form2FormStr (CONS), tag->code);
1830 }
1831
1832 }
1833 else
1834 {
1835 if (tag->tclass == UNIV)
1836 fprintf (src,"if (BDecTag (b, &totalElmtsLen%d, env) != MAKE_TAG_ID (%s, %s, %s))\n", totalLevel, classStr, formStr, codeStr);
1837 else
1838 fprintf (src,"if (BDecTag (b, &totalElmtsLen%d, env) != MAKE_TAG_ID (%s, %s, %d))\n", totalLevel, classStr, formStr, tag->code);
1839 }
1840
1841 fprintf (src," {\n");
1842 fprintf (src," Asn1Error (\"Unexpected Tag\\n\");\n");
1843 fprintf (src," longjmp (env, %d);\n", (*longJmpValG)--);
1844 fprintf (src," }\n\n");
1845 fprintf (src," elmtLen%d = BDecLen (b, &totalElmtsLen%d, env);\n", ++elmtLevel, totalLevel);
1846 }
1847 }
1848 }
1849
1850
1851 MakeChoiceIdValueRef (genDecCRulesG, td, t, e->type, varName, choiceIdVarName);
1852 fprintf (src, " %s = %s;\n", choiceIdVarName, ctri->choiceIdSymbol);
1853
1854 MakeVarPtrRef (genDecCRulesG, td, t, e->type, varName, tmpVarName);
1855
1856 PrintElmtAllocCode (src, e->type, tmpVarName);
1857
1858 PrintCBerElmtDecodeCode (src, td, t, e->type, elmtLevel, totalLevel, tagLevel, varName, tmpVarName, stoleChoiceTags);
1859
1860 /*
1861 * this is slightly diff from set/seq since
1862 * no loop checking for eoc (set) and no next elmt (seq)
1863 * so should check elmtLen0 for EOC if nec
1864 * (therefore (initialElmtLevel-1) instead of initialElmtLevel)
1865 *
1866 * must check for another EOC for ANYs
1867 * Since the any decode routines
1868 * decode their own first tag/len pair
1869 */
1870 if ((builtinType == BASICTYPE_ANY) ||
1871 (builtinType == BASICTYPE_ANYDEFINEDBY))
1872 PrintEocDecoders (src, elmtLevel, initialElmtLevel-1, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1873 /*
1874 * must check for another EOC for tagged CHOICEs
1875 * since the choice decoder routines do not check
1876 * for an EOC on the choice's overall length -
1877 * they are only passed the tag/len of the choice's
1878 * component.
1879 */
1880 else if ((builtinType == BASICTYPE_CHOICE) && (!stoleChoiceTags) &&
1881 ((tags != NULL) && !LIST_EMPTY (tags)))
1882 PrintEocDecoders (src, elmtLevel, initialElmtLevel-1, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1883
1884 else
1885 PrintEocDecoders (src, elmtLevel-1, initialElmtLevel-1, itemLenVarNameG, totalLevel, decodedLenVarNameG);
1886
1887
1888 FreeTags (tags);
1889
1890 fprintf (src," break;\n\n");
1891
1892 /* reset curr list node to value remember at beg of loop */
1893 SET_CURR_LIST_NODE (t->basicType->a.choice, tmp);
1894 } /* end for */
1895
1896 fprintf (src," default:\n");
1897 fprintf (src," Asn1Error (\"ERROR - unexpected tag in CHOICE\\n\");\n");
1898 fprintf (src," longjmp (env, %d);\n",(*longJmpValG)--);
1899 fprintf (src," break;\n");
1900
1901 fprintf (src, " } /* end switch */\n");
1902
1903} /* PrintCBerChoiceDecodeCode */
1904
1905
1906
1907static void
1908PrintCLenDecodingCode PARAMS ((f),
1909 FILE *f)
1910{
1911 fprintf (f, " itemLen += BDecDefLen (b, itemLen);");
1912} /* PrintCLenDecodingCode */