]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/random/YarrowCoreLib/src/comp.c
xnu-517.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 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
0b4e3aa0 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
0b4e3aa0
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
0b4e3aa0
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26/*
27 File: comp.c
28
29 Contains: NULL compression. Kernel version of Yarrow assumes
30 incoming seed data is truly random.
31*/
32#include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
33#include "comp.h"
34
35#ifdef YARROW_KERNEL
36
37/* null compression */
38comp_error_status comp_init(COMP_CTX* ctx)
39{
40 return COMP_SUCCESS;
41}
42
43
44comp_error_status comp_add_data(COMP_CTX* ctx,Bytef* inp,uInt inplen)
45{
46 return COMP_SUCCESS;
47}
48
49comp_error_status comp_get_ratio(COMP_CTX* ctx,float* out)
50{
51 *out = 1.0;
52 return COMP_SUCCESS;
53}
54
55comp_error_status comp_end(COMP_CTX* ctx)
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