bytes.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_BYTES_H
  6. #define TOR_BYTES_H
  7. #include <stdlib.h>
  8. #include "lib/cc/torint.h"
  9. /* The uint8 variants are defined to make the code more uniform. */
  10. static inline uint8_t
  11. get_uint8(const void *cp)
  12. {
  13. return *(const uint8_t*)(cp);
  14. }
  15. static inline void
  16. set_uint8(void *cp, uint8_t v)
  17. {
  18. *(uint8_t*)cp = v;
  19. }
  20. /**
  21. * Read a 16-bit value beginning at <b>cp</b>. Equivalent to
  22. * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
  23. * unaligned memory access.
  24. */
  25. static inline uint16_t
  26. get_uint16(const void *cp)
  27. {
  28. uint16_t v;
  29. memcpy(&v,cp,2);
  30. return v;
  31. }
  32. /**
  33. * Read a 32-bit value beginning at <b>cp</b>. Equivalent to
  34. * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
  35. * unaligned memory access.
  36. */
  37. static inline uint32_t
  38. get_uint32(const void *cp)
  39. {
  40. uint32_t v;
  41. memcpy(&v,cp,4);
  42. return v;
  43. }
  44. /**
  45. * Read a 64-bit value beginning at <b>cp</b>. Equivalent to
  46. * *(uint64_t*)(cp), but will not cause segfaults on platforms that forbid
  47. * unaligned memory access.
  48. */
  49. static inline uint64_t
  50. get_uint64(const void *cp)
  51. {
  52. uint64_t v;
  53. memcpy(&v,cp,8);
  54. return v;
  55. }
  56. /**
  57. * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
  58. * *(uint16_t*)(cp) = v, but will not cause segfaults on platforms that forbid
  59. * unaligned memory access. */
  60. static inline void
  61. set_uint16(void *cp, uint16_t v)
  62. {
  63. memcpy(cp,&v,2);
  64. }
  65. /**
  66. * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
  67. * *(uint32_t*)(cp) = v, but will not cause segfaults on platforms that forbid
  68. * unaligned memory access. */
  69. static inline void
  70. set_uint32(void *cp, uint32_t v)
  71. {
  72. memcpy(cp,&v,4);
  73. }
  74. /**
  75. * Set a 64-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
  76. * *(uint64_t*)(cp) = v, but will not cause segfaults on platforms that forbid
  77. * unaligned memory access. */
  78. static inline void
  79. set_uint64(void *cp, uint64_t v)
  80. {
  81. memcpy(cp,&v,8);
  82. }
  83. #ifdef WORDS_BIGENDIAN
  84. static inline uint32_t
  85. tor_htonl(uint32_t a)
  86. {
  87. return a;
  88. }
  89. static inline uint32_t
  90. tor_ntohl(uint64_t a)
  91. {
  92. return a;
  93. }
  94. static inline uint64_t
  95. tor_htonll(uint64_t a)
  96. {
  97. return a;
  98. }
  99. static inline uint64_t
  100. tor_ntohll(uint64_t a)
  101. {
  102. return a;
  103. }
  104. #else
  105. static inline uint32_t
  106. tor_htonl(uint32_t a)
  107. {
  108. /* Our compilers will indeed recognize this as bswap. */
  109. return
  110. ((a & 0x000000ff) <<24) |
  111. ((a & 0x0000ff00) << 8) |
  112. ((a & 0x00ff0000) >> 8) |
  113. ((a & 0xff000000) >>24);
  114. }
  115. static inline uint32_t
  116. tor_ntohl(uint32_t a)
  117. {
  118. return tor_htonl(a);
  119. }
  120. /** Return a uint64_t value from <b>a</b> in network byte order. */
  121. static inline uint64_t
  122. tor_htonll(uint64_t a)
  123. {
  124. /* Little endian. The worst... */
  125. return tor_htonl((uint32_t)(a>>32)) |
  126. (((uint64_t)tor_htonl((uint32_t)a))<<32);
  127. }
  128. /** Return a uint64_t value from <b>a</b> in host byte order. */
  129. static inline uint64_t
  130. tor_ntohll(uint64_t a)
  131. {
  132. return tor_htonll(a);
  133. }
  134. #endif
  135. #endif