]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/comp.c
e2a10df416115b29c3c7b43dc12da4b54db87932
[apple/xnu.git] / bsd / dev / random / YarrowCoreLib / src / comp.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 /*
32 File: comp.c
33
34 Contains: NULL compression. Kernel version of Yarrow assumes
35 incoming seed data is truly random.
36 */
37 #include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
38 #include "comp.h"
39
40 #ifdef YARROW_KERNEL
41
42 /* null compression */
43 comp_error_status comp_init(__unused COMP_CTX* ctx)
44 {
45 return COMP_SUCCESS;
46 }
47
48
49 comp_error_status comp_add_data( __unused COMP_CTX* ctx,
50 __unused Bytef* inp,
51 __unused uInt inplen )
52 {
53 return COMP_SUCCESS;
54 }
55
56 comp_error_status comp_get_ratio( __unused COMP_CTX* ctx,float* out )
57 {
58 *out = 1.0;
59 return COMP_SUCCESS;
60 }
61
62 comp_error_status comp_end( __unused COMP_CTX* ctx )
63 {
64 return COMP_SUCCESS;
65 }
66
67 #else
68
69 /* original Yarrow compression, must be linked with zlib */
70
71 #if defined(macintosh) || defined(__APPLE__)
72 #include "WindowsTypesForMac.h"
73 #include "yarrowUtils.h"
74 #include <string.h>
75 #include <stdlib.h>
76 #else
77 #include <windows.h>
78 #endif
79 #include <math.h>
80 #include "comp.h"
81
82 /* Check that the pointer is not NULL */
83 #define PCHECK(ptr) if(ptr==NULL) {return COMP_ERR_NULL_POINTER;}
84 #define MMPCHECK(mmptr) if(mmptr==MM_NULL) {return COMP_ERR_NULL_POINTER;}
85 /* Check that the important parts of the context are ok */
86 #define CTXCHECK(ctx) \
87 PCHECK(ctx) \
88 MMPCHECK(ctx->buf)
89
90 /* Might want to vary these by context */
91 #define BUFSIZE 16384 /* 16K */
92 #define OUTBUFSIZE 16800 /* = inbufsize*1.01 + 12 (See zlib docs) */
93 #define SHIFTSIZE 4096 /* BUFSIZE/4 */
94
95 #define _MIN(a,b) (((a)<(b))?(a):(b))
96
97
98 /* Initialize these routines */
99 comp_error_status comp_init(COMP_CTX* ctx)
100 {
101 ctx->buf = mmMalloc(BUFSIZE);
102 if(ctx->buf == MM_NULL) {goto cleanup_comp_init;}
103 ctx->spaceused = 0;
104
105 return COMP_SUCCESS;
106
107 cleanup_comp_init:
108 mmFree(ctx->buf);
109
110 return COMP_ERR_LOW_MEMORY;
111 }
112
113
114 comp_error_status comp_add_data(COMP_CTX* ctx,Bytef* inp,uInt inplen)
115 {
116 uInt shifts;
117 uInt blocksize;
118 BYTE* buf;
119
120 CTXCHECK(ctx);
121 PCHECK(inp);
122
123 buf = (BYTE*)mmGetPtr(ctx->buf);
124
125 if(inplen+SHIFTSIZE>BUFSIZE)
126 {
127 blocksize = _MIN(inplen,BUFSIZE);
128 memmove(buf,inp,blocksize);
129 ctx->spaceused = blocksize;
130 }
131 else
132 {
133 if(inplen+ctx->spaceused>BUFSIZE)
134 {
135 shifts = (uInt)ceil((inplen+ctx->spaceused-BUFSIZE)/(float)SHIFTSIZE);
136 blocksize = _MIN(shifts*SHIFTSIZE,ctx->spaceused);
137 memmove(buf,buf+blocksize,BUFSIZE-blocksize);
138 ctx->spaceused = ctx->spaceused - blocksize;
139 }
140 memmove(buf+ctx->spaceused,inp,inplen);
141 ctx->spaceused += inplen;
142 }
143
144 return COMP_SUCCESS;
145 }
146
147 comp_error_status comp_get_ratio(COMP_CTX* ctx,float* out)
148 {
149 Bytef *inbuf,*outbuf;
150 uLong insize,outsize;
151 int resp;
152
153 *out = 0;
154
155 CTXCHECK(ctx);
156 PCHECK(out);
157
158 if(ctx->spaceused == 0) {return COMP_SUCCESS;}
159
160 inbuf = (Bytef*)mmGetPtr(ctx->buf);
161 outbuf = (Bytef*)malloc(OUTBUFSIZE);
162 if(outbuf==NULL) {return COMP_ERR_LOW_MEMORY;}
163
164 insize = ctx->spaceused;
165 outsize = OUTBUFSIZE;
166
167 resp = compress(outbuf,&outsize,inbuf,insize);
168 if(resp==Z_MEM_ERROR) {return COMP_ERR_LOW_MEMORY;}
169 if(resp==Z_BUF_ERROR) {return COMP_ERR_LIB;}
170
171 *out = (float)outsize/(float)insize;
172
173 /* Thrash the memory and free it */
174 trashMemory(outbuf, OUTBUFSIZE);
175 free(outbuf);
176
177 return COMP_SUCCESS;
178 }
179
180 comp_error_status comp_end(COMP_CTX* ctx)
181 {
182 if(ctx == NULL) {return COMP_SUCCESS;} /* Since nothing is left undone */
183
184 mmFree(ctx->buf);
185 ctx->buf = MM_NULL;
186
187 return COMP_SUCCESS;
188 }
189
190 #endif /* YARROW_KERNEL */
191