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