2012-10-30 22:02:45 +01:00
|
|
|
/* LzmaDec.h -- LZMA Decoder
|
|
|
|
2009-02-07 : Igor Pavlov : Public domain */
|
|
|
|
|
|
|
|
#ifndef __LZMA_DEC_H
|
|
|
|
#define __LZMA_DEC_H
|
|
|
|
|
|
|
|
#include "Types.h"
|
|
|
|
|
2014-01-29 23:55:48 +01:00
|
|
|
typedef uint16_t CLzmaProb;
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* ---------- LZMA Properties ---------- */
|
|
|
|
|
|
|
|
#define LZMA_PROPS_SIZE 5
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
struct CLzmaProps
|
2012-10-30 22:02:45 +01:00
|
|
|
{
|
|
|
|
unsigned lc, lp, pb;
|
2014-01-27 05:55:01 +01:00
|
|
|
uint32_t dicSize;
|
2014-01-28 03:57:54 +01:00
|
|
|
};
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* LzmaProps_Decode - decodes properties
|
|
|
|
Returns:
|
|
|
|
SZ_OK
|
|
|
|
SZ_ERROR_UNSUPPORTED - Unsupported properties
|
|
|
|
*/
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
SRes LzmaProps_Decode(struct CLzmaProps *p, const uint8_t *data, unsigned size);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* ---------- 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
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
struct CLzmaDec
|
2012-10-30 22:02:45 +01:00
|
|
|
{
|
2014-01-28 03:57:54 +01:00
|
|
|
struct CLzmaProps prop;
|
2012-10-30 22:02:45 +01:00
|
|
|
CLzmaProb *probs;
|
2014-01-27 05:55:01 +01:00
|
|
|
uint8_t *dic;
|
|
|
|
const uint8_t *buf;
|
|
|
|
uint32_t range, code;
|
|
|
|
size_t dicPos;
|
|
|
|
size_t dicBufSize;
|
|
|
|
uint32_t processedPos;
|
|
|
|
uint32_t checkDicSize;
|
2012-10-30 22:02:45 +01:00
|
|
|
unsigned state;
|
2014-01-27 05:55:01 +01:00
|
|
|
uint32_t reps[4];
|
2012-10-30 22:02:45 +01:00
|
|
|
unsigned remainLen;
|
|
|
|
int needFlush;
|
|
|
|
int needInitState;
|
2014-01-27 05:55:01 +01:00
|
|
|
uint32_t numProbs;
|
2012-10-30 22:02:45 +01:00
|
|
|
unsigned tempBufSize;
|
2014-01-27 05:55:01 +01:00
|
|
|
uint8_t tempBuf[LZMA_REQUIRED_INPUT_MAX];
|
2014-01-28 03:57:54 +01:00
|
|
|
};
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
#define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; }
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
void LzmaDec_Init(struct CLzmaDec *p);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
enum ELzmaFinishMode
|
2012-10-30 22:02:45 +01:00
|
|
|
{
|
|
|
|
LZMA_FINISH_ANY, /* finish at any point */
|
|
|
|
LZMA_FINISH_END /* block must be finished at the end */
|
2014-01-28 03:57:54 +01:00
|
|
|
};
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
enum ELzmaStatus
|
2012-10-30 22:02:45 +01:00
|
|
|
{
|
|
|
|
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 */
|
2014-01-28 03:57:54 +01:00
|
|
|
};
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2013-09-13 06:57:49 +02:00
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
SRes LzmaDec_AllocateProbs(struct CLzmaDec *p, const uint8_t *props, unsigned propsSize, struct ISzAlloc *alloc);
|
|
|
|
void LzmaDec_FreeProbs(struct CLzmaDec *p, struct ISzAlloc *alloc);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
SRes LzmaDec_Allocate(struct CLzmaDec *state, const uint8_t *prop, unsigned propsSize, struct ISzAlloc *alloc);
|
|
|
|
void LzmaDec_Free(struct CLzmaDec *state, struct ISzAlloc *alloc);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
/* ---------- 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
|
2013-09-13 06:57:49 +02:00
|
|
|
|
2012-10-30 22:02:45 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
SRes LzmaDec_DecodeToDic(struct CLzmaDec *p, size_t dicLimit,
|
|
|
|
const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* ---------- 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).
|
|
|
|
*/
|
|
|
|
|
2014-01-28 03:57:54 +01:00
|
|
|
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);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* ---------- 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).
|
|
|
|
*/
|
|
|
|
|
2014-01-27 05:55:01 +01:00
|
|
|
SRes LzmaDecode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen,
|
2014-01-28 03:57:54 +01:00
|
|
|
const uint8_t *propData, unsigned propSize, enum ELzmaFinishMode finishMode,
|
|
|
|
enum ELzmaStatus *status, struct ISzAlloc *alloc);
|
2012-10-30 22:02:45 +01:00
|
|
|
|
|
|
|
#endif
|