armthumb.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file armthumb.c
  5. /// \brief Filter for ARM-Thumb binaries
  6. ///
  7. // Authors: Igor Pavlov
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "simple_private.h"
  12. static size_t
  13. armthumb_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 += 2) {
  19. if ((buffer[i + 1] & 0xF8) == 0xF0
  20. && (buffer[i + 3] & 0xF8) == 0xF8) {
  21. uint32_t src = (((uint32_t)(buffer[i + 1]) & 7) << 19)
  22. | ((uint32_t)(buffer[i + 0]) << 11)
  23. | (((uint32_t)(buffer[i + 3]) & 7) << 8)
  24. | (uint32_t)(buffer[i + 2]);
  25. src <<= 1;
  26. uint32_t dest;
  27. if (is_encoder)
  28. dest = now_pos + (uint32_t)(i) + 4 + src;
  29. else
  30. dest = src - (now_pos + (uint32_t)(i) + 4);
  31. dest >>= 1;
  32. buffer[i + 1] = 0xF0 | ((dest >> 19) & 0x7);
  33. buffer[i + 0] = (dest >> 11);
  34. buffer[i + 3] = 0xF8 | ((dest >> 8) & 0x7);
  35. buffer[i + 2] = (dest);
  36. i += 2;
  37. }
  38. }
  39. return i;
  40. }
  41. static lzma_ret
  42. armthumb_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. &armthumb_code, 0, 4, 2, is_encoder);
  47. }
  48. #ifdef HAVE_ENCODER_ARMTHUMB
  49. extern lzma_ret
  50. lzma_simple_armthumb_encoder_init(lzma_next_coder *next,
  51. const lzma_allocator *allocator,
  52. const lzma_filter_info *filters)
  53. {
  54. return armthumb_coder_init(next, allocator, filters, true);
  55. }
  56. #endif
  57. #ifdef HAVE_DECODER_ARMTHUMB
  58. extern lzma_ret
  59. lzma_simple_armthumb_decoder_init(lzma_next_coder *next,
  60. const lzma_allocator *allocator,
  61. const lzma_filter_info *filters)
  62. {
  63. return armthumb_coder_init(next, allocator, filters, false);
  64. }
  65. #endif