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