]>
git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/comp.c
e3ffa865d13a146b94412d6951b0a8e5c7015d97
2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 * @APPLE_LICENSE_HEADER_END@
27 Contains: NULL compression. Kernel version of Yarrow assumes
28 incoming seed data is truly random.
30 #include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
35 /* null compression */
36 comp_error_status
comp_init(__unused COMP_CTX
* ctx
)
42 comp_error_status
comp_add_data( __unused COMP_CTX
* ctx
,
44 __unused uInt inplen
)
49 comp_error_status
comp_get_ratio( __unused COMP_CTX
* ctx
,float* out
)
55 comp_error_status
comp_end( __unused COMP_CTX
* ctx
)
62 /* original Yarrow compression, must be linked with zlib */
64 #if defined(macintosh) || defined(__APPLE__)
65 #include "WindowsTypesForMac.h"
66 #include "yarrowUtils.h"
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) \
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 */
88 #define _MIN(a,b) (((a)<(b))?(a):(b))
91 /* Initialize these routines */
92 comp_error_status
comp_init(COMP_CTX
* ctx
)
94 ctx
->buf
= mmMalloc(BUFSIZE
);
95 if(ctx
->buf
== MM_NULL
) {goto cleanup_comp_init
;}
103 return COMP_ERR_LOW_MEMORY
;
107 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
116 buf
= (BYTE
*)mmGetPtr(ctx
->buf
);
118 if(inplen
+SHIFTSIZE
>BUFSIZE
)
120 blocksize
= _MIN(inplen
,BUFSIZE
);
121 memmove(buf
,inp
,blocksize
);
122 ctx
->spaceused
= blocksize
;
126 if(inplen
+ctx
->spaceused
>BUFSIZE
)
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
;
133 memmove(buf
+ctx
->spaceused
,inp
,inplen
);
134 ctx
->spaceused
+= inplen
;
140 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
142 Bytef
*inbuf
,*outbuf
;
143 uLong insize
,outsize
;
151 if(ctx
->spaceused
== 0) {return COMP_SUCCESS
;}
153 inbuf
= (Bytef
*)mmGetPtr(ctx
->buf
);
154 outbuf
= (Bytef
*)malloc(OUTBUFSIZE
);
155 if(outbuf
==NULL
) {return COMP_ERR_LOW_MEMORY
;}
157 insize
= ctx
->spaceused
;
158 outsize
= OUTBUFSIZE
;
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
;}
164 *out
= (float)outsize
/(float)insize
;
166 /* Thrash the memory and free it */
167 trashMemory(outbuf
, OUTBUFSIZE
);
173 comp_error_status
comp_end(COMP_CTX
* ctx
)
175 if(ctx
== NULL
) {return COMP_SUCCESS
;} /* Since nothing is left undone */
183 #endif /* YARROW_KERNEL */