OLD | NEW |
(Empty) | |
| 1 /* LzmaDec.h -- LZMA Decoder |
| 2 2009-02-07 : Igor Pavlov : Public domain */ |
| 3 |
| 4 #ifndef __LZMA_DEC_H |
| 5 #define __LZMA_DEC_H |
| 6 |
| 7 #include "Types.h" |
| 8 |
| 9 namespace ots { |
| 10 namespace lzma { |
| 11 |
| 12 /* #define _LZMA_PROB32 */ |
| 13 /* _LZMA_PROB32 can increase the speed on some CPUs, |
| 14 but memory usage for CLzmaDec::probs will be doubled in that case */ |
| 15 |
| 16 #ifdef _LZMA_PROB32 |
| 17 #define CLzmaProb UInt32 |
| 18 #else |
| 19 #define CLzmaProb UInt16 |
| 20 #endif |
| 21 |
| 22 |
| 23 /* ---------- LZMA Properties ---------- */ |
| 24 |
| 25 #define LZMA_PROPS_SIZE 5 |
| 26 |
| 27 typedef struct _CLzmaProps |
| 28 { |
| 29 unsigned lc, lp, pb; |
| 30 UInt32 dicSize; |
| 31 } CLzmaProps; |
| 32 |
| 33 /* LzmaProps_Decode - decodes properties |
| 34 Returns: |
| 35 SZ_OK |
| 36 SZ_ERROR_UNSUPPORTED - Unsupported properties |
| 37 */ |
| 38 |
| 39 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size); |
| 40 |
| 41 |
| 42 /* ---------- LZMA Decoder state ---------- */ |
| 43 |
| 44 /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. |
| 45 Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ |
| 46 |
| 47 #define LZMA_REQUIRED_INPUT_MAX 20 |
| 48 |
| 49 typedef struct |
| 50 { |
| 51 CLzmaProps prop; |
| 52 CLzmaProb *probs; |
| 53 Byte *dic; |
| 54 const Byte *buf; |
| 55 UInt32 range, code; |
| 56 SizeT dicPos; |
| 57 SizeT dicBufSize; |
| 58 UInt32 processedPos; |
| 59 UInt32 checkDicSize; |
| 60 unsigned state; |
| 61 UInt32 reps[4]; |
| 62 unsigned remainLen; |
| 63 int needFlush; |
| 64 int needInitState; |
| 65 UInt32 numProbs; |
| 66 unsigned tempBufSize; |
| 67 Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; |
| 68 } CLzmaDec; |
| 69 |
| 70 #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } |
| 71 |
| 72 void LzmaDec_Init(CLzmaDec *p); |
| 73 |
| 74 /* There are two types of LZMA streams: |
| 75 0) Stream with end mark. That end mark adds about 6 bytes to compressed siz
e. |
| 76 1) Stream without end mark. You must know exact uncompressed size to decomp
ress such stream. */ |
| 77 |
| 78 typedef enum |
| 79 { |
| 80 LZMA_FINISH_ANY, /* finish at any point */ |
| 81 LZMA_FINISH_END /* block must be finished at the end */ |
| 82 } ELzmaFinishMode; |
| 83 |
| 84 /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! |
| 85 |
| 86 You must use LZMA_FINISH_END, when you know that current output buffer |
| 87 covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. |
| 88 |
| 89 If LZMA decoder sees end marker before reaching output limit, it returns SZ_O
K, |
| 90 and output value of destLen will be less than output buffer size limit. |
| 91 You can check status result also. |
| 92 |
| 93 You can use multiple checks to test data integrity after full decompression: |
| 94 1) Check Result and "status" variable. |
| 95 2) Check that output(destLen) = uncompressedSize, if you know real uncompre
ssedSize. |
| 96 3) Check that output(srcLen) = compressedSize, if you know real compressedS
ize. |
| 97 You must use correct finish mode in that case. */ |
| 98 |
| 99 typedef enum |
| 100 { |
| 101 LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ |
| 102 LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark.
*/ |
| 103 LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ |
| 104 LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes
*/ |
| 105 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream w
as finished without end mark */ |
| 106 } ELzmaStatus; |
| 107 |
| 108 /* ELzmaStatus is used only as output value for function call */ |
| 109 |
| 110 |
| 111 /* ---------- Interfaces ---------- */ |
| 112 |
| 113 /* There are 3 levels of interfaces: |
| 114 1) Dictionary Interface |
| 115 2) Buffer Interface |
| 116 3) One Call Interface |
| 117 You can select any of these interfaces, but don't mix functions from differen
t |
| 118 groups for same object. */ |
| 119 |
| 120 |
| 121 /* There are two variants to allocate state for Dictionary Interface: |
| 122 1) LzmaDec_Allocate / LzmaDec_Free |
| 123 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs |
| 124 You can use variant 2, if you set dictionary buffer manually. |
| 125 For Buffer Interface you must always use variant 1. |
| 126 |
| 127 LzmaDec_Allocate* can return: |
| 128 SZ_OK |
| 129 SZ_ERROR_MEM - Memory allocation error |
| 130 SZ_ERROR_UNSUPPORTED - Unsupported properties |
| 131 */ |
| 132 |
| 133 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, I
SzAlloc *alloc); |
| 134 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc); |
| 135 |
| 136 SRes LzmaDec_Allocate(CLzmaDec *state, const Byte *prop, unsigned propsSize, ISz
Alloc *alloc); |
| 137 void LzmaDec_Free(CLzmaDec *state, ISzAlloc *alloc); |
| 138 |
| 139 /* ---------- Dictionary Interface ---------- */ |
| 140 |
| 141 /* You can use it, if you want to eliminate the overhead for data copying from |
| 142 dictionary to some other external buffer. |
| 143 You must work with CLzmaDec variables directly in this interface. |
| 144 |
| 145 STEPS: |
| 146 LzmaDec_Constr() |
| 147 LzmaDec_Allocate() |
| 148 for (each new stream) |
| 149 { |
| 150 LzmaDec_Init() |
| 151 while (it needs more decompression) |
| 152 { |
| 153 LzmaDec_DecodeToDic() |
| 154 use data from CLzmaDec::dic and update CLzmaDec::dicPos |
| 155 } |
| 156 } |
| 157 LzmaDec_Free() |
| 158 */ |
| 159 |
| 160 /* LzmaDec_DecodeToDic |
| 161 |
| 162 The decoding to internal dictionary buffer (CLzmaDec::dic). |
| 163 You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize
!!! |
| 164 |
| 165 finishMode: |
| 166 It has meaning only if the decoding reaches output limit (dicLimit). |
| 167 LZMA_FINISH_ANY - Decode just dicLimit bytes. |
| 168 LZMA_FINISH_END - Stream must be finished after dicLimit. |
| 169 |
| 170 Returns: |
| 171 SZ_OK |
| 172 status: |
| 173 LZMA_STATUS_FINISHED_WITH_MARK |
| 174 LZMA_STATUS_NOT_FINISHED |
| 175 LZMA_STATUS_NEEDS_MORE_INPUT |
| 176 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK |
| 177 SZ_ERROR_DATA - Data error |
| 178 */ |
| 179 |
| 180 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, |
| 181 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *sta
tus); |
| 182 |
| 183 |
| 184 /* ---------- Buffer Interface ---------- */ |
| 185 |
| 186 /* It's zlib-like interface. |
| 187 See LzmaDec_DecodeToDic description for information about STEPS and return re
sults, |
| 188 but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you d
on't need |
| 189 to work with CLzmaDec variables manually. |
| 190 |
| 191 finishMode: |
| 192 It has meaning only if the decoding reaches output limit (*destLen). |
| 193 LZMA_FINISH_ANY - Decode just destLen bytes. |
| 194 LZMA_FINISH_END - Stream must be finished after (*destLen). |
| 195 */ |
| 196 |
| 197 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, |
| 198 const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *sta
tus); |
| 199 |
| 200 |
| 201 /* ---------- One Call Interface ---------- */ |
| 202 |
| 203 /* LzmaDecode |
| 204 |
| 205 finishMode: |
| 206 It has meaning only if the decoding reaches output limit (*destLen). |
| 207 LZMA_FINISH_ANY - Decode just destLen bytes. |
| 208 LZMA_FINISH_END - Stream must be finished after (*destLen). |
| 209 |
| 210 Returns: |
| 211 SZ_OK |
| 212 status: |
| 213 LZMA_STATUS_FINISHED_WITH_MARK |
| 214 LZMA_STATUS_NOT_FINISHED |
| 215 LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK |
| 216 SZ_ERROR_DATA - Data error |
| 217 SZ_ERROR_MEM - Memory allocation error |
| 218 SZ_ERROR_UNSUPPORTED - Unsupported properties |
| 219 SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). |
| 220 */ |
| 221 |
| 222 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, |
| 223 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode, |
| 224 ELzmaStatus *status, ISzAlloc *alloc); |
| 225 |
| 226 } // namespace lzma |
| 227 } // namespace ots |
| 228 |
| 229 #endif |
OLD | NEW |