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