File indexing completed on 2025-02-02 04:25:59
0001 /* LzmaDec.h -- LZMA Decoder 0002 2013-01-18 : Igor Pavlov : Public domain */ 0003 0004 #ifndef __LZMA_DEC_H 0005 #define __LZMA_DEC_H 0006 0007 #include "7zTypes.h" 0008 0009 EXTERN_C_BEGIN 0010 0011 /* #define _LZMA_PROB32 */ 0012 /* _LZMA_PROB32 can increase the speed on some CPUs, 0013 but memory usage for CLzmaDec::probs will be doubled in that case */ 0014 0015 #ifdef _LZMA_PROB32 0016 #define CLzmaProb UInt32 0017 #else 0018 #define CLzmaProb UInt16 0019 #endif 0020 0021 0022 /* ---------- LZMA Properties ---------- */ 0023 0024 #define LZMA_PROPS_SIZE 5 0025 0026 typedef struct _CLzmaProps 0027 { 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 0041 /* ---------- LZMA Decoder state ---------- */ 0042 0043 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. 0044 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ 0045 0046 #define LZMA_REQUIRED_INPUT_MAX 20 0047 0048 typedef struct 0049 { 0050 CLzmaProps prop; 0051 CLzmaProb *probs; 0052 Byte *dic; 0053 const Byte *buf; 0054 UInt32 range, code; 0055 SizeT dicPos; 0056 SizeT dicBufSize; 0057 UInt32 processedPos; 0058 UInt32 checkDicSize; 0059 unsigned state; 0060 UInt32 reps[4]; 0061 unsigned remainLen; 0062 int needFlush; 0063 int needInitState; 0064 UInt32 numProbs; 0065 unsigned tempBufSize; 0066 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; 0067 } CLzmaDec; 0068 0069 #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } 0070 0071 void LzmaDec_Init(CLzmaDec *p); 0072 0073 /* There are two types of LZMA streams: 0074 0) Stream with end mark. That end mark adds about 6 bytes to compressed size. 0075 1) Stream without end mark. You must know exact uncompressed size to decompress such stream. */ 0076 0077 typedef enum 0078 { 0079 LZMA_FINISH_ANY, /* finish at any point */ 0080 LZMA_FINISH_END /* block must be finished at the end */ 0081 } ELzmaFinishMode; 0082 0083 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! 0084 0085 You must use LZMA_FINISH_END, when you know that current output buffer 0086 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. 0087 0088 If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, 0089 and output value of destLen will be less than output buffer size limit. 0090 You can check status result also. 0091 0092 You can use multiple checks to test data integrity after full decompression: 0093 1) Check Result and "status" variable. 0094 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. 0095 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. 0096 You must use correct finish mode in that case. */ 0097 0098 typedef enum 0099 { 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 0110 /* ---------- Interfaces ---------- */ 0111 0112 /* There are 3 levels of interfaces: 0113 1) Dictionary Interface 0114 2) Buffer Interface 0115 3) One Call Interface 0116 You can select any of these interfaces, but don't mix functions from different 0117 groups for same object. */ 0118 0119 0120 /* There are two variants to allocate state for Dictionary Interface: 0121 1) LzmaDec_Allocate / LzmaDec_Free 0122 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs 0123 You can use variant 2, if you set dictionary buffer manually. 0124 For Buffer Interface you must always use variant 1. 0125 0126 LzmaDec_Allocate* can return: 0127 SZ_OK 0128 SZ_ERROR_MEM - Memory allocation error 0129 SZ_ERROR_UNSUPPORTED - Unsupported properties 0130 */ 0131 0132 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc); 0133 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc); 0134 0135 SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISzAlloc *alloc); 0136 void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); 0137 0138 /* ---------- Dictionary Interface ---------- */ 0139 0140 /* You can use it, if you want to eliminate the overhead for data copying from 0141 dictionary to some other external buffer. 0142 You must work with CLzmaDec variables directly in this interface. 0143 0144 STEPS: 0145 LzmaDec_Constr() 0146 LzmaDec_Allocate() 0147 for (each new stream) 0148 { 0149 LzmaDec_Init() 0150 while (it needs more decompression) 0151 { 0152 LzmaDec_DecodeToDic() 0153 use data from CLzmaDec::dic and update CLzmaDec::dicPos 0154 } 0155 } 0156 LzmaDec_Free() 0157 */ 0158 0159 /* LzmaDec_DecodeToDic 0160 0161 The decoding to internal dictionary buffer (CLzmaDec::dic). 0162 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! 0163 0164 finishMode: 0165 It has meaning only if the decoding reaches output limit (dicLimit). 0166 LZMA_FINISH_ANY - Decode just dicLimit bytes. 0167 LZMA_FINISH_END - Stream must be finished after dicLimit. 0168 0169 Returns: 0170 SZ_OK 0171 status: 0172 LZMA_STATUS_FINISHED_WITH_MARK 0173 LZMA_STATUS_NOT_FINISHED 0174 LZMA_STATUS_NEEDS_MORE_INPUT 0175 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 0176 SZ_ERROR_DATA - Data error 0177 */ 0178 0179 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, 0180 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 0181 0182 0183 /* ---------- Buffer Interface ---------- */ 0184 0185 /* It's zlib-like interface. 0186 See LzmaDec_DecodeToDic description for information about STEPS and return results, 0187 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need 0188 to work with CLzmaDec variables manually. 0189 0190 finishMode: 0191 It has meaning only if the decoding reaches output limit (*destLen). 0192 LZMA_FINISH_ANY - Decode just destLen bytes. 0193 LZMA_FINISH_END - Stream must be finished after (*destLen). 0194 */ 0195 0196 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, 0197 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status); 0198 0199 0200 /* ---------- One Call Interface ---------- */ 0201 0202 /* LzmaDecode 0203 0204 finishMode: 0205 It has meaning only if the decoding reaches output limit (*destLen). 0206 LZMA_FINISH_ANY - Decode just destLen bytes. 0207 LZMA_FINISH_END - Stream must be finished after (*destLen). 0208 0209 Returns: 0210 SZ_OK 0211 status: 0212 LZMA_STATUS_FINISHED_WITH_MARK 0213 LZMA_STATUS_NOT_FINISHED 0214 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK 0215 SZ_ERROR_DATA - Data error 0216 SZ_ERROR_MEM - Memory allocation error 0217 SZ_ERROR_UNSUPPORTED - Unsupported properties 0218 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). 0219 */ 0220 0221 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, 0222 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, 0223 ELzmaStatus *status, ISzAlloc *alloc); 0224 0225 EXTERN_C_END 0226 0227 #endif