]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/inc/str-stk.h
Security-54.1.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / inc / str-stk.h
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 * str_stk.h - maintains a stack of the components of a bit string
21 * or octet string so they can be copied into a single chunk
22 *
23 *
24 * CONSTRUCTED BIT AND OCTET STRINGS SUCK. They should be
25 * specified in the application's ASN.1 spec as SEQUENCE OF OCTET STRING
26 *
27 * this stack stuff is for decoding constructed bit/octet strings
28 * so the user gets a single contiguous bit/octet str instead of
29 * irritating little pieces. This does not cost a lot more than
30 * a linked octet/bit string type since we're copying from the
31 * buffer anyway, not referencing it directly (even in simple case).
32 * It will cost more if the string stk overflows and
33 * needs to be enlarged via realloc - set the values of
34 * initialStkSizeG, and stkGrowSize carefully for your application.
35 * Once the StkSize grows, it doesn't shrink back ever.
36 *
37 * Only three routine use/deal with this stack garbage
38 * BDecConsAsnOcts
39 * BDecConsAsnBits
40 * SetupConsBitsOctsStringStk
41 *
42 * Copyright (C) 1992 Michael Sample and the University of British Columbia
43 *
44 * This library is free software; you can redistribute it and/or
45 * modify it provided that this copyright/license information is retained
46 * in original form.
47 *
48 * If you modify this file, you must clearly indicate your changes.
49 *
50 * This source code is distributed in the hope that it will be
51 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
52 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
53 *
54 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c-lib/inc/str-stk.h,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
55 * $Log: str-stk.h,v $
56 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
57 * Move from private repository to open source repository
58 *
59 * Revision 1.2 2001/05/05 00:59:23 rmurphy
60 * Adding darwin license headers
61 *
62 * Revision 1.1.1.1 1999/03/16 18:06:22 aram
63 * Originals from SMIME Free Library.
64 *
65 * Revision 1.2 1995/07/24 21:01:24 rj
66 * changed `_' to `-' in file names.
67 *
68 * Revision 1.1 1994/08/28 09:45:41 rj
69 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
70 *
71 */
72
73 typedef struct StrStkElmt
74 {
75 char *str;
76 unsigned long int len;
77 } StrStkElmt;
78
79 typedef struct StrStk
80 {
81 StrStkElmt *stk; /* ptr to array of SSElmts with 'size' elmts */
82 unsigned long int initialNumElmts;
83 unsigned long int numElmts; /* total # of elements in str stk */
84 unsigned long int growElmts; /* # elmts to increase size by when nec */
85 unsigned long int nextFreeElmt; /* index of next free element */
86 unsigned long int totalByteLen; /* octet len of string stored in stk */
87 } StrStk;
88
89
90 extern StrStk strStkG;
91
92 /*
93 * initializes stk (Allocates if nec.)
94 * once stk is enlarged, it doesn't shrink
95 */
96 #define RESET_STR_STK()\
97 {\
98 strStkG.nextFreeElmt = 0;\
99 strStkG.totalByteLen = 0;\
100 if (strStkG.stk == NULL){\
101 strStkG.stk = (StrStkElmt*) malloc ((strStkG.initialNumElmts) *sizeof (StrStkElmt));\
102 strStkG.numElmts = strStkG.initialNumElmts;}\
103 }
104
105
106 /*
107 * add a char*,len pair to top of stack.
108 * grows stack if necessary using realloc (!)
109 */
110 #define PUSH_STR(strPtr, strsLen, env)\
111 {\
112 if (strStkG.nextFreeElmt >= strStkG.numElmts)\
113 {\
114 strStkG.stk = (StrStkElmt*) realloc (strStkG.stk, (strStkG.numElmts + strStkG.growElmts) *sizeof (StrStkElmt));\
115 strStkG.numElmts += strStkG.growElmts;\
116 }\
117 strStkG.totalByteLen += strsLen;\
118 strStkG.stk[strStkG.nextFreeElmt].str = strPtr;\
119 strStkG.stk[strStkG.nextFreeElmt].len = strsLen;\
120 strStkG.nextFreeElmt++;\
121 }
122
123
124 /*
125 * Set up size values for the stack that is used for merging constructed
126 * octet or bit string into single strings.
127 * **** Call this before decoding anything. *****
128 * Note: you don't have to call this if the default values
129 * for initialStkSizeG and stkGrowSizeG are acceptable
130 */
131 #define SetupConsBitsOctsStringStk (initialNumberOfElmts, numberOfElmtsToGrowBy)\
132 {\
133 strStkG.initialNumElmts = initialNumberOfElmts; \
134 strStkG.growElmts = numberOfElmtsToGrowBy;\
135 }