LzmaDec.h 6.7 KB

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