]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/nibble-alloc.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / src / nibble-alloc.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 * .../c-lib/src/nibble-alloc.c - fast mem allocation for decoded values
21 *
22 * MS Dec 31/91
23 *
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
25 *
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
28 * in original form.
29 *
30 * If you modify this file, you must clearly indicate your changes.
31 *
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 *
36 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/nibble-alloc.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
37 * $Log: nibble-alloc.c,v $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
40 *
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
43 *
44 * Revision 1.1.1.1 1999/03/16 18:06:32 aram
45 * Originals from SMIME Free Library.
46 *
47 * Revision 1.4 1997/02/28 13:39:51 wan
48 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
49 *
50 * Revision 1.3 1995/07/27 09:06:37 rj
51 * use memzero that is defined in .../snacc.h to use either memset or bzero.
52 *
53 * changed `_' to `-' in file names.
54 *
55 * Revision 1.2 1994/09/01 00:07:16 rj
56 * more portable .h file inclusion.
57 *
58 * Revision 1.1 1994/08/28 09:46:07 rj
59 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
60 *
61 */
62
63 #include "asn-config.h"
64
65 #if STDC_HEADERS || HAVE_STRING_H
66 #include <string.h>
67 /* An ANSI string.h and pre-ANSI memory.h might conflict. */
68 #if !STDC_HEADERS && HAVE_MEMORY_H
69 #include <memory.h>
70 #endif /* not STDC_HEADERS and HAVE_MEMORY_H */
71 #endif /* not STDC_HEADERS and not HAVE_STRING_H */
72
73 #include "nibble-alloc.h"
74
75
76 NibbleMem *nmG = NULL;
77
78 void
79 InitNibbleMem PARAMS ((initialSize, incrementSize),
80 unsigned long int initialSize _AND_
81 unsigned long int incrementSize)
82 {
83 NibbleMem *nm;
84
85 nm = (NibbleMem*) malloc (sizeof (NibbleMem));
86 nm->incrementSize = incrementSize;
87
88 nm->currNibbleBuf = nm->firstNibbleBuf = (NibbleBuf*)malloc (sizeof (NibbleBuf));
89 nm->firstNibbleBuf->curr = nm->firstNibbleBuf->start = (char*) malloc (initialSize);
90 nm->firstNibbleBuf->end = nm->firstNibbleBuf->start + initialSize;
91 nm->firstNibbleBuf->next = NULL;
92 memzero (nm->currNibbleBuf->start, initialSize);
93
94 nmG = nm;/* set global */
95
96 } /* InitNibbleAlloc */
97
98
99 /*
100 * alloc new nibble buf, link in, reset to curr nibble buf
101 */
102 void
103 ServiceNibbleFault PARAMS ((size),
104 unsigned long size)
105 {
106 NibbleMem *nm;
107 unsigned long newBufSize;
108
109 nm = nmG;
110
111 if (size > nm->incrementSize)
112 newBufSize = size;
113 else
114 newBufSize = nm->incrementSize;
115
116 nm->currNibbleBuf->next = (NibbleBuf*) malloc (sizeof (NibbleBuf));
117 nm->currNibbleBuf = nm->currNibbleBuf->next;
118 nm->currNibbleBuf->curr = nm->currNibbleBuf->start = (char*) malloc (newBufSize);
119 nm->currNibbleBuf->end = nm->currNibbleBuf->start + newBufSize;
120 nm->currNibbleBuf->next = NULL;
121 memzero (nm->currNibbleBuf->start, newBufSize);
122 } /* serviceNibbleFault */
123
124
125
126 /*
127 * returns requested space filled with zeros
128 */
129 void*
130 NibbleAlloc PARAMS ((size),
131 unsigned long size)
132 {
133 NibbleMem *nm;
134 char *retVal;
135 unsigned long ndiff;
136
137 nm = nmG;
138
139 if ((nm->currNibbleBuf->end - nm->currNibbleBuf->curr) < size)
140 ServiceNibbleFault (size);
141
142 retVal = nm->currNibbleBuf->curr;
143
144 /*
145 * maintain word alignment
146 */
147 ndiff = size % sizeof (long);
148 if (ndiff != 0)
149 {
150 nm->currNibbleBuf->curr += size + sizeof (long) - ndiff;
151
152 /*
153 * this is a fix from Terry Sullivan <FCLTPS@nervm.nerdc.ufl.edu>
154 *
155 * makes sure curr does not go past the end ptr
156 */
157 if (nm->currNibbleBuf->curr > nm->currNibbleBuf->end)
158 nm->currNibbleBuf->curr = nm->currNibbleBuf->end;
159 }
160 else
161 nm->currNibbleBuf->curr += size;
162
163 return retVal;
164 } /* NibbleAlloc */
165
166
167
168 /*
169 * frees all nibble buffers except the first,
170 * resets the first to empty and zero's it
171 */
172 void
173 ResetNibbleMem()
174 {
175 NibbleMem *nm;
176 NibbleBuf *tmp;
177 NibbleBuf *nextTmp;
178
179 nm = nmG;
180
181 /*
182 * reset first nibble buf
183 */
184 memzero (nm->firstNibbleBuf->start, nm->firstNibbleBuf->curr - nm->firstNibbleBuf->start);
185
186 nm->firstNibbleBuf->curr = nm->firstNibbleBuf->start;
187
188 /*
189 * free incrementally added nibble bufs
190 */
191 for (tmp = nm->firstNibbleBuf->next; tmp != NULL; )
192 {
193 free (tmp->start);
194 nextTmp = tmp->next;
195 free (tmp);
196 tmp = nextTmp;
197 }
198
199 /* From ftp://ftp.cs.ubc.ca/pub/local/src/snacc/bugs-in-1.1 */
200 nm->firstNibbleBuf->next = NULL;
201 nm->currNibbleBuf = nm->firstNibbleBuf;
202
203 } /* ResetNibbleMem */
204
205
206 /*
207 * frees all nibble buffers, closing this
208 * NibbleMem completely
209 */
210 void
211 ShutdownNibbleMem()
212 {
213 NibbleMem *nm;
214 NibbleBuf *tmp;
215 NibbleBuf *nextTmp;
216
217 nm = nmG;
218 nmG = NULL;
219 /*
220 * free nibble bufs
221 */
222 for (tmp = nm->firstNibbleBuf; tmp != NULL; )
223 {
224 free (tmp->start);
225 nextTmp = tmp->next;
226 free (tmp);
227 tmp = nextTmp;
228 }
229
230 free (nm);
231 } /* ShutdownNibbleMem */