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