/* LzmaDec.h -- LZMA Decoder12023-04-02 : Igor Pavlov : Public domain */23#ifndef ZIP7_INC_LZMA_DEC_H4#define ZIP7_INC_LZMA_DEC_H56#include "7zTypes.h"78EXTERN_C_BEGIN910/* #define Z7_LZMA_PROB32 */11/* Z7_LZMA_PROB32 can increase the speed on some CPUs,12but memory usage for CLzmaDec::probs will be doubled in that case */1314typedef15#ifdef Z7_LZMA_PROB3216UInt3217#else18UInt1619#endif20CLzmaProb;212223/* ---------- LZMA Properties ---------- */2425#define LZMA_PROPS_SIZE 52627typedef struct28{29Byte lc;30Byte lp;31Byte pb;32Byte _pad_;33UInt32 dicSize;34} CLzmaProps;3536/* LzmaProps_Decode - decodes properties37Returns:38SZ_OK39SZ_ERROR_UNSUPPORTED - Unsupported properties40*/4142SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);434445/* ---------- LZMA Decoder state ---------- */4647/* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.48Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */4950#define LZMA_REQUIRED_INPUT_MAX 205152typedef struct53{54/* Don't change this structure. ASM code can use it. */55CLzmaProps prop;56CLzmaProb *probs;57CLzmaProb *probs_1664;58Byte *dic;59SizeT dicBufSize;60SizeT dicPos;61const Byte *buf;62UInt32 range;63UInt32 code;64UInt32 processedPos;65UInt32 checkDicSize;66UInt32 reps[4];67UInt32 state;68UInt32 remainLen;6970UInt32 numProbs;71unsigned tempBufSize;72Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];73} CLzmaDec;7475#define LzmaDec_CONSTRUCT(p) { (p)->dic = NULL; (p)->probs = NULL; }76#define LzmaDec_Construct(p) LzmaDec_CONSTRUCT(p)7778void LzmaDec_Init(CLzmaDec *p);7980/* There are two types of LZMA streams:81- Stream with end mark. That end mark adds about 6 bytes to compressed size.82- Stream without end mark. You must know exact uncompressed size to decompress such stream. */8384typedef enum85{86LZMA_FINISH_ANY, /* finish at any point */87LZMA_FINISH_END /* block must be finished at the end */88} ELzmaFinishMode;8990/* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!9192You must use LZMA_FINISH_END, when you know that current output buffer93covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.9495If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,96and output value of destLen will be less than output buffer size limit.97You can check status result also.9899You can use multiple checks to test data integrity after full decompression:1001) Check Result and "status" variable.1012) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.1023) Check that output(srcLen) = compressedSize, if you know real compressedSize.103You must use correct finish mode in that case. */104105typedef enum106{107LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */108LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */109LZMA_STATUS_NOT_FINISHED, /* stream was not finished */110LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */111LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */112} ELzmaStatus;113114/* ELzmaStatus is used only as output value for function call */115116117/* ---------- Interfaces ---------- */118119/* There are 3 levels of interfaces:1201) Dictionary Interface1212) Buffer Interface1223) One Call Interface123You can select any of these interfaces, but don't mix functions from different124groups for same object. */125126127/* There are two variants to allocate state for Dictionary Interface:1281) LzmaDec_Allocate / LzmaDec_Free1292) LzmaDec_AllocateProbs / LzmaDec_FreeProbs130You can use variant 2, if you set dictionary buffer manually.131For Buffer Interface you must always use variant 1.132133LzmaDec_Allocate* can return:134SZ_OK135SZ_ERROR_MEM - Memory allocation error136SZ_ERROR_UNSUPPORTED - Unsupported properties137*/138139SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);140void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc);141142SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc);143void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc);144145/* ---------- Dictionary Interface ---------- */146147/* You can use it, if you want to eliminate the overhead for data copying from148dictionary to some other external buffer.149You must work with CLzmaDec variables directly in this interface.150151STEPS:152LzmaDec_Construct()153LzmaDec_Allocate()154for (each new stream)155{156LzmaDec_Init()157while (it needs more decompression)158{159LzmaDec_DecodeToDic()160use data from CLzmaDec::dic and update CLzmaDec::dicPos161}162}163LzmaDec_Free()164*/165166/* LzmaDec_DecodeToDic167168The decoding to internal dictionary buffer (CLzmaDec::dic).169You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!170171finishMode:172It has meaning only if the decoding reaches output limit (dicLimit).173LZMA_FINISH_ANY - Decode just dicLimit bytes.174LZMA_FINISH_END - Stream must be finished after dicLimit.175176Returns:177SZ_OK178status:179LZMA_STATUS_FINISHED_WITH_MARK180LZMA_STATUS_NOT_FINISHED181LZMA_STATUS_NEEDS_MORE_INPUT182LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK183SZ_ERROR_DATA - Data error184SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure185*/186187SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit,188const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);189190191/* ---------- Buffer Interface ---------- */192193/* It's zlib-like interface.194See LzmaDec_DecodeToDic description for information about STEPS and return results,195but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need196to work with CLzmaDec variables manually.197198finishMode:199It has meaning only if the decoding reaches output limit (*destLen).200LZMA_FINISH_ANY - Decode just destLen bytes.201LZMA_FINISH_END - Stream must be finished after (*destLen).202*/203204SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,205const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);206207208/* ---------- One Call Interface ---------- */209210/* LzmaDecode211212finishMode:213It has meaning only if the decoding reaches output limit (*destLen).214LZMA_FINISH_ANY - Decode just destLen bytes.215LZMA_FINISH_END - Stream must be finished after (*destLen).216217Returns:218SZ_OK219status:220LZMA_STATUS_FINISHED_WITH_MARK221LZMA_STATUS_NOT_FINISHED222LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK223SZ_ERROR_DATA - Data error224SZ_ERROR_MEM - Memory allocation error225SZ_ERROR_UNSUPPORTED - Unsupported properties226SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).227SZ_ERROR_FAIL - Some unexpected error: internal error of code, memory corruption or hardware failure228*/229230SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,231const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,232ELzmaStatus *status, ISzAllocPtr alloc);233234EXTERN_C_END235236#endif237238239