armthumb.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file armthumb.c
  4. /// \brief Filter for ARM-Thumb 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. armthumb_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 += 2) {
  21. if ((buffer[i + 1] & 0xF8) == 0xF0
  22. && (buffer[i + 3] & 0xF8) == 0xF8) {
  23. uint32_t src = (((uint32_t)(buffer[i + 1]) & 7) << 19)
  24. | ((uint32_t)(buffer[i + 0]) << 11)
  25. | (((uint32_t)(buffer[i + 3]) & 7) << 8)
  26. | (uint32_t)(buffer[i + 2]);
  27. src <<= 1;
  28. uint32_t dest;
  29. if (is_encoder)
  30. dest = now_pos + (uint32_t)(i) + 4 + src;
  31. else
  32. dest = src - (now_pos + (uint32_t)(i) + 4);
  33. dest >>= 1;
  34. buffer[i + 1] = 0xF0 | ((dest >> 19) & 0x7);
  35. buffer[i + 0] = (dest >> 11);
  36. buffer[i + 3] = 0xF8 | ((dest >> 8) & 0x7);
  37. buffer[i + 2] = (dest);
  38. i += 2;
  39. }
  40. }
  41. return i;
  42. }
  43. static lzma_ret
  44. armthumb_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  45. const lzma_filter_info *filters, bool is_encoder)
  46. {
  47. return lzma_simple_coder_init(next, allocator, filters,
  48. &armthumb_code, 0, 4, 2, is_encoder);
  49. }
  50. #ifdef HAVE_ENCODER_ARMTHUMB
  51. extern lzma_ret
  52. lzma_simple_armthumb_encoder_init(lzma_next_coder *next,
  53. const lzma_allocator *allocator,
  54. const lzma_filter_info *filters)
  55. {
  56. return armthumb_coder_init(next, allocator, filters, true);
  57. }
  58. #endif
  59. #ifdef HAVE_DECODER_ARMTHUMB
  60. extern lzma_ret
  61. lzma_simple_armthumb_decoder_init(lzma_next_coder *next,
  62. const lzma_allocator *allocator,
  63. const lzma_filter_info *filters)
  64. {
  65. return armthumb_coder_init(next, allocator, filters, false);
  66. }
  67. #endif