LzmaDec.h 7.0 KB

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