00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdlib.h>
00018 #include <stdint.h>
00019 #include <assert.h>
00020 #include <string.h>
00021 #include <stdio.h>
00022 #include "zlib.h"
00023
00024 #if defined(MSDOS) || defined(OS2) || defined(_WIN32)
00025 # include <fcntl.h>
00026 # include <io.h>
00027 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
00028 #else
00029 # define SET_BINARY_MODE(file)
00030 #endif
00031
00032 #define COMP_DEBUG 0
00033
00034 void zlib_compress(void *in_data, void *out_data, int in_data_size, int *out_data_size)
00035 {
00036 #if 1
00037 double start_timer=CmiWallTimer();
00038 z_stream strm;
00039
00040 strm.zalloc = 0;
00041 strm.zfree = 0;
00042 strm.next_in = (uint8_t *)(in_data);
00043 strm.avail_in = in_data_size;
00044 strm.next_out = (uint8_t *)(out_data);
00045 strm.avail_out = in_data_size;
00046
00047 deflateInit(&strm, Z_BEST_COMPRESSION);
00048
00049 if(strm.avail_in != 0)
00050 {
00051 int res = deflate(&strm, Z_NO_FLUSH);
00052 assert(res == Z_OK);
00053
00054 int deflate_res = Z_OK;
00055 while (deflate_res == Z_OK)
00056 {
00057 deflate_res = deflate(&strm, Z_FINISH);
00058 }
00059 }
00060 deflateEnd(&strm);
00061
00062 *out_data_size = in_data_size - strm.avail_out;
00063 #if COMP_DEBUG
00064 CmiPrintf( "[%d]: %d \t\t %d \t\t %.3lf cost:%.3f\n", CmiMyPe(), in_data_size, *out_data_size, *out_data_size/(float)in_data_size, (CmiWallTimer()-start_timer)*1000);
00065 #endif
00066
00067
00068 #else
00069 memcpy(out_data, in_data, in_data_size);
00070 *out_data_size = in_data_size;
00071 #endif
00072 }
00073
00074 int zlib_decompress(const void *src, void *dst, int srcLen, int dstLen) {
00075 #if 1
00076 double start_timer=CmiWallTimer();
00077 z_stream strm = {0};
00078 strm.total_in = strm.avail_in = srcLen;
00079 strm.total_out = strm.avail_out = dstLen;
00080 strm.next_in = (Bytef *) src;
00081 strm.next_out = (Bytef *) dst;
00082
00083 strm.zalloc = Z_NULL;
00084 strm.zfree = Z_NULL;
00085 strm.opaque = Z_NULL;
00086
00087 int err = -1;
00088 int ret = -1;
00089
00090 err = inflateInit(&strm);
00091 if (err == Z_OK) {
00092 err = inflate(&strm, Z_NO_FLUSH);
00093 if (err == Z_STREAM_END) {
00094 ret = strm.total_out;
00095 }
00096 inflateEnd(&strm);
00097 }
00098 else {
00099 inflateEnd(&strm);
00100 ret = err;
00101 }
00102 #if COMP_DEBUG
00103 CmiPrintf("Decompress time:%d %.3f\n", dstLen, (CmiWallTimer()-start_timer));
00104 #endif
00105 return ret;
00106 #else
00107 memcpy(dst, src, srcLen);
00108 return 0;
00109 #endif
00110 }
00111
00112 void zlib_init(void) {}