]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/inc/asn-tag.h
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.
23 * Copyright (C) 1992 Michael Sample and the University of British Columbia
25 * This library is free software; you can redistribute it and/or
26 * modify it provided that this copyright/license information is retained
29 * If you modify this file, you must clearly indicate your changes.
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.
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 $
39 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
40 * Move from private repository to open source repository
42 * Revision 1.2 2001/05/05 00:59:23 rmurphy
43 * Adding darwin license headers
45 * Revision 1.1.1.1 1999/03/16 18:06:21 aram
46 * Originals from SMIME Free Library.
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.
51 * changed `_' to `-' in file names.
53 * Revision 1.2 1995/02/18 16:22:23 rj
54 * let cpp choose a 32 bit integer type.
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.
65 # define UL unsigned int
68 # define UL unsigned long
70 # if SIZEOF_SHORT == 4
71 # define UL unsigned short
76 #error "can't find integer type which is 4 bytes in size"
80 /* Tag Id's byte length */
81 #define TB sizeof (AsnTag)
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
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)))))
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_ */
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)))))
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)))
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)))
143 BOOLEAN_TAG_CODE
= 1,
146 OCTETSTRING_TAG_CODE
,
155 NUMERICSTRING_TAG_CODE
,
156 PRINTABLESTRING_TAG_CODE
,
157 TELETEXSTRING_TAG_CODE
,
158 VIDEOTEXSTRING_TAG_CODE
,
161 GENERALIZEDTIME_TAG_CODE
,
162 GRAPHICSTRING_TAG_CODE
,
163 VISIBLESTRING_TAG_CODE
,
167 GENERALSTRING_TAG_CODE
170 GENERALSTRING_TAG_CODE
,
171 UNIVERSALSTRING_TAG_CODE
= 28,
172 BMPSTRING_TAG_CODE
= 30
177 #define TT61STRING_TAG_CODE TELETEXSTRING_TAG_CODE
178 #define ISO646STRING_TAG_CODE VISIBLESTRING_TAG_CODE
182 * the TAG_ID_[CLASS/FORM/CODE] macros are not
183 * super fast - try not to use during encoding/decoding
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))
189 * TAG_IS_CONS evaluates to true if the given AsnTag type
190 * tag has the constructed bit set.
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)))
197 /* not a valid tag - usually the first EOC octet */
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)
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.
212 *AsnLen BEncTag PROTO ((BUF_TYPE b, BER_CLASS class, BER_FORM form, int code));
214 * WARNING: these are FRAGILE macros (What people will do for performance!)
215 * Be careful of situations like:
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.
221 #define BEncTag1( b, class, form, code)\
223 BufPutByteRvs (b, (class) | (form) | (code));
225 #define BEncTag2( b, class, form, code)\
227 BufPutByteRvs (b, code);\
228 BufPutByteRvs (b, (class) | (form) | 31);
230 #define BEncTag3( b, class, form, code)\
232 BufPutByteRvs (b, (code) & 0x7F);\
233 BufPutByteRvs (b, 0x80 | ((code) >> 7));\
234 BufPutByteRvs (b, (class) | (form) | 31);
236 #define BEncTag4( b, class, form, code)\
238 BufPutByteRvs (b, (code) & 0x7F);\
239 BufPutByteRvs (b, 0x80 | ((code) >> 7));\
240 BufPutByteRvs (b, 0x80 | ((code) >> 14));\
241 BufPutByteRvs (b, (class) | (form) | 31);
243 #define BEncTag5( b, class, form, code)\
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);
252 /* the following are protos for routines ins asn_tag.c */
255 AsnTag BDecTag
PROTO ((BUF_TYPE b
, AsnLen
*bytesDecoded
, ENV_TYPE env
));
257 AsnTag PeekTag
PROTO ((BUF_TYPE b
, ENV_TYPE env
));
260 #endif /* conditional include */