]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/inc/asn-config.h
Security-54.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / inc / asn-config.h
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 // file: .../c++-lib/inc/asn-config.h - decoder alloc routines and buffer routines and other configuration stuff.
20 //
21 // MS 92/06/18
22 // Copyright (C) 1992 Michael Sample and the University of British Columbia
23 //
24 // This library is free software; you can redistribute it and/or
25 // modify it provided that this copyright/license information is retained
26 // in original form.
27 //
28 // If you modify this file, you must clearly indicate your changes.
29 //
30 // This source code is distributed in the hope that it will be
31 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
32 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
33 //
34 // $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c++-lib/inc/asn-config.h,v 1.7 2002/06/12 18:36:31 dmitch Exp $
35 // $Log: asn-config.h,v $
36 // Revision 1.7 2002/06/12 18:36:31 dmitch
37 // Radar 2951933: Avoid including iostream in asn-config.h for NDEBUG builds.
38 //
39 // Revision 1.6.94.4 2002/06/11 22:59:03 dmitch
40 // Radar 2951933.
41 //
42 // Revision 1.6.94.3 2002/06/11 22:34:50 dmitch
43 // More endl cleanup.
44 //
45 // Revision 1.6.94.2 2002/06/11 22:27:28 dmitch
46 // Clean up endl declaration for NDEBUG case.
47 //
48 // Revision 1.6.94.1 2002/06/10 23:19:08 dmitch
49 // Radar 2934358 - avoid #include \<iostream\> in asn-config.h
50 //
51 // Revision 1.6 2002/04/18 18:58:08 dmitch
52 // Radar 2904404 - avoid deprecated iostream.h
53 //
54 // Revision 1.5.24.1 2002/04/17 00:48:53 dmitch
55 // Radar 2904404 - avoid deprecated iostream.h.
56 //
57 // Revision 1.5 2002/03/21 05:38:47 dmitch
58 // Radar 2868524: no more setjmp/longjmp in SNACC-generated code.
59 //
60 // Revision 1.4.32.4 2002/03/20 20:56:37 dmitch
61 // Further refinements for Radar 2868524: no more BDecPdu or BEncPdu.
62 //
63 // Revision 1.4.32.3 2002/03/20 02:54:08 dmitch
64 // Avoid the unused and uninitialized ENV_TYPE var in BDecPdu.
65 //
66 // Revision 1.4.32.2 2002/03/20 01:28:02 dmitch
67 // Added throw() to SnaccExcep destructor.
68 //
69 // Revision 1.4.32.1 2002/03/20 00:36:52 dmitch
70 // Radar 2868524: SNACC-generated code now uses throw/catch instead of setjmp/longjmp.
71 //
72 // Revision 1.4 2002/02/07 04:30:04 mb
73 // Fixes required to build with gcc3.
74 // Merged from branch PR-2848996
75 // Bug #: 2848996
76 // Submitted by:
77 // Reviewed by: Turly O'Connor <turly@apple.com>
78 //
79 // Revision 1.3.10.1 2002/02/06 23:45:04 mb
80 // Changes to allow building with gcc3
81 //
82 // Revision 1.3 2001/06/27 23:09:16 dmitch
83 // Pusuant to Radar 2664258, avoid all cerr-based output in NDEBUG configuration.
84 //
85 // Revision 1.2 2001/06/26 23:47:26 dmitch
86 // Implemented AsnNullError as replacement for cerr in NDEBUG configuration.
87 //
88 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
89 // Move from private repository to open source repository
90 //
91 // Revision 1.3 2001/05/05 00:59:18 rmurphy
92 // Adding darwin license headers
93 //
94 // Revision 1.2 2000/06/15 18:48:23 dmitch
95 // Snacc-generated source files, now part of CVS tree to allow for cross-platform build of snaccRuntime.
96 //
97 // Revision 1.1.1.1 2000/03/09 01:00:05 rmurphy
98 // Base Fortissimo Tree
99 //
100 // Revision 1.3 1999/04/08 21:20:18 aram
101 // Fixed Asn1Free macro.
102 //
103 // Revision 1.2 1999/03/16 03:04:30 mb
104 // Added common base class SecObject and implemented signing of the digest in SecSigner.
105 //
106 // Revision 1.1 1999/02/25 05:21:41 mb
107 // Added snacc c++ library
108 //
109 // Revision 1.7 1995/09/07 18:48:36 rj
110 // AsnIntType and AsnUIntType introduced to replace (unsigned) long int at a lot of places.
111 // they shall provide 32 bit integer types on all platforms.
112 //
113 // Revision 1.6 1995/07/25 20:19:00 rj
114 // changed `_' to `-' in file names.
115 //
116 // Revision 1.5 1995/02/13 14:47:46 rj
117 // settings for IEEE_REAL_FMT/IEEE_REAL_LIB moved from {c_lib,c++_lib}/inc/asn_config.h to acconfig.h.
118 //
119 // Revision 1.4 1994/10/08 04:17:59 rj
120 // code for meta structures added (provides information about the generated code itself).
121 //
122 // code for Tcl interface added (makes use of the above mentioned meta code).
123 //
124 // virtual inline functions (the destructor, the Clone() function, BEnc(), BDec() and Print()) moved from inc/*.h to src/*.C because g++ turns every one of them into a static non-inline function in every file where the .h file gets included.
125 //
126 // made Print() const (and some other, mainly comparison functions).
127 //
128 // several `unsigned long int' turned into `size_t'.
129 //
130 // Revision 1.3 1994/09/01 00:58:47 rj
131 // redundant code moved into ../../config.h.bot; semicolon removed from end of macro texts.
132 //
133 // Revision 1.2 1994/08/28 10:00:47 rj
134 // comment leader fixed.
135 //
136 // Revision 1.1 1994/08/28 09:20:29 rj
137 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
138
139 #ifndef _asn_config_h_
140 #define _asn_config_h_
141
142 #include <ctype.h> /* for isprint() in <<op on AsnOcts */
143
144 #ifdef NDEBUG
145 /* just get forward declarations */
146 #include <iosfwd>
147 namespace std {
148 extern ostream& endl(ostream& outs);
149 }
150 #else
151 #include <iostream>
152 #endif
153 /* assume these... */
154 using std::iostream;
155 using std::ostream;
156 using std::istream;
157 using std::endl;
158
159 #include <memory.h>
160 #include <string.h>
161 #include <setjmp.h>
162 #include <math.h> /* to get ieee conversion functions & pow */
163
164 #include "snacc.h"
165
166 using std::streamsize;
167
168 // used not only by AsnInt (asn-int.h), but by AsnNameDesc (meta.h) as well:
169 #if SIZEOF_INT == 4
170 # define I int
171 #else
172 # if SIZEOF_LONG == 4
173 # define I long
174 # else
175 # if SIZEOF_SHORT == 4
176 # define I short
177 # endif
178 # endif
179 #endif
180 #ifdef I
181 typedef I AsnIntType;
182 typedef unsigned I AsnUIntType;
183 # undef I
184 #else
185 # error "can't find integer type which is 4 bytes in size"
186 #endif
187
188 /* used to test if optionals are present */
189 #define NOT_NULL( ptr) ((ptr) != NULL)
190
191
192 /*
193 * Asn1Error (char *str)
194 * - configure error handler
195 */
196 #ifndef NDEBUG
197 #define Asn1Error std::cerr
198 #else
199
200 /* silent ostream */
201 class Asn1ErrorClass;
202 class Asn1ErrorClass /* public ostream */
203 {
204 public:
205 Asn1ErrorClass& put (char c) { return *this; };
206 Asn1ErrorClass& write (const char * s, streamsize n) { return *this; };
207 Asn1ErrorClass& flush () { return *this; };
208 template <class T>
209 Asn1ErrorClass &operator << (const T &) { return *this; }
210 };
211
212
213 #ifdef __APPLE__
214
215 /* Avoid the static load-time init of AsnErrorClass */
216 #include <Security/globalizer.h>
217 extern ModuleNexus<Asn1ErrorClass> AsnNullError;
218
219 /* Avoid instantiating cerr and endl */
220 #define Asn1Error AsnNullError()
221 #define endl AsnNullError()
222
223 #else /* NDEBUG, !__APPLE__ */
224
225 /* This does not currently have an instantiation */
226 extern Asn1ErrorClass Asn1Error;
227
228 #endif /* __APPLE__ */
229 #endif /* NDEBUG */
230
231 /*
232 * Asn1Warning (char *str) - configure warning mechanism
233 * (currently not called)
234 */
235 #define Asn1Warning Asn1Error
236
237
238 /*
239 * configure memory scheme used by decoder to allocate memory
240 * for the decoded value. The include file name cannot be
241 * be null.
242 */
243 #define Asn1New( class) new class
244 #define Asn1Delete( ptr) delete ptr
245 #define Asn1Alloc( size) new char[size]
246 #define Asn1Free( ptr) delete[] ptr
247
248 #include "asn-buf.h"
249 #define BUF_TYPE AsnBuf &
250
251 /*
252 * Enables throw/catch as replacement for setjmp/longjmp in C++ lib.
253 * BDecPdu still returns int (1 = OK, 0 = fail) in either config.
254 * The compiler gets this symbol from c-lib/inc/asn-config.h; runtime
255 * support gets this symbol from this file. There is currently no
256 * straightforward way to have one symbol used in both environments.
257 */
258 #define SNACC_EXCEPTION_ENABLE 1
259
260 /*
261 * With the SNACC_EXCEPTION_ENABLE mods, ENV_TYPE is not used, though
262 * it still appears in the BDec*() function.
263 */
264 #if SNACC_EXCEPTION_ENABLE
265 #define ENV_TYPE int
266 #else
267 #define ENV_TYPE jmp_buf
268 #endif /* SNACC_EXCEPTION_ENABLE */
269
270 /*
271 * Enable BEncPdu, BDecPdu. Same remarks apply as above w.r.t the
272 * c++ config file.
273 */
274 #define SNACC_ENABLE_PDU 0
275 #if SNACC_ENABLE_PDU
276
277 #if SNACC_EXCEPTION_ENABLE
278 /* return true if succeeded, false otherwise */
279 #define PDU_MEMBER_MACROS\
280 int BEncPdu (BUF_TYPE b, AsnLen &bytesEncoded)\
281 {\
282 bytesEncoded = BEnc (b);\
283 return !b.WriteError();\
284 }\
285 \
286 int BDecPdu (BUF_TYPE b, AsnLen &bytesDecoded)\
287 {\
288 \
289 bytesDecoded = 0;\
290 try\
291 {\
292 BDec (b, bytesDecoded, 0);\
293 return !b.ReadError();\
294 }\
295 catch(...) {\
296 return false;\
297 }\
298 }
299 #else /* SNACC_EXCEPTION_ENABLE */
300 #define PDU_MEMBER_MACROS\
301 int BEncPdu (BUF_TYPE b, AsnLen &bytesEncoded)\
302 {\
303 bytesEncoded = BEnc (b);\
304 return !b.WriteError();\
305 }\
306 \
307 int BDecPdu (BUF_TYPE b, AsnLen &bytesDecoded)\
308 {\
309 ENV_TYPE env;\
310 int val;\
311 \
312 bytesDecoded = 0;\
313 if ((val = setjmp (env)) == 0)\
314 {\
315 BDec (b, bytesDecoded, env);\
316 return !b.ReadError();\
317 }\
318 else\
319 return false;\
320 }
321 #endif /* SNACC_EXCEPTION_ENABLE */
322 #else /* !SNACC_ENABLE_PDU */
323 /* empty */
324 #define PDU_MEMBER_MACROS
325 #endif
326
327 /*
328 * SNACC exception class
329 */
330 #if SNACC_EXCEPTION_ENABLE
331
332 #include <exception>
333
334 class SnaccExcep : public std::exception {
335 protected:
336 SnaccExcep(int err) : mErrNum(err) { }
337 public:
338 virtual ~SnaccExcep() throw() {};
339 virtual int errNum() { return mErrNum; }
340 static void throwMe(int err); // implemented in cdsaUtils.cpp
341 protected:
342 int mErrNum;
343 };
344 #endif /* SNACC_EXCEPTION_ENABLE */
345
346 #endif /* conditional include */