mirror of
https://github.com/emmanuel-marty/lzsa.git
synced 2024-06-13 22:29:32 +00:00
Compare commits
3 Commits
5484395465
...
bea90736d5
Author | SHA1 | Date | |
---|---|---|---|
|
bea90736d5 | ||
|
3eaf926c1a | ||
|
1bca5b995a |
|
@ -39,9 +39,6 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Forward declaration */
|
||||
typedef enum _lzsa_status_t lzsa_status_t;
|
||||
|
||||
/*-------------- File API -------------- */
|
||||
|
||||
/**
|
||||
|
|
|
@ -60,6 +60,7 @@ int lzsa_get_frame_size(void) {
|
|||
*
|
||||
* @param pFrameData encoding buffer
|
||||
* @param nMaxFrameDataSize max encoding buffer size, in bytes
|
||||
* @param nFormatVersion version of format to use (1-2)
|
||||
*
|
||||
* @return number of encoded bytes, or -1 for failure
|
||||
*/
|
||||
|
@ -146,6 +147,7 @@ int lzsa_encode_footer_frame(unsigned char *pFrameData, const int nMaxFrameDataS
|
|||
*
|
||||
* @param pFrameData data bytes
|
||||
* @param nFrameDataSize number of bytes to decode
|
||||
* @param nFormatVersion pointer to returned format version, if successful
|
||||
*
|
||||
* @return 0 for success, or -1 for failure
|
||||
*/
|
||||
|
|
|
@ -56,6 +56,7 @@ int lzsa_get_frame_size(void);
|
|||
*
|
||||
* @param pFrameData encoding buffer
|
||||
* @param nMaxFrameDataSize max encoding buffer size, in bytes
|
||||
* @param nFormatVersion version of format to use (1-2)
|
||||
*
|
||||
* @return number of encoded bytes, or -1 for failure
|
||||
*/
|
||||
|
@ -98,6 +99,7 @@ int lzsa_encode_footer_frame(unsigned char *pFrameData, const int nMaxFrameDataS
|
|||
*
|
||||
* @param pFrameData data bytes
|
||||
* @param nFrameDataSize number of bytes to decode
|
||||
* @param nFormatVersion pointer to returned format version, if successful
|
||||
*
|
||||
* @return 0 for success, or -1 for failure
|
||||
*/
|
||||
|
|
18
src/lib.h
18
src/lib.h
|
@ -48,24 +48,6 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** High level status for compression and decompression */
|
||||
typedef enum _lzsa_status_t {
|
||||
LZSA_OK = 0, /**< Success */
|
||||
LZSA_ERROR_SRC, /**< Error reading input */
|
||||
LZSA_ERROR_DST, /**< Error reading output */
|
||||
LZSA_ERROR_DICTIONARY, /**< Error reading dictionary */
|
||||
LZSA_ERROR_MEMORY, /**< Out of memory */
|
||||
|
||||
/* Compression-specific status codes */
|
||||
LZSA_ERROR_COMPRESSION, /**< Internal compression error */
|
||||
LZSA_ERROR_RAW_TOOLARGE, /**< Input is too large to be compressed to a raw block */
|
||||
LZSA_ERROR_RAW_UNCOMPRESSED, /**< Input is incompressible and raw blocks don't support uncompressed data */
|
||||
|
||||
/* Decompression-specific status codes */
|
||||
LZSA_ERROR_FORMAT, /**< Invalid input format or magic number when decompressing */
|
||||
LZSA_ERROR_DECOMPRESSION /**< Internal decompression error */
|
||||
} lzsa_status_t;
|
||||
|
||||
/* Compression flags */
|
||||
#define LZSA_FLAG_FAVOR_RATIO (1<<0) /**< 1 to compress with the best ratio, 0 to trade some compression ratio for extra decompression speed */
|
||||
#define LZSA_FLAG_RAW_BLOCK (1<<1) /**< 1 to emit raw block */
|
||||
|
|
|
@ -75,8 +75,8 @@ void divsufsort_destroy(divsufsort_ctx_t *ctx);
|
|||
/**
|
||||
* Constructs the suffix array of a given string.
|
||||
* @param ctx suffix array context
|
||||
* @param T[0..n-1] The input string.
|
||||
* @param SA[0..n-1] The output array of suffixes.
|
||||
* @param T The input string.
|
||||
* @param SA The output array of suffixes.
|
||||
* @param n The length of the given string.
|
||||
* @return 0 if no error occurred, -1 or -2 otherwise.
|
||||
*/
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include <string.h>
|
||||
#include "matchfinder.h"
|
||||
#include "format.h"
|
||||
#include "lib.h"
|
||||
|
||||
/**
|
||||
* Hash index into TAG_BITS
|
||||
|
|
|
@ -33,14 +33,12 @@
|
|||
#ifndef _MATCHFINDER_H
|
||||
#define _MATCHFINDER_H
|
||||
|
||||
#include "shrink_context.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Forward declarations */
|
||||
typedef struct _lzsa_match lzsa_match;
|
||||
typedef struct _lzsa_compressor lzsa_compressor;
|
||||
|
||||
/**
|
||||
* Parse input data, build suffix array and overlaid data structures to speed up match finding
|
||||
*
|
||||
|
|
|
@ -141,7 +141,7 @@ static inline int lzsa_write_match_varlen_v1(unsigned char *pOutData, int nOutOf
|
|||
/**
|
||||
* Get offset encoding cost in bits
|
||||
*
|
||||
* @param nMatchOffset offset to get cost of
|
||||
* @param __nMatchOffset offset to get cost of
|
||||
*
|
||||
* @return cost in bits
|
||||
*/
|
||||
|
@ -625,11 +625,11 @@ static int lzsa_write_block_v1(lzsa_compressor *pCompressor, const lzsa_match *p
|
|||
* @return size of compressed data in output buffer, or -1 if the data is uncompressible
|
||||
*/
|
||||
static int lzsa_write_raw_uncompressed_block_v1(lzsa_compressor *pCompressor, const unsigned char *pInWindow, const int nStartOffset, const int nEndOffset, unsigned char *pOutData, const int nMaxOutDataSize) {
|
||||
int nNumLiterals = nEndOffset - nStartOffset;
|
||||
const int nNumLiterals = nEndOffset - nStartOffset;
|
||||
const int nTokenLiteralsLen = (nNumLiterals >= LITERALS_RUN_LEN_V1) ? LITERALS_RUN_LEN_V1 : nNumLiterals;
|
||||
int nOutOffset = 0;
|
||||
|
||||
int nCommandSize = 8 /* token */ + lzsa_get_literals_varlen_size_v1(nNumLiterals) + (nNumLiterals << 3) + 4;
|
||||
const int nCommandSize = 8 /* token */ + lzsa_get_literals_varlen_size_v1(nNumLiterals) + (nNumLiterals << 3) + 4;
|
||||
if ((nOutOffset + (nCommandSize >> 3)) > nMaxOutDataSize)
|
||||
return -1;
|
||||
|
||||
|
|
|
@ -33,8 +33,11 @@
|
|||
#ifndef _SHRINK_BLOCK_V1_H
|
||||
#define _SHRINK_BLOCK_V1_H
|
||||
|
||||
/* Forward declarations */
|
||||
typedef struct _lzsa_compressor lzsa_compressor;
|
||||
#include "shrink_context.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Select the most optimal matches, reduce the token count if possible, and then emit a block of compressed LZSA1 data
|
||||
|
@ -50,4 +53,8 @@ typedef struct _lzsa_compressor lzsa_compressor;
|
|||
*/
|
||||
int lzsa_optimize_and_write_block_v1(lzsa_compressor *pCompressor, const unsigned char *pInWindow, const int nPreviousBlockSize, const int nInDataSize, unsigned char *pOutData, const int nMaxOutDataSize);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SHRINK_BLOCK_V1_H */
|
||||
|
|
|
@ -33,8 +33,11 @@
|
|||
#ifndef _SHRINK_BLOCK_V2_H
|
||||
#define _SHRINK_BLOCK_V2_H
|
||||
|
||||
/* Forward declarations */
|
||||
typedef struct _lzsa_compressor lzsa_compressor;
|
||||
#include "shrink_context.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Select the most optimal matches, reduce the token count if possible, and then emit a block of compressed LZSA2 data
|
||||
|
@ -50,4 +53,8 @@ typedef struct _lzsa_compressor lzsa_compressor;
|
|||
*/
|
||||
int lzsa_optimize_and_write_block_v2(lzsa_compressor *pCompressor, const unsigned char *pInWindow, const int nPreviousBlockSize, const int nInDataSize, unsigned char *pOutData, const int nMaxOutDataSize);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _SHRINK_BLOCK_V2_H */
|
||||
|
|
|
@ -45,14 +45,15 @@
|
|||
* @param pCompressor compression context to initialize
|
||||
* @param nMaxWindowSize maximum size of input data window (previously compressed bytes + bytes to compress)
|
||||
* @param nMinMatchSize minimum match size (cannot be less than MIN_MATCH_SIZE)
|
||||
* @param nFormatVersion version of format to use (1-2)
|
||||
* @param nFlags compression flags
|
||||
*
|
||||
* @return 0 for success, non-zero for failure
|
||||
*/
|
||||
int lzsa_compressor_init(lzsa_compressor *pCompressor, const int nMaxWindowSize, const int nMinMatchSize, const int nFormatVersion, const int nFlags) {
|
||||
int nResult;
|
||||
int nMinMatchSizeForFormat = (nFormatVersion == 1) ? MIN_MATCH_SIZE_V1 : MIN_MATCH_SIZE_V2;
|
||||
int nMaxMinMatchForFormat = (nFormatVersion == 1) ? 5 : 3;
|
||||
const int nMinMatchSizeForFormat = (nFormatVersion == 1) ? MIN_MATCH_SIZE_V1 : MIN_MATCH_SIZE_V2;
|
||||
const int nMaxMinMatchForFormat = (nFormatVersion == 1) ? 5 : 3;
|
||||
|
||||
nResult = divsufsort_init(&pCompressor->divsufsort_context);
|
||||
pCompressor->intervals = NULL;
|
||||
|
|
|
@ -145,6 +145,7 @@ typedef struct _lzsa_compressor {
|
|||
* @param pCompressor compression context to initialize
|
||||
* @param nMaxWindowSize maximum size of input data window (previously compressed bytes + bytes to compress)
|
||||
* @param nMinMatchSize minimum match size (cannot be less than MIN_MATCH_SIZE)
|
||||
* @param nFormatVersion version of format to use (1-2)
|
||||
* @param nFlags compression flags
|
||||
*
|
||||
* @return 0 for success, non-zero for failure
|
||||
|
|
|
@ -33,16 +33,13 @@
|
|||
#ifndef _SHRINK_STREAMING_H
|
||||
#define _SHRINK_STREAMING_H
|
||||
|
||||
#include "shrink_context.h"
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Forward declaration */
|
||||
typedef enum _lzsa_status_t lzsa_status_t;
|
||||
typedef struct _lzsa_stats lzsa_stats;
|
||||
|
||||
/*-------------- File API -------------- */
|
||||
|
||||
/**
|
||||
|
|
10
src/stream.c
10
src/stream.c
|
@ -44,7 +44,7 @@
|
|||
*
|
||||
* @param stream stream
|
||||
*/
|
||||
static void lzsa_filestream_close(lzsa_stream_t *stream) {
|
||||
static void lzsa_filestream_close(struct _lzsa_stream_t *stream) {
|
||||
if (stream->obj) {
|
||||
fclose((FILE*)stream->obj);
|
||||
stream->obj = NULL;
|
||||
|
@ -64,7 +64,7 @@ static void lzsa_filestream_close(lzsa_stream_t *stream) {
|
|||
*
|
||||
* @return number of bytes read
|
||||
*/
|
||||
static size_t lzsa_filestream_read(lzsa_stream_t *stream, void *ptr, size_t size) {
|
||||
static size_t lzsa_filestream_read(struct _lzsa_stream_t *stream, void *ptr, size_t size) {
|
||||
return fread(ptr, 1, size, (FILE*)stream->obj);
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ static size_t lzsa_filestream_read(lzsa_stream_t *stream, void *ptr, size_t size
|
|||
*
|
||||
* @return number of bytes written
|
||||
*/
|
||||
static size_t lzsa_filestream_write(lzsa_stream_t *stream, void *ptr, size_t size) {
|
||||
static size_t lzsa_filestream_write(struct _lzsa_stream_t *stream, void *ptr, size_t size) {
|
||||
return fwrite(ptr, 1, size, (FILE*)stream->obj);
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ static size_t lzsa_filestream_write(lzsa_stream_t *stream, void *ptr, size_t siz
|
|||
*
|
||||
* @return nonzero if the end of the data has been reached, 0 if there is more data
|
||||
*/
|
||||
static int lzsa_filestream_eof(lzsa_stream_t *stream) {
|
||||
static int lzsa_filestream_eof(struct _lzsa_stream_t *stream) {
|
||||
return feof((FILE*)stream->obj);
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,9 @@ int lzsa_filestream_open(lzsa_stream_t *stream, const char *pszInFilename, const
|
|||
const char* stdInOutFile = "-";
|
||||
const char* stdInMode = "rb";
|
||||
const char* stdOutMode = "wb";
|
||||
#ifdef _WIN32
|
||||
int result;
|
||||
#endif
|
||||
|
||||
if (!strncmp(pszInFilename, stdInOutFile, 1)) {
|
||||
if (!strncmp(pszMode, stdInMode, 2)) {
|
||||
|
|
27
src/stream.h
27
src/stream.h
|
@ -37,8 +37,23 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Forward declaration */
|
||||
typedef struct _lzsa_stream_t lzsa_stream_t;
|
||||
/** High level status for compression and decompression */
|
||||
typedef enum _lzsa_status_t {
|
||||
LZSA_OK = 0, /**< Success */
|
||||
LZSA_ERROR_SRC, /**< Error reading input */
|
||||
LZSA_ERROR_DST, /**< Error reading output */
|
||||
LZSA_ERROR_DICTIONARY, /**< Error reading dictionary */
|
||||
LZSA_ERROR_MEMORY, /**< Out of memory */
|
||||
|
||||
/* Compression-specific status codes */
|
||||
LZSA_ERROR_COMPRESSION, /**< Internal compression error */
|
||||
LZSA_ERROR_RAW_TOOLARGE, /**< Input is too large to be compressed to a raw block */
|
||||
LZSA_ERROR_RAW_UNCOMPRESSED, /**< Input is incompressible and raw blocks don't support uncompressed data */
|
||||
|
||||
/* Decompression-specific status codes */
|
||||
LZSA_ERROR_FORMAT, /**< Invalid input format or magic number when decompressing */
|
||||
LZSA_ERROR_DECOMPRESSION /**< Internal decompression error */
|
||||
} lzsa_status_t;
|
||||
|
||||
/* I/O stream */
|
||||
typedef struct _lzsa_stream_t {
|
||||
|
@ -54,7 +69,7 @@ typedef struct _lzsa_stream_t {
|
|||
*
|
||||
* @return number of bytes read
|
||||
*/
|
||||
size_t(*read)(lzsa_stream_t *stream, void *ptr, size_t size);
|
||||
size_t(*read)(struct _lzsa_stream_t *stream, void *ptr, size_t size);
|
||||
|
||||
/**
|
||||
* Write to stream
|
||||
|
@ -65,7 +80,7 @@ typedef struct _lzsa_stream_t {
|
|||
*
|
||||
* @return number of bytes written
|
||||
*/
|
||||
size_t(*write)(lzsa_stream_t *stream, void *ptr, size_t size);
|
||||
size_t(*write)(struct _lzsa_stream_t *stream, void *ptr, size_t size);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -75,14 +90,14 @@ typedef struct _lzsa_stream_t {
|
|||
*
|
||||
* @return nonzero if the end of the data has been reached, 0 if there is more data
|
||||
*/
|
||||
int(*eof)(lzsa_stream_t *stream);
|
||||
int(*eof)(struct _lzsa_stream_t *stream);
|
||||
|
||||
/**
|
||||
* Close stream
|
||||
*
|
||||
* @param stream stream
|
||||
*/
|
||||
void(*close)(lzsa_stream_t *stream);
|
||||
void(*close)(struct _lzsa_stream_t *stream);
|
||||
} lzsa_stream_t;
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user