]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-any.c
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.
20 * asn_any.c - BER encode, decode, print, free, type set up and installation
21 * routines for the ASN.1 ANY and ANY DEFINED BY types.
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
30 * If you modify this file, you must clearly indicate your changes.
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c-lib/src/asn-any.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
44 * Revision 1.1.1.1 1999/03/16 18:06:30 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.3 1997/02/28 13:39:49 wan
48 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
50 * Revision 1.2 1995/07/24 21:04:48 rj
51 * changed `_' to `-' in file names.
53 * Revision 1.1 1994/08/28 09:45:49 rj
54 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
58 #include "asn-config.h"
66 * 2 hash tables. 1 for INTEGER to type mappings the other
67 * for OBJECT IDENTIFER to type mappings.
69 Table
*anyOidHashTblG
= NULL
;
70 Table
*anyIntHashTblG
= NULL
;
73 * given an ANY type value and a integer hash key, this defines
74 * this any values type (gets ptr to hash tbl entry from int key).
75 * The hash table entry contains ptrs to the encode/decode etc. routines.
78 SetAnyTypeByInt
PARAMS ((v
, id
),
85 /* use int as hash string */
86 hash
= MakeHash ((char*)&id
, sizeof (id
));
87 if (CheckForAndReturnValue (anyIntHashTblG
, hash
, &anyInfo
))
88 v
->ai
= (AnyInfo
*) anyInfo
;
90 v
->ai
= NULL
; /* indicates failure */
92 } /* SetAnyTypeByInt */
96 * Same as SetAnyTypeByInt except that the hash key is an OBJECT IDENTIFER.
98 void SetAnyTypeByOid
PARAMS ((v
, id
),
105 /* use encoded oid as hash string */
106 hash
= MakeHash (id
->octs
, id
->octetLen
);
107 if (CheckForAndReturnValue (anyOidHashTblG
, hash
, &anyInfo
))
108 v
->ai
= (AnyInfo
*) anyInfo
;
110 v
->ai
= NULL
; /* indicates failure */
112 } /* SetAnyTypeByOid */
116 * Creates an entry in the hash table that contains the
117 * type's size, encode, decode, free, and print routines and anyId.
118 * The given intId is used as the hash key so future calls to
119 * SetAnyTypeByInt with that intId as the id will reference this entry.
120 * The anyId is stored in the hash tbl entry as well so the user can
121 * figure out the type with a simple integer comparison.
123 * This routine is usually called from the AnyInit routine that
124 * the compiler generates from MACRO info. Call this routine
125 * once for each possible ANY type to set up the hash table.
126 * Future calls to SetAnyTypeByInt/Oid will reference this table.
129 InstallAnyByInt
PARAMS ((anyId
, intId
, size
, Encode
, Decode
, Free
, Print
),
132 unsigned int size _AND_
133 EncodeFcn Encode _AND_
134 DecodeFcn Decode _AND_
141 a
= (AnyInfo
*) malloc (sizeof (AnyInfo
));
152 if (anyIntHashTblG
== NULL
)
153 anyIntHashTblG
= InitHash();
155 h
= MakeHash ((char*)&intId
, sizeof (intId
));
156 Insert (anyIntHashTblG
, a
, h
);
158 } /* InstallAnyByOid */
162 * Same as InstallAnyByInt except the oid is used as the hash key
165 InstallAnyByOid
PARAMS ((anyId
, oid
, size
, Encode
, Decode
, Free
, Print
),
168 unsigned int size _AND_
169 EncodeFcn Encode _AND_
170 DecodeFcn Decode _AND_
177 a
= (AnyInfo
*) malloc (sizeof (AnyInfo
));
179 a
->oid
.octs
= oid
->octs
;
180 a
->oid
.octetLen
= oid
->octetLen
;
187 h
= MakeHash (oid
->octs
, oid
->octetLen
);
189 if (anyOidHashTblG
== NULL
)
190 anyOidHashTblG
= InitHash();
192 Insert (anyOidHashTblG
, a
, h
);
194 } /* InstallAnyByOid */
198 * Calls the free routine in this type's any info.
199 * If the routine ptr is NULL, nothing is done
200 * (This is the case for INTEGERs, BOOLEANs and other simple
204 FreeAsnAny
PARAMS ((v
),
207 if ((v
->ai
!= NULL
) && (v
->ai
->Free
!= NULL
))
208 v
->ai
->Free (v
->value
);
213 * Calls the Encode routine pointed to in the given type's
214 * Any Info. If the routine ptr is NULL nothing is encoded
215 * (This should set some type of error).
216 * Note: this calls the BEncFoo not BEncFooContent routine form
217 * since the tags are needed too.
220 BEncAsnAny
PARAMS ((b
, v
),
224 if ((v
->ai
!= NULL
) && (v
->ai
->Encode
!= NULL
))
225 return v
->ai
->Encode (b
, v
->value
);
232 * Calls the Decode routine pointed to in the given type's
233 * Any Info. If the routine ptr is NULL any error is flagged.
234 * Note: this calls the BDecFoo not BDecFooContent routine form
235 * since the tags are needed too.
237 void BDecAsnAny
PARAMS ((b
, result
, bytesDecoded
, env
),
240 AsnLen
*bytesDecoded _AND_
243 if ((result
->ai
!= NULL
) && (result
->ai
->Decode
!= NULL
))
245 result
->value
= (void*) Asn1Alloc (result
->ai
->size
);
246 result
->ai
->Decode (b
, result
->value
, bytesDecoded
, env
);
250 Asn1Error ("ERROR - ANY Decode routine is NULL\n");
256 * Calls the print routine pointed to from the given type's
257 * Any Info. Prints an error if the type does not have
258 * any 'AnyInfo' or if the AnyInfo has a NULL Print routine ptr.
260 void PrintAsnAny
PARAMS ((f
, v
, indent
),
263 unsigned short indent
)
265 if ((v
->ai
!= NULL
) && (v
->ai
->Print
!= NULL
))
266 v
->ai
->Print (f
, v
->value
);
268 fprintf (f
," -- ERROR: malformed ANY value --");