lha_endian.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "lha_endian.h"
  17. uint16_t lha_decode_uint16(uint8_t *buf)
  18. {
  19. return (uint16_t) (buf[0] | (buf[1] << 8));
  20. }
  21. uint32_t lha_decode_uint32(uint8_t *buf)
  22. {
  23. return ((uint32_t) buf[0])
  24. | ((uint32_t) buf[1] << 8)
  25. | ((uint32_t) buf[2] << 16)
  26. | ((uint32_t) buf[3] << 24);
  27. }
  28. uint64_t lha_decode_uint64(uint8_t *buf)
  29. {
  30. return ((uint64_t) buf[0])
  31. | ((uint64_t) buf[1] << 8)
  32. | ((uint64_t) buf[2] << 16)
  33. | ((uint64_t) buf[3] << 24)
  34. | ((uint64_t) buf[4] << 32)
  35. | ((uint64_t) buf[5] << 40)
  36. | ((uint64_t) buf[6] << 48)
  37. | ((uint64_t) buf[7] << 56);
  38. }
  39. uint16_t lha_decode_be_uint16(uint8_t *buf)
  40. {
  41. return (uint16_t) ((buf[0] << 8) | buf[1]);
  42. }
  43. uint32_t lha_decode_be_uint32(uint8_t *buf)
  44. {
  45. return ((uint32_t) buf[0] << 24)
  46. | ((uint32_t) buf[1] << 16)
  47. | ((uint32_t) buf[2] << 8)
  48. | ((uint32_t) buf[3]);
  49. }