-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e875505
commit 2a768bd
Showing
122 changed files
with
32,611 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
#include "DataProcessing.h" | ||
|
||
|
||
#ifdef HAVE_LIBZ | ||
|
||
#include <zlib.h> | ||
|
||
typedef struct | ||
{ | ||
z_stream z_in; | ||
z_stream z_out; | ||
} zlibData; | ||
#endif | ||
|
||
|
||
|
||
//Zlib is a little weird. It accepts a pointer to a buffer (next_in) and a buffer length (avail_in) to specify the input | ||
//and another buffer (next_out) and length (avail_out) to write data into. When called it reads bytes from next_in, updates | ||
//next_in to point to the end of what it read, and subtracts the number of bytes it read from avail_in so that avail_in | ||
//now says how many UNUSED bytes there are pointed to by next_in. Similarly it writes to next_out, updating that pointer | ||
//to point to the end of the write, and updating avail_out to say how much room is LEFT usused in the output buffer | ||
// | ||
//However, if zlib doesn't use all avail_in, then you can't mess with that buffer until it has. Hence you can't take the unusued | ||
//data from next_in/avail_in and copy it to a new buffer and pass that buffer into deflate/inflate on the next call. If zlib | ||
//doesn't use all the input the only way to handle it is to grow the output buffer and call inflate/deflate again, so that it | ||
//can write into the expanded buffer until it's used up all input. | ||
// | ||
//Finally, when you've supplied all the input you've got, you have to call deflate with 'Z_FINISH' so that it knows there's no | ||
//more data coming. | ||
|
||
int zlibProcessorWrite(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush) | ||
{ | ||
int wrote=0; | ||
#ifdef HAVE_LIBZ | ||
int val=0; | ||
zlibData *ZData; | ||
|
||
if (ProcMod->Flags & DPM_WRITE_FINAL) return(STREAM_CLOSED); | ||
ZData=(zlibData *) ProcMod->Data; | ||
|
||
|
||
ZData->z_out.avail_in=InLen; | ||
ZData->z_out.next_in=(char *) InData; | ||
ZData->z_out.avail_out=*OutLen; | ||
ZData->z_out.next_out=*OutData; | ||
|
||
while ((ZData->z_out.avail_in > 0) || Flush) | ||
{ | ||
if (Flush) val=deflate(& ZData->z_out, Z_FINISH); | ||
else val=deflate(& ZData->z_out, Z_NO_FLUSH); | ||
|
||
wrote=*OutLen-ZData->z_out.avail_out; | ||
if (val==Z_STREAM_END) | ||
{ | ||
ProcMod->Flags |= DPM_WRITE_FINAL; | ||
break; | ||
} | ||
|
||
if ((ZData->z_out.avail_in > 0) || Flush) | ||
{ | ||
*OutLen+=BUFSIZ; | ||
*OutData=(char *) realloc(*OutData,*OutLen); | ||
ZData->z_out.avail_out+=BUFSIZ; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
#endif | ||
return(wrote); | ||
} | ||
|
||
|
||
int zlibProcessorRead(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush) | ||
{ | ||
int wrote=0; | ||
#ifdef HAVE_LIBZ | ||
int result=0; | ||
zlibData *ZData; | ||
|
||
if (ProcMod->Flags & DPM_READ_FINAL) return(STREAM_CLOSED); | ||
ZData=(zlibData *) ProcMod->Data; | ||
|
||
|
||
ZData->z_in.avail_in=InLen; | ||
ZData->z_in.next_in=(char *) InData; | ||
ZData->z_in.avail_out=*OutLen; | ||
ZData->z_in.next_out=*OutData; | ||
|
||
while ((ZData->z_in.avail_in > 0) || Flush) | ||
{ | ||
if (Flush) result=inflate(& ZData->z_in, Z_FINISH); | ||
else result=inflate(& ZData->z_in, Z_NO_FLUSH); | ||
|
||
wrote=(*OutLen)-ZData->z_in.avail_out; | ||
|
||
if (result==Z_BUF_ERROR) break; | ||
switch (result) | ||
{ | ||
case Z_DATA_ERROR: inflateSync(&ZData->z_in); break; | ||
case Z_ERRNO: if (Flush) ProcMod->Flags |= DPM_READ_FINAL; break; | ||
case Z_STREAM_ERROR: | ||
case Z_STREAM_END: ProcMod->Flags |= DPM_READ_FINAL; break; | ||
} | ||
|
||
if (ProcMod->Flags & DPM_READ_FINAL) break; | ||
if ((ZData->z_in.avail_in > 0) || Flush) | ||
{ | ||
(*OutLen)+=BUFSIZ; | ||
*OutData=(char *) realloc(*OutData,*OutLen); | ||
ZData->z_in.next_out=(*OutData) + wrote; | ||
ZData->z_in.avail_out=(*OutLen) - wrote; | ||
} | ||
|
||
} | ||
|
||
|
||
#endif | ||
return(wrote); | ||
} | ||
|
||
|
||
/* | ||
int zlibProcessorRead(TProcessingModule *ProcMod, const char *InData, int InLen, char **OutData, int *OutLen, int Flush) | ||
{ | ||
int wrote=0, result; | ||
#ifdef HAVE_LIBZ | ||
zlibData *ZData; | ||
int len; | ||
ZData=(zlibData *) ProcMod->Data; | ||
if (InLen > 0) | ||
{ | ||
DataProcessorUpdateBuffer(&ProcMod->ReadBuff, &ProcMod->ReadUsed, &ProcMod->ReadSize, InData, InLen); | ||
ZData->z_in.next_in=ProcMod->ReadBuff; | ||
ZData->z_in.avail_in=ProcMod->ReadUsed; | ||
ZData->z_in.avail_out=*OutLen; | ||
ZData->z_in.next_out=*OutData; | ||
if (InLen==0) result=inflate(& ZData->z_in, Z_FINISH); | ||
else result=inflate(& ZData->z_in, Z_NO_FLUSH); | ||
if (ZData->z_in.avail_in > 0) memmove(ProcMod->ReadBuff,ZData->z_in.next_in,ZData->z_in.avail_in); | ||
ProcMod->ReadUsed=ZData->z_in.avail_in; | ||
wrote=OutLen-ZData->z_in.avail_out; | ||
} | ||
#endif | ||
return(wrote); | ||
} | ||
*/ | ||
|
||
|
||
int zlibProcessorClose(TProcessingModule *ProcMod) | ||
{ | ||
#ifdef HAVE_LIBZ | ||
zlibData *ZData; | ||
|
||
ZData=(zlibData *) ProcMod->Data; | ||
if (ZData) | ||
{ | ||
inflateEnd(&ZData->z_in); | ||
deflateEnd(&ZData->z_out); | ||
|
||
free(ZData); | ||
ProcMod->Data=NULL; | ||
} | ||
#endif | ||
return(TRUE); | ||
} | ||
|
||
|
||
#define COMP_ZLIB 0 | ||
#define COMP_GZIP 1 | ||
|
||
int zlibProcessorInit(TProcessingModule *ProcMod, const char *Args) | ||
{ | ||
int result=FALSE; | ||
|
||
#ifdef HAVE_LIBZ | ||
zlibData *ZData; | ||
int CompressionLevel=5; | ||
char *ptr, *Name=NULL, *Value=NULL; | ||
int Type=COMP_ZLIB; | ||
|
||
ptr=GetNameValuePair(Args,"\\S","=",&Name,&Value); | ||
while (ptr) | ||
{ | ||
if (strcasecmp(Name,"Alg")==0) | ||
{ | ||
if (strcasecmp(Value, "gzip")==0) Type=COMP_GZIP; | ||
} | ||
else if (strcasecmp(Name,"CompressionLevel")==0) CompressionLevel=atoi(Value); | ||
else if (strcasecmp(Name,"Level")==0) CompressionLevel=atoi(Value); | ||
ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value); | ||
} | ||
|
||
|
||
ProcMod->ReadMax=4096; | ||
ProcMod->WriteMax=4096; | ||
|
||
|
||
ZData=(zlibData *) calloc(1,sizeof(zlibData)); | ||
ZData->z_in.avail_in=0; | ||
ZData->z_in.avail_out=0; | ||
if (Type==COMP_GZIP) result=inflateInit2(&ZData->z_in,47); | ||
else result=inflateInit(&ZData->z_in); | ||
|
||
ZData->z_out.avail_in=0; | ||
ZData->z_out.avail_out=0; | ||
if (Type==COMP_GZIP) deflateInit2(&ZData->z_out,5,Z_DEFLATED,30,8,Z_DEFAULT_STRATEGY); | ||
else deflateInit(&ZData->z_out,CompressionLevel); | ||
|
||
ProcMod->Data=(void *) ZData; | ||
result=TRUE; | ||
|
||
ProcMod->Read=zlibProcessorRead; | ||
ProcMod->Write=zlibProcessorWrite; | ||
ProcMod->Close=zlibProcessorClose; | ||
|
||
DestroyString(Name); | ||
DestroyString(Value); | ||
|
||
#endif | ||
return(result); | ||
} | ||
|
||
|
||
|
||
|
||
int CompressBytes(char **Out, char *Alg, char *In, int Len, int Level) | ||
{ | ||
TProcessingModule *Mod=NULL; | ||
char *Tempstr=NULL; | ||
int result, val; | ||
|
||
Tempstr=FormatStr(Tempstr,"CompressionLevel=%d",Level); | ||
Mod=StandardDataProcessorCreate("compress",Alg,Tempstr); | ||
if (! Mod) return(-1); | ||
|
||
val=Len *2; | ||
*Out=SetStrLen(*Out,val); | ||
result=Mod->Write(Mod,In,Len,Out,&Len,TRUE); | ||
|
||
DestroyString(Tempstr); | ||
DataProcessorDestroy(Mod); | ||
|
||
return(result); | ||
} | ||
|
||
|
||
int DeCompressBytes(char **Out, char *Alg, char *In, int Len) | ||
{ | ||
TProcessingModule *Mod=NULL; | ||
int result, val; | ||
|
||
Mod=StandardDataProcessorCreate("decompress",Alg,""); | ||
if (! Mod) return(-1); | ||
|
||
val=Len *2; | ||
*Out=SetStrLen(*Out,val); | ||
result=Mod->Read(Mod,In,Len,Out,&Len,TRUE); | ||
|
||
DataProcessorDestroy(Mod); | ||
|
||
return(result); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef LIBUSEFUL_COMPRESSION_H | ||
#define LIBUSEFUL_COMPRESSION_H | ||
|
||
#include "includes.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
int zlibProcessorInit(TProcessingModule *ProcMod, const char *Args); | ||
int CompressBytes(char **Out, char *Alg, char *In, int Len, int Level); | ||
int DeCompressBytes(char **Out, char *Alg, char *In, int Len); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
|
||
|
||
#endif |
Oops, something went wrong.