archive_endian.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*-
  2. * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
  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. * $FreeBSD: head/lib/libarchive/archive_endian.h 201085 2009-12-28 02:17:15Z kientzle $
  27. *
  28. * Borrowed from FreeBSD's <sys/endian.h>
  29. */
  30. #ifndef __LIBARCHIVE_BUILD
  31. #error This header is only to be used internally to libarchive.
  32. #endif
  33. /* Note: This is a purely internal header! */
  34. /* Do not use this outside of libarchive internal code! */
  35. #ifndef ARCHIVE_ENDIAN_H_INCLUDED
  36. #define ARCHIVE_ENDIAN_H_INCLUDED
  37. /*
  38. * Disabling inline keyword for compilers known to choke on it:
  39. * - Watcom C++ in C code. (For any version?)
  40. * - SGI MIPSpro
  41. * - Microsoft Visual C++ 6.0 (supposedly newer versions too)
  42. * - IBM VisualAge 6 (XL v6)
  43. * - Sun WorkShop C (SunPro) before 5.9
  44. */
  45. #if defined(__WATCOMC__) || defined(__sgi) || defined(__hpux) || defined(__BORLANDC__)
  46. #define inline
  47. #elif defined(__IBMC__) && __IBMC__ < 700
  48. #define inline
  49. #elif defined(__SUNPRO_C) && __SUNPRO_C < 0x590
  50. #define inline
  51. #elif defined(_MSC_VER) || defined(__osf__)
  52. #define inline __inline
  53. #endif
  54. /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
  55. static inline uint16_t
  56. archive_be16dec(const void *pp)
  57. {
  58. unsigned char const *p = (unsigned char const *)pp;
  59. /* Store into unsigned temporaries before left shifting, to avoid
  60. promotion to signed int and then left shifting into the sign bit,
  61. which is undefined behaviour. */
  62. unsigned int p1 = p[1];
  63. unsigned int p0 = p[0];
  64. return ((p0 << 8) | p1);
  65. }
  66. static inline uint32_t
  67. archive_be32dec(const void *pp)
  68. {
  69. unsigned char const *p = (unsigned char const *)pp;
  70. /* Store into unsigned temporaries before left shifting, to avoid
  71. promotion to signed int and then left shifting into the sign bit,
  72. which is undefined behaviour. */
  73. unsigned int p3 = p[3];
  74. unsigned int p2 = p[2];
  75. unsigned int p1 = p[1];
  76. unsigned int p0 = p[0];
  77. return ((p0 << 24) | (p1 << 16) | (p2 << 8) | p3);
  78. }
  79. static inline uint64_t
  80. archive_be64dec(const void *pp)
  81. {
  82. unsigned char const *p = (unsigned char const *)pp;
  83. return (((uint64_t)archive_be32dec(p) << 32) | archive_be32dec(p + 4));
  84. }
  85. static inline uint16_t
  86. archive_le16dec(const void *pp)
  87. {
  88. unsigned char const *p = (unsigned char const *)pp;
  89. /* Store into unsigned temporaries before left shifting, to avoid
  90. promotion to signed int and then left shifting into the sign bit,
  91. which is undefined behaviour. */
  92. unsigned int p1 = p[1];
  93. unsigned int p0 = p[0];
  94. return ((p1 << 8) | p0);
  95. }
  96. static inline uint32_t
  97. archive_le32dec(const void *pp)
  98. {
  99. unsigned char const *p = (unsigned char const *)pp;
  100. /* Store into unsigned temporaries before left shifting, to avoid
  101. promotion to signed int and then left shifting into the sign bit,
  102. which is undefined behaviour. */
  103. unsigned int p3 = p[3];
  104. unsigned int p2 = p[2];
  105. unsigned int p1 = p[1];
  106. unsigned int p0 = p[0];
  107. return ((p3 << 24) | (p2 << 16) | (p1 << 8) | p0);
  108. }
  109. static inline uint64_t
  110. archive_le64dec(const void *pp)
  111. {
  112. unsigned char const *p = (unsigned char const *)pp;
  113. return (((uint64_t)archive_le32dec(p + 4) << 32) | archive_le32dec(p));
  114. }
  115. static inline void
  116. archive_be16enc(void *pp, uint16_t u)
  117. {
  118. unsigned char *p = (unsigned char *)pp;
  119. p[0] = (u >> 8) & 0xff;
  120. p[1] = u & 0xff;
  121. }
  122. static inline void
  123. archive_be32enc(void *pp, uint32_t u)
  124. {
  125. unsigned char *p = (unsigned char *)pp;
  126. p[0] = (u >> 24) & 0xff;
  127. p[1] = (u >> 16) & 0xff;
  128. p[2] = (u >> 8) & 0xff;
  129. p[3] = u & 0xff;
  130. }
  131. static inline void
  132. archive_be64enc(void *pp, uint64_t u)
  133. {
  134. unsigned char *p = (unsigned char *)pp;
  135. archive_be32enc(p, (uint32_t)(u >> 32));
  136. archive_be32enc(p + 4, (uint32_t)(u & 0xffffffff));
  137. }
  138. static inline void
  139. archive_le16enc(void *pp, uint16_t u)
  140. {
  141. unsigned char *p = (unsigned char *)pp;
  142. p[0] = u & 0xff;
  143. p[1] = (u >> 8) & 0xff;
  144. }
  145. static inline void
  146. archive_le32enc(void *pp, uint32_t u)
  147. {
  148. unsigned char *p = (unsigned char *)pp;
  149. p[0] = u & 0xff;
  150. p[1] = (u >> 8) & 0xff;
  151. p[2] = (u >> 16) & 0xff;
  152. p[3] = (u >> 24) & 0xff;
  153. }
  154. static inline void
  155. archive_le64enc(void *pp, uint64_t u)
  156. {
  157. unsigned char *p = (unsigned char *)pp;
  158. archive_le32enc(p, (uint32_t)(u & 0xffffffff));
  159. archive_le32enc(p + 4, (uint32_t)(u >> 32));
  160. }
  161. #endif