x86.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. struct lzma_simple_s {
  16. uint32_t prev_mask;
  17. uint32_t prev_pos;
  18. };
  19. static size_t
  20. x86_code(lzma_simple *simple, 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. uint32_t prev_mask = simple->prev_mask;
  28. uint32_t prev_pos = simple->prev_pos;
  29. size_t limit;
  30. size_t buffer_pos;
  31. if (size < 5)
  32. return 0;
  33. if (now_pos - prev_pos > 5)
  34. prev_pos = now_pos - 5;
  35. limit = size - 5;
  36. buffer_pos = 0;
  37. while (buffer_pos <= limit) {
  38. uint32_t offset;
  39. uint32_t i;
  40. uint8_t b = buffer[buffer_pos];
  41. if (b != 0xE8 && b != 0xE9) {
  42. ++buffer_pos;
  43. continue;
  44. }
  45. offset = now_pos + (uint32_t)(buffer_pos)
  46. - prev_pos;
  47. prev_pos = now_pos + (uint32_t)(buffer_pos);
  48. if (offset > 5) {
  49. prev_mask = 0;
  50. } else {
  51. for (i = 0; i < offset; ++i) {
  52. prev_mask &= 0x77;
  53. prev_mask <<= 1;
  54. }
  55. }
  56. b = buffer[buffer_pos + 4];
  57. if (Test86MSByte(b)
  58. && MASK_TO_ALLOWED_STATUS[(prev_mask >> 1) & 0x7]
  59. && (prev_mask >> 1) < 0x10) {
  60. uint32_t src = ((uint32_t)(b) << 24)
  61. | ((uint32_t)(buffer[buffer_pos + 3]) << 16)
  62. | ((uint32_t)(buffer[buffer_pos + 2]) << 8)
  63. | (buffer[buffer_pos + 1]);
  64. uint32_t dest;
  65. while (true) {
  66. uint32_t i;
  67. if (is_encoder)
  68. dest = src + (now_pos + (uint32_t)(
  69. buffer_pos) + 5);
  70. else
  71. dest = src - (now_pos + (uint32_t)(
  72. buffer_pos) + 5);
  73. if (prev_mask == 0)
  74. break;
  75. i = MASK_TO_BIT_NUMBER[prev_mask >> 1];
  76. b = (uint8_t)(dest >> (24 - i * 8));
  77. if (!Test86MSByte(b))
  78. break;
  79. src = dest ^ ((1u << (32 - i * 8)) - 1);
  80. }
  81. buffer[buffer_pos + 4]
  82. = (uint8_t)(~(((dest >> 24) & 1) - 1));
  83. buffer[buffer_pos + 3] = (uint8_t)(dest >> 16);
  84. buffer[buffer_pos + 2] = (uint8_t)(dest >> 8);
  85. buffer[buffer_pos + 1] = (uint8_t)(dest);
  86. buffer_pos += 5;
  87. prev_mask = 0;
  88. } else {
  89. ++buffer_pos;
  90. prev_mask |= 1;
  91. if (Test86MSByte(b))
  92. prev_mask |= 0x10;
  93. }
  94. }
  95. simple->prev_mask = prev_mask;
  96. simple->prev_pos = prev_pos;
  97. return buffer_pos;
  98. }
  99. static lzma_ret
  100. x86_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
  101. const lzma_filter_info *filters, bool is_encoder)
  102. {
  103. const lzma_ret ret = lzma_simple_coder_init(next, allocator, filters,
  104. &x86_code, sizeof(lzma_simple), 5, 1, is_encoder);
  105. if (ret == LZMA_OK) {
  106. next->coder->simple->prev_mask = 0;
  107. next->coder->simple->prev_pos = (uint32_t)(-5);
  108. }
  109. return ret;
  110. }
  111. extern lzma_ret
  112. lzma_simple_x86_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
  113. const lzma_filter_info *filters)
  114. {
  115. return x86_coder_init(next, allocator, filters, true);
  116. }
  117. extern lzma_ret
  118. lzma_simple_x86_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
  119. const lzma_filter_info *filters)
  120. {
  121. return x86_coder_init(next, allocator, filters, false);
  122. }