]>
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_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 Contains: NULL compression. Kernel version of Yarrow assumes
27 incoming seed data is truly random.
29 #include "dev/random/YarrowCoreLib/include/WindowsTypesForMac.h"
34 /* null compression */
35 comp_error_status
comp_init(__unused COMP_CTX
* ctx
)
41 comp_error_status
comp_add_data( __unused COMP_CTX
* ctx
,
43 __unused uInt inplen
)
48 comp_error_status
comp_get_ratio( __unused COMP_CTX
* ctx
,float* out
)
54 comp_error_status
comp_end( __unused COMP_CTX
* ctx
)
61 /* original Yarrow compression, must be linked with zlib */
63 #if defined(macintosh) || defined(__APPLE__)
64 #include "WindowsTypesForMac.h"
65 #include "yarrowUtils.h"
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) \
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 */
87 #define _MIN(a,b) (((a)<(b))?(a):(b))
90 /* Initialize these routines */
91 comp_error_status
comp_init(COMP_CTX
* ctx
)
93 ctx
->buf
= mmMalloc(BUFSIZE
);
94 if(ctx
->buf
== MM_NULL
) {goto cleanup_comp_init
;}
102 return COMP_ERR_LOW_MEMORY
;
106 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
115 buf
= (BYTE
*)mmGetPtr(ctx
->buf
);
117 if(inplen
+SHIFTSIZE
>BUFSIZE
)
119 blocksize
= _MIN(inplen
,BUFSIZE
);
120 memmove(buf
,inp
,blocksize
);
121 ctx
->spaceused
= blocksize
;
125 if(inplen
+ctx
->spaceused
>BUFSIZE
)
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
;
132 memmove(buf
+ctx
->spaceused
,inp
,inplen
);
133 ctx
->spaceused
+= inplen
;
139 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
141 Bytef
*inbuf
,*outbuf
;
142 uLong insize
,outsize
;
150 if(ctx
->spaceused
== 0) {return COMP_SUCCESS
;}
152 inbuf
= (Bytef
*)mmGetPtr(ctx
->buf
);
153 outbuf
= (Bytef
*)malloc(OUTBUFSIZE
);
154 if(outbuf
==NULL
) {return COMP_ERR_LOW_MEMORY
;}
156 insize
= ctx
->spaceused
;
157 outsize
= OUTBUFSIZE
;
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
;}
163 *out
= (float)outsize
/(float)insize
;
165 /* Thrash the memory and free it */
166 trashMemory(outbuf
, OUTBUFSIZE
);
172 comp_error_status
comp_end(COMP_CTX
* ctx
)
174 if(ctx
== NULL
) {return COMP_SUCCESS
;} /* Since nothing is left undone */
182 #endif /* YARROW_KERNEL */