]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/tag-util.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 * compiler/back_ends/c_gen/tag_util.c - utilities for dealing with tags
23 * Copyright (C) 1991, 1992 Michael Sample
24 * and the University of British Columbia
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
33 * $Header: /cvs/root/Security/SecuritySNACCRuntime/compiler/back-ends/Attic/tag-util.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
34 * $Log: tag-util.c,v $
35 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
36 * Move from private repository to open source repository
38 * Revision 1.2 2001/05/05 00:59:27 rmurphy
39 * Adding darwin license headers
41 * Revision 1.1.1.1 1999/03/16 18:06:39 aram
42 * Originals from SMIME Free Library.
44 * Revision 1.3 1995/07/25 18:15:28 rj
45 * changed `_' to `-' in file names.
47 * Revision 1.2 1994/09/01 00:26:07 rj
48 * snacc_config.h and other superfluous .h files removed.
50 * Revision 1.1 1994/08/28 09:48:39 rj
51 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
58 #include "asn1module.h"
61 #include "lib-types.h"
62 #include "c-gen/rules.h"
63 #include "c-gen/type-info.h"
65 #include "snacc-util.h"
66 #include "c-gen/util.h"
72 * returns the tags for the given type (stops at next type definition).
73 * if no tags have been grabbed yet and an untagged CHOICE is encountered,
74 * all of the CHOICE's top level tags are returned and the stoleChoiceTags
75 * flag is set. If the type has no tags an empty list is returned, not
78 * ASSUMES: tag list's and implicit flags have been adjusted according
79 * to module level IMPLICIT/EXPLICIT-TAGS and type level
80 * IMPLICIT/EXPLICIT tagging.
84 * typeX ::= SEQUENCE SomeChoice ::= CHOICE
86 * foo [0] INTEGER, [0] INTEGER,
87 * bar SomeChoice, [1] BOOLEAN,
88 * bell [1] IMPLICIT BOOLEAN, [2] IA5String
89 * gumby [2] SomeChoice, }
93 * SomeOtherChoice ::= [APPLICATION 99] CHOICE { ....}
95 * GetTags (foo's type) --> CNTX 0, UNIV INTEGER_TAG_CODE stoleChoiceTags = FALSE
96 * GetTags (bar) --> CNTX 0, CNTX 1, CNTX 2 (SomeChoice Elmt's first Tags)
97 * stoleChoiceTags = TRUE
98 * GetTags (bell) --> CNTX 1 stoleChoiceTags = FALSE
99 * GetTags (gumby) --> CNTX 2 stoleChoiceTags = FALSE
100 * GetTags (poki) --> APPLICATION 99 stoleChoiceTags = FALSE
102 * MS 92/03/04 Added tag form information
105 GetTags
PARAMS ((t
, stoleChoiceTags
),
107 int *stoleChoiceTags
)
116 int stoleChoicesAgain
;
123 retVal
= (TagList
*) AsnListNew (sizeof (void*));
125 *stoleChoiceTags
= FALSE
;
130 * go through tag list local to this type if any
133 FOR_REST_LIST_ELMT (tag
, tl
)
135 tagCopy
= (Tag
*)Malloc (sizeof (Tag
));
136 memcpy (tagCopy
, tag
, sizeof (Tag
));
137 tagHndl
= (Tag
**)AsnListAppend (retVal
);
143 * follow tags of referenced types
146 if ((t
->basicType
->choiceId
== BASICTYPE_LOCALTYPEREF
) ||
147 (t
->basicType
->choiceId
== BASICTYPE_IMPORTTYPEREF
))
150 implicitRef
= t
->implicit
;
153 if (t
->basicType
->a
.localTypeRef
->link
== NULL
)
155 fprintf (stderr
,"ERROR - unresolved type ref, cannot get tags for decoding>\n");
158 t
= t
->basicType
->a
.localTypeRef
->link
->type
;
163 AsnListFirst (tl
); /* set curr ptr to first node */
164 if ((!LIST_EMPTY (tl
)) && implicitRef
)
174 * if untagged choice and no tags found yet
176 else if ((t
->basicType
->choiceId
== BASICTYPE_CHOICE
) && (LIST_EMPTY (retVal
)))
179 * Return list of top level tags from this choice
180 * and set "stoleChoiceTags" bool param
183 fprintf (stderr
,"ERROR - IMPLICITLY Tagged CHOICE\n");
185 *stoleChoiceTags
= TRUE
;
187 FOR_EACH_LIST_ELMT (e
, t
->basicType
->a
.choice
)
189 stoleChoicesAgain
= FALSE
;
190 tl
= GetTags (e
->type
, &stoleChoicesAgain
);
196 if (stoleChoicesAgain
)
198 FOR_EACH_LIST_ELMT (tag
, tl
)
200 tagCopy
= (Tag
*)Malloc (sizeof (Tag
));
201 memcpy (tagCopy
, tag
, sizeof (Tag
));
202 tagHndl
= (Tag
**)AsnListAppend (retVal
);
209 tag
= (Tag
*)FIRST_LIST_ELMT (tl
);
210 tagCopy
= (Tag
*)Malloc (sizeof (Tag
));
211 memcpy (tagCopy
, tag
, sizeof (Tag
));
212 tagHndl
= (Tag
**)AsnListAppend (retVal
);
218 break; /* exit for loop */
222 break; /* exit for loop */
226 if (!*stoleChoiceTags
&& (retVal
!= NULL
) && !LIST_EMPTY (retVal
))
228 last
= (Tag
*)LAST_LIST_ELMT (retVal
);
229 FOR_EACH_LIST_ELMT (tag
, retVal
)
233 last
->form
= LIBTYPE_GET_TAG_FORM (GetBuiltinType (t
));
236 AsnListFirst (retVal
);
243 FreeTags
PARAMS ((tl
),
247 AsnListNode
*listNode
;
251 FOR_EACH_LIST_ELMT (tag
, tl
)
256 /* free list nodes */
257 for (ln
= FIRST_LIST_NODE (tl
); ln
!= NULL
; )
270 * Returns the number of tags that GetTags would return for
274 CountTags
PARAMS ((t
),
281 int stoleChoicesAgain
;
294 * go through tag list local to this type if any
297 FOR_REST_LIST_ELMT (tag
, tl
)
303 * follow tags of referenced types
306 if ((t
->basicType
->choiceId
== BASICTYPE_LOCALTYPEREF
) ||
307 (t
->basicType
->choiceId
== BASICTYPE_IMPORTTYPEREF
))
310 implicitRef
= t
->implicit
;
313 if (t
->basicType
->a
.localTypeRef
->link
== NULL
)
315 fprintf (stderr
,"ERROR - unresolved type ref, cannot get tags for decoding>\n");
318 t
= t
->basicType
->a
.localTypeRef
->link
->type
;
323 AsnListFirst (tl
); /* set curr ptr to first node */
324 if ((!LIST_EMPTY (tl
)) && implicitRef
)
342 TagByteLen
PARAMS ((tagCode
),
343 unsigned long int tagCode
)
345 unsigned long int tagLen
;
349 else if (tagCode
< 128)
351 else if (tagCode
< 16384)
353 else if (tagCode
< 2097152)
364 Class2ClassStr
PARAMS ((class),
389 } /* Class2ClassStr */
394 Form2FormStr
PARAMS ((form
),
416 Code2UnivCodeStr
PARAMS ((code
),
421 case BOOLEAN_TAG_CODE
:
422 return "BOOLEAN_TAG_CODE";
425 case INTEGER_TAG_CODE
:
426 return "INTEGER_TAG_CODE";
429 case BITSTRING_TAG_CODE
:
430 return "BITSTRING_TAG_CODE";
433 case OCTETSTRING_TAG_CODE
:
434 return "OCTETSTRING_TAG_CODE";
437 case NULLTYPE_TAG_CODE
:
438 return "NULLTYPE_TAG_CODE";
442 return "OID_TAG_CODE";
446 return "OD_TAG_CODE";
449 case EXTERNAL_TAG_CODE
:
450 return "EXTERNAL_TAG_CODE";
454 return "REAL_TAG_CODE";
458 return "ENUM_TAG_CODE";
462 return "SEQ_TAG_CODE";
466 return "SET_TAG_CODE";
469 case NUMERICSTRING_TAG_CODE
:
470 return "NUMERICSTRING_TAG_CODE";
473 case PRINTABLESTRING_TAG_CODE
:
474 return "PRINTABLESTRING_TAG_CODE";
477 case TELETEXSTRING_TAG_CODE
:
478 return "TELETEXSTRING_TAG_CODE";
481 case VIDEOTEXSTRING_TAG_CODE
:
482 return "VIDEOTEXSTRING_TAG_CODE";
485 case IA5STRING_TAG_CODE
:
486 return "IA5STRING_TAG_CODE";
489 case UTCTIME_TAG_CODE
:
490 return "UTCTIME_TAG_CODE";
493 case GENERALIZEDTIME_TAG_CODE
:
494 return "GENERALIZEDTIME_TAG_CODE";
497 case GRAPHICSTRING_TAG_CODE
:
498 return "GRAPHICSTRING_TAG_CODE";
501 case VISIBLESTRING_TAG_CODE
:
502 return "VISIBLESTRING_TAG_CODE";
505 case GENERALSTRING_TAG_CODE
:
506 return "GENERALSTRING_TAG_CODE";
511 case UNIVERSALSTRING_TAG_CODE
:
512 return "UNIVERSALSTRING_TAG_CODE";
515 case BMPSTRING_TAG_CODE
:
516 return "BMPSTRING_TAG_CODE";
521 /* if the universal type is not known then just return the
522 * unvisersal tag code. This is useful for defining new types
523 * in local modules w/o having to modify the compiler.
525 static char retstring
[3];
526 sprintf(retstring
, "%d", code
);
536 } /* TagId2FormStr */