00001 /* LzmaDec.h -- LZMA Decoder 00002 2009-02-07 : Igor Pavlov : Public domain */ 00003 00004 #ifndef __LZMA_DEC_H 00005 #define __LZMA_DEC_H 00006 00007 #include "Types.h" 00008 00009 #ifdef __cplusplus 00010 extern "C" { 00011 #endif 00012 00013 /* #define _LZMA_PROB32 */ 00014 /* _LZMA_PROB32 can increase the speed on some CPUs, 00015 but memory usage for CLzmaDec::probs will be doubled in that case */ 00016 00017 #ifdef _LZMA_PROB32 00018 #define CLzmaProb UInt32 00019 #else 00020 #define CLzmaProb UInt16 00021 #endif 00022 00023 00024 /* ---------- LZMA Properties ---------- */ 00025 00026 #define LZMA_PROPS_SIZE 5 00027 00028 typedef struct _CLzmaProps 00029 { 00030 unsigned lc, lp, pb; 00031 UInt32 dicSize; 00032 } CLzmaProps; 00033 00034 /* LzmaProps_Decode - decodes properties 00035 Returns: 00036 SZ_OK 00037 SZ_ERROR_UNSUPPORTED - Unsupported properties 00038 */ 00039 00040 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); 00041 00042 00043 /* ---------- LZMA Decoder state ---------- */ 00044 00045 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. 00046 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ 00047 00048 #define LZMA_REQUIRED_INPUT_MAX 20 00049 00050 typedef struct 00051 { 00052 CLzmaProps prop; 00053 CLzmaProb *probs; 00054 Byte *dic; 00055 const Byte *buf; 00056 UInt32 range, code; 00057 SizeT dicPos; 00058 SizeT dicBufSize; 00059 UInt32 processedPos; 00060 UInt32 checkDicSize; 00061 unsigned state; 00062 UInt32 reps[4]; 00063 unsigned remainLen; 00064 int needFlush; 00065 int needInitState; 00066 UInt32 numProbs; 00067 unsigned tempBufSize; 00068 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; 00069 } CLzmaDec; 00070 00071 #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } 00072 00073 void LzmaDec_Init(CLzmaDec *p); 00074 00075 /* There are two types of LZMA streams: 00076 0) Stream with end mark. That end mark adds about 6 bytes to compressed size. 00077 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */ 00078 00079 typedef enum 00080 { 00081 LZMA_FINISH_ANY, /* finish at any point */ 00082 LZMA_FINISH_END /* block must be finished at the end */ 00083 } ELzmaFinishMode; 00084 00085 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! 00086 00087 You must use LZMA_FINISH_END, when you know that current output buffer 00088 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. 00089 00090 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, 00091 and output value of destLen will be less than output buffer size limit. 00092 You can check status result also. 00093 00094 You can use multiple checks to test data integrity after full decompression: 00095 1) Check Result and "status" variable. 00096 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. 00097 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. 00098 You must use correct finish mode in that case. */ 00099 00100 typedef enum 00101 { 00102 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ 00103 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ 00104 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ 00105 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ 00106 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ 00107 } ELzmaStatus; 00108 00109 /* ELzmaStatus is used only as output value for function call */ 00110 00111 00112 /* ---------- Interfaces ---------- */ 00113 00114 /* There are 3 levels of interfaces: 00115 1) Dictionary Interface 00116 2) Buffer Interface 00117 3) One Call Interface 00118 You can select any of these interfaces, but don't mix functions from different 00119 groups for same object. */ 00120 00121 00122 /* There are two variants to allocate state for Dictionary Interface: 00123 1) LzmaDec_Allocate / LzmaDec_Free 00124 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs 00125 You can use variant 2, if you set dictionary buffer manually. 00126 For Buffer Interface you must always use variant 1. 00127 00128 LzmaDec_Allocate* can return: 00129 SZ_OK 00130 SZ_ERROR_MEM - Memory allocation error 00131 SZ_ERROR_UNSUPPORTED - Unsupported properties 00132 */ 00133 00134 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc); 00135 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc); 00136 00137 SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc); 00138 void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); 00139 00140 /* ---------- Dictionary Interface ---------- */ 00141 00142 /* You can use it, if you want to eliminate the overhead for data copying from 00143 dictionary to some other external buffer. 00144 You must work with CLzmaDec variables directly in this interface. 00145 00146 STEPS: 00147 LzmaDec_Constr() 00148 LzmaDec_Allocate() 00149 for (each new stream) 00150 { 00151 LzmaDec_Init() 00152 while (it needs more decompression) 00153 { 00154 LzmaDec_DecodeToDic() 00155 use data from CLzmaDec::dic and update CLzmaDec::dicPos 00156 } 00157 } 00158 LzmaDec_Free() 00159 */ 00160 00161 /* LzmaDec_DecodeToDic 00162 00163 The decoding to internal dictionary buffer (CLzmaDec::dic). 00164 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! 00165 00166 finishMode: 00167 It has meaning only if the decoding reaches output limit (dicLimit). 00168 LZMA_FINISH_ANY - Decode just dicLimit bytes. 00169 LZMA_FINISH_END - Stream must be finished after dicLimit. 00170 00171 Returns: 00172 SZ_OK 00173 status: 00174 LZMA_STATUS_FINISHED_WITH_MARK 00175 LZMA_STATUS_NOT_FINISHED 00176 LZMA_STATUS_NEEDS_MORE_INPUT 00177 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 00178 SZ_ERROR_DATA - Data error 00179 */ 00180 00181 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, 00182 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 00183 00184 00185 /* ---------- Buffer Interface ---------- */ 00186 00187 /* It's zlib-like interface. 00188 See LzmaDec_DecodeToDic description for information about STEPS and return results, 00189 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need 00190 to work with CLzmaDec variables manually. 00191 00192 finishMode: 00193 It has meaning only if the decoding reaches output limit (*destLen). 00194 LZMA_FINISH_ANY - Decode just destLen bytes. 00195 LZMA_FINISH_END - Stream must be finished after (*destLen). 00196 */ 00197 00198 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, 00199 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 00200 00201 00202 /* ---------- One Call Interface ---------- */ 00203 00204 /* LzmaDecode 00205 00206 finishMode: 00207 It has meaning only if the decoding reaches output limit (*destLen). 00208 LZMA_FINISH_ANY - Decode just destLen bytes. 00209 LZMA_FINISH_END - Stream must be finished after (*destLen). 00210 00211 Returns: 00212 SZ_OK 00213 status: 00214 LZMA_STATUS_FINISHED_WITH_MARK 00215 LZMA_STATUS_NOT_FINISHED 00216 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 00217 SZ_ERROR_DATA - Data error 00218 SZ_ERROR_MEM - Memory allocation error 00219 SZ_ERROR_UNSUPPORTED - Unsupported properties 00220 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). 00221 */ 00222 00223 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 00224 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, 00225 ELzmaStatus *status, ISzAlloc *alloc); 00226 00227 #ifdef __cplusplus 00228 } 00229 #endif 00230 00231 #endif