提交 bf3078cc 编写于 作者: T tickduan

TSZ support TSZ_IMPL macro to use or not use. remove zlig

上级 a215ba73
CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20)
PROJECT(TDengine)
ADD_DEFINITIONS(-DTSZ_IMPL)
IF (TD_ACCOUNT)
ADD_DEFINITIONS(-D_ACCT)
ENDIF ()
......
......@@ -3,23 +3,21 @@ PROJECT(TDengine)
# include
INCLUDE_DIRECTORIES(sz/include)
INCLUDE_DIRECTORIES(zlib/)
INCLUDE_DIRECTORIES(zstd/)
INCLUDE_DIRECTORIES(zstd/common/)
# source
AUX_SOURCE_DIRECTORY(sz/src SRC1)
AUX_SOURCE_DIRECTORY(zlib/ SRC2)
AUX_SOURCE_DIRECTORY(zstd/dictBuilder SRC2)
AUX_SOURCE_DIRECTORY(zstd/common SRC3)
AUX_SOURCE_DIRECTORY(zstd/compress SRC4)
AUX_SOURCE_DIRECTORY(zstd/decompress SRC5)
AUX_SOURCE_DIRECTORY(zstd/deprecated SRC6)
AUX_SOURCE_DIRECTORY(zstd/legacy SRC7)
AUX_SOURCE_DIRECTORY(zstd/dictBuilder SRC8)
# archive
ADD_LIBRARY(SZ STATIC ${SRC1} ${SRC2} ${SRC3} ${SRC4} ${SRC5} ${SRC6} ${SRC7} ${SRC8})
ADD_LIBRARY(SZ STATIC ${SRC1} ${SRC2} ${SRC3} ${SRC4} ${SRC5} ${SRC6} ${SRC7})
# windows ignore warning
IF (TD_WINDOWS)
......
/**
* @file callZlib.h
* @author Sheng Di
* @date July, 2017
* @brief Header file for the callZlib.c.
* (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
* See COPYRIGHT in top-level directory.
*/
#ifndef _CallZlib_H
#define _CallZlib_H
#ifdef __cplusplus
extern "C" {
#endif
//#define SZ_ZLIB_BUFFER_SIZE 1048576
#define SZ_ZLIB_BUFFER_SIZE 65536
#include <stdio.h>
int isZlibFormat(unsigned char magic1, unsigned char magic2);
//callZlib.c
unsigned long zlib_compress(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level);
unsigned long zlib_compress2(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level);
unsigned long zlib_compress3(unsigned char* data, unsigned long dataLength, unsigned char* compressBytes, int level);
unsigned long zlib_compress4(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level);
unsigned long zlib_compress5(unsigned char* data, unsigned long dataLength, unsigned char* compressBytes, int level);
unsigned long zlib_uncompress4(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
unsigned long zlib_uncompress5(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
unsigned long zlib_uncompress(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
unsigned long zlib_uncompress2(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
unsigned long zlib_uncompress3(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
unsigned long zlib_uncompress65536bytes(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData);
#ifdef __cplusplus
}
#endif
#endif /* ----- #ifndef _CallZlib_H ----- */
......@@ -31,7 +31,6 @@ typedef struct sz_params
int sampleDistance; //2 bytes
float predThreshold; // 2 bytes
int szMode; //* 0 (best speed) or 1 (better compression with Zstd/Gzip) or 3 temporal-dimension based compression
int gzipMode; //* four options: Z_NO_COMPRESSION, or Z_BEST_SPEED, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION
int errorBoundMode; //4bits (0.5byte), //SZ_ABS, REL, ABS_AND_REL, or ABS_OR_REL, PSNR, or PW_REL, PSNR
double absErrBound; //absolute error bound for float
double absErrBoundDouble; // for double
......
......@@ -18,7 +18,7 @@ extern "C" {
int is_lossless_compressed_data(unsigned char* compressedBytes, size_t cmpSize);
unsigned long sz_lossless_compress(int losslessCompressor, int level, unsigned char* data, unsigned long dataLength, unsigned char* compressBytes);
unsigned long sz_lossless_compress(int losslessCompressor, unsigned char* data, unsigned long dataLength, unsigned char* compressBytes);
unsigned long sz_lossless_decompress(int losslessCompressor, unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize);
......
......@@ -10,7 +10,6 @@
#include <stdlib.h>
#include <string.h>
#include "sz.h"
#include "zlib.h"
INLINE int bytesToInt_bigEndian(unsigned char* bytes)
{
......@@ -287,21 +286,6 @@ void convertSZParamsToBytes(sz_params* params, unsigned char* result, char optQu
buf = (buf << 1) | sysEndianType;
buf = (buf << 2) | params->szMode;
int tmp = 0;
switch(params->gzipMode)
{
case Z_BEST_SPEED:
tmp = 0;
break;
case Z_DEFAULT_STRATEGY:
tmp = 1;
break;
case Z_BEST_COMPRESSION:
tmp = 2;
break;
}
buf = (buf << 2) | tmp;
//buf = (buf << 2) | params->pwr_type; //deprecated
result[0] = buf;
}
......@@ -309,22 +293,6 @@ void convertBytesToSZParams(unsigned char* bytes, sz_params* params, sz_exedata*
{
unsigned char flag1 = bytes[0];
pde_exe->optQuantMode = (flag1 & 0x40) >> 6;
dataEndianType = (flag1 & 0x20) >> 5;
//sysEndianType = (flag1 & 0x10) >> 4;
dataEndianType = (flag1 & 0x20) >> 5;
params->szMode = (flag1 & 0x0c) >> 2;
int tmp = (flag1 & 0x03);
switch(tmp)
{
case 0:
params->gzipMode = Z_BEST_SPEED;
break;
case 1:
params->gzipMode = Z_DEFAULT_STRATEGY;
break;
case 2:
params->gzipMode = Z_BEST_COMPRESSION;
break;
}
}
/**
* @file callZlib.c
* @author Sheng Di
* @date June, 2016
* @brief gzip compressor code: the interface to call zlib
* (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
* See COPYRIGHT in top-level directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <sz.h>
#if MAX_MEM_LEVEL >= 8
#define DEF_MEM_LEVEL 8
#else
#define DEF_MEM_LEVEL MAX_MEM_LEVEL
#endif
#define CHECK_ERR(err, msg) { \
if (err != Z_OK && err != Z_STREAM_END) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
return SZ_FAILED; \
} \
}
int isZlibFormat(unsigned char magic1, unsigned char magic2)
{
if(magic1==104&&magic2==5) //DC+BS
return 1;
if(magic1==104&&magic2==129) //DC+DC
return 1;
if(magic1==104&&magic2==222) //DC+BC
return 1;
if(magic1==120&&magic2==1) //BC+BS
return 1;
if(magic1==120&&magic2==94) //BC+?
return 1;
if(magic1==120&&magic2==156) //BC+DC
return 1;
if(magic1==120&&magic2==218) //BC+BS
return 1;
return 0;
}
/*zlib_compress() is only valid for median-size data compression. */
unsigned long zlib_compress(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level)
{
z_stream stream = {0};
stream.next_in = data;
stream.avail_in = dataLength;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR;
#endif
uLong estCmpLen = deflateBound(&stream, dataLength);
unsigned long outSize = estCmpLen;
*compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen);
int err = compress2(*compressBytes, &outSize, data, dataLength, level);
if(err!=Z_OK)
{
printf("Error: err_code=%d; the reason may be your data size is too large (>=2^32), which cannot be compressed by standalone zlib_compress. Sol: inflace_init, ....\n", err);
exit(0);
}
return outSize;
}
unsigned long zlib_compress2(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level)
{
unsigned long outSize;
z_stream stream = {0};
int err;
stream.next_in = data;
stream.avail_in = dataLength;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR;
#endif
uLong estCmpLen = deflateBound(&stream, dataLength);
*compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen);
stream.next_out = *compressBytes;
stream.avail_out = estCmpLen;
//stream.avail_out = dataLength*10;
//if ((uLong)stream.avail_out != dataLength*10) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
// stream.data_type = Z_TEXT;
//err = deflateInit(&stream, level); //default windowBits == 15.
int windowBits = 14; //8-15
if(confparams_cpr->szMode==SZ_BEST_COMPRESSION)
windowBits = 15;
err = deflateInit2(&stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY
if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
err = deflateEnd(&stream);
outSize = stream.total_out;
return outSize;
}
unsigned long zlib_compress3(unsigned char* data, unsigned long dataLength, unsigned char* compressBytes, int level)
{
unsigned long outSize = 0;
z_stream stream = {0};
int err;
stream.next_in = data;
stream.avail_in = dataLength;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != dataLength) return Z_BUF_ERROR;
#endif
stream.next_out = compressBytes;
stream.avail_out = dataLength;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
//err = deflateInit(&stream, level); //default windowBits == 15.
int windowBits = 14; //8-15
if(confparams_cpr->szMode==SZ_BEST_COMPRESSION)
windowBits = 15;
err = deflateInit2(&stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY
if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
err = deflateEnd(&stream);
outSize = stream.total_out;
return outSize;
}
unsigned long zlib_compress4(unsigned char* data, unsigned long dataLength, unsigned char** compressBytes, int level)
{
z_stream c_stream = {0}; /* compression stream */
int err = 0;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
int windowBits = 14; //8-15
if(confparams_cpr->szMode==SZ_BEST_COMPRESSION)
windowBits = 15;
err = deflateInit2(&c_stream, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL,
Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY
CHECK_ERR(err, "deflateInit");
uLong estCmpLen = deflateBound(&c_stream, dataLength);
*compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen);
c_stream.next_in = data;
c_stream.next_out = *compressBytes;
while (c_stream.total_in < dataLength && c_stream.total_out < estCmpLen) {
c_stream.avail_in = c_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
return c_stream.total_out;
}
unsigned long zlib_compress5(unsigned char* data, unsigned long dataLength, unsigned char* compressBytes, int level)
{
int ret, flush;
unsigned have;
z_stream strm;
unsigned char* in = data;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, level);
//int windowBits = 15;
//ret = deflateInit2(&strm, level, Z_DEFLATED, windowBits, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);//Z_FIXED); //Z_DEFAULT_STRATEGY
if (ret != Z_OK)
return ret;
size_t p_size = 0, av_in = 0;
deflateBound(&strm, dataLength);
//*compressBytes = (unsigned char*)malloc(sizeof(unsigned char)*estCmpLen); // comment by tickduan no need malloc
unsigned char* out = compressBytes;
/* compress until end of file */
do {
p_size += SZ_ZLIB_BUFFER_SIZE;
if(p_size>=dataLength)
{
av_in = dataLength - (p_size - SZ_ZLIB_BUFFER_SIZE);
flush = Z_FINISH;
}
else
{
av_in = SZ_ZLIB_BUFFER_SIZE;
flush = Z_NO_FLUSH;
}
strm.avail_in = av_in;
strm.next_in = in;
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
strm.avail_out = SZ_ZLIB_BUFFER_SIZE;
strm.next_out = out;
ret = deflate(&strm, flush); /* no bad return value */
have = SZ_ZLIB_BUFFER_SIZE - strm.avail_out;
out += have;
} while (strm.avail_out == 0);
in+=av_in;
/* done when last data in file processed */
} while (flush != Z_FINISH);
/* clean up and return */
(void)deflateEnd(&strm);
return strm.total_out;
}
unsigned long zlib_uncompress(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
{
unsigned long outSize = targetOriSize;
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
int status = uncompress(*oriData, &outSize, compressBytes, cmpSize);
if(status!=Z_OK)
{
printf("Error: Zlib decompression error; status=%d\n", status);
exit(0);
}
return outSize;
}
unsigned long zlib_uncompress2 (unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
{
z_stream stream = {0};
unsigned long outSize;
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
// stream.data_type = Z_TEXT;
stream.next_in = compressBytes;
stream.avail_in = cmpSize;
/* Check for source > 64K on 16-bit machine: */
if ((unsigned long)stream.avail_in != cmpSize)
{
printf("Error: zlib_uncompress2: stream.avail_in != cmpSize");
//exit(1);
return SZ_FAILED; //-1
}
stream.next_out = *oriData;
stream.avail_out = targetOriSize;
//if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
int err = inflateInit(&stream);
//int windowBits = 15;
//int err = inflateInit2(&stream, windowBits);
if (err != Z_OK)
{
printf("Error: zlib_uncompress2: err != Z_OK\n");
return SZ_FAILED;
}
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
outSize = stream.total_out;
inflateEnd(&stream);
return outSize;
}
unsigned long zlib_uncompress3(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
{
int status;
z_stream z_strm; /* decompression stream */
size_t nalloc = 65536*4;
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
memset(&z_strm, 0, sizeof(z_strm));
/*d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;*/
z_strm.next_in = compressBytes;
z_strm.avail_in = 0;
z_strm.next_out = *oriData;
z_strm.avail_out = targetOriSize;
status = inflateInit(&z_strm);
CHECK_ERR(status, "inflateInit");
do{
z_strm.avail_in = z_strm.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */
/* Uncompress some data */
status = inflate(&z_strm, Z_SYNC_FLUSH);
/* Check if we are done uncompressing data */
if (Z_STREAM_END==status)
break; /*done*/
if (Z_OK!=status) {
(void)inflateEnd(&z_strm);
printf("Error: inflate() failed\n");
exit(0);
}
else
{
/* If we're not done and just ran out of buffer space, get more */
if(0 == z_strm.avail_out) {
void *new_outbuf; /* Pointer to new output buffer */
/* Allocate a buffer twice as big */
nalloc *= 2;
if(NULL == (new_outbuf = realloc(*oriData, nalloc))) {
(void)inflateEnd(&z_strm);
printf("Error: memory allocation failed for deflate uncompression\n");
exit(0);
} /* end if */
*oriData = new_outbuf;
/* Update pointers to buffer for next set of uncompressed data */
z_strm.next_out = (*oriData) + z_strm.total_out;
z_strm.avail_out = (uInt)(nalloc - z_strm.total_out);
} /* end if */
} /* end else*/
}while(status==Z_OK);
status = inflateEnd(&z_strm);
CHECK_ERR(status, "inflateEnd");
return z_strm.total_out;
}
unsigned long zlib_uncompress4(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
{
int ret;
unsigned int have;
z_stream strm;
unsigned char *in = compressBytes;
unsigned char *out;
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
out = *oriData;
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK)
{
return ret;
}
size_t p_size = 0, av_in = 0;
/* decompress until deflate stream ends or end of file */
do {
p_size += SZ_ZLIB_BUFFER_SIZE;
if(p_size>cmpSize)
av_in = cmpSize - (p_size - SZ_ZLIB_BUFFER_SIZE);
else
av_in = SZ_ZLIB_BUFFER_SIZE;
strm.avail_in = av_in;
if (strm.avail_in == 0)
break;
strm.next_in = in;
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = SZ_ZLIB_BUFFER_SIZE;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
have = SZ_ZLIB_BUFFER_SIZE - strm.avail_out;
out += have;
} while (strm.avail_out == 0);
in+=av_in;
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void)inflateEnd(&strm);
return strm.total_out;
}
unsigned long zlib_uncompress65536bytes(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData)
{
int err;
unsigned long targetOriSize = 65536;
z_stream d_stream = {0}; /* decompression stream */
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = compressBytes;
d_stream.avail_in = 0;
d_stream.next_out = *oriData;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
while (d_stream.total_out < targetOriSize && d_stream.total_in < cmpSize) {
d_stream.avail_in = d_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */
//err = inflate(&d_stream, Z_NO_FLUSH);
err = inflate(&d_stream, Z_SYNC_FLUSH);
if (err == Z_STREAM_END) break;
if(err<0)
break;
}
if(err<0)
return d_stream.total_out;
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
return d_stream.total_out;
}
unsigned long zlib_uncompress5(unsigned char* compressBytes, unsigned long cmpSize, unsigned char** oriData, unsigned long targetOriSize)
{
int err;
z_stream d_stream = {0}; /* decompression stream */
*oriData = (unsigned char*)malloc(sizeof(unsigned char)*targetOriSize);
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = compressBytes;
d_stream.avail_in = 0;
d_stream.next_out = *oriData;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
while (d_stream.total_out < targetOriSize && d_stream.total_in < cmpSize) {
d_stream.avail_in = d_stream.avail_out = SZ_ZLIB_BUFFER_SIZE; /* force small buffers */
//err = inflate(&d_stream, Z_NO_FLUSH);
err = inflate(&d_stream, Z_SYNC_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "inflate");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
return d_stream.total_out;
}
......@@ -35,10 +35,6 @@ void setDefaulParams(sz_exedata* exedata, sz_params* params)
params->predThreshold = 0.99;
params->sampleDistance = 100;
params->szMode = SZ_BEST_COMPRESSION;
if(params->losslessCompressor==ZSTD_COMPRESSOR)
params->gzipMode = 3; //fast mode
else
params->gzipMode = 1; //high speed mode
// other
params->psnr = 90;
......@@ -254,51 +250,7 @@ int SZ_ReadConf(const char* sz_cfgFile) {
confparams_cpr->withRegression = SZ_WITH_LINEAR_REGRESSION;
else
confparams_cpr->withRegression = SZ_NO_REGRESSION;
modeBuf = iniparser_getstring(ini, "PARAMETER:gzipMode", "Gzip_BEST_SPEED");
if(modeBuf==NULL)
{
printf("[SZ] Error: Null Gzip mode setting (please check sz.config file)\n");
iniparser_freedict(ini);
return SZ_FAILED;
}
else if(strcmp(modeBuf, "Gzip_NO_COMPRESSION")==0)
confparams_cpr->gzipMode = 0;
else if(strcmp(modeBuf, "Gzip_BEST_SPEED")==0)
confparams_cpr->gzipMode = 1;
else if(strcmp(modeBuf, "Gzip_BEST_COMPRESSION")==0)
confparams_cpr->gzipMode = 9;
else if(strcmp(modeBuf, "Gzip_DEFAULT_COMPRESSION")==0)
confparams_cpr->gzipMode = -1;
else
{
printf("[SZ] Error: Wrong gzip Mode (please check sz.config file)\n");
return SZ_FAILED;
}
modeBuf = iniparser_getstring(ini, "PARAMETER:zstdMode", "Zstd_HIGH_SPEED");
if(modeBuf==NULL)
{
printf("[SZ] Error: Null Zstd mode setting (please check sz.config file)\n");
iniparser_freedict(ini);
return SZ_FAILED;
}
else if(strcmp(modeBuf, "Zstd_BEST_SPEED")==0)
confparams_cpr->gzipMode = 1;
else if(strcmp(modeBuf, "Zstd_HIGH_SPEED")==0)
confparams_cpr->gzipMode = 3;
else if(strcmp(modeBuf, "Zstd_HIGH_COMPRESSION")==0)
confparams_cpr->gzipMode = 19;
else if(strcmp(modeBuf, "Zstd_BEST_COMPRESSION")==0)
confparams_cpr->gzipMode = 22;
else if(strcmp(modeBuf, "Zstd_DEFAULT_COMPRESSION")==0)
confparams_cpr->gzipMode = 3;
else
{
printf("[SZ] Error: Wrong zstd Mode (please check sz.config file)\n");
return SZ_FAILED;
}
modeBuf = iniparser_getstring(ini, "PARAMETER:protectValueRange", "YES");
if(strcmp(modeBuf, "YES")==0)
confparams_cpr->protectValueRange = 1;
......
......@@ -16,7 +16,6 @@
#include "DynamicByteArray.h"
#include "TightDataPointStorageD.h"
#include "TightDataPointStorageF.h"
#include "zlib.h"
#include "conf.h"
#include "utility.h"
......
......@@ -19,7 +19,6 @@
#include "TightDataPointStorageD.h"
#include "sz_double.h"
#include "szd_double.h"
#include "zlib.h"
#include "utility.h"
unsigned char* SZ_skip_compress_double(double* data, size_t dataLength, size_t* outSize)
......@@ -360,7 +359,7 @@ int SZ_compress_args_double(double *oriData, size_t r1, unsigned char* newByteDa
//
if(twoStage)
{
*outSize = sz_lossless_compress(params->losslessCompressor, params->gzipMode, tmpByteData, tmpOutSize, newByteData);
*outSize = sz_lossless_compress(params->losslessCompressor, tmpByteData, tmpOutSize, newByteData);
free(tmpByteData);
}
else
......
......@@ -19,7 +19,6 @@
#include "TightDataPointStorageF.h"
#include "sz_float.h"
#include "szd_float.h"
#include "zlib.h"
#include "utility.h"
# define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
......@@ -383,7 +382,7 @@ int SZ_compress_args_float(float *oriData, size_t r1, unsigned char* newByteData
}
else
{
*outSize = sz_lossless_compress(params->losslessCompressor, params->gzipMode, tmpByteData, tmpOutSize, newByteData);
*outSize = sz_lossless_compress(params->losslessCompressor, tmpByteData, tmpOutSize, newByteData);
free(tmpByteData);
}
......
......@@ -13,7 +13,6 @@
#include <math.h>
#include "utility.h"
#include "sz.h"
#include "callZlib.h"
#include "zstd.h"
......@@ -28,22 +27,17 @@ int is_lossless_compressed_data(unsigned char* compressedBytes, size_t cmpSize)
if(frameContentSize != 0)
return ZSTD_COMPRESSOR;
#endif
int flag = isZlibFormat(compressedBytes[0], compressedBytes[1]);
if(flag)
return GZIP_COMPRESSOR;
return -1; //fast mode (without GZIP or ZSTD)
}
unsigned long sz_lossless_compress(int losslessCompressor, int level, unsigned char* data, unsigned long dataLength, unsigned char* compressBytes)
unsigned long sz_lossless_compress(int losslessCompressor, unsigned char* data, unsigned long dataLength, unsigned char* compressBytes)
{
unsigned long outSize = 0;
int level = 3 ; // fast mode
size_t estimatedCompressedSize = 0;
switch(losslessCompressor)
{
case GZIP_COMPRESSOR:
outSize = zlib_compress5(data, dataLength, compressBytes, level);
break;
case ZSTD_COMPRESSOR:
if(dataLength < 100)
estimatedCompressedSize = 200;
......@@ -63,9 +57,6 @@ unsigned long sz_lossless_decompress(int losslessCompressor, unsigned char* comp
unsigned long outSize = 0;
switch(losslessCompressor)
{
case GZIP_COMPRESSOR:
outSize = zlib_uncompress5(compressBytes, cmpSize, oriData, targetOriSize);
break;
case ZSTD_COMPRESSOR:
*oriData = (unsigned char*)malloc(targetOriSize);
outSize = ZSTD_decompress(*oriData, targetOriSize, compressBytes, cmpSize);
......
AUTOMAKE_OPTIONS=foreign
include_HEADERS=inffixed.h inflate.h inftrees.h trees.h zconf.h crc32.h deflate.h gzguts.h inffast.h zlib.h zutil.h
lib_LTLIBRARIES=libzlib.la
libzlib_la_CFLAGS=-I./
libzlib_la_SOURCES=adler32.c crc32.c deflate.c gzclose.c gzlib.c gzwrite.c inffast.c zutil.c compress.c \
gzread.c infback.c inflate.c inftrees.c trees.c uncompr.c
此差异已折叠。
/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2011, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#include "zutil.h"
local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
#define BASE 65521U /* largest prime smaller than 65536 */
#define NMAX 5552
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
#define DO16(buf) DO8(buf,0); DO8(buf,8);
/* use NO_DIVIDE if your processor does not do division in hardware --
try it both ways to see which is faster */
#ifdef NO_DIVIDE
/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
(thank you to John Reiser for pointing this out) */
# define CHOP(a) \
do { \
unsigned long tmp = a >> 16; \
a &= 0xffffUL; \
a += (tmp << 4) - tmp; \
} while (0)
# define MOD28(a) \
do { \
CHOP(a); \
if (a >= BASE) a -= BASE; \
} while (0)
# define MOD(a) \
do { \
CHOP(a); \
MOD28(a); \
} while (0)
# define MOD63(a) \
do { /* this assumes a is not negative */ \
z_off64_t tmp = a >> 32; \
a &= 0xffffffffL; \
a += (tmp << 8) - (tmp << 5) + tmp; \
tmp = a >> 16; \
a &= 0xffffL; \
a += (tmp << 4) - tmp; \
tmp = a >> 16; \
a &= 0xffffL; \
a += (tmp << 4) - tmp; \
if (a >= BASE) a -= BASE; \
} while (0)
#else
# define MOD(a) a %= BASE
# define MOD28(a) a %= BASE
# define MOD63(a) a %= BASE
#endif
/* ========================================================================= */
uLong ZEXPORT adler32_z(adler, buf, len)
uLong adler;
const Bytef *buf;
z_size_t len;
{
unsigned long sum2;
unsigned n;
/* split Adler-32 into component sums */
sum2 = (adler >> 16) & 0xffff;
adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
adler += buf[0];
if (adler >= BASE)
adler -= BASE;
sum2 += adler;
if (sum2 >= BASE)
sum2 -= BASE;
return adler | (sum2 << 16);
}
/* initial Adler-32 value (deferred check for len == 1 speed) */
if (buf == Z_NULL)
return 1L;
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
adler += *buf++;
sum2 += adler;
}
if (adler >= BASE)
adler -= BASE;
MOD28(sum2); /* only added so many BASE's */
return adler | (sum2 << 16);
}
/* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) {
len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */
do {
DO16(buf); /* 16 sums unrolled */
buf += 16;
} while (--n);
MOD(adler);
MOD(sum2);
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
len -= 16;
DO16(buf);
buf += 16;
}
while (len--) {
adler += *buf++;
sum2 += adler;
}
MOD(adler);
MOD(sum2);
}
/* return recombined sums */
return adler | (sum2 << 16);
}
/* ========================================================================= */
uLong ZEXPORT adler32(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
{
return adler32_z(adler, buf, len);
}
/* ========================================================================= */
local uLong adler32_combine_(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off64_t len2;
{
unsigned long sum1;
unsigned long sum2;
unsigned rem;
/* for negative len, return invalid adler32 as a clue for debugging */
if (len2 < 0)
return 0xffffffffUL;
/* the derivation of this formula is left as an exercise for the reader */
MOD63(len2); /* assumes len2 >= 0 */
rem = (unsigned)len2;
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
/* ========================================================================= */
uLong ZEXPORT adler32_combine(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off_t len2;
{
return adler32_combine_(adler1, adler2, len2);
}
uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off64_t len2;
{
return adler32_combine_(adler1, adler2, len2);
}
/* compress.c -- compress a memory buffer
* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */
#define ZLIB_INTERNAL
#include "zlib.h"
/* ===========================================================================
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
length of the source buffer. Upon entry, destLen is the total size of the
destination buffer, which must be at least 0.1% larger than sourceLen plus
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid.
*/
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
int level;
{
z_stream stream;
int err;
const uInt max = (uInt)-1;
uLong left;
left = *destLen;
*destLen = 0;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
err = deflateInit(&stream, level);
if (err != Z_OK) return err;
stream.next_out = dest;
stream.avail_out = 0;
stream.next_in = (z_const Bytef *)source;
stream.avail_in = 0;
do {
if (stream.avail_out == 0) {
stream.avail_out = left > (uLong)max ? max : (uInt)left;
left -= stream.avail_out;
}
if (stream.avail_in == 0) {
stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
sourceLen -= stream.avail_in;
}
err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
} while (err == Z_OK);
*destLen = stream.total_out;
deflateEnd(&stream);
return err == Z_STREAM_END ? Z_OK : err;
}
/* ===========================================================================
*/
int ZEXPORT compress (dest, destLen, source, sourceLen)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
{
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}
/* ===========================================================================
If the default memLevel or windowBits for deflateInit() is changed, then
this function needs to be updated.
*/
uLong ZEXPORT compressBound (sourceLen)
uLong sourceLen;
{
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
(sourceLen >> 25) + 13;
}
/* crc32.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
* CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
* tables for updating the shift register in one step with three exclusive-ors
* instead of four steps with four exclusive-ors. This results in about a
* factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
*/
/* @(#) $Id$ */
/*
Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
protection on the static variables used to control the first-use generation
of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
first call get_crc_table() to initialize the tables before allowing more than
one thread to use crc32().
DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
*/
#ifdef MAKECRCH
# include <stdio.h>
# ifndef DYNAMIC_CRC_TABLE
# define DYNAMIC_CRC_TABLE
# endif /* !DYNAMIC_CRC_TABLE */
#endif /* MAKECRCH */
#include "zutil.h" /* for STDC and FAR definitions */
/* Definitions for doing the crc four data bytes at a time. */
#if !defined(NOBYFOUR) && defined(Z_U4)
# define BYFOUR
#endif
#ifdef BYFOUR
local unsigned long crc32_little OF((unsigned long,
const unsigned char FAR *, z_size_t));
local unsigned long crc32_big OF((unsigned long,
const unsigned char FAR *, z_size_t));
# define TBLS 8
#else
# define TBLS 1
#endif /* BYFOUR */
/* Local functions for crc concatenation */
local unsigned long gf2_matrix_times OF((unsigned long *mat,
unsigned long vec));
local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
#ifdef DYNAMIC_CRC_TABLE
local volatile int crc_table_empty = 1;
local z_crc_t FAR crc_table[TBLS][256];
local void make_crc_table OF((void));
#ifdef MAKECRCH
local void write_table OF((FILE *, const z_crc_t FAR *));
#endif /* MAKECRCH */
/*
Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
Polynomials over GF(2) are represented in binary, one bit per coefficient,
with the lowest powers in the most significant bit. Then adding polynomials
is just exclusive-or, and multiplying a polynomial by x is a right shift by
one. If we call the above polynomial p, and represent a byte as the
polynomial q, also with the lowest power in the most significant bit (so the
byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
where a mod b means the remainder after dividing a by b.
This calculation is done using the shift-register method of multiplying and
taking the remainder. The register is initialized to zero, and for each
incoming bit, x^32 is added mod p to the register if the bit is a one (where
x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
x (which is shifting right by one and adding x^32 mod p if the bit shifted
out is a one). We start with the highest power (least significant bit) of
q and repeat for all eight bits of q.
The first table is simply the CRC of all possible eight bit values. This is
all the information needed to generate CRCs on data a byte at a time for all
combinations of CRC register values and incoming bytes. The remaining tables
allow for word-at-a-time CRC calculation for both big-endian and little-
endian machines, where a word is four bytes.
*/
local void make_crc_table()
{
z_crc_t c;
int n, k;
z_crc_t poly; /* polynomial exclusive-or pattern */
/* terms of polynomial defining this crc (except x^32): */
static volatile int first = 1; /* flag to limit concurrent making */
static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
/* See if another task is already doing this (not thread-safe, but better
than nothing -- significantly reduces duration of vulnerability in
case the advice about DYNAMIC_CRC_TABLE is ignored) */
if (first) {
first = 0;
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
poly = 0;
for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
poly |= (z_crc_t)1 << (31 - p[n]);
/* generate a crc for every 8-bit value */
for (n = 0; n < 256; n++) {
c = (z_crc_t)n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[0][n] = c;
}
#ifdef BYFOUR
/* generate crc for each value followed by one, two, and three zeros,
and then the byte reversal of those as well as the first table */
for (n = 0; n < 256; n++) {
c = crc_table[0][n];
crc_table[4][n] = ZSWAP32(c);
for (k = 1; k < 4; k++) {
c = crc_table[0][c & 0xff] ^ (c >> 8);
crc_table[k][n] = c;
crc_table[k + 4][n] = ZSWAP32(c);
}
}
#endif /* BYFOUR */
crc_table_empty = 0;
}
else { /* not first */
/* wait for the other guy to finish (not efficient, but rare) */
while (crc_table_empty)
;
}
#ifdef MAKECRCH
/* write out CRC tables to crc32.h */
{
FILE *out;
out = fopen("crc32.h", "w");
if (out == NULL) return;
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
fprintf(out, "local const z_crc_t FAR ");
fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
write_table(out, crc_table[0]);
# ifdef BYFOUR
fprintf(out, "#ifdef BYFOUR\n");
for (k = 1; k < 8; k++) {
fprintf(out, " },\n {\n");
write_table(out, crc_table[k]);
}
fprintf(out, "#endif\n");
# endif /* BYFOUR */
fprintf(out, " }\n};\n");
fclose(out);
}
#endif /* MAKECRCH */
}
#ifdef MAKECRCH
local void write_table(out, table)
FILE *out;
const z_crc_t FAR *table;
{
int n;
for (n = 0; n < 256; n++)
fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
(unsigned long)(table[n]),
n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
}
#endif /* MAKECRCH */
#else /* !DYNAMIC_CRC_TABLE */
/* ========================================================================
* Tables of CRC-32s of all single-byte values, made by make_crc_table().
*/
#include "crc32.h"
#endif /* DYNAMIC_CRC_TABLE */
/* =========================================================================
* This function can be used by asm versions of crc32()
*/
const z_crc_t FAR * ZEXPORT get_crc_table()
{
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */
return (const z_crc_t FAR *)crc_table;
}
/* ========================================================================= */
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
/* ========================================================================= */
unsigned long ZEXPORT crc32_z(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
z_size_t len;
{
if (buf == Z_NULL) return 0UL;
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */
#ifdef BYFOUR
if (sizeof(void *) == sizeof(ptrdiff_t)) {
z_crc_t endian;
endian = 1;
if (*((unsigned char *)(&endian)))
return crc32_little(crc, buf, len);
else
return crc32_big(crc, buf, len);
}
#endif /* BYFOUR */
crc = crc ^ 0xffffffffUL;
while (len >= 8) {
DO8;
len -= 8;
}
if (len) do {
DO1;
} while (--len);
return crc ^ 0xffffffffUL;
}
/* ========================================================================= */
unsigned long ZEXPORT crc32(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
uInt len;
{
return crc32_z(crc, buf, len);
}
#ifdef BYFOUR
/*
This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
integer pointer type. This violates the strict aliasing rule, where a
compiler can assume, for optimization purposes, that two pointers to
fundamentally different types won't ever point to the same memory. This can
manifest as a problem only if one of the pointers is written to. This code
only reads from those pointers. So long as this code remains isolated in
this compilation unit, there won't be a problem. For this reason, this code
should not be copied and pasted into a compilation unit in which other code
writes to the buffer that is passed to these routines.
*/
/* ========================================================================= */
#define DOLIT4 c ^= *buf4++; \
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
/* ========================================================================= */
local unsigned long crc32_little(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
z_size_t len;
{
register z_crc_t c;
register const z_crc_t FAR *buf4;
c = (z_crc_t)crc;
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
len--;
}
buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
while (len >= 32) {
DOLIT32;
len -= 32;
}
while (len >= 4) {
DOLIT4;
len -= 4;
}
buf = (const unsigned char FAR *)buf4;
if (len) do {
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
} while (--len);
c = ~c;
return (unsigned long)c;
}
/* ========================================================================= */
#define DOBIG4 c ^= *buf4++; \
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
/* ========================================================================= */
local unsigned long crc32_big(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
z_size_t len;
{
register z_crc_t c;
register const z_crc_t FAR *buf4;
c = ZSWAP32((z_crc_t)crc);
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
len--;
}
buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
while (len >= 32) {
DOBIG32;
len -= 32;
}
while (len >= 4) {
DOBIG4;
len -= 4;
}
buf = (const unsigned char FAR *)buf4;
if (len) do {
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
} while (--len);
c = ~c;
return (unsigned long)(ZSWAP32(c));
}
#endif /* BYFOUR */
#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
/* ========================================================================= */
local unsigned long gf2_matrix_times(mat, vec)
unsigned long *mat;
unsigned long vec;
{
unsigned long sum;
sum = 0;
while (vec) {
if (vec & 1)
sum ^= *mat;
vec >>= 1;
mat++;
}
return sum;
}
/* ========================================================================= */
local void gf2_matrix_square(square, mat)
unsigned long *square;
unsigned long *mat;
{
int n;
for (n = 0; n < GF2_DIM; n++)
square[n] = gf2_matrix_times(mat, mat[n]);
}
/* ========================================================================= */
local uLong crc32_combine_(crc1, crc2, len2)
uLong crc1;
uLong crc2;
z_off64_t len2;
{
int n;
unsigned long row;
unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
/* degenerate case (also disallow negative lengths) */
if (len2 <= 0)
return crc1;
/* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
odd[n] = row;
row <<= 1;
}
/* put operator for two zero bits in even */
gf2_matrix_square(even, odd);
/* put operator for four zero bits in odd */
gf2_matrix_square(odd, even);
/* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */
do {
/* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd);
if (len2 & 1)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
/* if no more bits set, then done */
if (len2 == 0)
break;
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even);
if (len2 & 1)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
/* if no more bits set, then done */
} while (len2 != 0);
/* return combined crc */
crc1 ^= crc2;
return crc1;
}
/* ========================================================================= */
uLong ZEXPORT crc32_combine(crc1, crc2, len2)
uLong crc1;
uLong crc2;
z_off_t len2;
{
return crc32_combine_(crc1, crc2, len2);
}
uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
uLong crc1;
uLong crc2;
z_off64_t len2;
{
return crc32_combine_(crc1, crc2, len2);
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册