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