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 * compiler/back_ends/c++_gen/gen_vals.c - prints ASN.1 values in c++ format
23 * Copyright (C) 1991, 1992 Michael Sample
24 * and the University of British Columbia
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.
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 $
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.
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.
39 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
40 * Move from private repository to open source repository
42 * Revision 1.2 2001/05/05 00:59:27 rmurphy
43 * Adding darwin license headers
45 * Revision 1.1.1.1 1999/03/16 18:06:40 aram
46 * Originals from SMIME Free Library.
48 * Revision 1.4 1995/07/25 18:20:05 rj
49 * use true/false instead of AsnBool::true/false.
51 * changed `_' to `-' in file names.
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.
56 * Revision 1.2 1994/09/01 00:17:22 rj
57 * snacc_config.h removed.
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.
67 #include "asn1module.h"
71 #include "lib-types.h"
73 #include "snacc-util.h"
77 /* non-exported routines' prototypes */
79 static void PrintCxxValueDefsName
PROTO ((FILE *f
, CxxRules
*r
, ValueDef
*v
));
85 PrintCxxValueDef
PARAMS ((src
, r
, v
),
90 /* just do oid's, ints and bools for now */
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
))
102 * put instantiation in src file
104 fprintf (src
, "const ");
105 PrintCxxValuesClass (src
, r
, v
->value
);
107 PrintCxxValueDefsName (src
, r
, v
);
109 PrintCxxValueInstatiation (src
, r
, v
->value
);
110 fprintf (src
, ";\n\n");
113 } /* PrintCxxValueDef */
116 PrintCxxValueExtern
PARAMS ((hdr
, r
, v
),
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
))
127 #if SNACC_OIDS_AS_DEFINES
129 * Special case for OIDs. The format is
131 * #define oidname_arc x,y,z,...
132 * #define oidname AsnOid(oidname_arc)
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
);
142 /* now the declaration using the arc */
143 fprintf (hdr
, "#define ");
144 PrintCxxValueDefsName (hdr
, r
, v
);
146 PrintCxxValuesClass (hdr
, r
, v
->value
);
148 PrintCxxValueDefsName (hdr
, r
, v
);
149 fprintf (hdr
, "_arc)\n");
153 #endif /* SNACC_OIDS_AS_DEFINES */
155 #if SNACC_INTS_AS_DEFINES
157 * Special case for AsnInt. The format is
159 * #define oidname_val x
160 * #define oidname AsnInt(oidname_val)
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
);
168 /* now the declaration using the val */
169 fprintf (hdr
, "#define ");
170 PrintCxxValueDefsName (hdr
, r
, v
);
172 PrintCxxValuesClass (hdr
, r
, v
->value
);
174 PrintCxxValueDefsName (hdr
, r
, v
);
175 fprintf (hdr
, "_val)\n");
179 #endif /* SNACC_OIDS_AS_DEFINES */
182 * put extern declaration in hdr file
184 fprintf (hdr
, "extern const ");
185 PrintCxxValuesClass (hdr
, r
, v
->value
);
187 PrintCxxValueDefsName (hdr
, r
, v
);
188 fprintf (hdr
, ";\n");
190 } /* PrintCxxValueExtern */
194 PrintCxxValueDefsName
PARAMS ((f
, r
, v
),
200 cName
= Asn1ValueName2CValueName (v
->definedName
);
201 fprintf (f
, "%s", cName
);
206 PrintCxxValuesClass
PARAMS ((f
, r
, v
),
211 /* needs work - just do ints bools and oid's for now */
212 switch (v
->basicValue
->choiceId
)
215 fprintf (f
, "%s", r
->typeConvTbl
[BASICTYPE_OID
].className
);
218 case BASICVALUE_INTEGER
:
219 fprintf (f
, "%s", r
->typeConvTbl
[BASICTYPE_INTEGER
].className
);
222 case BASICVALUE_BOOLEAN
:
223 fprintf (f
, "%s", r
->typeConvTbl
[BASICTYPE_BOOLEAN
].className
);
233 PrintCxxValueInstatiation
PARAMS ((f
, r
, v
),
238 /* needs work - just do oids, ints and bools for now */
239 switch (v
->basicValue
->choiceId
)
242 PrintCxxOidValue (f
, r
, v
->basicValue
->a
.oid
);
245 case BASICVALUE_INTEGER
:
246 PrintCxxIntValue (f
, r
, v
->basicValue
->a
.integer
);
249 case BASICVALUE_BOOLEAN
:
250 fprintf (f
, "(%s)", v
->basicValue
->a
.boolean
? "true" : "false");
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.
265 * eg for the oid { 0 1 2 } (in AOID format)
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.
274 PrintCxxOidValue
PARAMS ((f
, r
, v
),
279 unsigned short int firstArcNum
;
280 unsigned long int arcNum
;
283 #if !SNACC_OIDS_AS_DEFINES
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);
291 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
293 firstArcNum
= arcNum
/40;
297 fprintf (f
, "%u, %u", firstArcNum
, arcNum
- (firstArcNum
* 40));
299 for (; i
< v
->octetLen
; )
301 for (arcNum
= 0; (i
< v
->octetLen
) && (v
->octs
[i
] & 0x80);i
++)
302 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
304 arcNum
= (arcNum
<< 7) + (v
->octs
[i
] & 0x7f);
307 fprintf (f
, ", %u", arcNum
);
310 #if !SNACC_OIDS_AS_DEFINES
314 } /* PrintCxxOidValue */
319 PrintCxxIntValue
PARAMS ((f
, r
, v
),
324 fprintf (f
, "(%d)", v
);
325 } /* PrintCxxIntValue */