Skip to content

Commit

Permalink
add doxygen documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed May 24, 2020
1 parent 6a3e038 commit 392d446
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 17 deletions.
Binary file removed private/zipmat.mexa64
Binary file not shown.
140 changes: 127 additions & 13 deletions src/zmatlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,50 @@
#include <assert.h>

#include "zmatlib.h"

#include "zlib.h"

/**
* @brief Coarse grained error messages (encoder-specific detailed error codes are in the status parameter)
*
*/

char *zmat_errcode[]={
"No error",
"input can not be empty",
"failed to initialize zlib",
"zlib error, see info.status for error flag, often a result of mismatch in compression method",
"easylzma error, see info.status for error flag, often a result of mismatch in compression method",
"can not allocate output buffer",
"lz4 error, see info.status for error flag, often a result of mismatch in compression method",
"unsupported method"
"No error", /*0*/
"input can not be empty", /*-1*/
"failed to initialize zlib", /*-2*/
"zlib error, see info.status for error flag, often a result of mismatch in compression method", /*-3*/
"easylzma error, see info.status for error flag, often a result of mismatch in compression method",/*-4*/
"can not allocate output buffer",/*-5*/
"lz4 error, see info.status for error flag, often a result of mismatch in compression method",/*-6*/
"unsupported method" /*-7*/
};


/**
* @brief Convert error code to a string error message
*
* @param[in] id: zmat error code
*/

char * zmat_error(int id){
if(id>=0 && id<(sizeof(zmat_errcode) / sizeof(zmat_errcode[0])))
return zmat_errcode[id];
else
return "unknown error";
}

/**
* @brief Main interface to perform compression/decompression
*
* @param[in] inputsize: input stream buffer length
* @param[in] inputstr: input stream buffer pointer
* @param[in] outputsize: output stream buffer length
* @param[in] outputbuf: output stream buffer pointer
* @param[in] ret: encoder/decoder specific detailed error code (if error occurs)
* @param[in] iscompress: 0: decompression, 1: use default compression level;
* negative interger: set compression level (-1, less, to -9, more compression)
* @return return the coarse grained zmat error code; detailed error code is in ret.
*/

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};
Expand All @@ -83,9 +105,18 @@ int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize
return -1;

if(iscompress){
/**
* perform compression or encoding
*/
if(zipid==zmBase64){
/**
* base64 encoding
*/
*outputbuf=base64_encode((const unsigned char*)inputstr, inputsize, outputsize);
}else if(zipid==zmZlib || zipid==zmGzip){
/**
* zlib (.zip) or gzip (.gz) compression
*/
if(zipid==zmZlib){
if(deflateInit(&zs, (iscompress>0) ? Z_DEFAULT_COMPRESSION : (-iscompress)) != Z_OK)
return -2;
Expand All @@ -108,13 +139,19 @@ int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize
deflateEnd(&zs);
#ifndef NO_LZMA
}else if(zipid==zmLzma || zipid==zmLzip){
/**
* lzma (.lzma) or lzip (.lzip) compression
*/
*ret = simpleCompress((elzma_file_format)(zipid-3), (unsigned char *)inputstr,
inputsize, outputbuf, outputsize, iscompress);
if(*ret!=ELZMA_E_OK)
return -4;
#endif
#ifndef NO_LZ4
}else if(zipid==zmLz4 || zipid==zmLz4hc){
/**
* lz4 or lz4hc compression
*/
*outputsize=LZ4_compressBound(inputsize);
if (!(*outputbuf = (unsigned char *)malloc(*outputsize)))
return -5;
Expand All @@ -130,9 +167,18 @@ int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize
return -7;
}
}else{
/**
* perform decompression or decoding
*/
if(zipid==zmBase64){
/**
* base64 decoding
*/
*outputbuf=base64_decode((const unsigned char*)inputstr, inputsize, outputsize);
}else if(zipid==zmZlib || zipid==zmGzip){
/**
* zlib (.zip) or gzip (.gz) decompression
*/
int count=1;
if(zipid==zmZlib){
if(inflateInit(&zs) != Z_OK)
Expand Down Expand Up @@ -163,13 +209,19 @@ int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize
inflateEnd(&zs);
#ifndef NO_LZMA
}else if(zipid==zmLzma || zipid==zmLzip){
/**
* lzma (.lzma) or lzip (.lzip) decompression
*/
*ret = simpleDecompress((elzma_file_format)(zipid-3), (unsigned char *)inputstr,
inputsize, outputbuf, outputsize);
if(*ret!=ELZMA_E_OK)
return -4;
#endif
#ifndef NO_LZ4
}else if(zipid==zmLz4 || zipid==zmLz4hc){
/**
* lz4 or lz4hc decompression
*/
int count=2;
*outputsize=(inputsize<<count);
if (!(*outputbuf = (unsigned char *)malloc(*outputsize))){
Expand All @@ -195,9 +247,32 @@ int zmat_run(const size_t inputsize, unsigned char *inputstr, size_t *outputsize
return 0;
}

/**
* @brief Simplified interface to perform compression (use default compression level)
*
* @param[in] inputsize: input stream buffer length
* @param[in] inputstr: input stream buffer pointer
* @param[in] outputsize: output stream buffer length
* @param[in] outputbuf: output stream buffer pointer
* @param[in] ret: encoder/decoder specific detailed error code (if error occurs)
* @return return the coarse grained zmat error code; detailed error code is in ret.
*/

int zmat_encode(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret){
return zmat_run(inputsize, inputstr, outputsize, outputbuf, zipid, ret, 1);
}

/**
* @brief Simplified interface to perform decompression
*
* @param[in] inputsize: input stream buffer length
* @param[in] inputstr: input stream buffer pointer
* @param[in] outputsize: output stream buffer length
* @param[in] outputbuf: output stream buffer pointer
* @param[in] ret: encoder/decoder specific detailed error code (if error occurs)
* @return return the coarse grained zmat error code; detailed error code is in ret.
*/

int zmat_decode(const size_t inputsize, unsigned char *inputstr, size_t *outputsize, unsigned char **outputbuf, const int zipid, int *ret){
return zmat_run(inputsize, inputstr, outputsize, outputbuf, zipid, ret, 0);
}
Expand Down Expand Up @@ -232,8 +307,8 @@ int zmat_keylookup(char *origkey, const char *table[]){


/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <[email protected]>
* @brief Base64 encoding/decoding (RFC1341)
* @author Copyright (c) 2005-2011, Jouni Malinen <[email protected]>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
Expand All @@ -243,7 +318,7 @@ static const unsigned char base64_table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
* base64_encode - Base64 encode
* @brief 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
Expand All @@ -254,6 +329,7 @@ static const unsigned char base64_table[65] =
* 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)
{
Expand Down Expand Up @@ -322,6 +398,7 @@ unsigned char * base64_encode(const unsigned char *src, size_t len,
*
* Caller is responsible for freeing the returned buffer.
*/

unsigned char * base64_decode(const unsigned char *src, size_t len,
size_t *out_len)
{
Expand Down Expand Up @@ -384,6 +461,10 @@ unsigned char * base64_decode(const unsigned char *src, size_t len,

#ifndef NO_LZMA

/**
* @brief Easylzma compression interface
*/

struct dataStream
{
const unsigned char * inData;
Expand All @@ -393,6 +474,10 @@ struct dataStream
size_t outLen;
};

/**
* @brief Easylzma input callback function
*/

static int
inputCallback(void *ctx, void *buf, size_t * size)
{
Expand All @@ -413,6 +498,10 @@ inputCallback(void *ctx, void *buf, size_t * size)
return 0;
}

/**
* @brief Easylzma output callback function
*/

static size_t
outputCallback(void *ctx, const void *buf, size_t size)
{
Expand All @@ -428,6 +517,19 @@ outputCallback(void *ctx, const void *buf, size_t size)
return size;
}

/**
* @brief Easylzma interface to perform compression
*
* @param[in] format: output format (0 for lzip format, 1 for lzma-alone format)
* @param[in] inData: input stream buffer pointer
* @param[in] inLen: input stream buffer length
* @param[in] outData: output stream buffer pointer
* @param[in] outLen: output stream buffer length
* @param[in] level: positive number: use default compression level (5);
* negative interger: set compression level (-1, less, to -9, more compression)
* @return return the fine grained lzma error code.
*/

int
simpleCompress(elzma_file_format format, const unsigned char * inData,
size_t inLen, unsigned char ** outData,
Expand Down Expand Up @@ -475,6 +577,18 @@ simpleCompress(elzma_file_format format, const unsigned char * inData,
return rc;
}


/**
* @brief Easylzma interface to perform decompression
*
* @param[in] format: output format (0 for lzip format, 1 for lzma-alone format)
* @param[in] inData: input stream buffer pointer
* @param[in] inLen: input stream buffer length
* @param[in] outData: output stream buffer pointer
* @param[in] outLen: output stream buffer length
* @return return the fine grained lzma error code.
*/

int
simpleDecompress(elzma_file_format format, const unsigned char * inData,
size_t inLen, unsigned char ** outData,
Expand Down
Loading

0 comments on commit 392d446

Please sign in to comment.