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