powerpc.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(lzma_simple *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 = ((buffer[i + 0] & 3) << 24)
  25. | (buffer[i + 1] << 16)
  26. | (buffer[i + 2] << 8)
  27. | (buffer[i + 3] & (~3));
  28. uint32_t dest;
  29. if (is_encoder)
  30. dest = now_pos + (uint32_t)(i) + src;
  31. else
  32. dest = src - (now_pos + (uint32_t)(i));
  33. buffer[i + 0] = 0x48 | ((dest >> 24) & 0x03);
  34. buffer[i + 1] = (dest >> 16);
  35. buffer[i + 2] = (dest >> 8);
  36. buffer[i + 3] &= 0x03;
  37. buffer[i + 3] |= dest;
  38. }
  39. }
  40. return i;
  41. }
  42. static lzma_ret
  43. powerpc_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
  44. const lzma_filter_info *filters, bool is_encoder)
  45. {
  46. return lzma_simple_coder_init(next, allocator, filters,
  47. &powerpc_code, 0, 4, 4, is_encoder);
  48. }
  49. extern lzma_ret
  50. lzma_simple_powerpc_encoder_init(lzma_next_coder *next,
  51. lzma_allocator *allocator, const lzma_filter_info *filters)
  52. {
  53. return powerpc_coder_init(next, allocator, filters, true);
  54. }
  55. extern lzma_ret
  56. lzma_simple_powerpc_decoder_init(lzma_next_coder *next,
  57. lzma_allocator *allocator, const lzma_filter_info *filters)
  58. {
  59. return powerpc_coder_init(next, allocator, filters, false);
  60. }