powerpc.c 1.9 KB

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