]>
Commit | Line | Data |
---|---|---|
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 | * asn_tag.h | |
21 | * | |
22 | * MS 92 | |
23 | * Copyright (C) 1992 Michael Sample and the University of British Columbia | |
24 | * | |
25 | * This library is free software; you can redistribute it and/or | |
26 | * modify it provided that this copyright/license information is retained | |
27 | * in original form. | |
28 | * | |
29 | * If you modify this file, you must clearly indicate your changes. | |
30 | * | |
31 | * This source code is distributed in the hope that it will be | |
32 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
33 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
34 | * | |
35 | * INSERT_VDA_COMMENTS | |
36 | * | |
5a719ac8 | 37 | * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/inc/asn-tag.h,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $ |
bac41a7b A |
38 | * $Log: asn-tag.h,v $ |
39 | * Revision 1.1.1.1 2001/05/18 23:14:08 mb | |
40 | * Move from private repository to open source repository | |
41 | * | |
42 | * Revision 1.2 2001/05/05 00:59:23 rmurphy | |
43 | * Adding darwin license headers | |
44 | * | |
45 | * Revision 1.1.1.1 1999/03/16 18:06:21 aram | |
46 | * Originals from SMIME Free Library. | |
47 | * | |
48 | * Revision 1.3 1995/07/27 08:44:15 rj | |
49 | * cpp macro TBL changed to TTBL since some type table code uses TBL as a type name. | |
50 | * | |
51 | * changed `_' to `-' in file names. | |
52 | * | |
53 | * Revision 1.2 1995/02/18 16:22:23 rj | |
54 | * let cpp choose a 32 bit integer type. | |
55 | * | |
56 | * Revision 1.1 1994/08/28 09:21:37 rj | |
57 | * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog. | |
58 | * | |
59 | */ | |
60 | ||
61 | #ifndef _asn_tag_h_ | |
62 | #define _asn_tag_h_ | |
63 | ||
64 | #if SIZEOF_INT == 4 | |
65 | # define UL unsigned int | |
66 | #else | |
67 | # if SIZEOF_LONG == 4 | |
68 | # define UL unsigned long | |
69 | # else | |
70 | # if SIZEOF_SHORT == 4 | |
71 | # define UL unsigned short | |
72 | # endif | |
73 | # endif | |
74 | #endif | |
75 | #ifndef UL | |
76 | #error "can't find integer type which is 4 bytes in size" | |
77 | #endif | |
78 | typedef UL AsnTag; | |
79 | ||
80 | /* Tag Id's byte length */ | |
81 | #define TB sizeof (AsnTag) | |
82 | ||
83 | /* | |
84 | * The MAKE_TAG_ID macro generates the TAG_ID rep for the | |
85 | * the given class/form/code (rep'd in long integer form) | |
86 | * if the class/form/code are constants the compiler (should) | |
87 | * calculate the tag completely --> zero runtime overhead. | |
88 | * This is good for efficiently comparing tags in switch statements | |
89 | * (decoding) etc. because run-time bit fiddling (eliminated) minimized | |
90 | */ | |
91 | #ifndef _IBM_ENC_ | |
92 | #define MAKE_TAG_ID( cl, fm, cd)\ | |
93 | ((((UL)(cl)) << ((TB -1) * 8)) | (((UL)(fm)) << ((TB -1) * 8)) | (MAKE_TAG_ID_CODE (((UL)(cd))))) | |
94 | #else | |
95 | #define MAKE_TAG_ID( cl, fm, cd)\ | |
96 | ((MAKE_TAG_ID_CODE (cd)) | (cl << ((TB -1) * 8)) | (fm << ((TB -1) * 8))) | |
97 | #endif /* _IBM_ENC_ */ | |
98 | ||
99 | #define MAKE_TAG_ID_CODE(cd)\ | |
100 | ( (cd < 31) ? (MAKE_TAG_ID_CODE1 (cd)):\ | |
101 | ((cd < 128)? (MAKE_TAG_ID_CODE2 (cd)):\ | |
102 | ((cd < 16384)? (MAKE_TAG_ID_CODE3 (cd)):\ | |
103 | (MAKE_TAG_ID_CODE4 (cd))))) | |
104 | ||
105 | #define MAKE_TAG_ID_CODE1(cd) (cd << ((TB -1) * 8)) | |
106 | #define MAKE_TAG_ID_CODE2(cd) ((31 << ((TB -1) * 8)) | (cd << ((TB-2) * 8))) | |
107 | #define MAKE_TAG_ID_CODE3(cd) ((31 << ((TB -1) * 8))\ | |
108 | | ((cd & 0x3f80) << 9)\ | |
109 | | ( 0x0080 << ((TB-2) * 8))\ | |
110 | | ((cd & 0x007F) << ((TB-3)* 8))) | |
111 | ||
112 | #define MAKE_TAG_ID_CODE4(cd) ((31 << ((TB -1) * 8))\ | |
113 | | ((cd & 0x1fc000) << 2)\ | |
114 | | ( 0x0080 << ((TB-2) * 8))\ | |
115 | | ((cd & 0x3f80) << 1)\ | |
116 | | ( 0x0080 << ((TB-3) * 8))\ | |
117 | | ((cd & 0x007F) << ((TB-4)*8))) | |
118 | ||
119 | ||
120 | ||
121 | typedef enum | |
122 | { | |
123 | ANY_CLASS = -2, | |
124 | NULL_CLASS = -1, | |
125 | UNIV = 0, | |
126 | APPL = (1 << 6), | |
127 | CNTX = (2 << 6), | |
128 | PRIV = (3 << 6) | |
129 | } BER_CLASS; | |
130 | ||
131 | typedef enum | |
132 | { | |
133 | ANY_FORM = -2, | |
134 | NULL_FORM = -1, | |
135 | PRIM = 0, | |
136 | CONS = (1 << 5) | |
137 | } BER_FORM; | |
138 | ||
139 | ||
140 | typedef enum | |
141 | { | |
142 | NO_TAG_CODE = 0, | |
143 | BOOLEAN_TAG_CODE = 1, | |
144 | INTEGER_TAG_CODE, | |
145 | BITSTRING_TAG_CODE, | |
146 | OCTETSTRING_TAG_CODE, | |
147 | NULLTYPE_TAG_CODE, | |
148 | OID_TAG_CODE, | |
149 | OD_TAG_CODE, | |
150 | EXTERNAL_TAG_CODE, | |
151 | REAL_TAG_CODE, | |
152 | ENUM_TAG_CODE, | |
153 | SEQ_TAG_CODE = 16, | |
154 | SET_TAG_CODE, | |
155 | NUMERICSTRING_TAG_CODE, | |
156 | PRINTABLESTRING_TAG_CODE, | |
157 | TELETEXSTRING_TAG_CODE, | |
158 | VIDEOTEXSTRING_TAG_CODE, | |
159 | IA5STRING_TAG_CODE, | |
160 | UTCTIME_TAG_CODE, | |
161 | GENERALIZEDTIME_TAG_CODE, | |
162 | GRAPHICSTRING_TAG_CODE, | |
163 | VISIBLESTRING_TAG_CODE, | |
164 | ||
165 | #ifndef VDADER_RULES | |
166 | ||
167 | GENERALSTRING_TAG_CODE | |
168 | ||
169 | #else | |
170 | GENERALSTRING_TAG_CODE, | |
171 | UNIVERSALSTRING_TAG_CODE = 28, | |
172 | BMPSTRING_TAG_CODE = 30 | |
173 | #endif | |
174 | ||
175 | } BER_UNIV_CODE; | |
176 | ||
177 | #define TT61STRING_TAG_CODE TELETEXSTRING_TAG_CODE | |
178 | #define ISO646STRING_TAG_CODE VISIBLESTRING_TAG_CODE | |
179 | ||
180 | ||
181 | /* | |
182 | * the TAG_ID_[CLASS/FORM/CODE] macros are not | |
183 | * super fast - try not to use during encoding/decoding | |
184 | */ | |
185 | #define TAG_ID_CLASS( tid) ((tid & (0xC0 << ((TB-1) *8))) >> ((TB -1) * 8)) | |
186 | #define TAG_ID_FORM( tid) ((tid & (0x20 << ((TB-1) *8))) >> ((TB -1) * 8)) | |
187 | ||
188 | /* | |
189 | * TAG_IS_CONS evaluates to true if the given AsnTag type | |
190 | * tag has the constructed bit set. | |
191 | */ | |
192 | #define TAG_IS_CONS( tag) ((tag) & (CONS << ((TB-1) *8))) | |
193 | #define CONSIFY( tag) (tag | (CONS << ((TB-1) *8))) | |
194 | #define DECONSIFY( tag) (tag & ~(CONS << ((TB-1) *8))) | |
195 | ||
196 | ||
197 | /* not a valid tag - usually the first EOC octet */ | |
198 | #define EOC_TAG_ID 0 | |
199 | ||
200 | ||
201 | ||
202 | /* | |
203 | * tag encoders. given constant values for class form & | |
204 | * code in the source, these can be optimized by the compiler | |
205 | * (e.g. do the shifts and bitwise ands & ors etc) | |
206 | * | |
207 | * This is the prototype that the following BEncTag routines | |
208 | * would use if they were routines. They return the number of | |
209 | * octets written to the buffer. | |
210 | * | |
211 | * | |
212 | *AsnLen BEncTag PROTO ((BUF_TYPE b, BER_CLASS class, BER_FORM form, int code)); | |
213 | * | |
214 | * WARNING: these are FRAGILE macros (What people will do for performance!) | |
215 | * Be careful of situations like: | |
216 | * if (foo) | |
217 | * encLen += BEncTag1 (...); | |
218 | * Use {}'s to enclose any ASN.1 related routine that you are | |
219 | * treating as a single statement in your code. | |
220 | */ | |
221 | #define BEncTag1( b, class, form, code)\ | |
222 | 1;\ | |
223 | BufPutByteRvs (b, (class) | (form) | (code)); | |
224 | ||
225 | #define BEncTag2( b, class, form, code)\ | |
226 | 2;\ | |
227 | BufPutByteRvs (b, code);\ | |
228 | BufPutByteRvs (b, (class) | (form) | 31); | |
229 | ||
230 | #define BEncTag3( b, class, form, code)\ | |
231 | 3;\ | |
232 | BufPutByteRvs (b, (code) & 0x7F);\ | |
233 | BufPutByteRvs (b, 0x80 | ((code) >> 7));\ | |
234 | BufPutByteRvs (b, (class) | (form) | 31); | |
235 | ||
236 | #define BEncTag4( b, class, form, code)\ | |
237 | 4;\ | |
238 | BufPutByteRvs (b, (code) & 0x7F);\ | |
239 | BufPutByteRvs (b, 0x80 | ((code) >> 7));\ | |
240 | BufPutByteRvs (b, 0x80 | ((code) >> 14));\ | |
241 | BufPutByteRvs (b, (class) | (form) | 31); | |
242 | ||
243 | #define BEncTag5( b, class, form, code)\ | |
244 | 5;\ | |
245 | BufPutByteRvs (b, (code) & 0x7F);\ | |
246 | BufPutByteRvs (b, 0x80 | ((code) >> 7));\ | |
247 | BufPutByteRvs (b, 0x80 | ((code) >> 14));\ | |
248 | BufPutByteRvs (b, 0x80 | ((code) >> 21));\ | |
249 | BufPutByteRvs (b, (class) | (form) | 31); | |
250 | ||
251 | ||
252 | /* the following are protos for routines ins asn_tag.c */ | |
253 | ||
254 | ||
255 | AsnTag BDecTag PROTO ((BUF_TYPE b, AsnLen *bytesDecoded, ENV_TYPE env)); | |
256 | #if TTBL | |
257 | AsnTag PeekTag PROTO ((BUF_TYPE b, ENV_TYPE env)); | |
258 | #endif | |
259 | ||
260 | #endif /* conditional include */ |