byteswap.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. byteswap.h - functions and macros for byte swapping.
  3. Copyright (C) 1995 - 2001 Michael Riepe
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  15. */
  16. /* @(#) $Id: byteswap.h,v 1.7 2008/05/23 08:15:34 michael Exp $ */
  17. #ifndef _BYTESWAP_H
  18. #define _BYTESWAP_H
  19. #define lu(from,i,s) (((__libelf_u32_t)((unsigned char*)(from))[i])<<(s))
  20. #define li(from,i,s) (((__libelf_i32_t)(( signed char*)(from))[i])<<(s))
  21. #define __load_u16L(from) ((__libelf_u32_t) \
  22. (lu(from,1,8) | lu(from,0,0)))
  23. #define __load_u16M(from) ((__libelf_u32_t) \
  24. (lu(from,0,8) | lu(from,1,0)))
  25. #define __load_i16L(from) ((__libelf_i32_t) \
  26. (li(from,1,8) | lu(from,0,0)))
  27. #define __load_i16M(from) ((__libelf_i32_t) \
  28. (li(from,0,8) | lu(from,1,0)))
  29. #define __load_u32L(from) ((__libelf_u32_t) \
  30. (lu(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0)))
  31. #define __load_u32M(from) ((__libelf_u32_t) \
  32. (lu(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0)))
  33. #define __load_i32L(from) ((__libelf_i32_t) \
  34. (li(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0)))
  35. #define __load_i32M(from) ((__libelf_i32_t) \
  36. (li(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0)))
  37. #define su(to,i,v,s) (((char*)(to))[i]=((__libelf_u32_t)(v)>>(s)))
  38. #define si(to,i,v,s) (((char*)(to))[i]=((__libelf_i32_t)(v)>>(s)))
  39. #define __store_u16L(to,v) \
  40. (su(to,1,v,8), su(to,0,v,0))
  41. #define __store_u16M(to,v) \
  42. (su(to,0,v,8), su(to,1,v,0))
  43. #define __store_i16L(to,v) \
  44. (si(to,1,v,8), si(to,0,v,0))
  45. #define __store_i16M(to,v) \
  46. (si(to,0,v,8), si(to,1,v,0))
  47. #define __store_u32L(to,v) \
  48. (su(to,3,v,24), su(to,2,v,16), su(to,1,v,8), su(to,0,v,0))
  49. #define __store_u32M(to,v) \
  50. (su(to,0,v,24), su(to,1,v,16), su(to,2,v,8), su(to,3,v,0))
  51. #define __store_i32L(to,v) \
  52. (si(to,3,v,24), si(to,2,v,16), si(to,1,v,8), si(to,0,v,0))
  53. #define __store_i32M(to,v) \
  54. (si(to,0,v,24), si(to,1,v,16), si(to,2,v,8), si(to,3,v,0))
  55. #if __LIBELF64
  56. /*
  57. * conversion functions from swap64.c
  58. */
  59. extern __libelf_u64_t _elf_load_u64L(const unsigned char *from);
  60. extern __libelf_u64_t _elf_load_u64M(const unsigned char *from);
  61. extern __libelf_i64_t _elf_load_i64L(const unsigned char *from);
  62. extern __libelf_i64_t _elf_load_i64M(const unsigned char *from);
  63. extern void _elf_store_u64L(unsigned char *to, __libelf_u64_t v);
  64. extern void _elf_store_u64M(unsigned char *to, __libelf_u64_t v);
  65. extern void _elf_store_i64L(unsigned char *to, __libelf_u64_t v);
  66. extern void _elf_store_i64M(unsigned char *to, __libelf_u64_t v);
  67. /*
  68. * aliases for existing conversion code
  69. */
  70. #define __load_u64L _elf_load_u64L
  71. #define __load_u64M _elf_load_u64M
  72. #define __load_i64L _elf_load_i64L
  73. #define __load_i64M _elf_load_i64M
  74. #define __store_u64L _elf_store_u64L
  75. #define __store_u64M _elf_store_u64M
  76. #define __store_i64L _elf_store_i64L
  77. #define __store_i64M _elf_store_i64M
  78. #endif /* __LIBELF64 */
  79. #endif /* _BYTESWAP_H */