00001 #include "zlib-compress.C"
00002 
00003 #include "lz4.h"
00004 #define     CMODE_NOCOMPRESS 0
00005 #define     CMODE_ZLIB  1
00006 
00007 #define     CMODE_LZ4     3
00008 #define OUT_PLACE 1
00009 
00010 
00011 #include <sys/time.h>
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 static int compress_mode = CMODE_ZLIB;
00024 
00025 static int initDone=0;
00026 void initCompress(void)
00027 {
00028     switch(compress_mode)
00029     {
00030         case CMODE_ZLIB:    zlib_init();    break;
00031         
00032         case CMODE_LZ4: break;
00033     }
00034 
00035 }
00036 
00037 
00038 void compressZlib(void *src, void *dst, int size, int *compressSize, void *bData)
00039 {
00040 #if DEBUG
00041     double t1 = get_clock();
00042 #endif
00043     char *src_ptr = (char*)src;
00044     char *bdata_ptr = (char*)bData;
00045     if(initDone==0)
00046     {
00047         initCompress();
00048         initDone = 1;
00049     }
00050     auto my_xor = (char *)malloc(size);
00051     int i;
00052     for(i=0; i<size; i++)
00053         my_xor[i] = (src_ptr[i])^(bdata_ptr[i]);
00054     zlib_compress(my_xor, dst, size, compressSize);
00055 #if DEBUG
00056     double t = get_clock()-t1;
00057     printf("+%d external compression done compressing(%d===>%d) (reduction:%d) ration=%f time=%d us\n", compress_mode, (int)(size*sizeof(char)), *compressSize, (int)(size*sizeof(char)-*compressSize), (1-(float)*compressSize/(size*sizeof(char)))*100, (int)(t*1000000));
00058 #endif
00059 }
00060 
00061 void decompressZlib(void *cData, void *dData, int size, int compressSize, void *bData) {
00062 
00063 #if DEBUG
00064     double t1 = get_clock();
00065 #endif
00066     char *my_xor=(char*)malloc(size);
00067     zlib_decompress(cData, my_xor, compressSize, size);
00068     int i;
00069     char *dptr = (char*)dData;
00070     char *bptr = (char*)bData; 
00071     for(i=0; i<size; i++)
00072         dptr[i] = (bptr[i])^(my_xor[i]);
00073     free(my_xor);
00074 #if DEBUG
00075     double t = get_clock()-t1;
00076     printf("------done decompressing.....  orig size:%d time:%d us \n", (int)size, (int)(t*1000000)) ;
00077 #endif
00078 }
00079 
00080 void compressLz4(void *src, void *dst, int size, int *compressSize, void *bData)
00081 {
00082 #if DEBUG
00083     double t1 = get_clock();
00084 #endif
00085     char *src_ptr = (char*)src;
00086     char *bdata_ptr = (char*)bData;
00087     if(initDone==0)
00088     {
00089         initCompress();
00090         initDone = 1;
00091     }
00092     auto my_xor = (char *)malloc(size);
00093     int i;
00094     for(i=0; i<size; i++)
00095         my_xor[i] = (src_ptr[i])^(bdata_ptr[i]);
00096 
00097     *compressSize = LZ4_compress_default(my_xor, (char *)dst, size, LZ4_compressBound(size));
00098 
00099 #if DEBUG
00100     double t = get_clock()-t1;
00101     printf("+%d external compression done compressing(%d===>%d) (reduction:%d) ration=%f time=%d us\n", compress_mode, (int)(size*sizeof(char)), *compressSize, (int)(size*sizeof(char)-*compressSize), (1-(float)*compressSize/(size*sizeof(char)))*100, (int)(t*1000000));
00102 #endif
00103 }
00104 
00105 void decompressLz4(void *cData, void *dData, int size, int compressSize, void *bData) {
00106 
00107 #if DEBUG
00108     double t1 = get_clock();
00109 #endif
00110     char *my_xor=(char*)malloc(size);
00111 
00112     if (LZ4_decompress_safe((char *)cData, my_xor, compressSize, size) < 0)
00113         CmiAbort("decode fails\n");
00114 
00115     int i;
00116     char *dptr = (char*)dData;
00117     char *bptr = (char*)bData; 
00118     for(i=0; i<size; i++)
00119         dptr[i] = (bptr[i])^(my_xor[i]);
00120     free(my_xor);
00121 #if DEBUG
00122     double t = get_clock()-t1;
00123     printf("------done decompressing.....  orig size:%d time:%d us \n", (int)size, (int)(t*1000000)) ;
00124 #endif
00125 }