ia64.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file ia64.c
  4. /// \brief Filter for IA64 (Itanium) binaries
  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. static size_t
  15. ia64_code(lzma_simple *simple lzma_attribute((__unused__)),
  16. uint32_t now_pos, bool is_encoder,
  17. uint8_t *buffer, size_t size)
  18. {
  19. static const uint32_t BRANCH_TABLE[32] = {
  20. 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0,
  22. 4, 4, 6, 6, 0, 0, 7, 7,
  23. 4, 4, 0, 0, 4, 4, 0, 0
  24. };
  25. size_t i;
  26. for (i = 0; i + 16 <= size; i += 16) {
  27. size_t slot;
  28. const uint32_t instr_template = buffer[i] & 0x1F;
  29. const uint32_t mask = BRANCH_TABLE[instr_template];
  30. uint32_t bit_pos = 5;
  31. for (slot = 0; slot < 3; ++slot, bit_pos += 41) {
  32. const size_t byte_pos = (bit_pos >> 3);
  33. const uint32_t bit_res = bit_pos & 0x7;
  34. uint64_t instruction = 0;
  35. uint64_t inst_norm;
  36. size_t j;
  37. if (((mask >> slot) & 1) == 0)
  38. continue;
  39. for (j = 0; j < 6; ++j)
  40. instruction += (uint64_t)(
  41. buffer[i + j + byte_pos])
  42. << (8 * j);
  43. inst_norm = instruction >> bit_res;
  44. if (((inst_norm >> 37) & 0xF) == 0x5
  45. && ((inst_norm >> 9) & 0x7) == 0
  46. /* && (inst_norm & 0x3F)== 0 */
  47. ) {
  48. uint32_t dest;
  49. size_t j;
  50. uint32_t src = (uint32_t)(
  51. (inst_norm >> 13) & 0xFFFFF);
  52. src |= ((inst_norm >> 36) & 1) << 20;
  53. src <<= 4;
  54. if (is_encoder)
  55. dest = now_pos + (uint32_t)(i) + src;
  56. else
  57. dest = src - (now_pos + (uint32_t)(i));
  58. dest >>= 4;
  59. inst_norm &= ~((uint64_t)(0x8FFFFF) << 13);
  60. inst_norm |= (uint64_t)(dest & 0xFFFFF) << 13;
  61. inst_norm |= (uint64_t)(dest & 0x100000)
  62. << (36 - 20);
  63. instruction &= (1 << bit_res) - 1;
  64. instruction |= (inst_norm << bit_res);
  65. for (j = 0; j < 6; j++)
  66. buffer[i + j + byte_pos] = (uint8_t)(
  67. instruction
  68. >> (8 * j));
  69. }
  70. }
  71. }
  72. return i;
  73. }
  74. static lzma_ret
  75. ia64_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
  76. const lzma_filter_info *filters, bool is_encoder)
  77. {
  78. return lzma_simple_coder_init(next, allocator, filters,
  79. &ia64_code, 0, 16, 16, is_encoder);
  80. }
  81. extern lzma_ret
  82. lzma_simple_ia64_encoder_init(lzma_next_coder *next,
  83. lzma_allocator *allocator, const lzma_filter_info *filters)
  84. {
  85. return ia64_coder_init(next, allocator, filters, true);
  86. }
  87. extern lzma_ret
  88. lzma_simple_ia64_decoder_init(lzma_next_coder *next,
  89. lzma_allocator *allocator, const lzma_filter_info *filters)
  90. {
  91. return ia64_coder_init(next, allocator, filters, false);
  92. }