]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sslalloc.c
Security-54.tar.gz
[apple/security.git] / SecureTransport / sslalloc.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 File: sslalloc.c
21
22 Contains: memory allocator implementation
23
24 Written by: Doug Mitchell, based on Netscape SSLRef 3.0
25
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
27
28 */
29 /* *********************************************************************
30 File: sslalloc.c
31
32 SSLRef 3.0 Final -- 11/19/96
33
34 Copyright (c)1996 by Netscape Communications Corp.
35
36 By retrieving this software you are bound by the licensing terms
37 disclosed in the file "LICENSE.txt". Please read it, and if you don't
38 accept the terms, delete this software.
39
40 SSLRef 3.0 was developed by Netscape Communications Corp. of Mountain
41 View, California <http://home.netscape.com/> and Consensus Development
42 Corporation of Berkeley, California <http://www.consensus.com/>.
43
44 *********************************************************************
45
46 File: sslalloc.c Utility functions for doing allocation
47
48 These functions call the user-supplied callbacks to
49 allocate/free/reallocate memory
50
51 ****************************************************************** */
52
53 #include "sslalloc.h"
54 #include "sslctx.h"
55 #include "sslDebug.h"
56
57 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
58
59 #pragma mark *** Basic low-level malloc/free ***
60
61 /*
62 * For now, all allocs/frees go thru here.
63 */
64 #include <string.h> /* memset */
65 #include <stdlib.h>
66
67 void *
68 sslMalloc(UInt32 length)
69 {
70 return malloc(length);
71 }
72
73 void
74 sslFree(void *p)
75 {
76 if(p != nil) {
77 free(p);
78 }
79 }
80
81 void *
82 sslRealloc(void *oldPtr, UInt32 oldLen, UInt32 newLen)
83 {
84 return realloc(oldPtr, newLen);
85 }
86
87 #pragma mark *** SSLBuffer-level alloc/free ***
88
89 SSLErr SSLAllocBuffer(
90 SSLBuffer *buf,
91 UInt32 length,
92 const SystemContext *ctx)
93 {
94 buf->data = sslMalloc(length);
95 if(buf->data == NULL) {
96 buf->length = 0;
97 return SSLMemoryErr;
98 }
99 buf->length = length;
100 return SSLNoErr;
101 }
102
103 SSLErr
104 SSLFreeBuffer(SSLBuffer *buf, const SystemContext *ctx)
105 {
106 if(buf == NULL) {
107 errorLog0("SSLFreeBuffer: NULL buf!\n");
108 return SSLInternalError;
109 }
110 sslFree(buf->data);
111 buf->data = NULL;
112 buf->length = 0;
113 return SSLNoErr;
114 }
115
116 SSLErr
117 SSLReallocBuffer(SSLBuffer *buf, UInt32 newSize, const SystemContext *ctx)
118 {
119 buf->data = sslRealloc(buf->data, buf->length, newSize);
120 if(buf->data == NULL) {
121 buf->length = 0;
122 return SSLMemoryErr;
123 }
124 buf->length = newSize;
125 return SSLNoErr;
126 }
127
128 #pragma mark *** Convenience routines ***
129
130 UInt8 *sslAllocCopy(
131 const UInt8 *src,
132 UInt32 len)
133 {
134 UInt8 *dst;
135
136 dst = sslMalloc(len);
137 if(dst == NULL) {
138 return NULL;
139 }
140 memmove(dst, src, len);
141 return dst;
142 }
143
144 SSLErr SSLAllocCopyBuffer(
145 const SSLBuffer *src,
146 SSLBuffer **dst) // buffer and data mallocd and returned
147 {
148 SSLErr serr;
149
150 SSLBuffer *rtn = sslMalloc(sizeof(SSLBuffer));
151 if(rtn == NULL) {
152 return SSLMemoryErr;
153 }
154 serr = SSLCopyBuffer(src, rtn);
155 if(serr) {
156 sslFree(rtn);
157 }
158 else {
159 *dst = rtn;
160 }
161 return serr;
162 }
163
164 SSLErr SSLCopyBuffer(
165 const SSLBuffer *src,
166 SSLBuffer *dst) // data mallocd and returned
167 {
168 dst->data = sslAllocCopy(src->data, src->length);
169 if(dst->data == NULL) {
170 return SSLMemoryErr;
171 }
172 dst->length = src->length;
173 return SSLNoErr;
174 }