x86.c 3.3 KB

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