]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-any2.c
Security-54.1.7.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / gen-any2.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 * compiler/back-ends/c-gen/gen-any.c
21 *
22 * prints Routine to initialize the ANY Hash table. The
23 * ANY Hash table maps the OBJECT IDENTIFIERS or INTEGERS
24 * to the correct encoding/decoding etc routines.
25 *
26 * Also prints an enum to identify each ANY mapping.
27 *
28 * if the given module has no ANY or ANY DEFINED BY types
29 * nothing is printed.
30 *
31 * MS 92
32 * Copyright (C) 1991, 1992 Michael Sample
33 * and the University of British Columbia
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * $Header: /cvs/root/Security/SecuritySNACCRuntime/compiler/back-ends/c-gen/Attic/gen-any2.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
41 * $Log: gen-any2.c,v $
42 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
43 * Move from private repository to open source repository
44 *
45 * Revision 1.2 2001/05/05 00:59:28 rmurphy
46 * Adding darwin license headers
47 *
48 * Revision 1.1.1.1 1999/03/16 18:06:41 aram
49 * Originals from SMIME Free Library.
50 *
51 * Revision 1.3 1995/07/25 18:33:43 rj
52 * file name has been shortened for redundant part: c-gen/gen-c-any -> c-gen/gen-any.
53 *
54 * changed `_' to `-' in file names.
55 *
56 * Revision 1.2 1994/09/01 00:21:15 rj
57 * snacc_config.h removed.
58 *
59 * Revision 1.1 1994/08/28 09:48:15 rj
60 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
61 *
62 */
63
64 #include <stdio.h>
65
66 #include "asn-incl.h"
67 #include "mem.h"
68 #include "asn1module.h"
69 #include "rules.h"
70 #include "define.h"
71 #include "str-util.h"
72 #include "gen-vals.h"
73 #include "lib-types.h"
74 #include "gen-any.h"
75
76 int anyEnumValG = 0;
77
78
79 void PrintCAnyEnum PROTO ((FILE *hdr, Module *m, CRules *r));
80
81 void PrintCAnyHashInitRoutine PROTO ((FILE *src, FILE *hdr, ModuleList *mods, Module *m, CRules *r));
82
83
84
85
86 void
87 PrintCAnyCode PARAMS ((src, hdr, r, mods, m),
88 FILE *src _AND_
89 FILE *hdr _AND_
90 CRules *r _AND_
91 ModuleList *mods _AND_
92 Module *m)
93 {
94
95 if (!m->hasAnys)
96 return;
97
98 PrintCAnyEnum (hdr, m, r);
99 PrintCAnyHashInitRoutine (src, hdr, mods, m, r);
100
101 } /* PrintAnyCode */
102
103
104
105 void
106 PrintCAnyEnum PARAMS ((hdr, m, r),
107 FILE *hdr _AND_
108 Module *m _AND_
109 CRules *r)
110 {
111 TypeDef *td;
112 AnyRef *ar;
113 AnyRefList *arl;
114 int i;
115 int firstPrinted = TRUE;
116 char *modName;
117
118 modName = Asn1TypeName2CTypeName (m->modId->name);
119
120 fprintf (hdr,"typedef enum %sAnyId\n", modName);
121 fprintf (hdr,"{\n");
122
123 /* do any lib types */
124 for (i = BASICTYPE_BOOLEAN; i < BASICTYPE_MACRODEF; i++)
125 {
126 arl = LIBTYPE_GET_ANY_REFS (i);
127 if (arl != NULL)
128 {
129 FOR_EACH_LIST_ELMT (ar, arl)
130 {
131 if (!firstPrinted)
132 fprintf (hdr,",\n");
133 fprintf (hdr," %s = %d", ar->anyIdName, anyEnumValG++);
134 firstPrinted = FALSE;
135 }
136 }
137 }
138
139 FOR_EACH_LIST_ELMT (td, m->typeDefs)
140 {
141 if (td->anyRefs != NULL)
142 {
143 FOR_EACH_LIST_ELMT (ar, td->anyRefs)
144 {
145 if (!firstPrinted)
146 fprintf (hdr,",\n");
147 fprintf (hdr," %s = %d", ar->anyIdName, anyEnumValG++);
148 firstPrinted = FALSE;
149 }
150 }
151 }
152 if (firstPrinted) /* none have been printed */
153 fprintf (hdr,"/* NO INTEGER or OBJECT IDENTIFIER to ANY type relationships were defined (via MACROs or other mechanism) */\n???\n");
154
155 fprintf (hdr,"} %sAnyId;\n\n\n", modName);
156 Free (modName);
157
158 } /* PrintAnyEnum */
159
160
161 void
162 PrintCAnyHashInitRoutine PARAMS ((src, hdr, mods, m, r),
163 FILE *src _AND_
164 FILE *hdr _AND_
165 ModuleList *mods _AND_
166 Module *m _AND_
167 CRules *r)
168 {
169 TypeDef *td;
170 AnyRef *ar;
171 AnyRefList *arl;
172 char *modName;
173 CTDI *ctdi;
174 int i,j;
175 enum BasicTypeChoiceId typeId;
176 char *encRoutineName;
177 char *decRoutineName;
178 char *freeRoutineName;
179 char *printRoutineName;
180 int installedSomeHashes = FALSE;
181
182 /* print proto in hdr file */
183 modName = Asn1TypeName2CTypeName (m->modId->name);
184 fprintf (hdr,"void InitAny%s();\n\n", modName);
185
186 /* print routine to src file */
187 fprintf (src,"void\nInitAny%s()\n", modName);
188 fprintf (src,"{\n");
189
190 /* first print value for OID's */
191 /* do any lib types first */
192 i = 0;
193 for (j = BASICTYPE_BOOLEAN; j < BASICTYPE_MACRODEF; j++)
194 {
195 arl = LIBTYPE_GET_ANY_REFS (j);
196 if (arl != NULL)
197 {
198 FOR_EACH_LIST_ELMT (ar, arl)
199 {
200 installedSomeHashes = TRUE;
201 if (ar->id->choiceId == OIDORINT_OID)
202 {
203 fprintf (src," %s oid%d =", r->typeConvTbl[BASICTYPE_OID].cTypeName, i++);
204 PrintCOidValue (src, r, ar->id->a.oid);
205 fprintf (src,";\n");
206 }
207 }
208 }
209 }
210
211 FOR_EACH_LIST_ELMT (td, m->typeDefs)
212 {
213 if (td->anyRefs != NULL)
214 {
215 ctdi = td->cTypeDefInfo;
216 FOR_EACH_LIST_ELMT (ar, td->anyRefs)
217 {
218 installedSomeHashes = TRUE;
219 if (ar->id->choiceId == OIDORINT_OID)
220 {
221 fprintf (src," %s oid%d =", r->typeConvTbl[BASICTYPE_OID].cTypeName, i++);
222 PrintCOidValue (src, r, ar->id->a.oid);
223 fprintf (src,";\n");
224 }
225 }
226 }
227 }
228
229 fprintf (src,"\n\n");
230
231 /* now print hash init calls */
232 i = 0;
233
234 /* do lib types first */
235 for (j = BASICTYPE_BOOLEAN; j < BASICTYPE_MACRODEF; j++)
236 {
237 arl = LIBTYPE_GET_ANY_REFS (j);
238 if (arl != NULL)
239 {
240 FOR_EACH_LIST_ELMT (ar, arl)
241 {
242
243 encRoutineName = r->typeConvTbl[j].encodeRoutineName;
244 decRoutineName = r->typeConvTbl[j].decodeRoutineName;
245 printRoutineName = r->typeConvTbl[j].printRoutineName;
246
247 /*
248 * use NULL free routine for types that
249 * have empyt macros for their free routines
250 * (since the any hash tbl needs the addr of the routine)
251 */
252 switch (j)
253 {
254 case BASICTYPE_BOOLEAN:
255 case BASICTYPE_INTEGER:
256 case BASICTYPE_NULL:
257 case BASICTYPE_REAL:
258 case BASICTYPE_ENUMERATED:
259 freeRoutineName = "NULL";
260 break;
261 default:
262 freeRoutineName = r->typeConvTbl[j].freeRoutineName;
263 }
264
265 if (ar->id->choiceId == OIDORINT_OID)
266 fprintf (src," InstallAnyByOid (%s, &oid%d, sizeof (%s), (EncodeFcn) B%s, (DecodeFcn)B%s, (FreeFcn)%s, (PrintFcn)%s);\n\n", ar->anyIdName, i++, r->typeConvTbl[j].cTypeName, encRoutineName, decRoutineName, freeRoutineName, printRoutineName);
267 else
268 fprintf (src," InstallAnyByInt (%s, %d, sizeof (%s), (EncodeFcn) B%s, (DecodeFcn)B%s, (FreeFcn)%s, (PrintFcn)%s);\n\n", ar->anyIdName, ar->id->a.intId, r->typeConvTbl[j].cTypeName, encRoutineName, decRoutineName, freeRoutineName, printRoutineName);
269 }
270 }
271 }
272
273 FOR_EACH_LIST_ELMT (td, m->typeDefs)
274 {
275 if (td->anyRefs != NULL)
276 {
277 ctdi = td->cTypeDefInfo;
278 FOR_EACH_LIST_ELMT (ar, td->anyRefs)
279 {
280 typeId = GetBuiltinType (td->type);
281
282 encRoutineName = ctdi->encodeRoutineName;
283 decRoutineName = ctdi->decodeRoutineName;
284 printRoutineName = ctdi->printRoutineName;
285
286 /*
287 * use NULL free routine for types that
288 * have empyt macros for their free routines
289 * (since the any hash tbl needs the addr of the routine)
290 */
291 switch (typeId)
292 {
293 case BASICTYPE_BOOLEAN:
294 case BASICTYPE_INTEGER:
295 case BASICTYPE_NULL:
296 case BASICTYPE_REAL:
297 case BASICTYPE_ENUMERATED:
298 freeRoutineName = "NULL";
299 break;
300 default:
301 freeRoutineName = ctdi->freeRoutineName;
302 }
303
304 if (ar->id->choiceId == OIDORINT_OID)
305 fprintf (src," InstallAnyByOid (%s, &oid%d, sizeof (%s), (EncodeFcn) B%s, (DecodeFcn)B%s, (FreeFcn)%s, (PrintFcn)%s);\n\n", ar->anyIdName, i++, ctdi->cTypeName, encRoutineName, decRoutineName, freeRoutineName, printRoutineName);
306 else
307 fprintf (src," InstallAnyByInt (%s, %d, sizeof (%s), (EncodeFcn) B%s, (DecodeFcn)B%s, (FreeFcn)%s, (PrintFcn)%s);\n\n", ar->anyIdName, ar->id->a.intId, ctdi->cTypeName, encRoutineName, decRoutineName, freeRoutineName, printRoutineName);
308 }
309 }
310 }
311
312
313 if (!installedSomeHashes)
314 {
315 fprintf (src," /* Since no INTEGER/OID to ANY type relations were defined\n");
316 fprintf (src," * (usually done via MACROs) you must manually do the code\n");
317 fprintf (src," * to fill the hash tbl.\n");
318 fprintf (src," * if the ids are INTEGER use the following:\n");
319 fprintf (src," * InstallAnyByInt (??_ANY_ID, intVal, sizeof (Foo), (EncodeFcn) BEncFoo, (DecodeFcn)BDecFoo, (FreeFcn)FreeFoo, (PrintFcn)PrintFoo);\n");
320 fprintf (src," * if the ids are OBJECT IDENTIFIERs use the following:\n");
321 fprintf (src," * InstallAnyByOid (??_ANY_ID, oidVal, sizeof (Foo), (EncodeFcn) BEncFoo, (DecodeFcn)BDecFoo, (FreeFcn)FreeFoo, (PrintFcn)PrintFoo);\n");
322 fprintf (src," * put the ??_ANY_IDs in the AnyId enum.\n\n");
323 fprintf (src," * For example if you have some thing like\n");
324 fprintf (src," * T1 ::= SEQUENCE { id INTEGER, ANY DEFINED BY id }\n");
325 fprintf (src," * and the id 1 maps to the type BOOLEAN use the following:\n");
326 fprintf (src," * InstallAnyByInt (SOMEBOOL_ANY_ID, 1, sizeof (AsnBool), (EncodeFcn) BEncAsnBool, (DecodeFcn)BDecAsnBool, (FreeFcn)NULL, (PrintFcn)PrintAsnBool);;\n");
327 fprintf (src," */\n ???????\n"); /* generate compile error */
328 }
329
330
331 fprintf (src,"} /* InitAny%s */\n\n\n", modName);
332
333 Free (modName);
334
335 } /* PrintAnyHashInitRoutine */