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