Bcj2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Bcj2.h -- BCJ2 converter for x86 code (Branch CALL/JUMP variant2)
  2. 2023-03-02 : Igor Pavlov : Public domain */
  3. #ifndef ZIP7_INC_BCJ2_H
  4. #define ZIP7_INC_BCJ2_H
  5. #include "7zTypes.h"
  6. EXTERN_C_BEGIN
  7. #define BCJ2_NUM_STREAMS 4
  8. enum
  9. {
  10. BCJ2_STREAM_MAIN,
  11. BCJ2_STREAM_CALL,
  12. BCJ2_STREAM_JUMP,
  13. BCJ2_STREAM_RC
  14. };
  15. enum
  16. {
  17. BCJ2_DEC_STATE_ORIG_0 = BCJ2_NUM_STREAMS,
  18. BCJ2_DEC_STATE_ORIG_1,
  19. BCJ2_DEC_STATE_ORIG_2,
  20. BCJ2_DEC_STATE_ORIG_3,
  21. BCJ2_DEC_STATE_ORIG,
  22. BCJ2_DEC_STATE_ERROR /* after detected data error */
  23. };
  24. enum
  25. {
  26. BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS,
  27. BCJ2_ENC_STATE_FINISHED /* it's state after fully encoded stream */
  28. };
  29. /* #define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP) */
  30. #define BCJ2_IS_32BIT_STREAM(s) ((unsigned)((unsigned)(s) - (unsigned)BCJ2_STREAM_CALL) < 2)
  31. /*
  32. CBcj2Dec / CBcj2Enc
  33. bufs sizes:
  34. BUF_SIZE(n) = lims[n] - bufs[n]
  35. bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be multiply of 4:
  36. (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0
  37. (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0
  38. */
  39. // typedef UInt32 CBcj2Prob;
  40. typedef UInt16 CBcj2Prob;
  41. /*
  42. BCJ2 encoder / decoder internal requirements:
  43. - If last bytes of stream contain marker (e8/e8/0f8x), then
  44. there is also encoded symbol (0 : no conversion) in RC stream.
  45. - One case of overlapped instructions is supported,
  46. if last byte of converted instruction is (0f) and next byte is (8x):
  47. marker [xx xx xx 0f] 8x
  48. then the pair (0f 8x) is treated as marker.
  49. */
  50. /* ---------- BCJ2 Decoder ---------- */
  51. /*
  52. CBcj2Dec:
  53. (dest) is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions:
  54. bufs[BCJ2_STREAM_MAIN] >= dest &&
  55. bufs[BCJ2_STREAM_MAIN] - dest >=
  56. BUF_SIZE(BCJ2_STREAM_CALL) +
  57. BUF_SIZE(BCJ2_STREAM_JUMP)
  58. reserve = bufs[BCJ2_STREAM_MAIN] - dest -
  59. ( BUF_SIZE(BCJ2_STREAM_CALL) +
  60. BUF_SIZE(BCJ2_STREAM_JUMP) )
  61. and additional conditions:
  62. if (it's first call of Bcj2Dec_Decode() after Bcj2Dec_Init())
  63. {
  64. (reserve != 1) : if (ver < v23.00)
  65. }
  66. else // if there are more than one calls of Bcj2Dec_Decode() after Bcj2Dec_Init())
  67. {
  68. (reserve >= 6) : if (ver < v23.00)
  69. (reserve >= 4) : if (ver >= v23.00)
  70. We need that (reserve) because after first call of Bcj2Dec_Decode(),
  71. CBcj2Dec::temp can contain up to 4 bytes for writing to (dest).
  72. }
  73. (reserve == 0) is allowed, if we decode full stream via single call of Bcj2Dec_Decode().
  74. (reserve == 0) also is allowed in case of multi-call, if we use fixed buffers,
  75. and (reserve) is calculated from full (final) sizes of all streams before first call.
  76. */
  77. typedef struct
  78. {
  79. const Byte *bufs[BCJ2_NUM_STREAMS];
  80. const Byte *lims[BCJ2_NUM_STREAMS];
  81. Byte *dest;
  82. const Byte *destLim;
  83. unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
  84. UInt32 ip; /* property of starting base for decoding */
  85. UInt32 temp; /* Byte temp[4]; */
  86. UInt32 range;
  87. UInt32 code;
  88. CBcj2Prob probs[2 + 256];
  89. } CBcj2Dec;
  90. /* Note:
  91. Bcj2Dec_Init() sets (CBcj2Dec::ip = 0)
  92. if (ip != 0) property is required, the caller must set CBcj2Dec::ip after Bcj2Dec_Init()
  93. */
  94. void Bcj2Dec_Init(CBcj2Dec *p);
  95. /* Bcj2Dec_Decode():
  96. returns:
  97. SZ_OK
  98. SZ_ERROR_DATA : if data in 5 starting bytes of BCJ2_STREAM_RC stream are not correct
  99. */
  100. SRes Bcj2Dec_Decode(CBcj2Dec *p);
  101. /* To check that decoding was finished you can compare
  102. sizes of processed streams with sizes known from another sources.
  103. You must do at least one mandatory check from the two following options:
  104. - the check for size of processed output (ORIG) stream.
  105. - the check for size of processed input (MAIN) stream.
  106. additional optional checks:
  107. - the checks for processed sizes of all input streams (MAIN, CALL, JUMP, RC)
  108. - the checks Bcj2Dec_IsMaybeFinished*()
  109. also before actual decoding you can check that the
  110. following condition is met for stream sizes:
  111. ( size(ORIG) == size(MAIN) + size(CALL) + size(JUMP) )
  112. */
  113. /* (state == BCJ2_STREAM_MAIN) means that decoder is ready for
  114. additional input data in BCJ2_STREAM_MAIN stream.
  115. Note that (state == BCJ2_STREAM_MAIN) is allowed for non-finished decoding.
  116. */
  117. #define Bcj2Dec_IsMaybeFinished_state_MAIN(_p_) ((_p_)->state == BCJ2_STREAM_MAIN)
  118. /* if the stream decoding was finished correctly, then range decoder
  119. part of CBcj2Dec also was finished, and then (CBcj2Dec::code == 0).
  120. Note that (CBcj2Dec::code == 0) is allowed for non-finished decoding.
  121. */
  122. #define Bcj2Dec_IsMaybeFinished_code(_p_) ((_p_)->code == 0)
  123. /* use Bcj2Dec_IsMaybeFinished() only as additional check
  124. after at least one mandatory check from the two following options:
  125. - the check for size of processed output (ORIG) stream.
  126. - the check for size of processed input (MAIN) stream.
  127. */
  128. #define Bcj2Dec_IsMaybeFinished(_p_) ( \
  129. Bcj2Dec_IsMaybeFinished_state_MAIN(_p_) && \
  130. Bcj2Dec_IsMaybeFinished_code(_p_))
  131. /* ---------- BCJ2 Encoder ---------- */
  132. typedef enum
  133. {
  134. BCJ2_ENC_FINISH_MODE_CONTINUE,
  135. BCJ2_ENC_FINISH_MODE_END_BLOCK,
  136. BCJ2_ENC_FINISH_MODE_END_STREAM
  137. } EBcj2Enc_FinishMode;
  138. /*
  139. BCJ2_ENC_FINISH_MODE_CONTINUE:
  140. process non finished encoding.
  141. It notifies the encoder that additional further calls
  142. can provide more input data (src) than provided by current call.
  143. In that case the CBcj2Enc encoder still can move (src) pointer
  144. up to (srcLim), but CBcj2Enc encoder can store some of the last
  145. processed bytes (up to 4 bytes) from src to internal CBcj2Enc::temp[] buffer.
  146. at return:
  147. (CBcj2Enc::src will point to position that includes
  148. processed data and data copied to (temp[]) buffer)
  149. That data from (temp[]) buffer will be used in further calls.
  150. BCJ2_ENC_FINISH_MODE_END_BLOCK:
  151. finish encoding of current block (ended at srcLim) without RC flushing.
  152. at return: if (CBcj2Enc::state == BCJ2_ENC_STATE_ORIG) &&
  153. CBcj2Enc::src == CBcj2Enc::srcLim)
  154. : it shows that block encoding was finished. And the encoder is
  155. ready for new (src) data or for stream finish operation.
  156. finished block means
  157. {
  158. CBcj2Enc has completed block encoding up to (srcLim).
  159. (1 + 4 bytes) or (2 + 4 bytes) CALL/JUMP cortages will
  160. not cross block boundary at (srcLim).
  161. temporary CBcj2Enc buffer for (ORIG) src data is empty.
  162. 3 output uncompressed streams (MAIN, CALL, JUMP) were flushed.
  163. RC stream was not flushed. And RC stream will cross block boundary.
  164. }
  165. Note: some possible implementation of BCJ2 encoder could
  166. write branch marker (e8/e8/0f8x) in one call of Bcj2Enc_Encode(),
  167. and it could calculate symbol for RC in another call of Bcj2Enc_Encode().
  168. BCJ2 encoder uses ip/fileIp/fileSize/relatLimit values to calculate RC symbol.
  169. And these CBcj2Enc variables can have different values in different Bcj2Enc_Encode() calls.
  170. So caller must finish each block with BCJ2_ENC_FINISH_MODE_END_BLOCK
  171. to ensure that RC symbol is calculated and written in proper block.
  172. BCJ2_ENC_FINISH_MODE_END_STREAM
  173. finish encoding of stream (ended at srcLim) fully including RC flushing.
  174. at return: if (CBcj2Enc::state == BCJ2_ENC_STATE_FINISHED)
  175. : it shows that stream encoding was finished fully,
  176. and all output streams were flushed fully.
  177. also Bcj2Enc_IsFinished() can be called.
  178. */
  179. /*
  180. 32-bit relative offset in JUMP/CALL commands is
  181. - (mod 4 GiB) for 32-bit x86 code
  182. - signed Int32 for 64-bit x86-64 code
  183. BCJ2 encoder also does internal relative to absolute address conversions.
  184. And there are 2 possible ways to do it:
  185. before v23: we used 32-bit variables and (mod 4 GiB) conversion
  186. since v23: we use 64-bit variables and (signed Int32 offset) conversion.
  187. The absolute address condition for conversion in v23:
  188. ((UInt64)((Int64)ip64 - (Int64)fileIp64 + 5 + (Int32)offset) < (UInt64)fileSize64)
  189. note that if (fileSize64 > 2 GiB). there is difference between
  190. old (mod 4 GiB) way (v22) and new (signed Int32 offset) way (v23).
  191. And new (v23) way is more suitable to encode 64-bit x86-64 code for (fileSize64 > 2 GiB) cases.
  192. */
  193. /*
  194. // for old (v22) way for conversion:
  195. typedef UInt32 CBcj2Enc_ip_unsigned;
  196. typedef Int32 CBcj2Enc_ip_signed;
  197. #define BCJ2_ENC_FileSize_MAX ((UInt32)1 << 31)
  198. */
  199. typedef UInt64 CBcj2Enc_ip_unsigned;
  200. typedef Int64 CBcj2Enc_ip_signed;
  201. /* maximum size of file that can be used for conversion condition */
  202. #define BCJ2_ENC_FileSize_MAX ((CBcj2Enc_ip_unsigned)0 - 2)
  203. /* default value of fileSize64_minus1 variable that means
  204. that absolute address limitation will not be used */
  205. #define BCJ2_ENC_FileSizeField_UNLIMITED ((CBcj2Enc_ip_unsigned)0 - 1)
  206. /* calculate value that later can be set to CBcj2Enc::fileSize64_minus1 */
  207. #define BCJ2_ENC_GET_FileSizeField_VAL_FROM_FileSize(fileSize) \
  208. ((CBcj2Enc_ip_unsigned)(fileSize) - 1)
  209. /* set CBcj2Enc::fileSize64_minus1 variable from size of file */
  210. #define Bcj2Enc_SET_FileSize(p, fileSize) \
  211. (p)->fileSize64_minus1 = BCJ2_ENC_GET_FileSizeField_VAL_FROM_FileSize(fileSize);
  212. typedef struct
  213. {
  214. Byte *bufs[BCJ2_NUM_STREAMS];
  215. const Byte *lims[BCJ2_NUM_STREAMS];
  216. const Byte *src;
  217. const Byte *srcLim;
  218. unsigned state;
  219. EBcj2Enc_FinishMode finishMode;
  220. Byte context;
  221. Byte flushRem;
  222. Byte isFlushState;
  223. Byte cache;
  224. UInt32 range;
  225. UInt64 low;
  226. UInt64 cacheSize;
  227. // UInt32 context; // for marker version, it can include marker flag.
  228. /* (ip64) and (fileIp64) correspond to virtual source stream position
  229. that doesn't include data in temp[] */
  230. CBcj2Enc_ip_unsigned ip64; /* current (ip) position */
  231. CBcj2Enc_ip_unsigned fileIp64; /* start (ip) position of current file */
  232. CBcj2Enc_ip_unsigned fileSize64_minus1; /* size of current file (for conversion limitation) */
  233. UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)) : 0 means disable_conversion */
  234. // UInt32 relatExcludeBits;
  235. UInt32 tempTarget;
  236. unsigned tempPos; /* the number of bytes that were copied to temp[] buffer
  237. (tempPos <= 4) outside of Bcj2Enc_Encode() */
  238. // Byte temp[4]; // for marker version
  239. Byte temp[8];
  240. CBcj2Prob probs[2 + 256];
  241. } CBcj2Enc;
  242. void Bcj2Enc_Init(CBcj2Enc *p);
  243. /*
  244. Bcj2Enc_Encode(): at exit:
  245. p->State < BCJ2_NUM_STREAMS : we need more buffer space for output stream
  246. (bufs[p->State] == lims[p->State])
  247. p->State == BCJ2_ENC_STATE_ORIG : we need more data in input src stream
  248. (src == srcLim)
  249. p->State == BCJ2_ENC_STATE_FINISHED : after fully encoded stream
  250. */
  251. void Bcj2Enc_Encode(CBcj2Enc *p);
  252. /* Bcj2Enc encoder can look ahead for up 4 bytes of source stream.
  253. CBcj2Enc::tempPos : is the number of bytes that were copied from input stream to temp[] buffer.
  254. (CBcj2Enc::src) after Bcj2Enc_Encode() is starting position after
  255. fully processed data and after data copied to temp buffer.
  256. So if the caller needs to get real number of fully processed input
  257. bytes (without look ahead data in temp buffer),
  258. the caller must subtruct (CBcj2Enc::tempPos) value from processed size
  259. value that is calculated based on current (CBcj2Enc::src):
  260. cur_processed_pos = Calc_Big_Processed_Pos(enc.src)) -
  261. Bcj2Enc_Get_AvailInputSize_in_Temp(&enc);
  262. */
  263. /* get the size of input data that was stored in temp[] buffer: */
  264. #define Bcj2Enc_Get_AvailInputSize_in_Temp(p) ((p)->tempPos)
  265. #define Bcj2Enc_IsFinished(p) ((p)->flushRem == 0)
  266. /* Note : the decoder supports overlapping of marker (0f 80).
  267. But we can eliminate such overlapping cases by setting
  268. the limit for relative offset conversion as
  269. CBcj2Enc::relatLimit <= (0x0f << 24) == (240 MiB)
  270. */
  271. /* default value for CBcj2Enc::relatLimit */
  272. #define BCJ2_ENC_RELAT_LIMIT_DEFAULT ((UInt32)0x0f << 24)
  273. #define BCJ2_ENC_RELAT_LIMIT_MAX ((UInt32)1 << 31)
  274. // #define BCJ2_RELAT_EXCLUDE_NUM_BITS 5
  275. EXTERN_C_END
  276. #endif