]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/tbl-util.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_util.c - type table utilities.
24 * Copyright (C) 1993 Michael Sample
25 * and the University of British Columbia
27 * This library is free software; you can redistribute it and/or
28 * modify it provided that this copyright/license information is retained
31 * If you modify this file, you must clearly indicate your changes.
33 * This source code is distributed in the hope that it will be
34 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
35 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
43 /* non -exported routine protos */
44 void TblLinkIndexes
PROTO ((TBL
*tbl
));
45 void TblLinkTypeRefs
PROTO ((TBL
*tbl
, TBLType
*tblT
));
47 void TblFixTags
PROTO ((TBL
*tbl
));
48 void TblFixTypeTags
PROTO ((TBLType
*tblT
));
49 void TblSetTagForms
PROTO ((TBLType
*t
));
53 * opens given filename, determines its size, allocs a block
54 * of that size and reads the file into it. returns a pointer
55 * to this block. Prints an err msgs is something screwed up
56 * and returns NULL. Sets the size param to the size of the file.
59 LoadFile
PARAMS ((fileName
, size
),
61 unsigned long int *size
)
64 unsigned long int fsize
;
67 f
= fopen (fileName
, "r");
71 Asn1Error("Could not open file for reading.\n");
75 fseek (f
, 0, 2); /* seek to end */
76 fsize
= ftell (f
); /* get size of file */
77 fseek (f
, 0, 0); /* seek to beginning */
80 fileData
= (char *) malloc (fsize
);
84 Asn1Error("Not enough memory to read in file.\n");
88 if (fread (fileData
, sizeof (char), fsize
, f
) != fsize
)
92 Asn1Error("Trouble reading file.\n");
101 LoadTblFile
PARAMS ((tblFileName
),
108 unsigned long int fsize
;
115 fileData
= LoadFile (tblFileName
, &fsize
);
116 if (fileData
== NULL
)
119 SBufInstallData (&sb
, fileData
, fsize
);
120 SBufResetInReadMode (&sb
);
121 PutSBufInGenBuf (&sb
, &gb
);
125 tbl
= (TBL
*)Asn1Alloc (sizeof (TBL
));
127 if ((val
= setjmp (env
)) == 0)
128 BDecTBL (&gb
, tbl
, &decodedLen
, env
);
132 /* convert the typeDefIndexes into real pointers */
133 TblLinkIndexes (tbl
);
137 free (fileData
); /* malloc'd in LoadFile */
144 * just use slow individual lookup instead of creating a table
145 * (a conversion tbl could be built during decoding)
148 TblLinkIndexes
PARAMS ((tbl
),
154 FOR_EACH_LIST_ELMT (tblMod
, tbl
->modules
)
156 FOR_EACH_LIST_ELMT (tblTd
, tblMod
->typeDefs
)
158 /* go through the types looking for TBLTypeRefs */
159 TblLinkTypeRefs (tbl
, tblTd
->type
);
162 } /* TBLLinkIndexes */
166 * set tags forms and include encoded version to improve
167 * decoding and encoding performance.
170 TblFixTags
PARAMS ((tbl
),
176 FOR_EACH_LIST_ELMT (tblMod
, tbl
->modules
)
178 FOR_EACH_LIST_ELMT (tblTd
, tblMod
->typeDefs
)
180 TblFixTypeTags (tblTd
->type
);
188 * recursively descends type looking for typeDefIds in type refs
189 * to convert to the type defs actual ptr
191 * Also sets the form field for each tag. (this speeds up enc/dec).
192 * Note that the form bit is not in the encoded version of a TBLTag.
195 TblLinkTypeRefs
PARAMS ((tbl
, tblT
),
202 switch (tblT
->typeId
)
207 case TBL_OCTETSTRING
:
212 /* not contained type refs so return */
220 /* look for contained type refs */
221 tmp
= CURR_LIST_NODE (tblT
->content
->a
.elmts
);
222 FOR_EACH_LIST_ELMT (tblElmtT
, tblT
->content
->a
.elmts
)
224 TblLinkTypeRefs (tbl
, tblElmtT
);
226 SET_CURR_LIST_NODE (tblT
->content
->a
.elmts
, tmp
);
230 /* convert type def index into a pointer to the type def */
231 tblT
->content
->a
.typeRef
->typeDefPtr
=
232 TblFindTypeDefByIndex (tbl
, tblT
->content
->a
.typeRef
->typeDef
);
235 } /* TblLinkTypeRefs */
237 TblFixTypeTags
PARAMS ((tblT
),
243 TblSetTagForms (tblT
);
244 switch (tblT
->typeId
)
251 /* fix tags in elmt types */
252 tmp
= CURR_LIST_NODE (tblT
->content
->a
.elmts
);
253 FOR_EACH_LIST_ELMT (tblElmtT
, tblT
->content
->a
.elmts
)
255 TblFixTypeTags (tblElmtT
);
257 SET_CURR_LIST_NODE (tblT
->content
->a
.elmts
, tmp
);
266 TblSetTagForms
PARAMS ((tblT
),
275 if (tblT
->tagList
== NULL
)
278 numTags
= LIST_COUNT (tblT
->tagList
);
281 * get real type id (skip through type refs)
282 * count total number of tags too.
284 for (tmpTblT
= tblT
; tmpTblT
->typeId
== TBL_TYPEREF
; tmpTblT
= tmpTblT
->content
->a
.typeRef
->typeDefPtr
->type
)
286 if (tmpTblT
->tagList
)
287 numTags
+= LIST_COUNT (tmpTblT
->tagList
);
288 if (tmpTblT
->content
->a
.typeRef
->implicit
)
291 tid
= tmpTblT
->typeId
;
293 /* only traverse this types tags */
294 FOR_EACH_LIST_ELMT (tblTag
, tblT
->tagList
)
297 form
= tblTag
->form
= CONS
;
306 form
= tblTag
->form
= CONS
;
309 case TBL_OCTETSTRING
:
311 tblTag
->form
= ANY_FORM
;
312 form
= PRIM
; /* store as prim (for encoder - always prim) */
316 form
= tblTag
->form
= PRIM
;
320 tblTag
->encTag
= MAKE_TAG_ID (TblTagClassToBer (tblTag
->tclass
), form
, tblTag
->code
);
323 } /* TblSetTagForms */
328 TblFindTypeDef
PARAMS ((tbl
, modName
, typeName
, tblModHndl
),
332 TBLModule
**tblModHndl
)
338 /* look in named module only if given */
341 tblMod
= TblFindModule (tbl
, modName
);
342 *tblModHndl
= tblMod
;
346 return TblFindTypeDefInMod (tblMod
, typeName
);
348 else /* look in all modules and return first instance */
350 tmp
= CURR_LIST_NODE (tbl
->modules
);
351 FOR_EACH_LIST_ELMT (tblMod
, tbl
->modules
)
353 tblTd
= TblFindTypeDefInMod (tblMod
, typeName
);
356 *tblModHndl
= tblMod
;
357 SET_CURR_LIST_NODE (tbl
->modules
, tmp
);
361 SET_CURR_LIST_NODE (tbl
->modules
, tmp
);
363 return NULL
; /* not found */
364 } /* TblFindTypeDef */
368 TblFindTypeDefInMod
PARAMS ((tblMod
, typeName
),
369 TBLModule
*tblMod _AND_
375 tmp
= CURR_LIST_NODE (tblMod
->typeDefs
);
376 FOR_EACH_LIST_ELMT (tblTd
, tblMod
->typeDefs
)
378 if (strcmp (tblTd
->typeName
.octs
, typeName
) == 0)
380 SET_CURR_LIST_NODE (tblMod
->typeDefs
, tmp
);
384 SET_CURR_LIST_NODE (tblMod
->typeDefs
, tmp
);
386 } /* TblFindTypeDefInMod */
390 TblFindTypeDefByIndex
PARAMS ((tbl
, id
),
399 /* look in all modules and return typedef with given id */
400 tmp1
= CURR_LIST_NODE (tbl
->modules
);
401 FOR_EACH_LIST_ELMT (tblMod
, tbl
->modules
)
403 tmp2
= CURR_LIST_NODE (tblMod
->typeDefs
);
404 FOR_EACH_LIST_ELMT (tblTd
, tblMod
->typeDefs
)
406 if (tblTd
->typeDefId
== id
)
408 SET_CURR_LIST_NODE (tblMod
->typeDefs
, tmp2
);
409 SET_CURR_LIST_NODE (tbl
->modules
, tmp1
);
413 SET_CURR_LIST_NODE (tblMod
->typeDefs
, tmp2
);
415 SET_CURR_LIST_NODE (tbl
->modules
, tmp1
);
418 } /* TblFindTypeDefByIndex */
422 TblFindModule
PARAMS ((tbl
, modName
),
429 tmp
= CURR_LIST_NODE (tbl
->modules
);
430 FOR_EACH_LIST_ELMT (tblMod
, tbl
->modules
)
432 if (strcmp (tblMod
->name
.octs
, modName
) == 0)
434 SET_CURR_LIST_NODE (tbl
->modules
, tmp
);
438 SET_CURR_LIST_NODE (tbl
->modules
, tmp
);
441 } /* TblFindModule */