]>
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(COMP_CTX
* ctx
)
41 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
46 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
52 comp_error_status
comp_end(COMP_CTX
* ctx
)
59 /* original Yarrow compression, must be linked with zlib */
61 #if defined(macintosh) || defined(__APPLE__)
62 #include "WindowsTypesForMac.h"
63 #include "yarrowUtils.h"
72 /* Check that the pointer is not NULL */
73 #define PCHECK(ptr) if(ptr==NULL) {return COMP_ERR_NULL_POINTER;}
74 #define MMPCHECK(mmptr) if(mmptr==MM_NULL) {return COMP_ERR_NULL_POINTER;}
75 /* Check that the important parts of the context are ok */
76 #define CTXCHECK(ctx) \
80 /* Might want to vary these by context */
81 #define BUFSIZE 16384 /* 16K */
82 #define OUTBUFSIZE 16800 /* = inbufsize*1.01 + 12 (See zlib docs) */
83 #define SHIFTSIZE 4096 /* BUFSIZE/4 */
85 #define _MIN(a,b) (((a)<(b))?(a):(b))
88 /* Initialize these routines */
89 comp_error_status
comp_init(COMP_CTX
* ctx
)
91 ctx
->buf
= mmMalloc(BUFSIZE
);
92 if(ctx
->buf
== MM_NULL
) {goto cleanup_comp_init
;}
100 return COMP_ERR_LOW_MEMORY
;
104 comp_error_status
comp_add_data(COMP_CTX
* ctx
,Bytef
* inp
,uInt inplen
)
113 buf
= (BYTE
*)mmGetPtr(ctx
->buf
);
115 if(inplen
+SHIFTSIZE
>BUFSIZE
)
117 blocksize
= _MIN(inplen
,BUFSIZE
);
118 memmove(buf
,inp
,blocksize
);
119 ctx
->spaceused
= blocksize
;
123 if(inplen
+ctx
->spaceused
>BUFSIZE
)
125 shifts
= (uInt
)ceil((inplen
+ctx
->spaceused
-BUFSIZE
)/(float)SHIFTSIZE
);
126 blocksize
= _MIN(shifts
*SHIFTSIZE
,ctx
->spaceused
);
127 memmove(buf
,buf
+blocksize
,BUFSIZE
-blocksize
);
128 ctx
->spaceused
= ctx
->spaceused
- blocksize
;
130 memmove(buf
+ctx
->spaceused
,inp
,inplen
);
131 ctx
->spaceused
+= inplen
;
137 comp_error_status
comp_get_ratio(COMP_CTX
* ctx
,float* out
)
139 Bytef
*inbuf
,*outbuf
;
140 uLong insize
,outsize
;
148 if(ctx
->spaceused
== 0) {return COMP_SUCCESS
;}
150 inbuf
= (Bytef
*)mmGetPtr(ctx
->buf
);
151 outbuf
= (Bytef
*)malloc(OUTBUFSIZE
);
152 if(outbuf
==NULL
) {return COMP_ERR_LOW_MEMORY
;}
154 insize
= ctx
->spaceused
;
155 outsize
= OUTBUFSIZE
;
157 resp
= compress(outbuf
,&outsize
,inbuf
,insize
);
158 if(resp
==Z_MEM_ERROR
) {return COMP_ERR_LOW_MEMORY
;}
159 if(resp
==Z_BUF_ERROR
) {return COMP_ERR_LIB
;}
161 *out
= (float)outsize
/(float)insize
;
163 /* Thrash the memory and free it */
164 trashMemory(outbuf
, OUTBUFSIZE
);
170 comp_error_status
comp_end(COMP_CTX
* ctx
)
172 if(ctx
== NULL
) {return COMP_SUCCESS
;} /* Since nothing is left undone */
180 #endif /* YARROW_KERNEL */