]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-any.cpp
c629bd5e45556e6ba193f20b3cbfd1d3acbbfafb
[apple/security.git] / SecuritySNACCRuntime / c++-lib / c++ / asn-any.cpp
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/src/asn-any.C
20 //
21 // MS 92
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 //
35 // ------------------------------------------------------------------------
36 // - J.G. Van Dyke & Associates, Inc. Modification History of SNACC 1.3 -
37 // ------------------------------------------------------------------------
38 //
39 // All modification are relative to the v1.3 of SNACC. We used SunOS 4.1.3's
40 // SCCS. The revision #'s start at 1.1, which is the original version from
41 // SNACC 1.3.
42 //
43 //
44 // ../SCCS/s.asn-any.C:
45 //
46 // D 1.2 98/05/01 13:47:09 pleonber 2 1 00046/00007/00164
47 // added destructor and copy for CSM_Buffer handling.
48 //
49 // D 1.1 98/05/01 13:19:19 pleonber 1 0 00171/00000/00000
50 // date and time created 98/05/01 13:19:19 by pleonber
51 //
52 // ----------------------- End of VDA Modifications ---------------------------
53 //
54 //
55 //
56 // $Header: /cvs/root/Security/SecuritySNACCRuntime/c++-lib/c++/Attic/asn-any.cpp,v 1.4 2002/03/21 05:38:44 dmitch Exp $
57 // $Log: asn-any.cpp,v $
58 // Revision 1.4 2002/03/21 05:38:44 dmitch
59 // Radar 2868524: no more setjmp/longjmp in SNACC-generated code.
60 //
61 // Revision 1.3.44.1 2002/03/20 00:36:48 dmitch
62 // Radar 2868524: SNACC-generated code now uses throw/catch instead of setjmp/longjmp.
63 //
64 // Revision 1.3 2001/06/27 23:09:14 dmitch
65 // Pusuant to Radar 2664258, avoid all cerr-based output in NDEBUG configuration.
66 //
67 // Revision 1.2 2001/06/25 22:44:17 dmitch
68 // Globalize hashTblLock with a ModuleNexus. Partial fix for Radar 2664258.
69 //
70 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
71 // Move from private repository to open source repository
72 //
73 // Revision 1.6 2001/05/05 00:59:18 rmurphy
74 // Adding darwin license headers
75 //
76 // Revision 1.5 2000/12/20 00:51:37 dmitch
77 // Cosmetic changwe to resync with ../c++/asn-any.cpp.
78 //
79 // Revision 1.4 2000/12/20 00:43:14 dmitch
80 // Acquire and release hashTblLock via an StLock.
81 //
82 // Revision 1.3 2000/12/07 22:32:03 dmitch
83 // Thread-safe mods: see comments for same file in ../c++/.
84 //
85 // Revision 1.2 2000/12/07 22:13:45 dmitch
86 // Thread-safe mods: added hashTblLock.
87 //
88 // Revision 1.1 2000/06/15 18:44:59 dmitch
89 // These snacc-generated source files are now checked in to allow cross-platform build.
90 //
91 // Revision 1.2 2000/06/08 20:05:37 dmitch
92 // Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
93 //
94 // Revision 1.1.1.1 2000/03/09 01:00:05 rmurphy
95 // Base Fortissimo Tree
96 //
97 // Revision 1.5 1999/03/21 02:07:35 mb
98 // Added Copy to every AsnType.
99 //
100 // Revision 1.4 1999/03/19 23:59:21 mb
101 // Invoke Print on our value since CSM_Buffer::Print now implements print too.
102 //
103 // Revision 1.3 1999/03/19 00:55:01 mb
104 // Made CSM_Buffer a subclass of AsnType.
105 //
106 // Revision 1.2 1999/03/18 22:35:28 mb
107 // Made all destructors virtual.
108 //
109 // Revision 1.1 1999/02/25 05:21:49 mb
110 // Added snacc c++ library
111 //
112 // Revision 1.6 1997/02/28 13:39:43 wan
113 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
114 //
115 // Revision 1.5 1997/02/16 20:26:01 rj
116 // check-in of a few cosmetic changes
117 //
118 // Revision 1.4 1995/07/24 20:12:48 rj
119 // changed `_' to `-' in file names.
120 //
121 // Revision 1.3 1994/10/08 04:18:20 rj
122 // code for meta structures added (provides information about the generated code itself).
123 //
124 // code for Tcl interface added (makes use of the above mentioned meta code).
125 //
126 // 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.
127 //
128 // made Print() const (and some other, mainly comparison functions).
129 //
130 // several `unsigned long int' turned into `size_t'.
131 //
132 // Revision 1.2 1994/08/28 10:01:10 rj
133 // comment leader fixed.
134 //
135 // Revision 1.1 1994/08/28 09:20:55 rj
136 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
137
138 #include "asn-incl.h"
139 #include "sm_vdasnacc.h"
140
141 //#include "asn-config.h"
142 //#include "asn-len.h"
143 //#include "asn-tag.h"
144 //#include "asn-type.h"
145 //#include "asn-oid.h"
146 //#include "asn-int.h"
147 //#include "asn-any.h"
148
149
150 #ifdef __APPLE__
151 #include <Security/threading.h>
152 #include <Security/globalizer.h>
153 Table *AsnAny::oidHashTbl = NULL;
154 Table *AsnAny::intHashTbl = NULL;
155 ModuleNexus<Mutex> hashTblLock;
156 #endif
157
158 // Define this ANY value's type to the one that the given id hashes
159 // to in the ANY table.
160 void
161 AsnAny::SetTypeByInt (AsnInt id)
162 {
163 Hash hash;
164 void *anyInfo;
165
166 /* use int as hash string */
167 AsnIntType idval = (AsnIntType) id;
168 hash = MakeHash ((char*)&idval, sizeof (idval));
169 #ifdef __APPLE__
170 StLock<Mutex> _(hashTblLock());
171 #endif
172 if (CheckForAndReturnValue (intHashTbl, hash, &anyInfo))
173 ai = (AnyInfo*) anyInfo;
174 else
175 ai = NULL; /* indicates failure */
176
177 } /* SetAnyTypeByInt */
178
179 // Define this ANY value's type to the one that the given id hashes
180 // to in the ANY table.
181 void AsnAny::SetTypeByOid (AsnOid &id)
182 {
183 Hash hash;
184 void *anyInfo;
185
186 /* use encoded oid as hash string */
187 hash = MakeHash (id.Str(), id.Len());
188
189 #ifdef __APPLE__
190 StLock<Mutex> _(hashTblLock());
191 #endif
192
193 if (CheckForAndReturnValue (oidHashTbl, hash, &anyInfo))
194 ai = (AnyInfo*) anyInfo;
195 else
196 ai = NULL; /* indicates failure */
197
198 } /* SetAnyTypeByOid */
199
200
201
202 // Given an integer, intId, to hash on, the type and it's anyId
203 // are installed in the integer id hash tbl
204 void
205 AsnAny::InstallAnyByInt (AsnInt intId, int anyId, AsnType *type)
206 {
207 AnyInfo *a;
208 Hash h;
209
210 a = new AnyInfo;
211 // Oid will be NULL and 0 len by default constructor
212 a->anyId = anyId;
213 a->intId = intId;
214 a->typeToClone = type;
215
216 #ifdef __APPLE__
217 StLock<Mutex> _(hashTblLock());
218 #endif
219 if (AsnAny::intHashTbl == NULL)
220 AsnAny::intHashTbl = InitHash();
221
222 AsnIntType idval = (AsnIntType) intId;
223 h = MakeHash ((char*)&idval, sizeof (idval));
224 Insert (AsnAny::intHashTbl, a, h);
225
226 } /* InstallAnyByInt */
227
228
229 // given an OBJECT IDENTIFIER, oid, to hash on, the type and it's anyId
230 // are installed in the OBJECT IDENTIFIER id hash tbl
231 void
232 AsnAny::InstallAnyByOid (AsnOid &oid, int anyId, AsnType *type)
233 {
234 AnyInfo *a;
235 Hash h;
236
237 a = new AnyInfo;
238 a->anyId = anyId;
239 a->oid = oid; // copy given oid
240 a->typeToClone = type;
241
242 h = MakeHash (oid.Str(), oid.Len());
243
244 #ifdef __APPLE__
245 StLock<Mutex> _(hashTblLock());
246 #endif
247 if (AsnAny::oidHashTbl == NULL)
248 AsnAny::oidHashTbl = InitHash();
249
250 Insert (AsnAny::oidHashTbl, a, h);
251 } /* InstallAnyByOid */
252
253
254 AsnType *AsnAny::Clone() const
255 {
256 return new AsnAny;
257 }
258
259 AsnType *AsnAny::Copy() const
260 {
261 return new AsnAny (*this);
262 }
263
264
265 //
266 // if you haven't set up the value properly
267 // this will croak (since it's a programming error
268 // - ie, you didn't initialize the data structure properly
269 //
270 AsnLen
271 AsnAny::BEnc (BUF_TYPE b)
272 {
273 return value->BEnc (b);
274 }
275
276
277
278 void
279 AsnAny::BDec (BUF_TYPE b, AsnLen &bytesDecoded, ENV_TYPE env)
280 {
281 if (ai == NULL)
282 #if SNACC_EXCEPTION_ENABLE
283 SnaccExcep::throwMe(-81);
284 #else
285 longjmp (env, -81);
286 #endif
287
288 // XXX This is wrong.
289 value = static_cast<CSM_Buffer *>(ai->typeToClone->Clone());
290
291 if (value == NULL)
292 #if SNACC_EXCEPTION_ENABLE
293 SnaccExcep::throwMe(-82);
294 #else
295 longjmp (env, -82);
296 #endif
297 else
298 value->BDec (b, bytesDecoded, env);
299 }
300
301
302 void AsnAny::Print (ostream &os) const
303 {
304 #ifndef NDEBUG
305 value->Print(os);
306 #endif
307 }
308
309 #ifdef VDADER_RULES
310
311 AsnAny::~AsnAny()
312 {
313 delete this->value;
314 }
315
316 AsnAny &AsnAny::operator = (const AsnAny &o)
317 {
318 if (this->ai) // take care of most copies.
319 delete this->ai;
320 this->ai = NULL;
321 if (o.ai)
322 {
323 this->ai = new AnyInfo;
324 *this->ai = *o.ai;
325 }
326 /* __APPLE__ - I don't think this needs a lock since it's
327 * not modifying the hash tables */
328 if (o.intHashTbl)
329 {
330 this->intHashTbl = o.intHashTbl; // same pointer.
331 }
332 if (o.oidHashTbl)
333 {
334 this->oidHashTbl = o.oidHashTbl;
335 }
336
337 if (o.value)
338 this->value = static_cast<CSM_Buffer *>(o.value->Copy());
339
340 return *this;
341 }
342
343 #endif
344