]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/comp.c
2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 Contains: NULL compression. Kernel version of Yarrow assumes
33 incoming seed data is truly random.
35 #include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
40 /* null compression */
41 comp_error_status
comp_init(__unused COMP_CTX
* ctx
)
47 comp_error_status
comp_add_data( __unused COMP_CTX
* ctx
,
49 __unused uInt inplen
)
54 comp_error_status
comp_get_ratio( __unused COMP_CTX
* ctx
,float* out
)
60 comp_error_status
comp_end( __unused COMP_CTX
* ctx
)
67 /* original Yarrow compression, must be linked with zlib */
69 #if defined(macintosh) || defined(__APPLE__)
70 #include "WindowsTypesForMac.h"
71 #include "yarrowUtils.h"
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) \
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 */
93 #define _MIN(a,b) (((a)<(b))?(a):(b))
96 /* Initialize these routines */
97 comp_error_status
comp_init(COMP_CTX
* ctx
)
99 ctx
->buf
= mmMalloc(BUFSIZE
);
100 if(ctx
->buf
== MM_NULL
) {goto cleanup_comp_init
;}
108 return COMP_ERR_LOW_MEMORY
;
112 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
121 buf
= (BYTE
*)mmGetPtr(ctx
->buf
);
123 if(inplen
+SHIFTSIZE
>BUFSIZE
)
125 blocksize
= _MIN(inplen
,BUFSIZE
);
126 memmove(buf
,inp
,blocksize
);
127 ctx
->spaceused
= blocksize
;
131 if(inplen
+ctx
->spaceused
>BUFSIZE
)
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
;
138 memmove(buf
+ctx
->spaceused
,inp
,inplen
);
139 ctx
->spaceused
+= inplen
;
145 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
147 Bytef
*inbuf
,*outbuf
;
148 uLong insize
,outsize
;
156 if(ctx
->spaceused
== 0) {return COMP_SUCCESS
;}
158 inbuf
= (Bytef
*)mmGetPtr(ctx
->buf
);
159 outbuf
= (Bytef
*)malloc(OUTBUFSIZE
);
160 if(outbuf
==NULL
) {return COMP_ERR_LOW_MEMORY
;}
162 insize
= ctx
->spaceused
;
163 outsize
= OUTBUFSIZE
;
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
;}
169 *out
= (float)outsize
/(float)insize
;
171 /* Thrash the memory and free it */
172 trashMemory(outbuf
, OUTBUFSIZE
);
178 comp_error_status
comp_end(COMP_CTX
* ctx
)
180 if(ctx
== NULL
) {return COMP_SUCCESS
;} /* Since nothing is left undone */
188 #endif /* YARROW_KERNEL */