]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sslMemory.cpp
Security-164.1.tar.gz
[apple/security.git] / SecureTransport / sslMemory.cpp
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: sslMemory.c
21
22 Contains: memory allocator implementation
23
24 Written by: Doug Mitchell
25
26 Copyright: (c) 1999 by Apple Computer, Inc., all rights reserved.
27
28 */
29
30 #include "sslMemory.h"
31 #include "sslContext.h"
32 #include "sslDebug.h"
33
34 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
35
36 #pragma mark *** Basic low-level malloc/free ***
37
38 /*
39 * For now, all allocs/frees go thru here.
40 */
41 #include <string.h> /* memset */
42 #include <stdlib.h>
43
44 void *
45 sslMalloc(UInt32 length)
46 {
47 return malloc(length);
48 }
49
50 void
51 sslFree(void *p)
52 {
53 if(p != nil) {
54 free(p);
55 }
56 }
57
58 void *
59 sslRealloc(void *oldPtr, UInt32 oldLen, UInt32 newLen)
60 {
61 return realloc(oldPtr, newLen);
62 }
63
64 #pragma mark *** SSLBuffer-level alloc/free ***
65
66 OSStatus SSLAllocBuffer(
67 SSLBuffer &buf,
68 UInt32 length,
69 const SSLContext *ctx) // currently unused
70 {
71 buf.data = (UInt8 *)sslMalloc(length);
72 if(buf.data == NULL) {
73 buf.length = 0;
74 return memFullErr;
75 }
76 buf.length = length;
77 return noErr;
78 }
79
80 OSStatus
81 SSLFreeBuffer(SSLBuffer &buf, const SSLContext *ctx)
82 {
83 if(&buf == NULL) {
84 sslErrorLog("SSLFreeBuffer: NULL buf!\n");
85 return errSSLInternal;
86 }
87 sslFree(buf.data);
88 buf.data = NULL;
89 buf.length = 0;
90 return noErr;
91 }
92
93 OSStatus
94 SSLReallocBuffer(SSLBuffer &buf, UInt32 newSize, const SSLContext *ctx)
95 {
96 buf.data = (UInt8 *)sslRealloc(buf.data, buf.length, newSize);
97 if(buf.data == NULL) {
98 buf.length = 0;
99 return memFullErr;
100 }
101 buf.length = newSize;
102 return noErr;
103 }
104
105 #pragma mark *** Convenience routines ***
106
107 UInt8 *sslAllocCopy(
108 const UInt8 *src,
109 UInt32 len)
110 {
111 UInt8 *dst;
112
113 dst = (UInt8 *)sslMalloc(len);
114 if(dst == NULL) {
115 return NULL;
116 }
117 memmove(dst, src, len);
118 return dst;
119 }
120
121 OSStatus SSLAllocCopyBuffer(
122 const SSLBuffer &src,
123 SSLBuffer **dst) // buffer and data mallocd and returned
124 {
125 OSStatus serr;
126
127 SSLBuffer *rtn = (SSLBuffer *)sslMalloc(sizeof(SSLBuffer));
128 if(rtn == NULL) {
129 return memFullErr;
130 }
131 serr = SSLCopyBuffer(src, *rtn);
132 if(serr) {
133 sslFree(rtn);
134 }
135 else {
136 *dst = rtn;
137 }
138 return serr;
139 }
140
141 OSStatus SSLCopyBufferFromData(
142 const void *src,
143 UInt32 len,
144 SSLBuffer &dst) // data mallocd and returned
145 {
146 dst.data = sslAllocCopy((const UInt8 *)src, len);
147 if(dst.data == NULL) {
148 return memFullErr;
149 }
150 dst.length = len;
151 return noErr;
152 }
153
154 OSStatus SSLCopyBuffer(
155 const SSLBuffer &src,
156 SSLBuffer &dst) // data mallocd and returned
157 {
158 return SSLCopyBufferFromData(src.data, src.length, dst);
159 }
160