diff --git a/src/Makefile b/src/Makefile index e1053b0..4e8e424 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,8 +14,9 @@ MKDIR := mkdir MEX=mex AR=$(CC) +ECHO := echo -BINARY=zmat +BINARY:=zmat OUTPUT_DIR=$(ZMATDIR) DOXY := doxygen @@ -32,13 +33,13 @@ PLATFORM = $(shell uname -s) DLLFLAG= OMP=-fopenmp -CPPOPT=#-g -Wall -std=c99 # -DUSE_OS_TIMER +CPPOPT=-g -Wall -O3 #-g -Wall -std=c99 # -DUSE_OS_TIMER OUTPUTFLAG:=-o OBJSUFFIX=.o EXESUFFIX=.mex* -FILES= +FILES=zmatlib ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) CC=nvcc @@ -86,18 +87,29 @@ mex: CXX=$(MEX) mex: OUTPUTFLAG:=-output mex: AR=$(MEX) zmat.cpp easylzma/easylzma-0.0.8/lib/libeasylzma_s.a $(INCLUDEDIRS) mex: LINKOPT+= -cxx CXXLIBS='$$CXXLIBS -lz' -outdir $(ZMATDIR) +mex: ARFLAGS := mex: OUTPUT_DIR=.. +lib: BINARY=libzmat.a +lib: AR :=ar +lib: ARFLAGS :=cr +lib: AROUTPUT := + all: mex +TARGETSUFFIX:=$(suffix $(BINARY)) + +ifeq ($(TARGETSUFFIX),.so) + CCFLAGS+= $(DLLFLAG) + ARFLAGS+= -shared -Wl,-soname,$(BINARY).1 +endif + doc: makedocdir $(DOXY) $(DOXYCFG) OBJS := $(addsuffix $(OBJSUFFIX), $(FILES)) -TARGETSUFFIX:=$(suffix $(BINARY)) - -all mex oct: $(OUTPUT_DIR)/$(BINARY) +all lib mex oct: $(OUTPUT_DIR)/$(BINARY) makedirs: @if test ! -d $(OUTPUT_DIR); then $(MKDIR) $(OUTPUT_DIR); fi @@ -107,19 +119,24 @@ makedocdir: $(OUTPUT_DIR)/$(BINARY): makedirs $(OBJS) $(OUTPUT_DIR)/$(BINARY): $(OBJS) - $(AR) $(OBJS) $(OUTPUTFLAG) $(OUTPUT_DIR)/$(BINARY) $(LINKOPT) $(USERLINKOPT) + @$(ECHO) Building $@ + $(AR) $(ARFLAGS) $(OUTPUTFLAG) $@ $(OBJS) $(LINKOPT) $(USERLINKOPT) %$(OBJSUFFIX): %.cpp $(CXX) $(INCLUDEDIRS) $(CPPOPT) -c -o $@ $< %$(OBJSUFFIX): %.c + @$(ECHO) Building $@ $(CC) $(INCLUDEDIRS) $(CPPOPT) -c -o $@ $< %$(OBJSUFFIX): %.cu + @$(ECHO) Building $@ $(CUDACC) -c $(CUCCOPT) -o $@ $< clean: -rm -f $(OBJS) $(OUTPUT_DIR)/$(BINARY)$(EXESUFFIX) +.PHONY: all mex oct + .DEFAULT_GOAL := all diff --git a/src/zmat.cpp b/src/zmat.cpp index a47dc6b..fe49375 100644 --- a/src/zmat.cpp +++ b/src/zmat.cpp @@ -25,42 +25,15 @@ #include #include "mex.h" +#include "zmatlib.h" #include "zlib.h" -#ifndef NO_LZMA - #include "easylzma/compress.h" - #include "easylzma/decompress.h" -#endif - void zmat_usage(); -int zmat_keylookup(char *origkey, const char *table[]); -unsigned char * base64_encode(const unsigned char *src, size_t len, - size_t *out_len); -unsigned char * base64_decode(const unsigned char *src, size_t len, - size_t *out_len); - - -#ifndef NO_LZMA -/* compress a chunk of memory and return a dynamically allocated buffer - * if successful. return value is an easylzma error code */ -int simpleCompress(elzma_file_format format, - const unsigned char * inData, - size_t inLen, - unsigned char ** outData, - size_t * outLen); - -/* decompress a chunk of memory and return a dynamically allocated buffer - * if successful. return value is an easylzma error code */ -int simpleDecompress(elzma_file_format format, - const unsigned char * inData, - size_t inLen, - unsigned char ** outData, - size_t * outLen); -#endif -enum TZipMethod {zmZlib, zmGzip, zmBase64, zmLzip, zmLzma}; const char *metadata[]={"type","size","status"}; +extern char *zmat_err[]; + /** @brief Mex function for the zmat - an interface to compress/decompress binary data * This is the master function to interface for zipping and unzipping a char/int8 buffer */ @@ -96,100 +69,23 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ try{ if(mxIsChar(prhs[0]) || mxIsUint8(prhs[0]) || mxIsInt8(prhs[0])){ - z_stream zs; int ret; mwSize inputsize=mxGetNumberOfElements(prhs[0]); mwSize buflen[2]={0}; - unsigned char *temp=NULL; + unsigned char *outputbuf=NULL; size_t outputsize=0; - char * inputstr=(mxIsChar(prhs[0])? mxArrayToString(prhs[0]) : (char *)mxGetData(prhs[0])); - - zs.zalloc = Z_NULL; - zs.zfree = Z_NULL; - zs.opaque = Z_NULL; + unsigned char * inputstr=(mxIsChar(prhs[0])? (unsigned char *)mxArrayToString(prhs[0]) : (unsigned char *)mxGetData(prhs[0])); - if(inputsize==0) - mexErrMsgTxt("input can not be empty"); + int errcode=zmat_run(inputsize, inputstr, &outputsize, &outputbuf, zipid, &ret, iscompress); + if(errcode<0) + mexErrMsgTxt(zmat_err[-errcode]); - if(iscompress){ - if(zipid==zmBase64){ - temp=base64_encode((const unsigned char*)inputstr, inputsize, &outputsize); - }else if(zipid==zmZlib || zipid==zmGzip){ - if(zipid==zmZlib){ - if(deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) - mexErrMsgTxt("failed to initialize zlib"); - }else{ - if(deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15|16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) - mexErrMsgTxt("failed to initialize zlib"); - } - buflen[0] =deflateBound(&zs,inputsize); - temp=(unsigned char *)malloc(buflen[0]); - zs.avail_in = inputsize; // size of input, string + terminator - zs.next_in = (Bytef *)inputstr; // input char array - zs.avail_out = buflen[0]; // size of output - - zs.next_out = (Bytef *)(temp); //(Bytef *)(); // output char array - - ret=deflate(&zs, Z_FINISH); - outputsize=zs.total_out; - if(ret!=Z_STREAM_END && ret!=Z_OK) - mexErrMsgTxt("zlib error, see info.status for error flag"); - deflateEnd(&zs); -#ifndef NO_LZMA - }else{ - ret = simpleCompress((elzma_file_format)(zipid-3), (unsigned char *)inputstr, - inputsize, &temp, &outputsize); - if(ret!=ELZMA_E_OK) - mexErrMsgTxt("easylzma error, see info.status for error flag"); -#endif - } - }else{ - if(zipid==zmBase64){ - temp=base64_decode((const unsigned char*)inputstr, inputsize, &outputsize); - }else if(zipid==zmZlib || zipid==zmGzip){ - int count=1; - if(zipid==zmZlib){ - if(inflateInit(&zs) != Z_OK) - mexErrMsgTxt("failed to initialize zlib"); - }else{ - if(inflateInit2(&zs, 15|32) != Z_OK) - mexErrMsgTxt("failed to initialize zlib"); - } - buflen[0] =inputsize*20; - temp=(unsigned char *)malloc(buflen[0]); - - zs.avail_in = inputsize; // size of input, string + terminator - zs.next_in =(Bytef *)(mxGetData(prhs[0])); // input char array - zs.avail_out = buflen[0]; // size of output - - zs.next_out = (Bytef *)(temp); //(Bytef *)(); // output char array - - while((ret=inflate(&zs, Z_SYNC_FLUSH))!=Z_STREAM_END && count<=10){ - temp=(unsigned char *)realloc(temp, (buflen[0]<1){ mwSize inputdim[2]={1,0}, *dims=(mwSize *)mxGetDimensions(prhs[0]); @@ -223,7 +119,6 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ return; } - /** * @brief Print a brief help information if nothing is provided */ @@ -231,312 +126,3 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ void zmat_usage(){ printf("Usage:\n [output,info]=zmat(input,iscompress,method);\n\nPlease run 'help zmat' for more details.\n"); } - -/** - * @brief Look up a string in a string list and return the index - * - * @param[in] origkey: string to be looked up - * @param[out] table: the dictionary where the string is searched - * @return if found, return the index of the string in the dictionary, otherwise -1. - */ - -int zmat_keylookup(char *origkey, const char *table[]){ - int i=0; - char *key=(char *)malloc(strlen(origkey)+1); - memcpy(key,origkey,strlen(origkey)+1); - while(key[i]){ - key[i]=tolower(key[i]); - i++; - } - i=0; - while(table[i] && table[i][0]!='\0'){ - if(strcmp(key,table[i])==0){ - free(key); - return i; - } - i++; - } - free(key); - return -1; -} - - -/* - * Base64 encoding/decoding (RFC1341) - * Copyright (c) 2005-2011, Jouni Malinen - * - * This software may be distributed under the terms of the BSD license. - * See README for more details. - */ - -static const unsigned char base64_table[65] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -/** - * base64_encode - Base64 encode - * @src: Data to be encoded - * @len: Length of the data to be encoded - * @out_len: Pointer to output length variable, or %NULL if not used - * Returns: Allocated buffer of out_len bytes of encoded data, - * or %NULL on failure - * - * Caller is responsible for freeing the returned buffer. Returned buffer is - * nul terminated to make it easier to use as a C string. The nul terminator is - * not included in out_len. - */ -unsigned char * base64_encode(const unsigned char *src, size_t len, - size_t *out_len) -{ - unsigned char *out, *pos; - const unsigned char *end, *in; - size_t olen; - int line_len; - - olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ - olen += olen / 72; /* line feeds */ - olen++; /* nul termination */ - if (olen < len) - return NULL; /* integer overflow */ - out = (unsigned char *)malloc(olen); - if (out == NULL) - return NULL; - - end = src + len; - in = src; - pos = out; - line_len = 0; - while (end - in >= 3) { - *pos++ = base64_table[in[0] >> 2]; - *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; - *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; - *pos++ = base64_table[in[2] & 0x3f]; - in += 3; - line_len += 4; - if (line_len >= 72) { - *pos++ = '\n'; - line_len = 0; - } - } - - if (end - in) { - *pos++ = base64_table[in[0] >> 2]; - if (end - in == 1) { - *pos++ = base64_table[(in[0] & 0x03) << 4]; - *pos++ = '='; - } else { - *pos++ = base64_table[((in[0] & 0x03) << 4) | - (in[1] >> 4)]; - *pos++ = base64_table[(in[1] & 0x0f) << 2]; - } - *pos++ = '='; - line_len += 4; - } - - if (line_len) - *pos++ = '\n'; - - *pos = '\0'; - if (out_len) - *out_len = pos - out; - return out; -} - - -/** - * base64_decode - Base64 decode - * @src: Data to be decoded - * @len: Length of the data to be decoded - * @out_len: Pointer to output length variable - * Returns: Allocated buffer of out_len bytes of decoded data, - * or %NULL on failure - * - * Caller is responsible for freeing the returned buffer. - */ -unsigned char * base64_decode(const unsigned char *src, size_t len, - size_t *out_len) -{ - unsigned char dtable[256], *out, *pos, block[4], tmp; - size_t i, count, olen; - int pad = 0; - - memset(dtable, 0x80, 256); - for (i = 0; i < sizeof(base64_table) - 1; i++) - dtable[base64_table[i]] = (unsigned char) i; - dtable['='] = 0; - - count = 0; - for (i = 0; i < len; i++) { - if (dtable[src[i]] != 0x80) - count++; - } - - if (count == 0 || count % 4) - return NULL; - - olen = count / 4 * 3; - pos = out = (unsigned char *)malloc(olen); - if (out == NULL) - return NULL; - - count = 0; - for (i = 0; i < len; i++) { - tmp = dtable[src[i]]; - if (tmp == 0x80) - continue; - - if (src[i] == '=') - pad++; - block[count] = tmp; - count++; - if (count == 4) { - *pos++ = (block[0] << 2) | (block[1] >> 4); - *pos++ = (block[1] << 4) | (block[2] >> 2); - *pos++ = (block[2] << 6) | block[3]; - count = 0; - if (pad) { - if (pad == 1) - pos--; - else if (pad == 2) - pos -= 2; - else { - /* Invalid padding */ - free(out); - return NULL; - } - break; - } - } - } - - *out_len = pos - out; - return out; -} - -#ifndef NO_LZMA - -struct dataStream -{ - const unsigned char * inData; - size_t inLen; - - unsigned char * outData; - size_t outLen; -}; - -static int -inputCallback(void *ctx, void *buf, size_t * size) -{ - size_t rd = 0; - struct dataStream * ds = (struct dataStream *) ctx; - assert(ds != NULL); - - rd = (ds->inLen < *size) ? ds->inLen : *size; - - if (rd > 0) { - memcpy(buf, (void *) ds->inData, rd); - ds->inData += rd; - ds->inLen -= rd; - } - - *size = rd; - - return 0; -} - -static size_t -outputCallback(void *ctx, const void *buf, size_t size) -{ - struct dataStream * ds = (struct dataStream *) ctx; - assert(ds != NULL); - - if (size > 0) { - ds->outData = (unsigned char *)realloc(ds->outData, ds->outLen + size); - memcpy((void *) (ds->outData + ds->outLen), buf, size); - ds->outLen += size; - } - - return size; -} - -int -simpleCompress(elzma_file_format format, const unsigned char * inData, - size_t inLen, unsigned char ** outData, - size_t * outLen) -{ - int rc; - elzma_compress_handle hand; - - /* allocate compression handle */ - hand = elzma_compress_alloc(); - assert(hand != NULL); - - rc = elzma_compress_config(hand, ELZMA_LC_DEFAULT, - ELZMA_LP_DEFAULT, ELZMA_PB_DEFAULT, - 5, (1 << 20) /* 1mb */, - format, inLen); - - if (rc != ELZMA_E_OK) { - elzma_compress_free(&hand); - return rc; - } - - /* now run the compression */ - { - struct dataStream ds; - ds.inData = inData; - ds.inLen = inLen; - ds.outData = NULL; - ds.outLen = 0; - - rc = elzma_compress_run(hand, inputCallback, (void *) &ds, - outputCallback, (void *) &ds, - NULL, NULL); - - if (rc != ELZMA_E_OK) { - if (ds.outData != NULL) free(ds.outData); - elzma_compress_free(&hand); - return rc; - } - - *outData = ds.outData; - *outLen = ds.outLen; - } - - return rc; -} - -int -simpleDecompress(elzma_file_format format, const unsigned char * inData, - size_t inLen, unsigned char ** outData, - size_t * outLen) -{ - int rc; - elzma_decompress_handle hand; - - hand = elzma_decompress_alloc(); - - /* now run the compression */ - { - struct dataStream ds; - ds.inData = inData; - ds.inLen = inLen; - ds.outData = NULL; - ds.outLen = 0; - - rc = elzma_decompress_run(hand, inputCallback, (void *) &ds, - outputCallback, (void *) &ds, format); - - if (rc != ELZMA_E_OK) { - if (ds.outData != NULL) free(ds.outData); - elzma_decompress_free(&hand); - return rc; - } - - *outData = ds.outData; - *outLen = ds.outLen; - } - - return rc; -} - -#endif \ No newline at end of file diff --git a/src/zmatlib.c b/src/zmatlib.c new file mode 100644 index 0000000..8598269 --- /dev/null +++ b/src/zmatlib.c @@ -0,0 +1,420 @@ +#include +#include +#include +#include + +#include "zmatlib.h" + +#include "zlib.h" + +char *zmat_err[]={ + "No error", + "input can not be empty", + "failed to initialize zlib", + "zlib error, see info.status for error flag", + "easylzma error, see info.status for error flag" + }; + +int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret, const int iscompress){ + z_stream zs; + size_t buflen[2]={0}; + *outputbuf=NULL; + + zs.zalloc = Z_NULL; + zs.zfree = Z_NULL; + zs.opaque = Z_NULL; + + if(inputsize==0) + return -1; + + if(iscompress){ + if(zipid==zmBase64){ + *outputbuf=base64_encode((const unsigned char*)inputstr, inputsize, outputsize); + }else if(zipid==zmZlib || zipid==zmGzip){ + if(zipid==zmZlib){ + if(deflateInit(&zs, Z_DEFAULT_COMPRESSION) != Z_OK) + return -2; + }else{ + if(deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15|16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) + return -2; + } + buflen[0] =deflateBound(&zs,inputsize); + *outputbuf=(unsigned char *)malloc(buflen[0]); + zs.avail_in = inputsize; /* size of input, string + terminator*/ + zs.next_in = (Bytef *)inputstr; /* input char array*/ + zs.avail_out = buflen[0]; /* size of output*/ + + zs.next_out = (Bytef *)(*outputbuf); /*(Bytef *)(); // output char array*/ + + *ret=deflate(&zs, Z_FINISH); + *outputsize=zs.total_out; + if(*ret!=Z_STREAM_END && *ret!=Z_OK) + return -3; + deflateEnd(&zs); +#ifndef NO_LZMA + }else{ + *ret = simpleCompress((elzma_file_format)(zipid-3), (unsigned char *)inputstr, + inputsize, outputbuf, outputsize); + if(*ret!=ELZMA_E_OK) + return -4; +#endif + } + }else{ + if(zipid==zmBase64){ + *outputbuf=base64_decode((const unsigned char*)inputstr, inputsize, outputsize); + }else if(zipid==zmZlib || zipid==zmGzip){ + int count=1; + if(zipid==zmZlib){ + if(inflateInit(&zs) != Z_OK) + return -2; + }else{ + if(inflateInit2(&zs, 15|32) != Z_OK) + return -2; + } + buflen[0] =inputsize*20; + *outputbuf=(unsigned char *)malloc(buflen[0]); + + zs.avail_in = inputsize; /* size of input, string + terminator*/ + zs.next_in =inputstr; /* input char array*/ + zs.avail_out = buflen[0]; /* size of output*/ + + zs.next_out = (Bytef *)(*outputbuf); /*(Bytef *)(); // output char array*/ + + while((*ret=inflate(&zs, Z_SYNC_FLUSH))!=Z_STREAM_END && count<=10){ + *outputbuf=(unsigned char *)realloc(*outputbuf, (buflen[0]< + * + * This software may be distributed under the terms of the BSD license. + * See README for more details. + */ + +static const unsigned char base64_table[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * base64_encode - Base64 encode + * @src: Data to be encoded + * @len: Length of the data to be encoded + * @out_len: Pointer to output length variable, or %NULL if not used + * Returns: Allocated buffer of out_len bytes of encoded data, + * or %NULL on failure + * + * Caller is responsible for freeing the returned buffer. Returned buffer is + * nul terminated to make it easier to use as a C string. The nul terminator is + * not included in out_len. + */ +unsigned char * base64_encode(const unsigned char *src, size_t len, + size_t *out_len) +{ + unsigned char *out, *pos; + const unsigned char *end, *in; + size_t olen; + int line_len; + + olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ + olen += olen / 72; /* line feeds */ + olen++; /* nul termination */ + if (olen < len) + return NULL; /* integer overflow */ + out = (unsigned char *)malloc(olen); + if (out == NULL) + return NULL; + + end = src + len; + in = src; + pos = out; + line_len = 0; + while (end - in >= 3) { + *pos++ = base64_table[in[0] >> 2]; + *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; + *pos++ = base64_table[in[2] & 0x3f]; + in += 3; + line_len += 4; + if (line_len >= 72) { + *pos++ = '\n'; + line_len = 0; + } + } + + if (end - in) { + *pos++ = base64_table[in[0] >> 2]; + if (end - in == 1) { + *pos++ = base64_table[(in[0] & 0x03) << 4]; + *pos++ = '='; + } else { + *pos++ = base64_table[((in[0] & 0x03) << 4) | + (in[1] >> 4)]; + *pos++ = base64_table[(in[1] & 0x0f) << 2]; + } + *pos++ = '='; + line_len += 4; + } + + if (line_len) + *pos++ = '\n'; + + *pos = '\0'; + if (out_len) + *out_len = pos - out; + return out; +} + + +/** + * base64_decode - Base64 decode + * @src: Data to be decoded + * @len: Length of the data to be decoded + * @out_len: Pointer to output length variable + * Returns: Allocated buffer of out_len bytes of decoded data, + * or %NULL on failure + * + * Caller is responsible for freeing the returned buffer. + */ +unsigned char * base64_decode(const unsigned char *src, size_t len, + size_t *out_len) +{ + unsigned char dtable[256], *out, *pos, block[4], tmp; + size_t i, count, olen; + int pad = 0; + + memset(dtable, 0x80, 256); + for (i = 0; i < sizeof(base64_table) - 1; i++) + dtable[base64_table[i]] = (unsigned char) i; + dtable['='] = 0; + + count = 0; + for (i = 0; i < len; i++) { + if (dtable[src[i]] != 0x80) + count++; + } + + if (count == 0 || count % 4) + return NULL; + + olen = count / 4 * 3; + pos = out = (unsigned char *)malloc(olen); + if (out == NULL) + return NULL; + + count = 0; + for (i = 0; i < len; i++) { + tmp = dtable[src[i]]; + if (tmp == 0x80) + continue; + + if (src[i] == '=') + pad++; + block[count] = tmp; + count++; + if (count == 4) { + *pos++ = (block[0] << 2) | (block[1] >> 4); + *pos++ = (block[1] << 4) | (block[2] >> 2); + *pos++ = (block[2] << 6) | block[3]; + count = 0; + if (pad) { + if (pad == 1) + pos--; + else if (pad == 2) + pos -= 2; + else { + /* Invalid padding */ + free(out); + return NULL; + } + break; + } + } + } + + *out_len = pos - out; + return out; +} + +#ifndef NO_LZMA + +struct dataStream +{ + const unsigned char * inData; + size_t inLen; + + unsigned char * outData; + size_t outLen; +}; + +static int +inputCallback(void *ctx, void *buf, size_t * size) +{ + size_t rd = 0; + struct dataStream * ds = (struct dataStream *) ctx; + assert(ds != NULL); + + rd = (ds->inLen < *size) ? ds->inLen : *size; + + if (rd > 0) { + memcpy(buf, (void *) ds->inData, rd); + ds->inData += rd; + ds->inLen -= rd; + } + + *size = rd; + + return 0; +} + +static size_t +outputCallback(void *ctx, const void *buf, size_t size) +{ + struct dataStream * ds = (struct dataStream *) ctx; + assert(ds != NULL); + + if (size > 0) { + ds->outData = (unsigned char *)realloc(ds->outData, ds->outLen + size); + memcpy((void *) (ds->outData + ds->outLen), buf, size); + ds->outLen += size; + } + + return size; +} + +int +simpleCompress(elzma_file_format format, const unsigned char * inData, + size_t inLen, unsigned char ** outData, + size_t * outLen) +{ + int rc; + elzma_compress_handle hand; + + /* allocate compression handle */ + hand = elzma_compress_alloc(); + assert(hand != NULL); + + rc = elzma_compress_config(hand, ELZMA_LC_DEFAULT, + ELZMA_LP_DEFAULT, ELZMA_PB_DEFAULT, + 5, (1 << 20) /* 1mb */, + format, inLen); + + if (rc != ELZMA_E_OK) { + elzma_compress_free(&hand); + return rc; + } + + /* now run the compression */ + { + struct dataStream ds; + ds.inData = inData; + ds.inLen = inLen; + ds.outData = NULL; + ds.outLen = 0; + + rc = elzma_compress_run(hand, inputCallback, (void *) &ds, + outputCallback, (void *) &ds, + NULL, NULL); + + if (rc != ELZMA_E_OK) { + if (ds.outData != NULL) free(ds.outData); + elzma_compress_free(&hand); + return rc; + } + + *outData = ds.outData; + *outLen = ds.outLen; + } + + return rc; +} + +int +simpleDecompress(elzma_file_format format, const unsigned char * inData, + size_t inLen, unsigned char ** outData, + size_t * outLen) +{ + int rc; + elzma_decompress_handle hand; + + hand = elzma_decompress_alloc(); + + /* now run the compression */ + { + struct dataStream ds; + ds.inData = inData; + ds.inLen = inLen; + ds.outData = NULL; + ds.outLen = 0; + + rc = elzma_decompress_run(hand, inputCallback, (void *) &ds, + outputCallback, (void *) &ds, format); + + if (rc != ELZMA_E_OK) { + if (ds.outData != NULL) free(ds.outData); + elzma_decompress_free(&hand); + return rc; + } + + *outData = ds.outData; + *outLen = ds.outLen; + } + + return rc; +} + +#endif \ No newline at end of file diff --git a/src/zmatlib.h b/src/zmatlib.h new file mode 100644 index 0000000..15e1de2 --- /dev/null +++ b/src/zmatlib.h @@ -0,0 +1,47 @@ +#ifndef ZMAT_LIB_H +#define ZMAT_LIB_H + +#ifndef NO_LZMA + #include "easylzma/compress.h" + #include "easylzma/decompress.h" +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +enum TZipMethod {zmZlib, zmGzip, zmBase64, zmLzip, zmLzma}; + +int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret, const int iscompress); +int zmat_encode(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret); +int zmat_decode(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret); + +int zmat_keylookup(char *origkey, const char *table[]); +unsigned char * base64_encode(const unsigned char *src, size_t len, + size_t *out_len); +unsigned char * base64_decode(const unsigned char *src, size_t len, + size_t *out_len); +#ifndef NO_LZMA +/* compress a chunk of memory and return a dynamically allocated buffer + * if successful. return value is an easylzma error code */ +int simpleCompress(elzma_file_format format, + const unsigned char * inData, + size_t inLen, + unsigned char ** outData, + size_t * outLen); + +/* decompress a chunk of memory and return a dynamically allocated buffer + * if successful. return value is an easylzma error code */ +int simpleDecompress(elzma_file_format format, + const unsigned char * inData, + size_t inLen, + unsigned char ** outData, + size_t * outLen); +#endif + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file