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