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