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