powerpc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file powerpc.c
  4. /// \brief Filter for PowerPC (big endian) 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. powerpc_code(void *simple lzma_attribute((__unused__)),
  16. uint32_t now_pos, bool is_encoder,
  17. uint8_t *buffer, size_t size)
  18. {
  19. size_t i;
  20. for (i = 0; i + 4 <= size; i += 4) {
  21. // PowerPC branch 6(48) 24(Offset) 1(Abs) 1(Link)
  22. if ((buffer[i] >> 2) == 0x12
  23. && ((buffer[i + 3] & 3) == 1)) {
  24. const uint32_t src
  25. = (((uint32_t)(buffer[i + 0]) & 3) << 24)
  26. | ((uint32_t)(buffer[i + 1]) << 16)
  27. | ((uint32_t)(buffer[i + 2]) << 8)
  28. | ((uint32_t)(buffer[i + 3]) & ~UINT32_C(3));
  29. uint32_t dest;
  30. if (is_encoder)
  31. dest = now_pos + (uint32_t)(i) + src;
  32. else
  33. dest = src - (now_pos + (uint32_t)(i));
  34. buffer[i + 0] = 0x48 | ((dest >> 24) & 0x03);
  35. buffer[i + 1] = (dest >> 16);
  36. buffer[i + 2] = (dest >> 8);
  37. buffer[i + 3] &= 0x03;
  38. buffer[i + 3] |= dest;
  39. }
  40. }
  41. return i;
  42. }
  43. static lzma_ret
  44. powerpc_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  45. const lzma_filter_info *filters, bool is_encoder)
  46. {
  47. return lzma_simple_coder_init(next, allocator, filters,
  48. &powerpc_code, 0, 4, 4, is_encoder);
  49. }
  50. #ifdef HAVE_ENCODER_POWERPC
  51. extern lzma_ret
  52. lzma_simple_powerpc_encoder_init(lzma_next_coder *next,
  53. const lzma_allocator *allocator,
  54. const lzma_filter_info *filters)
  55. {
  56. return powerpc_coder_init(next, allocator, filters, true);
  57. }
  58. #endif
  59. #ifdef HAVE_DECODER_POWERPC
  60. extern lzma_ret
  61. lzma_simple_powerpc_decoder_init(lzma_next_coder *next,
  62. const lzma_allocator *allocator,
  63. const lzma_filter_info *filters)
  64. {
  65. return powerpc_coder_init(next, allocator, filters, false);
  66. }
  67. #endif