Bcj2.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Bcj2.h -- BCJ2 Converter for x86 code
  2. 2014-11-10 : Igor Pavlov : Public domain */
  3. #ifndef __BCJ2_H
  4. #define __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_OK
  23. };
  24. enum
  25. {
  26. BCJ2_ENC_STATE_ORIG = BCJ2_NUM_STREAMS,
  27. BCJ2_ENC_STATE_OK
  28. };
  29. #define BCJ2_IS_32BIT_STREAM(s) ((s) == BCJ2_STREAM_CALL || (s) == BCJ2_STREAM_JUMP)
  30. /*
  31. CBcj2Dec / CBcj2Enc
  32. bufs sizes:
  33. BUF_SIZE(n) = lims[n] - bufs[n]
  34. bufs sizes for BCJ2_STREAM_CALL and BCJ2_STREAM_JUMP must be mutliply of 4:
  35. (BUF_SIZE(BCJ2_STREAM_CALL) & 3) == 0
  36. (BUF_SIZE(BCJ2_STREAM_JUMP) & 3) == 0
  37. */
  38. /*
  39. CBcj2Dec:
  40. dest is allowed to overlap with bufs[BCJ2_STREAM_MAIN], with the following conditions:
  41. bufs[BCJ2_STREAM_MAIN] >= dest &&
  42. bufs[BCJ2_STREAM_MAIN] - dest >= tempReserv +
  43. BUF_SIZE(BCJ2_STREAM_CALL) +
  44. BUF_SIZE(BCJ2_STREAM_JUMP)
  45. tempReserv = 0 : for first call of Bcj2Dec_Decode
  46. tempReserv = 4 : for any other calls of Bcj2Dec_Decode
  47. overlap with offset = 1 is not allowed
  48. */
  49. typedef struct
  50. {
  51. const Byte *bufs[BCJ2_NUM_STREAMS];
  52. const Byte *lims[BCJ2_NUM_STREAMS];
  53. Byte *dest;
  54. const Byte *destLim;
  55. unsigned state; /* BCJ2_STREAM_MAIN has more priority than BCJ2_STATE_ORIG */
  56. UInt32 ip;
  57. Byte temp[4];
  58. UInt32 range;
  59. UInt32 code;
  60. UInt16 probs[2 + 256];
  61. } CBcj2Dec;
  62. void Bcj2Dec_Init(CBcj2Dec *p);
  63. /* Returns: SZ_OK or SZ_ERROR_DATA */
  64. SRes Bcj2Dec_Decode(CBcj2Dec *p);
  65. #define Bcj2Dec_IsFinished(_p_) ((_p_)->code == 0)
  66. typedef enum
  67. {
  68. BCJ2_ENC_FINISH_MODE_CONTINUE,
  69. BCJ2_ENC_FINISH_MODE_END_BLOCK,
  70. BCJ2_ENC_FINISH_MODE_END_STREAM
  71. } EBcj2Enc_FinishMode;
  72. typedef struct
  73. {
  74. Byte *bufs[BCJ2_NUM_STREAMS];
  75. const Byte *lims[BCJ2_NUM_STREAMS];
  76. const Byte *src;
  77. const Byte *srcLim;
  78. unsigned state;
  79. EBcj2Enc_FinishMode finishMode;
  80. Byte prevByte;
  81. Byte cache;
  82. UInt32 range;
  83. UInt64 low;
  84. UInt64 cacheSize;
  85. UInt32 ip;
  86. /* 32-bit ralative offset in JUMP/CALL commands is
  87. - (mod 4 GB) in 32-bit mode
  88. - signed Int32 in 64-bit mode
  89. We use (mod 4 GB) check for fileSize.
  90. Use fileSize up to 2 GB, if you want to support 32-bit and 64-bit code conversion. */
  91. UInt32 fileIp;
  92. UInt32 fileSize; /* (fileSize <= ((UInt32)1 << 31)), 0 means no_limit */
  93. UInt32 relatLimit; /* (relatLimit <= ((UInt32)1 << 31)), 0 means desable_conversion */
  94. UInt32 tempTarget;
  95. unsigned tempPos;
  96. Byte temp[4 * 2];
  97. unsigned flushPos;
  98. UInt16 probs[2 + 256];
  99. } CBcj2Enc;
  100. void Bcj2Enc_Init(CBcj2Enc *p);
  101. void Bcj2Enc_Encode(CBcj2Enc *p);
  102. #define Bcj2Enc_Get_InputData_Size(p) ((SizeT)((p)->srcLim - (p)->src) + (p)->tempPos)
  103. #define Bcj2Enc_IsFinished(p) ((p)->flushPos == 5)
  104. #define BCJ2_RELAT_LIMIT_NUM_BITS 26
  105. #define BCJ2_RELAT_LIMIT ((UInt32)1 << BCJ2_RELAT_LIMIT_NUM_BITS)
  106. /* limit for CBcj2Enc::fileSize variable */
  107. #define BCJ2_FileSize_MAX ((UInt32)1 << 31)
  108. EXTERN_C_END
  109. #endif