]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-code.c
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / gen-code.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-code.c - generate C hdr and src files
21 *
22 * Assumes you have called FillCTypeInfo
23 *
24 * MS 92
25 * Copyright (C) 1991, 1992 Michael Sample
26 * and the University of British Columbia
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/compiler/back-ends/c-gen/gen-code.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
34 * $Log: gen-code.c,v $
35 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
36 * Move from private repository to open source repository
37 *
38 * Revision 1.2 2001/05/05 00:59:28 rmurphy
39 * Adding darwin license headers
40 *
41 * Revision 1.1 2000/05/10 21:35:01 rmurphy
42 * Adding back in base code files which had been moved to "2" versions.
43 *
44 * Revision 1.1.1.1 1999/03/16 18:06:41 aram
45 * Originals from SMIME Free Library.
46 *
47 * Revision 1.4 1995/07/25 18:39:46 rj
48 * file name has been shortened for redundant part: c-gen/gen-c-code -> c-gen/gen-code.
49 *
50 * PrintConditionalIncludeOpen() and PrintConditionalIncludeClose() moved to back-ends/cond.c
51 *
52 * changed `_' to `-' in file names.
53 *
54 * Revision 1.3 1995/02/18 12:50:53 rj
55 * typo fixed.
56 *
57 * Revision 1.2 1994/09/01 00:21:54 rj
58 * snacc_config.h and other superfluous .h files removed.
59 *
60 * Revision 1.1 1994/08/28 09:48:17 rj
61 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
62 *
63 */
64
65 #include <stdio.h>
66
67 #include "asn-incl.h"
68 #include "asn1module.h"
69 #include "mem.h"
70 #include "print.h"
71 #include "rules.h"
72 #include "type-info.h"
73 #include "util.h"
74 #include "cond.h"
75 #include "gen-type.h"
76 #include "gen-enc.h"
77 #include "gen-dec.h"
78 #include "gen-vals.h"
79 #include "gen-free.h"
80 #include "gen-print.h"
81 #include "gen-any.h"
82 #include "gen-code.h"
83
84 /* unexported prototypes */
85 static void PrintCSrcComment PROTO ((FILE *src, Module *m));
86 static void PrintCSrcIncludes PROTO ((FILE *src, Module *m, ModuleList *mods));
87 static void PrintCHdrComment PROTO ((FILE *hdr, Module *m));
88
89 /*
90 * Fills the hdr file with the C type and encode/decode prototypes
91 * Fills the src file with the encoded/decode routine definitions
92 */
93 void
94 PrintCCode PARAMS ((src, hdr, mods, m, r, longJmpVal, printTypes, printValues, printEncoders, printDecoders, printPrinters, printFree),
95 FILE *src _AND_
96 FILE *hdr _AND_
97 ModuleList *mods _AND_
98 Module *m _AND_
99 CRules *r _AND_
100 long int longJmpVal _AND_
101 int printTypes _AND_
102 int printValues _AND_
103 int printEncoders _AND_
104 int printDecoders _AND_
105 int printPrinters _AND_
106 int printFree)
107 {
108 TypeDef *td;
109 ValueDef *vd;
110
111 PrintCSrcComment (src, m);
112 PrintCSrcIncludes (src, m, mods);
113
114 PrintCHdrComment (hdr, m);
115 PrintConditionalIncludeOpen (hdr, m->cHdrFileName);
116
117 fprintf (hdr,"\n\n");
118 fprintf (src,"\n\n");
119
120
121 if (printValues)
122 {
123 /* put value defs at beginning of .c file */
124 FOR_EACH_LIST_ELMT (vd, m->valueDefs)
125 {
126 PrintCValueDef (src, r, vd);
127 }
128 }
129
130 PrintCAnyCode (src, hdr, r, mods, m);
131
132 FOR_EACH_LIST_ELMT (td, m->typeDefs)
133 {
134 if (printTypes)
135 PrintCTypeDef (hdr, r, m, td);
136
137 /* for PDU type or types ref'd with ANY/ANY DEF BY */
138 if (printEncoders &&
139 ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu))
140 PrintCBerEncoder (src, hdr, r, m, td);
141
142 /* for PDU type or types ref'd with ANY/ANY DEF BY */
143 if (printDecoders &&
144 ((td->anyRefs != NULL) || td->cTypeDefInfo->isPdu))
145 PrintCBerDecoder (src, hdr, r, m, td, &longJmpVal);
146
147 if (printEncoders)
148 PrintCBerContentEncoder (src, hdr, r, m, td);
149
150 if (printDecoders)
151 PrintCBerContentDecoder (src, hdr, r, m, td, &longJmpVal);
152
153
154 if (printPrinters)
155 PrintCPrinter (src, hdr, r, mods, m, td);
156
157 if (printFree)
158 PrintCFree (src, hdr, r, mods, m, td);
159
160 /* only print new lines for normal types */
161 switch (td->type->basicType->choiceId)
162 {
163 case BASICTYPE_SEQUENCEOF: /* list types */
164 case BASICTYPE_SETOF:
165 case BASICTYPE_CHOICE:
166 case BASICTYPE_SET:
167 case BASICTYPE_SEQUENCE:
168 fprintf (src, "\n\n\n");
169 /* fall through */
170
171 case BASICTYPE_IMPORTTYPEREF: /* type references */
172 case BASICTYPE_LOCALTYPEREF:
173 case BASICTYPE_BOOLEAN: /* library type */
174 case BASICTYPE_REAL: /* library type */
175 case BASICTYPE_OCTETSTRING: /* library type */
176 case BASICTYPE_NULL: /* library type */
177 case BASICTYPE_OID: /* library type */
178 case BASICTYPE_INTEGER: /* library type */
179 case BASICTYPE_BITSTRING: /* library type */
180 case BASICTYPE_ENUMERATED: /* library type */
181 case BASICTYPE_ANYDEFINEDBY: /* ANY types */
182 case BASICTYPE_ANY:
183 fprintf (hdr, "\n\n\n");
184 break;
185 }
186
187 }
188
189 if (printValues)
190 {
191 /* put value externs at end of .h file */
192 FOR_EACH_LIST_ELMT (vd, m->valueDefs)
193 {
194 PrintCValueExtern (hdr, r, vd);
195 }
196 }
197
198 PrintConditionalIncludeClose (hdr, m->cHdrFileName);
199
200 } /* PrintCCode */
201
202
203 static void
204 PrintCSrcComment PARAMS ((src, m),
205 FILE *src _AND_
206 Module *m)
207 {
208 long int t;
209
210 t = time (0);
211 fprintf (src, "/*\n");
212 fprintf (src, " * %s\n *\n", m->cSrcFileName);
213 fprintf (src, " * \"%s\" ASN.1 module encode/decode/print/free C src.\n *\n", m->modId->name);
214 fprintf (src, " * This file was generated by snacc on %s *\n", ctime (&t));
215 fprintf (src, " * UBC snacc written by Mike Sample\n *\n");
216 fprintf (src, " * NOTE: This is a machine generated file - editing not recommended\n");
217 fprintf (src, " */\n\n\n");
218
219 } /* PrintSrcComment */
220
221
222
223 static void
224 PrintCSrcIncludes PARAMS ((src, m, mods),
225 FILE *src _AND_
226 Module *m _AND_
227 ModuleList *mods)
228 {
229 void *tmp;
230 Module *impMod;
231
232 /*
233 * include snacc runtime library related hdrs
234 */
235 fprintf (src, "\n#include \"asn-incl.h\"\n");
236
237 /*
238 * print out include files in same order of the module
239 * list. every module in the list includes the others and it's
240 * own .h
241 */
242 tmp = (void*)CURR_LIST_NODE (mods);
243 FOR_EACH_LIST_ELMT (impMod, mods)
244 {
245 fprintf (src, "#include \"%s\"\n", impMod->cHdrFileName);
246 }
247 SET_CURR_LIST_NODE (mods, tmp);
248
249 } /* PrintCSrcIncludes */
250
251
252 static void
253 PrintCHdrComment PARAMS ((f, m),
254 FILE *f _AND_
255 Module *m)
256 {
257 long int t;
258
259 t = time (0);
260 fprintf (f, "/*\n");
261 fprintf (f, " * %s\n *\n", m->cHdrFileName);
262 fprintf (f, " * \"%s\" ASN.1 module C type definitions and prototypes\n *\n", m->modId->name);
263 fprintf (f, " * This .h file was generated by snacc on %s *\n", ctime (&t));
264 fprintf (f, " * UBC snacc written compiler by Mike Sample\n *\n");
265 fprintf (f, " * NOTE: This is a machine generated file--editing not recommended\n");
266 fprintf (f, " */\n\n\n");
267 } /* PrintCHdrComment */