LzmaDec.h 7.5 KB

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