powerpc.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = ((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, const 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. const lzma_allocator *allocator,
  52. const lzma_filter_info *filters)
  53. {
  54. return powerpc_coder_init(next, allocator, filters, true);
  55. }
  56. extern lzma_ret
  57. lzma_simple_powerpc_decoder_init(lzma_next_coder *next,
  58. const lzma_allocator *allocator,
  59. const lzma_filter_info *filters)
  60. {
  61. return powerpc_coder_init(next, allocator, filters, false);
  62. }