sparc.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file sparc.c
  4. /// \brief Filter for SPARC 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. sparc_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. if ((buffer[i] == 0x40 && (buffer[i + 1] & 0xC0) == 0x00)
  22. || (buffer[i] == 0x7F
  23. && (buffer[i + 1] & 0xC0) == 0xC0)) {
  24. uint32_t src = ((uint32_t)buffer[i + 0] << 24)
  25. | ((uint32_t)buffer[i + 1] << 16)
  26. | ((uint32_t)buffer[i + 2] << 8)
  27. | ((uint32_t)buffer[i + 3]);
  28. src <<= 2;
  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. dest >>= 2;
  35. dest = (((0 - ((dest >> 22) & 1)) << 22) & 0x3FFFFFFF)
  36. | (dest & 0x3FFFFF)
  37. | 0x40000000;
  38. buffer[i + 0] = (uint8_t)(dest >> 24);
  39. buffer[i + 1] = (uint8_t)(dest >> 16);
  40. buffer[i + 2] = (uint8_t)(dest >> 8);
  41. buffer[i + 3] = (uint8_t)(dest);
  42. }
  43. }
  44. return i;
  45. }
  46. static lzma_ret
  47. sparc_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  48. const lzma_filter_info *filters, bool is_encoder)
  49. {
  50. return lzma_simple_coder_init(next, allocator, filters,
  51. &sparc_code, 0, 4, 4, is_encoder);
  52. }
  53. #ifdef HAVE_ENCODER_SPARC
  54. extern lzma_ret
  55. lzma_simple_sparc_encoder_init(lzma_next_coder *next,
  56. const lzma_allocator *allocator,
  57. const lzma_filter_info *filters)
  58. {
  59. return sparc_coder_init(next, allocator, filters, true);
  60. }
  61. #endif
  62. #ifdef HAVE_DECODER_SPARC
  63. extern lzma_ret
  64. lzma_simple_sparc_decoder_init(lzma_next_coder *next,
  65. const lzma_allocator *allocator,
  66. const lzma_filter_info *filters)
  67. {
  68. return sparc_coder_init(next, allocator, filters, false);
  69. }
  70. #endif