224 lines
6.8 KiB
C
224 lines
6.8 KiB
C
/* LzmaDec.h -- LZMA Decoder
|
|
2009-02-07 : Igor Pavlov : Public domain */
|
|
|
|
#ifndef __LZMA_DEC_H
|
|
#define __LZMA_DEC_H
|
|
|
|
#include "Types.h"
|
|
|
|
/* #define _LZMA_PROB32 */
|
|
/* _LZMA_PROB32 can increase the speed on some CPUs,
|
|
but memory usage for CLzmaDec::probs will be doubled in that case */
|
|
|
|
#ifdef _LZMA_PROB32
|
|
#define CLzmaProb uint32_t
|
|
#else
|
|
#define CLzmaProb uint16_t
|
|
#endif
|
|
|
|
|
|
/* ---------- LZMA Properties ---------- */
|
|
|
|
#define LZMA_PROPS_SIZE 5
|
|
|
|
struct CLzmaProps
|
|
{
|
|
unsigned lc, lp, pb;
|
|
uint32_t dicSize;
|
|
};
|
|
|
|
/* LzmaProps_Decode - decodes properties
|
|
Returns:
|
|
SZ_OK
|
|
SZ_ERROR_UNSUPPORTED - Unsupported properties
|
|
*/
|
|
|
|
SRes LzmaProps_Decode(struct CLzmaProps *p, const uint8_t *data, unsigned size);
|
|
|
|
|
|
/* ---------- LZMA Decoder state ---------- */
|
|
|
|
/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.
|
|
Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
|
|
|
|
#define LZMA_REQUIRED_INPUT_MAX 20
|
|
|
|
struct CLzmaDec
|
|
{
|
|
struct CLzmaProps prop;
|
|
CLzmaProb *probs;
|
|
uint8_t *dic;
|
|
const uint8_t *buf;
|
|
uint32_t range, code;
|
|
size_t dicPos;
|
|
size_t dicBufSize;
|
|
uint32_t processedPos;
|
|
uint32_t checkDicSize;
|
|
unsigned state;
|
|
uint32_t reps[4];
|
|
unsigned remainLen;
|
|
int needFlush;
|
|
int needInitState;
|
|
uint32_t numProbs;
|
|
unsigned tempBufSize;
|
|
uint8_t tempBuf[LZMA_REQUIRED_INPUT_MAX];
|
|
};
|
|
|
|
#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
|
|
|
|
void LzmaDec_Init(struct CLzmaDec *p);
|
|
|
|
/* There are two types of LZMA streams:
|
|
0) Stream with end mark. That end mark adds about 6 bytes to compressed size.
|
|
1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
|
|
|
|
enum ELzmaFinishMode
|
|
{
|
|
LZMA_FINISH_ANY, /* finish at any point */
|
|
LZMA_FINISH_END /* block must be finished at the end */
|
|
};
|
|
|
|
/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!
|
|
|
|
You must use LZMA_FINISH_END, when you know that current output buffer
|
|
covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.
|
|
|
|
If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,
|
|
and output value of destLen will be less than output buffer size limit.
|
|
You can check status result also.
|
|
|
|
You can use multiple checks to test data integrity after full decompression:
|
|
1) Check Result and "status" variable.
|
|
2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.
|
|
3) Check that output(srcLen) = compressedSize, if you know real compressedSize.
|
|
You must use correct finish mode in that case. */
|
|
|
|
enum ELzmaStatus
|
|
{
|
|
LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */
|
|
LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */
|
|
LZMA_STATUS_NOT_FINISHED, /* stream was not finished */
|
|
LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */
|
|
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
|
|
};
|
|
|
|
/* ELzmaStatus is used only as output value for function call */
|
|
|
|
|
|
/* ---------- Interfaces ---------- */
|
|
|
|
/* There are 3 levels of interfaces:
|
|
1) Dictionary Interface
|
|
2) Buffer Interface
|
|
3) One Call Interface
|
|
You can select any of these interfaces, but don't mix functions from different
|
|
groups for same object. */
|
|
|
|
|
|
/* There are two variants to allocate state for Dictionary Interface:
|
|
1) LzmaDec_Allocate / LzmaDec_Free
|
|
2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs
|
|
You can use variant 2, if you set dictionary buffer manually.
|
|
For Buffer Interface you must always use variant 1.
|
|
|
|
LzmaDec_Allocate* can return:
|
|
SZ_OK
|
|
SZ_ERROR_MEM - Memory allocation error
|
|
SZ_ERROR_UNSUPPORTED - Unsupported properties
|
|
*/
|
|
|
|
SRes LzmaDec_AllocateProbs(struct CLzmaDec *p, const uint8_t *props, unsigned propsSize, struct ISzAlloc *alloc);
|
|
void LzmaDec_FreeProbs(struct CLzmaDec *p, struct ISzAlloc *alloc);
|
|
|
|
SRes LzmaDec_Allocate(struct CLzmaDec *state, const uint8_t *prop, unsigned propsSize, struct ISzAlloc *alloc);
|
|
void LzmaDec_Free(struct CLzmaDec *state, struct ISzAlloc *alloc);
|
|
|
|
/* ---------- Dictionary Interface ---------- */
|
|
|
|
/* You can use it, if you want to eliminate the overhead for data copying from
|
|
dictionary to some other external buffer.
|
|
You must work with CLzmaDec variables directly in this interface.
|
|
|
|
STEPS:
|
|
LzmaDec_Constr()
|
|
LzmaDec_Allocate()
|
|
for (each new stream)
|
|
{
|
|
LzmaDec_Init()
|
|
while (it needs more decompression)
|
|
{
|
|
LzmaDec_DecodeToDic()
|
|
use data from CLzmaDec::dic and update CLzmaDec::dicPos
|
|
}
|
|
}
|
|
LzmaDec_Free()
|
|
*/
|
|
|
|
/* LzmaDec_DecodeToDic
|
|
|
|
The decoding to internal dictionary buffer (CLzmaDec::dic).
|
|
You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!
|
|
|
|
finishMode:
|
|
It has meaning only if the decoding reaches output limit (dicLimit).
|
|
LZMA_FINISH_ANY - Decode just dicLimit bytes.
|
|
LZMA_FINISH_END - Stream must be finished after dicLimit.
|
|
|
|
Returns:
|
|
SZ_OK
|
|
status:
|
|
LZMA_STATUS_FINISHED_WITH_MARK
|
|
LZMA_STATUS_NOT_FINISHED
|
|
LZMA_STATUS_NEEDS_MORE_INPUT
|
|
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
|
|
SZ_ERROR_DATA - Data error
|
|
*/
|
|
|
|
SRes LzmaDec_DecodeToDic(struct CLzmaDec *p, size_t dicLimit,
|
|
const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status);
|
|
|
|
|
|
/* ---------- Buffer Interface ---------- */
|
|
|
|
/* It's zlib-like interface.
|
|
See LzmaDec_DecodeToDic description for information about STEPS and return results,
|
|
but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need
|
|
to work with CLzmaDec variables manually.
|
|
|
|
finishMode:
|
|
It has meaning only if the decoding reaches output limit (*destLen).
|
|
LZMA_FINISH_ANY - Decode just destLen bytes.
|
|
LZMA_FINISH_END - Stream must be finished after (*destLen).
|
|
*/
|
|
|
|
SRes LzmaDec_DecodeToBuf(struct CLzmaDec *p, uint8_t *dest, size_t *destLen,
|
|
const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status);
|
|
|
|
|
|
/* ---------- One Call Interface ---------- */
|
|
|
|
/* LzmaDecode
|
|
|
|
finishMode:
|
|
It has meaning only if the decoding reaches output limit (*destLen).
|
|
LZMA_FINISH_ANY - Decode just destLen bytes.
|
|
LZMA_FINISH_END - Stream must be finished after (*destLen).
|
|
|
|
Returns:
|
|
SZ_OK
|
|
status:
|
|
LZMA_STATUS_FINISHED_WITH_MARK
|
|
LZMA_STATUS_NOT_FINISHED
|
|
LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
|
|
SZ_ERROR_DATA - Data error
|
|
SZ_ERROR_MEM - Memory allocation error
|
|
SZ_ERROR_UNSUPPORTED - Unsupported properties
|
|
SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).
|
|
*/
|
|
|
|
SRes LzmaDecode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen,
|
|
const uint8_t *propData, unsigned propSize, enum ELzmaFinishMode finishMode,
|
|
enum ELzmaStatus *status, struct ISzAlloc *alloc);
|
|
|
|
#endif
|