]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/compiler/back-ends/c-gen/util.c
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / compiler / back-ends / c-gen / util.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/util.c - utilities for generating C encoders and decoders
21 *
22 * MS 91/11/04
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 *
31 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/compiler/back-ends/c-gen/util.c,v 1.1.1.1 2001/05/18 23:14:09 mb Exp $
32 * $Log: util.c,v $
33 * Revision 1.1.1.1 2001/05/18 23:14:09 mb
34 * Move from private repository to open source repository
35 *
36 * Revision 1.2 2001/05/05 00:59:28 rmurphy
37 * Adding darwin license headers
38 *
39 * Revision 1.1.1.1 1999/03/16 18:06:44 aram
40 * Originals from SMIME Free Library.
41 *
42 * Revision 1.3 1995/07/25 18:48:38 rj
43 * changed `_' to `-' in file names.
44 *
45 * Revision 1.2 1994/09/01 00:26:52 rj
46 * snacc_config.h removed.
47 *
48 * Revision 1.1 1994/08/28 09:48:44 rj
49 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
50 *
51 */
52
53 #include <stdio.h>
54
55 #include "asn-incl.h"
56 #include "asn1module.h"
57 #include "rules.h"
58 #include "snacc-util.h"
59 #include "util.h"
60
61
62 void
63 MakeVarPtrRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
64 CRules *r _AND_
65 TypeDef *td _AND_
66 Type *parent _AND_
67 Type *fieldType _AND_
68 char *parentVarName _AND_
69 char *newVarName)
70 {
71 CTRI *ctri;
72
73 ctri = fieldType->cTypeRefInfo;
74
75 /* always put in brackets to save future referencing hassles */
76 strcpy (newVarName, "(");
77
78 /* make ref'd field into a ptr by taking it's addr if nec */
79 if (!ctri->isPtr)
80 strcat (newVarName, "&");
81
82 /* start with ref to parent */
83 strcat (newVarName, parentVarName);
84
85 /* ref this field */
86 if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
87 strcat (newVarName, "->");
88 else
89 strcat (newVarName, ".");
90
91 /* ref choice union field if nec */
92 if (parent->basicType->choiceId == BASICTYPE_CHOICE)
93 {
94 strcat (newVarName, r->choiceUnionFieldName);
95 strcat (newVarName, ".");
96 }
97
98 strcat (newVarName, ctri->cFieldName);
99 strcat (newVarName, ")");
100
101 } /* MakeVarPtrRef */
102
103
104
105
106 void
107 MakeVarValueRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
108 CRules *r _AND_
109 TypeDef *td _AND_
110 Type *parent _AND_
111 Type *fieldType _AND_
112 char *parentVarName _AND_
113 char *newVarName)
114 {
115 CTRI *ctri;
116
117 ctri = fieldType->cTypeRefInfo;
118
119 /* always put in brackets to save future referencing hassles */
120 strcpy (newVarName, "(");
121
122 /* make ref'd field into a value by de-referencing if nec */
123 if (ctri->isPtr)
124 strcat (newVarName, "*");
125
126 /* start with ref to parent */
127 strcat (newVarName, parentVarName);
128
129 /* ref this field */
130 if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
131 strcat (newVarName, "->");
132 else
133 strcat (newVarName, ".");
134
135 /* ref choice union field if nec */
136 if (parent->basicType->choiceId == BASICTYPE_CHOICE)
137 {
138 strcat (newVarName, r->choiceUnionFieldName);
139 strcat (newVarName, ".");
140 }
141
142 strcat (newVarName, ctri->cFieldName);
143 strcat (newVarName, ")");
144
145 } /* MakeVarValueRef */
146
147 void
148 MakeChoiceIdValueRef PARAMS ((r, td, parent, fieldType, parentVarName, newVarName),
149 CRules *r _AND_
150 TypeDef *td _AND_
151 Type *parent _AND_
152 Type *fieldType _AND_
153 char *parentVarName _AND_
154 char *newVarName)
155 {
156 CTRI *ctri;
157
158 ctri = fieldType->cTypeRefInfo;
159
160 /* always put in brackets to save future referencing hassles */
161 strcpy (newVarName, "(");
162
163 /* start with ref to parent */
164 strcat (newVarName, parentVarName);
165
166 /* ref this field */
167 if ((td->type == parent) || (parent->cTypeRefInfo->isPtr))
168 strcat (newVarName, "->");
169 else
170 strcat (newVarName, ".");
171
172 strcat (newVarName, parent->cTypeRefInfo->choiceIdEnumFieldName);
173 strcat (newVarName, ")");
174
175 } /* MakeChoiceIdValueRef */
176
177
178 void
179 PrintElmtAllocCode PARAMS ((src, type, varRefPtrName),
180 FILE *src _AND_
181 Type *type _AND_
182 char *varRefPtrName)
183 {
184 CTRI *ctri1;
185 CTRI *ctri2;
186 Type *t;
187
188 t = GetType (type);
189 ctri1 = type->cTypeRefInfo;
190 ctri2 = t->cTypeRefInfo;
191 if (ctri1->isPtr)
192 {
193 if (ctri2->cTypeId == C_LIST)
194 fprintf (src, " %s = AsnListNew (sizeof (char*));\n", varRefPtrName);
195 else
196 fprintf (src, " %s = (%s*) Asn1Alloc (sizeof (%s));\n", varRefPtrName, ctri1->cTypeName, ctri1->cTypeName);
197 fprintf (src," CheckAsn1Alloc (%s, env);\n", varRefPtrName);
198 }
199
200 } /* PrintElmtAllocCode */
201
202
203 /*
204 * prints code to decode EOCs for the lengths that go with extra tagging
205 * maxLenLevel - the highest used length variable (ie 2 for elmtLen2)
206 * minLenLevel - the lowest valid length variable (ie 0 for elmtLen0)
207 * lenBaseVarName - len var name sans number (ie elmtLen for elmtLen2)
208 * totalLevel - current level for the running total
209 * totalBaseName - total var name sans number
210 * (ie totalElmtLen for totalElmtLen1)
211 */
212 void
213 PrintEocDecoders PARAMS ((f, maxLenLevel, minLenLevel, lenBaseVarName, totalLevel, totalBaseVarName),
214 FILE *f _AND_
215 int maxLenLevel _AND_
216 int minLenLevel _AND_
217 char *lenBaseVarName _AND_
218 int totalLevel _AND_
219 char *totalBaseVarName)
220 {
221 int i;
222 for (i = maxLenLevel; i > minLenLevel; i--)
223 {
224 fprintf (f," if (%s%d == INDEFINITE_LEN)\n", lenBaseVarName, i);
225 fprintf (f," BDecEoc (b, &%s%d, env);\n", totalBaseVarName, totalLevel);
226 }
227 } /* PrintEocDeocoders */