]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/comp.c
e2a10df416115b29c3c7b43dc12da4b54db87932
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 Contains: NULL compression. Kernel version of Yarrow assumes
35 incoming seed data is truly random.
37 #include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
42 /* null compression */
43 comp_error_status
comp_init(__unused COMP_CTX
* ctx
)
49 comp_error_status
comp_add_data( __unused COMP_CTX
* ctx
,
51 __unused uInt inplen
)
56 comp_error_status
comp_get_ratio( __unused COMP_CTX
* ctx
,float* out
)
62 comp_error_status
comp_end( __unused COMP_CTX
* ctx
)
69 /* original Yarrow compression, must be linked with zlib */
71 #if defined(macintosh) || defined(__APPLE__)
72 #include "WindowsTypesForMac.h"
73 #include "yarrowUtils.h"
82 /* Check that the pointer is not NULL */
83 #define PCHECK(ptr) if(ptr==NULL) {return COMP_ERR_NULL_POINTER;}
84 #define MMPCHECK(mmptr) if(mmptr==MM_NULL) {return COMP_ERR_NULL_POINTER;}
85 /* Check that the important parts of the context are ok */
86 #define CTXCHECK(ctx) \
90 /* Might want to vary these by context */
91 #define BUFSIZE 16384 /* 16K */
92 #define OUTBUFSIZE 16800 /* = inbufsize*1.01 + 12 (See zlib docs) */
93 #define SHIFTSIZE 4096 /* BUFSIZE/4 */
95 #define _MIN(a,b) (((a)<(b))?(a):(b))
98 /* Initialize these routines */
99 comp_error_status
comp_init(COMP_CTX
* ctx
)
101 ctx
->buf
= mmMalloc(BUFSIZE
);
102 if(ctx
->buf
== MM_NULL
) {goto cleanup_comp_init
;}
110 return COMP_ERR_LOW_MEMORY
;
114 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
123 buf
= (BYTE
*)mmGetPtr(ctx
->buf
);
125 if(inplen
+SHIFTSIZE
>BUFSIZE
)
127 blocksize
= _MIN(inplen
,BUFSIZE
);
128 memmove(buf
,inp
,blocksize
);
129 ctx
->spaceused
= blocksize
;
133 if(inplen
+ctx
->spaceused
>BUFSIZE
)
135 shifts
= (uInt
)ceil((inplen
+ctx
->spaceused
-BUFSIZE
)/(float)SHIFTSIZE
);
136 blocksize
= _MIN(shifts
*SHIFTSIZE
,ctx
->spaceused
);
137 memmove(buf
,buf
+blocksize
,BUFSIZE
-blocksize
);
138 ctx
->spaceused
= ctx
->spaceused
- blocksize
;
140 memmove(buf
+ctx
->spaceused
,inp
,inplen
);
141 ctx
->spaceused
+= inplen
;
147 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
149 Bytef
*inbuf
,*outbuf
;
150 uLong insize
,outsize
;
158 if(ctx
->spaceused
== 0) {return COMP_SUCCESS
;}
160 inbuf
= (Bytef
*)mmGetPtr(ctx
->buf
);
161 outbuf
= (Bytef
*)malloc(OUTBUFSIZE
);
162 if(outbuf
==NULL
) {return COMP_ERR_LOW_MEMORY
;}
164 insize
= ctx
->spaceused
;
165 outsize
= OUTBUFSIZE
;
167 resp
= compress(outbuf
,&outsize
,inbuf
,insize
);
168 if(resp
==Z_MEM_ERROR
) {return COMP_ERR_LOW_MEMORY
;}
169 if(resp
==Z_BUF_ERROR
) {return COMP_ERR_LIB
;}
171 *out
= (float)outsize
/(float)insize
;
173 /* Thrash the memory and free it */
174 trashMemory(outbuf
, OUTBUFSIZE
);
180 comp_error_status
comp_end(COMP_CTX
* ctx
)
182 if(ctx
== NULL
) {return COMP_SUCCESS
;} /* Since nothing is left undone */
190 #endif /* YARROW_KERNEL */