]> git.saurik.com Git - apple/security.git/blob - libsecurity_ssl/lib/sslMemory.c
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_ssl / lib / sslMemory.c
1 /*
2 * Copyright (c) 1999-2001,2005-2007,2010-2012 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * sslMemory.c - Memory allocator implementation
26 */
27
28 #include "sslMemory.h"
29 #include "sslContext.h"
30 #include "sslDebug.h"
31
32 #pragma mark -
33 #pragma mark Basic low-level malloc/free
34
35 /*
36 * For now, all allocs/frees go thru here.
37 */
38 #include <string.h> /* memset */
39 #include <stdlib.h>
40
41 void *
42 sslMalloc(size_t length)
43 {
44 return malloc(length);
45 }
46
47 void
48 sslFree(void *p)
49 {
50 if(p != nil) {
51 free(p);
52 }
53 }
54
55 void *
56 sslRealloc(void *oldPtr, size_t oldLen, size_t newLen)
57 {
58 return realloc(oldPtr, newLen);
59 }
60
61 #pragma mark -
62 #pragma mark SSLBuffer-level alloc/free
63
64 OSStatus SSLAllocBuffer(
65 SSLBuffer *buf,
66 size_t length,
67 const SSLContext *ctx) // currently unused
68 {
69 buf->data = (UInt8 *)sslMalloc(length);
70 if(buf->data == NULL) {
71 buf->length = 0;
72 return memFullErr;
73 }
74 buf->length = length;
75 return noErr;
76 }
77
78 OSStatus
79 SSLFreeBuffer(SSLBuffer *buf, const SSLContext *ctx)
80 {
81 if(buf == NULL) {
82 sslErrorLog("SSLFreeBuffer: NULL buf!\n");
83 return errSSLInternal;
84 }
85 sslFree(buf->data);
86 buf->data = NULL;
87 buf->length = 0;
88 return noErr;
89 }
90
91 OSStatus
92 SSLReallocBuffer(SSLBuffer *buf, size_t newSize, const SSLContext *ctx)
93 {
94 buf->data = (UInt8 *)sslRealloc(buf->data, buf->length, newSize);
95 if(buf->data == NULL) {
96 buf->length = 0;
97 return memFullErr;
98 }
99 buf->length = newSize;
100 return noErr;
101 }
102
103 #pragma mark -
104 #pragma mark Convenience routines
105
106 UInt8 *sslAllocCopy(
107 const UInt8 *src,
108 size_t len)
109 {
110 UInt8 *dst;
111
112 dst = (UInt8 *)sslMalloc(len);
113 if(dst == NULL) {
114 return NULL;
115 }
116 memmove(dst, src, len);
117 return dst;
118 }
119
120 OSStatus SSLAllocCopyBuffer(
121 const SSLBuffer *src,
122 SSLBuffer **dst) // buffer and data mallocd and returned
123 {
124 OSStatus serr;
125
126 SSLBuffer *rtn = (SSLBuffer *)sslMalloc(sizeof(SSLBuffer));
127 if(rtn == NULL) {
128 return memFullErr;
129 }
130 serr = SSLCopyBuffer(src, rtn);
131 if(serr) {
132 sslFree(rtn);
133 }
134 else {
135 *dst = rtn;
136 }
137 return serr;
138 }
139
140 OSStatus SSLCopyBufferFromData(
141 const void *src,
142 size_t len,
143 SSLBuffer *dst) // data mallocd and returned
144 {
145 dst->data = sslAllocCopy((const UInt8 *)src, len);
146 if(dst->data == NULL) {
147 return memFullErr;
148 }
149 dst->length = len;
150 return noErr;
151 }
152
153 OSStatus SSLCopyBuffer(
154 const SSLBuffer *src,
155 SSLBuffer *dst) // data mallocd and returned
156 {
157 return SSLCopyBufferFromData(src->data, src->length, dst);
158 }
159