]>
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_vals.c - prints ASN.1 values in c++ format | |
21 | * | |
22 | * MS 92 | |
23 | * Copyright (C) 1991, 1992 Michael Sample | |
24 | * and the University of British Columbia | |
25 | * | |
26 | * This program is free software; you can redistribute it and/or modify | |
27 | * it under the terms of the GNU General Public License as published by | |
28 | * the Free Software Foundation; either version 2 of the License, or | |
29 | * (at your option) any later version. | |
30 | * | |
a66d0d4a | 31 | * $Header: /cvs/root/Security/SecuritySNACCRuntime/compiler/back-ends/c++-gen/Attic/gen-vals.c,v 1.3 2001/06/25 21:51:10 dmitch Exp $ |
bac41a7b A |
32 | * $Log: gen-vals.c,v $ |
33 | * Revision 1.3 2001/06/25 21:51:10 dmitch | |
34 | * Avoid instantiating AsnInt constants; use #define instead. Partial fix for Radar 2664258. | |
35 | * | |
36 | * Revision 1.2 2001/06/20 21:30:32 dmitch | |
37 | * Per SNACC_OIDS_AS_DEFINES #define, optionally define OIDs as #defines in the header rather than as statically initialized objects in the .cpp file. | |
38 | * | |
39 | * Revision 1.1.1.1 2001/05/18 23:14:09 mb | |
40 | * Move from private repository to open source repository | |
41 | * | |
42 | * Revision 1.2 2001/05/05 00:59:27 rmurphy | |
43 | * Adding darwin license headers | |
44 | * | |
45 | * Revision 1.1.1.1 1999/03/16 18:06:40 aram | |
46 | * Originals from SMIME Free Library. | |
47 | * | |
48 | * Revision 1.4 1995/07/25 18:20:05 rj | |
49 | * use true/false instead of AsnBool::true/false. | |
50 | * | |
51 | * changed `_' to `-' in file names. | |
52 | * | |
53 | * Revision 1.3 1994/10/08 03:47:57 rj | |
54 | * since i was still irritated by cpp standing for c++ and not the C preprocessor, i renamed them to cxx (which is one known suffix for C++ source files). since the standard #define is __cplusplus, cplusplus would have been the more obvious choice, but it is a little too long. | |
55 | * | |
56 | * Revision 1.2 1994/09/01 00:17:22 rj | |
57 | * snacc_config.h removed. | |
58 | * | |
59 | * Revision 1.1 1994/08/28 09:48:05 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 "asn1module.h" | |
68 | #include "oid.h" | |
69 | #include "mem.h" | |
70 | #include "define.h" | |
71 | #include "lib-types.h" | |
72 | #include "str-util.h" | |
73 | #include "snacc-util.h" | |
74 | #include "rules.h" | |
75 | #include "gen-vals.h" | |
76 | ||
77 | /* non-exported routines' prototypes */ | |
78 | ||
79 | static void PrintCxxValueDefsName PROTO ((FILE *f, CxxRules *r, ValueDef *v)); | |
80 | ||
81 | ||
82 | ||
83 | ||
84 | void | |
85 | PrintCxxValueDef PARAMS ((src, r, v), | |
86 | FILE *src _AND_ | |
87 | CxxRules *r _AND_ | |
88 | ValueDef *v) | |
89 | { | |
90 | /* just do oid's, ints and bools for now */ | |
91 | if ( | |
92 | #if !SNACC_OIDS_AS_DEFINES | |
93 | (v->value->basicValue->choiceId != BASICVALUE_OID) && | |
94 | #endif /* SNACC_OIDS_AS_DEFINES */ | |
95 | #if !SNACC_INTS_AS_DEFINES | |
96 | (v->value->basicValue->choiceId != BASICVALUE_INTEGER) && | |
97 | #endif /* !SNACC_INTS_AS_DEFINES */ | |
98 | (v->value->basicValue->choiceId != BASICVALUE_BOOLEAN)) | |
99 | return; | |
100 | ||
101 | /* | |
102 | * put instantiation in src file | |
103 | */ | |
104 | fprintf (src, "const "); | |
105 | PrintCxxValuesClass (src, r, v->value); | |
106 | fprintf (src, " "); | |
107 | PrintCxxValueDefsName (src, r, v); | |
108 | fprintf (src, " "); | |
109 | PrintCxxValueInstatiation (src, r, v->value); | |
110 | fprintf (src, ";\n\n"); | |
111 | ||
112 | ||
113 | } /* PrintCxxValueDef */ | |
114 | ||
115 | void | |
116 | PrintCxxValueExtern PARAMS ((hdr, r, v), | |
117 | FILE *hdr _AND_ | |
118 | CxxRules *r _AND_ | |
119 | ValueDef *v) | |
120 | { | |
121 | /* just do oid's, ints and bools for now */ | |
122 | if ((v->value->basicValue->choiceId != BASICVALUE_OID) && | |
123 | (v->value->basicValue->choiceId != BASICVALUE_INTEGER) && | |
124 | (v->value->basicValue->choiceId != BASICVALUE_BOOLEAN)) | |
125 | return; | |
126 | ||
127 | #if SNACC_OIDS_AS_DEFINES | |
128 | /* | |
129 | * Special case for OIDs. The format is | |
130 | * | |
131 | * #define oidname_arc x,y,z,... | |
132 | * #define oidname AsnOid(oidname_arc) | |
133 | */ | |
134 | if (v->value->basicValue->choiceId == BASICVALUE_OID) { | |
135 | /* first just the arc */ | |
136 | fprintf (hdr, "#define "); | |
137 | PrintCxxValueDefsName (hdr, r, v); | |
138 | fprintf (hdr, "_arc "); | |
139 | PrintCxxValueInstatiation (hdr, r, v->value); | |
140 | fprintf (hdr, "\n"); | |
141 | ||
142 | /* now the declaration using the arc */ | |
143 | fprintf (hdr, "#define "); | |
144 | PrintCxxValueDefsName (hdr, r, v); | |
145 | fprintf (hdr, " "); | |
146 | PrintCxxValuesClass (hdr, r, v->value); | |
147 | fprintf (hdr, "("); | |
148 | PrintCxxValueDefsName (hdr, r, v); | |
149 | fprintf (hdr, "_arc)\n"); | |
150 | ||
151 | return; | |
152 | } | |
153 | #endif /* SNACC_OIDS_AS_DEFINES */ | |
154 | ||
155 | #if SNACC_INTS_AS_DEFINES | |
156 | /* | |
157 | * Special case for AsnInt. The format is | |
158 | * | |
159 | * #define oidname_val x | |
160 | * #define oidname AsnInt(oidname_val) | |
161 | */ | |
162 | if (v->value->basicValue->choiceId == BASICVALUE_INTEGER) { | |
163 | /* first just the integer value */ | |
164 | fprintf (hdr, "#define "); | |
165 | PrintCxxValueDefsName (hdr, r, v); | |
166 | fprintf (hdr, "_val %d\n", v->value->basicValue->a.integer); | |
167 | ||
168 | /* now the declaration using the val */ | |
169 | fprintf (hdr, "#define "); | |
170 | PrintCxxValueDefsName (hdr, r, v); | |
171 | fprintf (hdr, " "); | |
172 | PrintCxxValuesClass (hdr, r, v->value); | |
173 | fprintf (hdr, "("); | |
174 | PrintCxxValueDefsName (hdr, r, v); | |
175 | fprintf (hdr, "_val)\n"); | |
176 | ||
177 | return; | |
178 | } | |
179 | #endif /* SNACC_OIDS_AS_DEFINES */ | |
180 | ||
181 | /* | |
182 | * put extern declaration in hdr file | |
183 | */ | |
184 | fprintf (hdr, "extern const "); | |
185 | PrintCxxValuesClass (hdr, r, v->value); | |
186 | fprintf (hdr, " "); | |
187 | PrintCxxValueDefsName (hdr, r, v); | |
188 | fprintf (hdr, ";\n"); | |
189 | ||
190 | } /* PrintCxxValueExtern */ | |
191 | ||
192 | ||
193 | static void | |
194 | PrintCxxValueDefsName PARAMS ((f, r, v), | |
195 | FILE *f _AND_ | |
196 | CxxRules *r _AND_ | |
197 | ValueDef *v) | |
198 | { | |
199 | char *cName; | |
200 | cName = Asn1ValueName2CValueName (v->definedName); | |
201 | fprintf (f, "%s", cName); | |
202 | Free (cName); | |
203 | } | |
204 | ||
205 | void | |
206 | PrintCxxValuesClass PARAMS ((f, r, v), | |
207 | FILE *f _AND_ | |
208 | CxxRules *r _AND_ | |
209 | Value *v) | |
210 | { | |
211 | /* needs work - just do ints bools and oid's for now */ | |
212 | switch (v->basicValue->choiceId) | |
213 | { | |
214 | case BASICVALUE_OID: | |
215 | fprintf (f, "%s", r->typeConvTbl[BASICTYPE_OID].className); | |
216 | break; | |
217 | ||
218 | case BASICVALUE_INTEGER: | |
219 | fprintf (f, "%s", r->typeConvTbl[BASICTYPE_INTEGER].className); | |
220 | break; | |
221 | ||
222 | case BASICVALUE_BOOLEAN: | |
223 | fprintf (f, "%s", r->typeConvTbl[BASICTYPE_BOOLEAN].className); | |
224 | break; | |
225 | ||
226 | default: | |
227 | break; | |
228 | } | |
229 | } | |
230 | ||
231 | ||
232 | void | |
233 | PrintCxxValueInstatiation PARAMS ((f, r, v), | |
234 | FILE *f _AND_ | |
235 | CxxRules *r _AND_ | |
236 | Value *v) | |
237 | { | |
238 | /* needs work - just do oids, ints and bools for now */ | |
239 | switch (v->basicValue->choiceId) | |
240 | { | |
241 | case BASICVALUE_OID: | |
242 | PrintCxxOidValue (f, r, v->basicValue->a.oid); | |
243 | break; | |
244 | ||
245 | case BASICVALUE_INTEGER: | |
246 | PrintCxxIntValue (f, r, v->basicValue->a.integer); | |
247 | break; | |
248 | ||
249 | case BASICVALUE_BOOLEAN: | |
250 | fprintf (f, "(%s)", v->basicValue->a.boolean ? "true" : "false"); | |
251 | break; | |
252 | ||
253 | default: | |
254 | break; | |
255 | } | |
256 | } | |
257 | ||
258 | ||
259 | ||
260 | /* | |
261 | * given an AOID, c++ AOID constructors params are produced. | |
262 | * This is used for turning ASN.1 OBJECT ID values | |
263 | * into usable c++ values. | |
264 | * | |
265 | * eg for the oid { 0 1 2 } (in AOID format) | |
266 | * (0,1,2) | |
267 | * is produced. | |
268 | * | |
269 | * The parentheses are omitted in the SNACC_OIDS_AS_DEFINES case; we're just printing | |
270 | * the arc which can be used as an arg elsewhere. | |
271 | ||
272 | */ | |
273 | void | |
274 | PrintCxxOidValue PARAMS ((f, r, v), | |
275 | FILE *f _AND_ | |
276 | CxxRules *r _AND_ | |
277 | AsnOid *v) | |
278 | { | |
279 | unsigned short int firstArcNum; | |
280 | unsigned long int arcNum; | |
281 | int i; | |
282 | ||
283 | #if !SNACC_OIDS_AS_DEFINES | |
284 | fprintf (f, "("); | |
285 | #endif | |
286 | ||
287 | /* un-munge first two arc numbers */ | |
288 | for (arcNum = 0, i=0; (i < v->octetLen) && (v->octs[i] & 0x80);i++) | |
289 | arcNum = (arcNum << 7) + (v->octs[i] & 0x7f); | |
290 | ||
291 | arcNum = (arcNum << 7) + (v->octs[i] & 0x7f); | |
292 | i++; | |
293 | firstArcNum = arcNum/40; | |
294 | if (firstArcNum > 2) | |
295 | firstArcNum = 2; | |
296 | ||
297 | fprintf (f, "%u, %u", firstArcNum, arcNum - (firstArcNum * 40)); | |
298 | ||
299 | for (; i < v->octetLen; ) | |
300 | { | |
301 | for (arcNum = 0; (i < v->octetLen) && (v->octs[i] & 0x80);i++) | |
302 | arcNum = (arcNum << 7) + (v->octs[i] & 0x7f); | |
303 | ||
304 | arcNum = (arcNum << 7) + (v->octs[i] & 0x7f); | |
305 | i++; | |
306 | ||
307 | fprintf (f, ", %u", arcNum); | |
308 | } | |
309 | ||
310 | #if !SNACC_OIDS_AS_DEFINES | |
311 | fprintf (f, ")"); | |
312 | #endif | |
313 | ||
314 | } /* PrintCxxOidValue */ | |
315 | ||
316 | ||
317 | ||
318 | void | |
319 | PrintCxxIntValue PARAMS ((f, r, v), | |
320 | FILE *f _AND_ | |
321 | CxxRules *r _AND_ | |
322 | AsnInt v) | |
323 | { | |
324 | fprintf (f, "(%d)", v); | |
325 | } /* PrintCxxIntValue */ |