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