Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:06

0001 /* LzmaDec.h -- LZMA Decoder

0002 2009-02-07 : Igor Pavlov : Public domain */
0003 
0004 #ifndef __LZMA_DEC_H
0005 #define __LZMA_DEC_H
0006 
0007 #include "Types.h"
0008 
0009 #ifdef __cplusplus
0010 extern "C" {
0011 #endif
0012 
0013 /* #define _LZMA_PROB32 */
0014 /* _LZMA_PROB32 can increase the speed on some CPUs,

0015    but memory usage for CLzmaDec::probs will be doubled in that case */
0016 
0017 #ifdef _LZMA_PROB32
0018 #define CLzmaProb UInt32
0019 #else
0020 #define CLzmaProb UInt16
0021 #endif
0022 
0023 /* ---------- LZMA Properties ---------- */
0024 
0025 #define LZMA_PROPS_SIZE 5
0026 
0027 typedef struct _CLzmaProps {
0028   unsigned lc, lp, pb;
0029   UInt32 dicSize;
0030 } CLzmaProps;
0031 
0032 /* LzmaProps_Decode - decodes properties

0033 Returns:

0034   SZ_OK

0035   SZ_ERROR_UNSUPPORTED - Unsupported properties

0036 */
0037 
0038 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size);
0039 
0040 /* ---------- LZMA Decoder state ---------- */
0041 
0042 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case.

0043    Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */
0044 
0045 #define LZMA_REQUIRED_INPUT_MAX 20
0046 
0047 typedef struct {
0048   CLzmaProps prop;
0049   CLzmaProb *probs;
0050   Byte *dic;
0051   const Byte *buf;
0052   UInt32 range, code;
0053   SizeT dicPos;
0054   SizeT dicBufSize;
0055   UInt32 processedPos;
0056   UInt32 checkDicSize;
0057   unsigned state;
0058   UInt32 reps[4];
0059   unsigned remainLen;
0060   int needFlush;
0061   int needInitState;
0062   UInt32 numProbs;
0063   unsigned tempBufSize;
0064   Byte tempBuf[LZMA_REQUIRED_INPUT_MAX];
0065 } CLzmaDec;
0066 
0067 #define LzmaDec_Construct(p) \
0068   {                          \
0069     (p)->dic = 0;            \
0070     (p)->probs = 0;          \
0071   }
0072 
0073 void LzmaDec_Init(CLzmaDec *p);
0074 
0075 /* There are two types of LZMA streams:

0076      0) Stream with end mark. That end mark adds about 6 bytes to compressed size.

0077      1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */
0078 
0079 typedef enum {
0080   LZMA_FINISH_ANY, /* finish at any point */
0081   LZMA_FINISH_END  /* block must be finished at the end */
0082 } ELzmaFinishMode;
0083 
0084 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!!

0085 

0086    You must use LZMA_FINISH_END, when you know that current output buffer

0087    covers last bytes of block. In other cases you must use LZMA_FINISH_ANY.

0088 

0089    If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK,

0090    and output value of destLen will be less than output buffer size limit.

0091    You can check status result also.

0092 

0093    You can use multiple checks to test data integrity after full decompression:

0094      1) Check Result and "status" variable.

0095      2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize.

0096      3) Check that output(srcLen) = compressedSize, if you know real compressedSize.

0097         You must use correct finish mode in that case. */
0098 
0099 typedef enum {
0100   LZMA_STATUS_NOT_SPECIFIED,              /* use main error code instead */
0101   LZMA_STATUS_FINISHED_WITH_MARK,         /* stream was finished with end mark. */
0102   LZMA_STATUS_NOT_FINISHED,               /* stream was not finished */
0103   LZMA_STATUS_NEEDS_MORE_INPUT,           /* you must provide more input bytes */
0104   LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */
0105 } ELzmaStatus;
0106 
0107 /* ELzmaStatus is used only as output value for function call */
0108 
0109 /* ---------- Interfaces ---------- */
0110 
0111 /* There are 3 levels of interfaces:

0112      1) Dictionary Interface

0113      2) Buffer Interface

0114      3) One Call Interface

0115    You can select any of these interfaces, but don't mix functions from different

0116    groups for same object. */
0117 
0118 /* There are two variants to allocate state for Dictionary Interface:

0119      1) LzmaDec_Allocate / LzmaDec_Free

0120      2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs

0121    You can use variant 2, if you set dictionary buffer manually.

0122    For Buffer Interface you must always use variant 1.

0123 

0124 LzmaDec_Allocate* can return:

0125   SZ_OK

0126   SZ_ERROR_MEM         - Memory allocation error

0127   SZ_ERROR_UNSUPPORTED - Unsupported properties

0128 */
0129 
0130 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc);
0131 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc);
0132 
0133 SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc);
0134 void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc);
0135 
0136 /* ---------- Dictionary Interface ---------- */
0137 
0138 /* You can use it, if you want to eliminate the overhead for data copying from

0139    dictionary to some other external buffer.

0140    You must work with CLzmaDec variables directly in this interface.

0141 

0142    STEPS:

0143      LzmaDec_Constr()

0144      LzmaDec_Allocate()

0145      for (each new stream)

0146      {

0147        LzmaDec_Init()

0148        while (it needs more decompression)

0149        {

0150          LzmaDec_DecodeToDic()

0151          use data from CLzmaDec::dic and update CLzmaDec::dicPos

0152        }

0153      }

0154      LzmaDec_Free()

0155 */
0156 
0157 /* LzmaDec_DecodeToDic

0158 

0159    The decoding to internal dictionary buffer (CLzmaDec::dic).

0160    You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!!

0161 

0162 finishMode:

0163   It has meaning only if the decoding reaches output limit (dicLimit).

0164   LZMA_FINISH_ANY - Decode just dicLimit bytes.

0165   LZMA_FINISH_END - Stream must be finished after dicLimit.

0166 

0167 Returns:

0168   SZ_OK

0169     status:

0170       LZMA_STATUS_FINISHED_WITH_MARK

0171       LZMA_STATUS_NOT_FINISHED

0172       LZMA_STATUS_NEEDS_MORE_INPUT

0173       LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK

0174   SZ_ERROR_DATA - Data error

0175 */
0176 
0177 SRes LzmaDec_DecodeToDic(
0178     CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
0179 
0180 /* ---------- Buffer Interface ---------- */
0181 
0182 /* It's zlib-like interface.

0183    See LzmaDec_DecodeToDic description for information about STEPS and return results,

0184    but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need

0185    to work with CLzmaDec variables manually.

0186 

0187 finishMode:

0188   It has meaning only if the decoding reaches output limit (*destLen).

0189   LZMA_FINISH_ANY - Decode just destLen bytes.

0190   LZMA_FINISH_END - Stream must be finished after (*destLen).

0191 */
0192 
0193 SRes LzmaDec_DecodeToBuf(CLzmaDec *p,
0194                          Byte *dest,
0195                          SizeT *destLen,
0196                          const Byte *src,
0197                          SizeT *srcLen,
0198                          ELzmaFinishMode finishMode,
0199                          ELzmaStatus *status);
0200 
0201 /* ---------- One Call Interface ---------- */
0202 
0203 /* LzmaDecode

0204 

0205 finishMode:

0206   It has meaning only if the decoding reaches output limit (*destLen).

0207   LZMA_FINISH_ANY - Decode just destLen bytes.

0208   LZMA_FINISH_END - Stream must be finished after (*destLen).

0209 

0210 Returns:

0211   SZ_OK

0212     status:

0213       LZMA_STATUS_FINISHED_WITH_MARK

0214       LZMA_STATUS_NOT_FINISHED

0215       LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK

0216   SZ_ERROR_DATA - Data error

0217   SZ_ERROR_MEM  - Memory allocation error

0218   SZ_ERROR_UNSUPPORTED - Unsupported properties

0219   SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src).

0220 */
0221 
0222 SRes LzmaDecode(Byte *dest,
0223                 SizeT *destLen,
0224                 const Byte *src,
0225                 SizeT *srcLen,
0226                 const Byte *propData,
0227                 unsigned propSize,
0228                 ELzmaFinishMode finishMode,
0229                 ELzmaStatus *status,
0230                 ISzAlloc *alloc);
0231 
0232 #ifdef __cplusplus
0233 }
0234 #endif
0235 
0236 #endif