]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-any.c
Security-54.1.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / src / asn-any.c
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 /*
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.
22 *
23 * MS 92
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
25 *
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
28 * in original form.
29 *
30 * If you modify this file, you must clearly indicate your changes.
31 *
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.
35 *
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 $
37 * $Log: asn-any.c,v $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
40 *
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
43 *
44 * Revision 1.1.1.1 1999/03/16 18:06:30 aram
45 * Originals from SMIME Free Library.
46 *
47 * Revision 1.3 1997/02/28 13:39:49 wan
48 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
49 *
50 * Revision 1.2 1995/07/24 21:04:48 rj
51 * changed `_' to `-' in file names.
52 *
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.
55 *
56 */
57
58 #include "asn-config.h"
59 #include "asn-len.h"
60 #include "asn-tag.h"
61 #include "asn-oid.h"
62 #include "asn-int.h"
63 #include "asn-any.h"
64
65 /*
66 * 2 hash tables. 1 for INTEGER to type mappings the other
67 * for OBJECT IDENTIFER to type mappings.
68 */
69 Table *anyOidHashTblG = NULL;
70 Table *anyIntHashTblG = NULL;
71
72 /*
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.
76 */
77 void
78 SetAnyTypeByInt PARAMS ((v, id),
79 AsnAny *v _AND_
80 AsnInt id)
81 {
82 Hash hash;
83 void *anyInfo;
84
85 /* use int as hash string */
86 hash = MakeHash ((char*)&id, sizeof (id));
87 if (CheckForAndReturnValue (anyIntHashTblG, hash, &anyInfo))
88 v->ai = (AnyInfo*) anyInfo;
89 else
90 v->ai = NULL; /* indicates failure */
91
92 } /* SetAnyTypeByInt */
93
94
95 /*
96 * Same as SetAnyTypeByInt except that the hash key is an OBJECT IDENTIFER.
97 */
98 void SetAnyTypeByOid PARAMS ((v, id),
99 AsnAny *v _AND_
100 AsnOid *id)
101 {
102 Hash hash;
103 void *anyInfo;
104
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;
109 else
110 v->ai = NULL; /* indicates failure */
111
112 } /* SetAnyTypeByOid */
113
114
115 /*
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.
122 *
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.
127 */
128 void
129 InstallAnyByInt PARAMS ((anyId, intId, size, Encode, Decode, Free, Print),
130 int anyId _AND_
131 AsnInt intId _AND_
132 unsigned int size _AND_
133 EncodeFcn Encode _AND_
134 DecodeFcn Decode _AND_
135 FreeFcn Free _AND_
136 PrintFcn Print)
137 {
138 AnyInfo *a;
139 Hash h;
140
141 a = (AnyInfo*) malloc (sizeof (AnyInfo));
142 a->anyId = anyId;
143 a->oid.octs = NULL;
144 a->oid.octetLen = 0;
145 a->intId = intId;
146 a->size = size;
147 a->Encode = Encode;
148 a->Decode = Decode;
149 a->Free = Free;
150 a->Print = Print;
151
152 if (anyIntHashTblG == NULL)
153 anyIntHashTblG = InitHash();
154
155 h = MakeHash ((char*)&intId, sizeof (intId));
156 Insert (anyIntHashTblG, a, h);
157
158 } /* InstallAnyByOid */
159
160
161 /*
162 * Same as InstallAnyByInt except the oid is used as the hash key
163 */
164 void
165 InstallAnyByOid PARAMS ((anyId, oid, size, Encode, Decode, Free, Print),
166 int anyId _AND_
167 AsnOid *oid _AND_
168 unsigned int size _AND_
169 EncodeFcn Encode _AND_
170 DecodeFcn Decode _AND_
171 FreeFcn Free _AND_
172 PrintFcn Print)
173 {
174 AnyInfo *a;
175 Hash h;
176
177 a = (AnyInfo*) malloc (sizeof (AnyInfo));
178 a->anyId = anyId;
179 a->oid.octs = oid->octs;
180 a->oid.octetLen = oid->octetLen;
181 a->size = size;
182 a->Encode = Encode;
183 a->Decode = Decode;
184 a->Free = Free;
185 a->Print = Print;
186
187 h = MakeHash (oid->octs, oid->octetLen);
188
189 if (anyOidHashTblG == NULL)
190 anyOidHashTblG = InitHash();
191
192 Insert (anyOidHashTblG, a, h);
193
194 } /* InstallAnyByOid */
195
196
197 /*
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
201 * values)
202 */
203 void
204 FreeAsnAny PARAMS ((v),
205 AsnAny *v)
206 {
207 if ((v->ai != NULL) && (v->ai->Free != NULL))
208 v->ai->Free (v->value);
209 } /* FreeAsnAny */
210
211
212 /*
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.
218 */
219 AsnLen
220 BEncAsnAny PARAMS ((b, v),
221 BUF_TYPE b _AND_
222 AsnAny *v)
223 {
224 if ((v->ai != NULL) && (v->ai->Encode != NULL))
225 return v->ai->Encode (b, v->value);
226 else
227 return 0;
228 } /* BEncAsnAny */
229
230
231 /*
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.
236 */
237 void BDecAsnAny PARAMS ((b, result, bytesDecoded, env),
238 BUF_TYPE b _AND_
239 AsnAny *result _AND_
240 AsnLen *bytesDecoded _AND_
241 ENV_TYPE env)
242 {
243 if ((result->ai != NULL) && (result->ai->Decode != NULL))
244 {
245 result->value = (void*) Asn1Alloc (result->ai->size);
246 result->ai->Decode (b, result->value, bytesDecoded, env);
247 }
248 else
249 {
250 Asn1Error ("ERROR - ANY Decode routine is NULL\n");
251 longjmp (env, -44);
252 }
253 }
254
255 /*
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.
259 */
260 void PrintAsnAny PARAMS ((f, v, indent),
261 FILE *f _AND_
262 AsnAny *v _AND_
263 unsigned short indent)
264 {
265 if ((v->ai != NULL) && (v->ai->Print != NULL))
266 v->ai->Print (f, v->value);
267 else
268 fprintf (f," -- ERROR: malformed ANY value --");
269 }