]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-any.cpp
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.
19 // file: .../c++-lib/src/asn-any.C
22 // Copyright (C) 1992 Michael Sample and the University of British Columbia
24 // This library is free software; you can redistribute it and/or
25 // modify it provided that this copyright/license information is retained
28 // If you modify this file, you must clearly indicate your changes.
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.
35 // ------------------------------------------------------------------------
36 // - J.G. Van Dyke & Associates, Inc. Modification History of SNACC 1.3 -
37 // ------------------------------------------------------------------------
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
44 // ../SCCS/s.asn-any.C:
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.
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
52 // ----------------------- End of VDA Modifications ---------------------------
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.
61 // Revision 1.2 2001/06/25 22:44:17 dmitch
62 // Globalize hashTblLock with a ModuleNexus. Partial fix for Radar 2664258.
64 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
65 // Move from private repository to open source repository
67 // Revision 1.6 2001/05/05 00:59:18 rmurphy
68 // Adding darwin license headers
70 // Revision 1.5 2000/12/20 00:51:37 dmitch
71 // Cosmetic changwe to resync with ../c++/asn-any.cpp.
73 // Revision 1.4 2000/12/20 00:43:14 dmitch
74 // Acquire and release hashTblLock via an StLock.
76 // Revision 1.3 2000/12/07 22:32:03 dmitch
77 // Thread-safe mods: see comments for same file in ../c++/.
79 // Revision 1.2 2000/12/07 22:13:45 dmitch
80 // Thread-safe mods: added hashTblLock.
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.
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....
88 // Revision 1.1.1.1 2000/03/09 01:00:05 rmurphy
89 // Base Fortissimo Tree
91 // Revision 1.5 1999/03/21 02:07:35 mb
92 // Added Copy to every AsnType.
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.
97 // Revision 1.3 1999/03/19 00:55:01 mb
98 // Made CSM_Buffer a subclass of AsnType.
100 // Revision 1.2 1999/03/18 22:35:28 mb
101 // Made all destructors virtual.
103 // Revision 1.1 1999/02/25 05:21:49 mb
104 // Added snacc c++ library
106 // Revision 1.6 1997/02/28 13:39:43 wan
107 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
109 // Revision 1.5 1997/02/16 20:26:01 rj
110 // check-in of a few cosmetic changes
112 // Revision 1.4 1995/07/24 20:12:48 rj
113 // changed `_' to `-' in file names.
115 // Revision 1.3 1994/10/08 04:18:20 rj
116 // code for meta structures added (provides information about the generated code itself).
118 // code for Tcl interface added (makes use of the above mentioned meta code).
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.
122 // made Print() const (and some other, mainly comparison functions).
124 // several `unsigned long int' turned into `size_t'.
126 // Revision 1.2 1994/08/28 10:01:10 rj
127 // comment leader fixed.
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.
132 #include "asn-incl.h"
133 #include "sm_vdasnacc.h"
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"
145 #include <Security/threading.h>
146 #include <Security/globalizer.h>
147 Table
* AsnAny :: oidHashTbl
= NULL
;
148 Table
* AsnAny :: intHashTbl
= NULL
;
149 ModuleNexus
< Mutex
> hashTblLock
;
152 // Define this ANY value's type to the one that the given id hashes
153 // to in the ANY table.
155 AsnAny :: SetTypeByInt ( AsnInt id
)
160 /* use int as hash string */
161 AsnIntType idval
= ( AsnIntType
) id
;
162 hash
= MakeHash (( char *)& idval
, sizeof ( idval
));
164 StLock
< Mutex
> _ ( hashTblLock ());
166 if ( CheckForAndReturnValue ( intHashTbl
, hash
, & anyInfo
))
167 ai
= ( AnyInfo
*) anyInfo
;
169 ai
= NULL
; /* indicates failure */
171 } /* SetAnyTypeByInt */
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
)
180 /* use encoded oid as hash string */
181 hash
= MakeHash ( id
. Str (), id
. Len ());
184 StLock
< Mutex
> _ ( hashTblLock ());
187 if ( CheckForAndReturnValue ( oidHashTbl
, hash
, & anyInfo
))
188 ai
= ( AnyInfo
*) anyInfo
;
190 ai
= NULL
; /* indicates failure */
192 } /* SetAnyTypeByOid */
196 // Given an integer, intId, to hash on, the type and it's anyId
197 // are installed in the integer id hash tbl
199 AsnAny :: InstallAnyByInt ( AsnInt intId
, int anyId
, AsnType
* type
)
205 // Oid will be NULL and 0 len by default constructor
208 a
-> typeToClone
= type
;
211 StLock
< Mutex
> _ ( hashTblLock ());
213 if ( AsnAny :: intHashTbl
== NULL
)
214 AsnAny :: intHashTbl
= InitHash ();
216 AsnIntType idval
= ( AsnIntType
) intId
;
217 h
= MakeHash (( char *)& idval
, sizeof ( idval
));
218 Insert ( AsnAny :: intHashTbl
, a
, h
);
220 } /* InstallAnyByInt */
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
226 AsnAny :: InstallAnyByOid ( AsnOid
& oid
, int anyId
, AsnType
* type
)
233 a
-> oid
= oid
; // copy given oid
234 a
-> typeToClone
= type
;
236 h
= MakeHash ( oid
. Str (), oid
. Len ());
239 StLock
< Mutex
> _ ( hashTblLock ());
241 if ( AsnAny :: oidHashTbl
== NULL
)
242 AsnAny :: oidHashTbl
= InitHash ();
244 Insert ( AsnAny :: oidHashTbl
, a
, h
);
245 } /* InstallAnyByOid */
248 AsnType
* AsnAny :: Clone () const
253 AsnType
* AsnAny :: Copy () const
255 return new AsnAny (* this );
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
265 AsnAny :: BEnc ( BUF_TYPE b
)
267 return value
-> BEnc ( b
);
273 AsnAny :: BDec ( BUF_TYPE b
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
278 // XXX This is wrong.
279 value
= static_cast < CSM_Buffer
*>( ai
-> typeToClone
-> Clone ());
284 value
-> BDec ( b
, bytesDecoded
, env
);
288 void AsnAny :: Print ( ostream
& os
) const
302 AsnAny
& AsnAny :: operator = ( const AsnAny
& o
)
304 if ( this -> ai
) // take care of most copies.
309 this -> ai
= new AnyInfo
;
312 /* __APPLE__ - I don't think this needs a lock since it's
313 * not modifying the hash tables */
316 this -> intHashTbl
= o
. intHashTbl
; // same pointer.
320 this -> oidHashTbl
= o
. oidHashTbl
;
324 this -> value
= static_cast < CSM_Buffer
*>( o
. value
-> Copy ());