LzmaDec.h 6.6 KB

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