]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/tbl-enc.c
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.
22 * tbl_enc.c - type table encoder
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
33 * If you modify this file, you must clearly indicate your changes.
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.
44 /* protos for non exported routines */
47 int TblEncodePrimTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
49 int TblEncodeConsTagsAndLens PROTO ((TBLType *tblT, BUF_TYPE b, int implicit, unsigned long int *bytesEncoded));
52 int TblEncodeTagsAndLens
PROTO ((TBLType
*tblT
, BUF_TYPE b
, int implicit
, unsigned long int *bytesEncoded
));
54 AsnLen TblEncTag
PROTO ((BUF_TYPE b
, TBLTag
*tag
));
60 * Encode value v as though it is of type modName.typeName.
61 * bytesEncoded is set the actual number of bytes in the
63 * returns less than zero if an error occurs otherwise
64 * returns 0 for success.
67 TblEncode
PARAMS ((tbl
, modName
, typeName
, b
, v
, bytesEncoded
),
73 unsigned long int *bytesEncoded
)
79 tblTd
= TblFindTypeDef (tbl
, modName
, typeName
, &tblMod
);
82 TblError ("TblEncode: Could not find a type definition with the given module and name");
86 retVal
= TblEncodeType (tblTd
->type
, b
, v
, FALSE
, bytesEncoded
);
88 if (BufWriteError (b
))
96 * returns less than zero if an error occurs
99 TblEncodeType
PARAMS ((tblT
, b
, v
, implicit
, bytesEncoded
),
104 unsigned long int *bytesEncoded
)
109 unsigned long int tmpBytesEncoded
= 0;
110 unsigned int currElmt
;
111 TBLType
*listElmtType
;
112 TBLType
*structElmtType
;
113 TBLType
*choiceElmtType
;
119 switch (tblT
->typeId
)
124 * carry over implicit ref if goes
125 * through typeref with no tags
127 implicitRef
= tblT
->content
->a
.typeRef
->implicit
||
129 ((tblT
->tagList
== NULL
) || LIST_EMPTY (tblT
->tagList
)));
131 retVal
= TblEncodeType (tblT
->content
->a
.typeRef
->typeDefPtr
->type
, b
, v
, implicitRef
, &tmpBytesEncoded
);
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
)
142 elmtV
= sVal
[currElmt
--];
143 if (!(structElmtType
->optional
&& (elmtV
== NULL
)))
145 retVal
= TblEncodeType (structElmtType
, b
, elmtV
, FALSE
, &tmpBytesEncoded
);
147 break; /* exit for loop */
150 /* restore list curr in case recursive type */
151 SET_CURR_LIST_NODE (tblT
->content
->a
.elmts
, tmp
);
157 listElmtType
= FIRST_LIST_ELMT (tblT
->content
->a
.elmts
);
158 FOR_EACH_LIST_ELMT_RVS (elmtV
, lVal
)
160 retVal
= TblEncodeType (listElmtType
, b
, elmtV
, FALSE
, &tmpBytesEncoded
);
162 break; /* exit for loop */
167 cVal
= (AChoiceVal
*) v
;
168 choiceElmtType
= (TBLType
*)GetAsnListElmt (tblT
->content
->a
.elmts
, cVal
->choiceId
);
169 retVal
= TblEncodeType (choiceElmtType
, b
, cVal
->val
, FALSE
, &tmpBytesEncoded
);
173 tmpBytesEncoded
+= BEncAsnBoolContent (b
, (AsnBool
*)v
);
178 tmpBytesEncoded
+= BEncAsnIntContent (b
, (AsnInt
*)v
);
182 tmpBytesEncoded
+= BEncAsnBitsContent (b
, (AsnBits
*)v
);
185 case TBL_OCTETSTRING
:
186 tmpBytesEncoded
+= BEncAsnOctsContent (b
, (AsnOcts
*)v
);
190 tmpBytesEncoded
+= BEncAsnNullContent (b
, (AsnNull
*)v
);
194 tmpBytesEncoded
+= BEncAsnOidContent (b
, (AsnOid
*)v
);
198 tmpBytesEncoded
+= BEncAsnRealContent (b
, (AsnReal
*)v
);
206 retVal
= TblEncodeTagsAndLens (tblT
, b
, implicit
, &tmpBytesEncoded
);
208 (*bytesEncoded
) += tmpBytesEncoded
;
216 TblEncodeTagsAndLens
PARAMS ((tblT
, b
, implicit
, bytesEncoded
),
220 unsigned long int *bytesEncoded
)
224 FOR_EACH_LIST_ELMT_RVS (tag
, tblT
->tagList
)
226 if (!(implicit
&& (tag
== FIRST_LIST_ELMT (tblT
->tagList
))))
228 if (tag
->form
== CONS
)
229 (*bytesEncoded
) += BEncConsLen (b
, *bytesEncoded
);
230 else /* ANY_FORM or PRIM */
231 (*bytesEncoded
) += BEncDefLen (b
, *bytesEncoded
);
233 (*bytesEncoded
) += TblEncTag (b
, tag
);
236 return 0; /* no errors */
237 } /* TblEncodeTagsAndLens */
241 TblEncodePrimTagsAndLens PARAMS ((tblT, b, implicit, bytesEncoded),
245 unsigned long int *bytesEncoded)
251 if ((tblT->tagList == NULL) ||(LIST_EMPTY (tblT->tagList)))
252 untaggedPrimG = TRUE;
255 numTags = LIST_COUNT (tblT->tagList);
256 if ((numTags != 1) || !implicit)
258 untaggedPrimG = FALSE;
259 tag = FIRST_LIST_ELMT (tblT->tagList);
260 (*bytesEncoded) += BEncDefLen (b, *bytesEncoded);
261 (*bytesEncoded) += TblEncTag (b, tag->tclass, PRIM, tag->code);
263 SET_CURR_LIST_NODE (tblT->tagList, LAST_LIST_NODE (tblT->tagList)->prev);
264 FOR_REST_LIST_ELMT_RVS (tag, tblT->tagList)
266 if (implicit && (tagsEncoded == (numTags -1)))
268 (*bytesEncoded) += BEncConsLen (b, *bytesEncoded);
269 (*bytesEncoded) += TblEncTag (b, tag->tclass, CONS, tag->code);
274 untaggedPrimG = TRUE;
277 } TblEncodeTagsAndLens */
281 * write encoded version of tag stored in the tag to
284 AsnLen TblEncTag
PARAMS ((b
, tag
),
293 for (i
= 0; i
< sizeof (AsnTag
); i
++)
295 shifted
= (tag
->encTag
>> (i
* 8));
296 octet
= shifted
& 0xff;
297 if (octet
|| i
<sizeof(AsnTag
)-2 && (shifted
& 0x8000))
300 BufPutByteRvs (b
, octet
);
308 /* OLD NOT USED ANY MORE
309 returns encoded length of tag
310 AsnLen TblEncTag PARAMS ((b, tclass, form, code),
312 TBLTagClass tclass _AND_
319 bclass = TblTagClassToBer (tclass);
321 warning: keep the BEncTagX calls in braces ({}) cause macros
324 retVal = BEncTag1 (b, bclass, form, code);
328 retVal = BEncTag2 (b, bclass, form, code);
330 else if (code < 16384)
332 retVal = BEncTag3 (b, bclass, form, code);
334 else if (code < 2097152)
336 retVal = BEncTag4 (b, bclass, form, code);
340 retVal = BEncTag5 (b, bclass, form, code);