Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 7c9bb41 | 2014-01-29 16:55:48 -0600 | [diff] [blame] | 9 | typedef uint16_t CLzmaProb; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 10 | |
| 11 | /* ---------- LZMA Properties ---------- */ |
| 12 | |
| 13 | #define LZMA_PROPS_SIZE 5 |
| 14 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 15 | struct CLzmaProps |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 16 | { |
| 17 | unsigned lc, lp, pb; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 18 | uint32_t dicSize; |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 19 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 20 | |
| 21 | /* LzmaProps_Decode - decodes properties |
| 22 | Returns: |
| 23 | SZ_OK |
| 24 | SZ_ERROR_UNSUPPORTED - Unsupported properties |
| 25 | */ |
| 26 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 27 | SRes LzmaProps_Decode(struct CLzmaProps *p, const uint8_t *data, unsigned size); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 37 | struct CLzmaDec |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 38 | { |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 39 | struct CLzmaProps prop; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 40 | CLzmaProb *probs; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 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; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 48 | unsigned state; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 49 | uint32_t reps[4]; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 50 | unsigned remainLen; |
| 51 | int needFlush; |
| 52 | int needInitState; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 53 | uint32_t numProbs; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 54 | unsigned tempBufSize; |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 55 | uint8_t tempBuf[LZMA_REQUIRED_INPUT_MAX]; |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 56 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 57 | |
| 58 | #define LzmaDec_Construct(p) { (p)->dic = 0; (p)->probs = 0; } |
| 59 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 60 | void LzmaDec_Init(struct CLzmaDec *p); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 66 | enum ELzmaFinishMode |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 67 | { |
| 68 | LZMA_FINISH_ANY, /* finish at any point */ |
| 69 | LZMA_FINISH_END /* block must be finished at the end */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 70 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 87 | enum ELzmaStatus |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 */ |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 94 | }; |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | */ |
Kyösti Mälkki | ecd8424 | 2013-09-13 07:57:49 +0300 | [diff] [blame] | 120 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 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); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 123 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 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); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 |
Kyösti Mälkki | ecd8424 | 2013-09-13 07:57:49 +0300 | [diff] [blame] | 149 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 168 | SRes LzmaDec_DecodeToDic(struct CLzmaDec *p, size_t dicLimit, |
| 169 | const uint8_t *src, size_t *srcLen, enum ELzmaFinishMode finishMode, enum ELzmaStatus *status); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 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); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 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 | |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 210 | SRes LzmaDecode(uint8_t *dest, size_t *destLen, const uint8_t *src, size_t *srcLen, |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 211 | const uint8_t *propData, unsigned propSize, enum ELzmaFinishMode finishMode, |
| 212 | enum ELzmaStatus *status, struct ISzAlloc *alloc); |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 213 | |
Stefan Reinauer | 2e200cd | 2012-10-30 14:02:45 -0700 | [diff] [blame] | 214 | #endif |