x86.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file x86.c
  4. /// \brief Filter for x86 binaries (BCJ filter)
  5. ///
  6. // Authors: Igor Pavlov
  7. // Lasse Collin
  8. //
  9. // This file has been put into the public domain.
  10. // You can do whatever you want with this file.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "simple_private.h"
  14. #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
  15. typedef struct {
  16. uint32_t prev_mask;
  17. uint32_t prev_pos;
  18. } lzma_simple_x86;
  19. static size_t
  20. x86_code(void *simple_ptr, uint32_t now_pos, bool is_encoder,
  21. uint8_t *buffer, size_t size)
  22. {
  23. static const bool MASK_TO_ALLOWED_STATUS[8]
  24. = { true, true, true, false, true, false, false, false };
  25. static const uint32_t MASK_TO_BIT_NUMBER[8]
  26. = { 0, 1, 2, 2, 3, 3, 3, 3 };
  27. lzma_simple_x86 *simple = simple_ptr;
  28. uint32_t prev_mask = simple->prev_mask;
  29. uint32_t prev_pos = simple->prev_pos;
  30. if (size < 5)
  31. return 0;
  32. if (now_pos - prev_pos > 5)
  33. prev_pos = now_pos - 5;
  34. const size_t limit = size - 5;
  35. size_t buffer_pos = 0;
  36. while (buffer_pos <= limit) {
  37. uint8_t b = buffer[buffer_pos];
  38. if (b != 0xE8 && b != 0xE9) {
  39. ++buffer_pos;
  40. continue;
  41. }
  42. const uint32_t offset = now_pos + (uint32_t)(buffer_pos)
  43. - prev_pos;
  44. prev_pos = now_pos + (uint32_t)(buffer_pos);
  45. if (offset > 5) {
  46. prev_mask = 0;
  47. } else {
  48. for (uint32_t i = 0; i < offset; ++i) {
  49. prev_mask &= 0x77;
  50. prev_mask <<= 1;
  51. }
  52. }
  53. b = buffer[buffer_pos + 4];
  54. if (Test86MSByte(b)
  55. && MASK_TO_ALLOWED_STATUS[(prev_mask >> 1) & 0x7]
  56. && (prev_mask >> 1) < 0x10) {
  57. uint32_t src = ((uint32_t)(b) << 24)
  58. | ((uint32_t)(buffer[buffer_pos + 3]) << 16)
  59. | ((uint32_t)(buffer[buffer_pos + 2]) << 8)
  60. | (buffer[buffer_pos + 1]);
  61. uint32_t dest;
  62. while (true) {
  63. if (is_encoder)
  64. dest = src + (now_pos + (uint32_t)(
  65. buffer_pos) + 5);
  66. else
  67. dest = src - (now_pos + (uint32_t)(
  68. buffer_pos) + 5);
  69. if (prev_mask == 0)
  70. break;
  71. const uint32_t i = MASK_TO_BIT_NUMBER[
  72. prev_mask >> 1];
  73. b = (uint8_t)(dest >> (24 - i * 8));
  74. if (!Test86MSByte(b))
  75. break;
  76. src = dest ^ ((1U << (32 - i * 8)) - 1);
  77. }
  78. buffer[buffer_pos + 4]
  79. = (uint8_t)(~(((dest >> 24) & 1) - 1));
  80. buffer[buffer_pos + 3] = (uint8_t)(dest >> 16);
  81. buffer[buffer_pos + 2] = (uint8_t)(dest >> 8);
  82. buffer[buffer_pos + 1] = (uint8_t)(dest);
  83. buffer_pos += 5;
  84. prev_mask = 0;
  85. } else {
  86. ++buffer_pos;
  87. prev_mask |= 1;
  88. if (Test86MSByte(b))
  89. prev_mask |= 0x10;
  90. }
  91. }
  92. simple->prev_mask = prev_mask;
  93. simple->prev_pos = prev_pos;
  94. return buffer_pos;
  95. }
  96. static lzma_ret
  97. x86_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  98. const lzma_filter_info *filters, bool is_encoder)
  99. {
  100. const lzma_ret ret = lzma_simple_coder_init(next, allocator, filters,
  101. &x86_code, sizeof(lzma_simple_x86), 5, 1, is_encoder);
  102. if (ret == LZMA_OK) {
  103. lzma_simple_coder *coder = next->coder;
  104. lzma_simple_x86 *simple = coder->simple;
  105. simple->prev_mask = 0;
  106. simple->prev_pos = (uint32_t)(-5);
  107. }
  108. return ret;
  109. }
  110. #ifdef HAVE_ENCODER_X86
  111. extern lzma_ret
  112. lzma_simple_x86_encoder_init(lzma_next_coder *next,
  113. const lzma_allocator *allocator,
  114. const lzma_filter_info *filters)
  115. {
  116. return x86_coder_init(next, allocator, filters, true);
  117. }
  118. #endif
  119. #ifdef HAVE_DECODER_X86
  120. extern lzma_ret
  121. lzma_simple_x86_decoder_init(lzma_next_coder *next,
  122. const lzma_allocator *allocator,
  123. const lzma_filter_info *filters)
  124. {
  125. return x86_coder_init(next, allocator, filters, false);
  126. }
  127. #endif