]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/tbl-enc.c
Security-28.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / src / tbl-enc.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 #ifdef TTBL
20
21 /*
22 * tbl_enc.c - type table encoder
23 *
24 *
25 * Mike Sample
26 *
27 * Copyright (C) 1993 Michael Sample
28 * and the University of British Columbia
29 * This library is free software; you can redistribute it and/or
30 * modify it provided that this copyright/license information is retained
31 * in original form.
32 *
33 * If you modify this file, you must clearly indicate your changes.
34 *
35 * This source code is distributed in the hope that it will be
36 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
37 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
38 */
39
40
41 #include <stdio.h>
42 #include "tbl-incl.h"
43
44 /* protos for non exported routines */
45
46 /*
47 int TblEncodePrimTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
48
49 int TblEncodeConsTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
50 */
51
52 int TblEncodeTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
53
54 AsnLen TblEncTag PROTO ((BUF_TYPE b, TBLTag *tag));
55
56
57
58
59 /*
60 * Encode value v as though it is of type modName.typeName.
61 * bytesEncoded is set the actual number of bytes in the
62 * encode value.
63 * returns less than zero if an error occurs otherwise
64 * returns 0 for success.
65 */
66 int
67 TblEncode PARAMS ((tbl, modName, typeName, b, v, bytesEncoded),
68 TBL *tbl _AND_
69 char *modName _AND_
70 char *typeName _AND_
71 BUF_TYPE b _AND_
72 AVal *v _AND_
73 unsigned long int *bytesEncoded)
74 {
75 TBLModule *tblMod;
76 TBLTypeDef *tblTd;
77 int retVal;
78
79 tblTd = TblFindTypeDef (tbl, modName, typeName, &tblMod);
80 if (tblTd == NULL)
81 {
82 TblError ("TblEncode: Could not find a type definition with the given module and name");
83 return -1;
84 }
85 *bytesEncoded = 0;
86 retVal = TblEncodeType (tblTd->type, b, v, FALSE, bytesEncoded);
87
88 if (BufWriteError (b))
89 retVal = -1;
90
91 return retVal;
92 } /* TblEncode */
93
94
95 /*
96 * returns less than zero if an error occurs
97 */
98 int
99 TblEncodeType PARAMS ((tblT, b, v, implicit, bytesEncoded),
100 TBLType *tblT _AND_
101 BUF_TYPE b _AND_
102 AVal *v _AND_
103 int implicit _AND_
104 unsigned long int *bytesEncoded)
105 {
106 AVal *elmtV;
107 AsnList *lVal;
108 int retVal = 0;
109 unsigned long int tmpBytesEncoded = 0;
110 unsigned int currElmt;
111 TBLType *listElmtType;
112 TBLType *structElmtType;
113 TBLType *choiceElmtType;
114 AChoiceVal *cVal;
115 AStructVal *sVal;
116 int implicitRef;
117 void *tmp;
118
119 switch (tblT->typeId)
120 {
121 case TBL_TYPEREF:
122
123 /*
124 * carry over implicit ref if goes
125 * through typeref with no tags
126 */
127 implicitRef = tblT->content->a.typeRef->implicit ||
128 (implicit &&
129 ((tblT->tagList == NULL) || LIST_EMPTY (tblT->tagList)));
130
131 retVal = TblEncodeType (tblT->content->a.typeRef->typeDefPtr->type, b, v, implicitRef, &tmpBytesEncoded);
132 break;
133
134 case TBL_SEQUENCE:
135 case TBL_SET:
136 /* rvs though list value and list type def */
137 currElmt = LIST_COUNT (tblT->content->a.elmts)-1;
138 sVal = (AStructVal*)v;
139 tmp = CURR_LIST_NODE (tblT->content->a.elmts);
140 FOR_EACH_LIST_ELMT_RVS (structElmtType, tblT->content->a.elmts)
141 {
142 elmtV = sVal[currElmt--];
143 if (!(structElmtType->optional && (elmtV == NULL)))
144 {
145 retVal = TblEncodeType (structElmtType, b, elmtV, FALSE, &tmpBytesEncoded);
146 if (retVal < 0)
147 break; /* exit for loop */
148 }
149 }
150 /* restore list curr in case recursive type */
151 SET_CURR_LIST_NODE (tblT->content->a.elmts, tmp);
152 break;
153
154 case TBL_SEQUENCEOF:
155 case TBL_SETOF:
156 lVal = (AsnList*)v;
157 listElmtType = FIRST_LIST_ELMT (tblT->content->a.elmts);
158 FOR_EACH_LIST_ELMT_RVS (elmtV, lVal)
159 {
160 retVal = TblEncodeType (listElmtType, b, elmtV, FALSE, &tmpBytesEncoded);
161 if (retVal < 0)
162 break; /* exit for loop */
163 }
164 break;
165
166 case TBL_CHOICE:
167 cVal = (AChoiceVal*) v;
168 choiceElmtType = (TBLType*)GetAsnListElmt (tblT->content->a.elmts, cVal->choiceId);
169 retVal = TblEncodeType (choiceElmtType, b, cVal->val, FALSE, &tmpBytesEncoded);
170 break;
171
172 case TBL_BOOLEAN:
173 tmpBytesEncoded += BEncAsnBoolContent (b, (AsnBool*)v);
174 break;
175
176 case TBL_INTEGER:
177 case TBL_ENUMERATED:
178 tmpBytesEncoded += BEncAsnIntContent (b, (AsnInt*)v);
179 break;
180
181 case TBL_BITSTRING:
182 tmpBytesEncoded += BEncAsnBitsContent (b, (AsnBits*)v);
183 break;
184
185 case TBL_OCTETSTRING:
186 tmpBytesEncoded += BEncAsnOctsContent (b, (AsnOcts*)v);
187 break;
188
189 case TBL_NULL:
190 tmpBytesEncoded += BEncAsnNullContent (b, (AsnNull*)v);
191 break;
192
193 case TBL_OID:
194 tmpBytesEncoded += BEncAsnOidContent (b, (AsnOid*)v);
195 break;
196
197 case TBL_REAL:
198 tmpBytesEncoded += BEncAsnRealContent (b, (AsnReal*)v);
199 break;
200
201 default:
202 retVal = -1;
203 }
204
205 if (retVal >= 0)
206 retVal = TblEncodeTagsAndLens (tblT, b, implicit, &tmpBytesEncoded);
207
208 (*bytesEncoded) += tmpBytesEncoded;
209
210 return retVal;
211
212 } /* TblEncodeTd */
213
214
215 int
216 TblEncodeTagsAndLens PARAMS ((tblT, b, implicit, bytesEncoded),
217 TBLType *tblT _AND_
218 BUF_TYPE b _AND_
219 int implicit _AND_
220 unsigned long int *bytesEncoded)
221 {
222 TBLTag *tag;
223
224 FOR_EACH_LIST_ELMT_RVS (tag, tblT->tagList)
225 {
226 if (!(implicit && (tag == FIRST_LIST_ELMT (tblT->tagList))))
227 {
228 if (tag->form == CONS)
229 (*bytesEncoded) += BEncConsLen (b, *bytesEncoded);
230 else /* ANY_FORM or PRIM */
231 (*bytesEncoded) += BEncDefLen (b, *bytesEncoded);
232
233 (*bytesEncoded) += TblEncTag (b, tag);
234 }
235 }
236 return 0; /* no errors */
237 } /* TblEncodeTagsAndLens */
238
239 /*
240 int
241 TblEncodePrimTagsAndLens PARAMS ((tblT, b, implicit, bytesEncoded),
242 TBLType *tblT _AND_
243 BUF_TYPE b _AND_
244 int implicit _AND_
245 unsigned long int *bytesEncoded)
246 {
247 TBLTag *tag;
248 int tagsEncoded;
249 int numTags;
250
251 if ((tblT->tagList == NULL) ||(LIST_EMPTY (tblT->tagList)))
252 untaggedPrimG = TRUE;
253 else
254 {
255 numTags = LIST_COUNT (tblT->tagList);
256 if ((numTags != 1) || !implicit)
257 {
258 untaggedPrimG = FALSE;
259 tag = FIRST_LIST_ELMT (tblT->tagList);
260 (*bytesEncoded) += BEncDefLen (b, *bytesEncoded);
261 (*bytesEncoded) += TblEncTag (b, tag->tclass, PRIM, tag->code);
262 tagsEncoded = 1;
263 SET_CURR_LIST_NODE (tblT->tagList, LAST_LIST_NODE (tblT->tagList)->prev);
264 FOR_REST_LIST_ELMT_RVS (tag, tblT->tagList)
265 {
266 if (implicit && (tagsEncoded == (numTags -1)))
267 break;
268 (*bytesEncoded) += BEncConsLen (b, *bytesEncoded);
269 (*bytesEncoded) += TblEncTag (b, tag->tclass, CONS, tag->code);
270 tagsEncoded++;
271 }
272 }
273 else
274 untaggedPrimG = TRUE;
275 }
276 return 0;
277 } TblEncodeTagsAndLens */
278
279
280 /*
281 * write encoded version of tag stored in the tag to
282 * the bufer
283 */
284 AsnLen TblEncTag PARAMS ((b, tag),
285 BUF_TYPE b _AND_
286 TBLTag *tag)
287 {
288 AsnTag shifted;
289 unsigned char octet;
290 AsnLen encLen = 0;
291 int i;
292
293 for (i = 0; i < sizeof (AsnTag); i++)
294 {
295 shifted = (tag->encTag >> (i * 8));
296 octet = shifted & 0xff;
297 if (octet || i<sizeof(AsnTag)-2 && (shifted & 0x8000))
298 {
299 encLen++;
300 BufPutByteRvs (b, octet);
301 }
302
303 }
304 return encLen;
305 } /* TblEncTag */
306
307
308 /* OLD NOT USED ANY MORE
309 returns encoded length of tag
310 AsnLen TblEncTag PARAMS ((b, tclass, form, code),
311 BUF_TYPE b _AND_
312 TBLTagClass tclass _AND_
313 BER_FORM form _AND_
314 AsnInt code)
315 {
316 AsnLen retVal;
317 BER_CLASS bclass;
318
319 bclass = TblTagClassToBer (tclass);
320
321 warning: keep the BEncTagX calls in braces ({}) cause macros
322 if (code < 31)
323 {
324 retVal = BEncTag1 (b, bclass, form, code);
325 }
326 else if (code < 128)
327 {
328 retVal = BEncTag2 (b, bclass, form, code);
329 }
330 else if (code < 16384)
331 {
332 retVal = BEncTag3 (b, bclass, form, code);
333 }
334 else if (code < 2097152)
335 {
336 retVal = BEncTag4 (b, bclass, form, code);
337 }
338 else
339 {
340 retVal = BEncTag5 (b, bclass, form, code);
341 }
342 return retVal;
343 }
344 */
345
346 #endif /* TTBL */