sysendian.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*-
  2. * Copyright 2007-2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. #ifndef _SYSENDIAN_H_
  30. #define _SYSENDIAN_H_
  31. #include "scrypt_platform.h"
  32. #ifdef HAVE_SYS_ENDIAN_H
  33. #include <sys/endian.h>
  34. #else
  35. #include <stdint.h>
  36. static inline uint32_t
  37. be32dec(const void *pp)
  38. {
  39. const uint8_t *p = (uint8_t const *)pp;
  40. return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
  41. ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
  42. }
  43. static inline void
  44. be32enc(void *pp, uint32_t x)
  45. {
  46. uint8_t * p = (uint8_t *)pp;
  47. p[3] = x & 0xff;
  48. p[2] = (x >> 8) & 0xff;
  49. p[1] = (x >> 16) & 0xff;
  50. p[0] = (x >> 24) & 0xff;
  51. }
  52. static inline uint64_t
  53. be64dec(const void *pp)
  54. {
  55. const uint8_t *p = (uint8_t const *)pp;
  56. return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
  57. ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
  58. ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
  59. ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
  60. }
  61. static inline void
  62. be64enc(void *pp, uint64_t x)
  63. {
  64. uint8_t * p = (uint8_t *)pp;
  65. p[7] = x & 0xff;
  66. p[6] = (x >> 8) & 0xff;
  67. p[5] = (x >> 16) & 0xff;
  68. p[4] = (x >> 24) & 0xff;
  69. p[3] = (x >> 32) & 0xff;
  70. p[2] = (x >> 40) & 0xff;
  71. p[1] = (x >> 48) & 0xff;
  72. p[0] = (x >> 56) & 0xff;
  73. }
  74. static inline uint32_t
  75. le32dec(const void *pp)
  76. {
  77. const uint8_t *p = (uint8_t const *)pp;
  78. return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
  79. ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
  80. }
  81. static inline void
  82. le32enc(void *pp, uint32_t x)
  83. {
  84. uint8_t * p = (uint8_t *)pp;
  85. p[0] = x & 0xff;
  86. p[1] = (x >> 8) & 0xff;
  87. p[2] = (x >> 16) & 0xff;
  88. p[3] = (x >> 24) & 0xff;
  89. }
  90. static inline uint64_t
  91. le64dec(const void *pp)
  92. {
  93. const uint8_t *p = (uint8_t const *)pp;
  94. return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
  95. ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
  96. ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
  97. ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
  98. }
  99. static inline void
  100. le64enc(void *pp, uint64_t x)
  101. {
  102. uint8_t * p = (uint8_t *)pp;
  103. p[0] = x & 0xff;
  104. p[1] = (x >> 8) & 0xff;
  105. p[2] = (x >> 16) & 0xff;
  106. p[3] = (x >> 24) & 0xff;
  107. p[4] = (x >> 32) & 0xff;
  108. p[5] = (x >> 40) & 0xff;
  109. p[6] = (x >> 48) & 0xff;
  110. p[7] = (x >> 56) & 0xff;
  111. }
  112. #endif /* !HAVE_SYS_ENDIAN_H */
  113. #endif /* !_SYSENDIAN_H_ */