/src/zziplib/zzip/fetch.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 126 lines · 74 code · 9 blank · 43 comment · 0 complexity · 84f92a283eb04dd7ace25572125c98b6 MD5 · raw file

  1. /*
  2. * These routines are helpers - note that this file gets linked into all the
  3. * zip-access variants that refer to <zzip/format.h>. On the x86 platform the
  4. * actual definitions will be empty - fetching is done on native machine-level
  5. *
  6. * Author:
  7. * Guido Draheim <guidod@gmx.de>
  8. *
  9. * Copyright (c) 2004,2005,2006 Guido Draheim
  10. * All rights reserved,
  11. * use under the restrictions of the
  12. * Lesser GNU General Public License
  13. * or alternatively the restrictions
  14. * of the Mozilla Public License 1.1
  15. */
  16. #include <zzip/fetch.h>
  17. /* ------------------------- fetch helpers --------------------------------- */
  18. /**
  19. * Make 32 bit value in host byteorder from little-endian mapped octet-data
  20. * (works also on machines which SIGBUS on misaligned data access (eg. 68000))
  21. */
  22. uint32_t
  23. __zzip_get32(unsigned char *s)
  24. {
  25. #if defined __ZZIP_GET32
  26. return __ZZIP_GET32(s);
  27. #else
  28. return ((uint32_t) s[3] << 24) | ((uint32_t) s[2] << 16)
  29. | ((uint32_t) s[1] << 8) | ((uint32_t) s[0]);
  30. #endif
  31. }
  32. /** => __zzip_get32
  33. * This function does the same for a 16 bit value.
  34. */
  35. uint16_t
  36. __zzip_get16(unsigned char *s)
  37. {
  38. #if defined __ZZIP_GET16
  39. return __ZZIP_GET16(s);
  40. #else
  41. return ((uint16_t) s[1] << 8) | ((uint16_t) s[0]);
  42. #endif
  43. }
  44. /** => __zzip_get32
  45. * This function does the same for an off64_t value.
  46. */
  47. uint64_t
  48. __zzip_get64(unsigned char *s)
  49. {
  50. #ifdef __GNUC__
  51. /* *INDENT-OFF* */
  52. register uint64_t v
  53. = s[7]; v <<= 8;
  54. v |= s[6]; v <<= 8;
  55. v |= s[5]; v <<= 8;
  56. v |= s[4]; v <<= 8;
  57. v |= s[3]; v <<= 8;
  58. v |= s[2]; v <<= 8;
  59. v |= s[1]; v <<= 8;
  60. v |= s[0]; return v;
  61. /* *INDENT-ON* */
  62. #else
  63. return ((uint64_t) s[7] << 56) | ((uint64_t) s[6] << 48)
  64. | ((uint64_t) s[5] << 40) | ((uint64_t) s[4] << 32)
  65. | ((uint64_t) s[3] << 24) | ((uint64_t) s[2] << 16)
  66. | ((uint64_t) s[1] << 8) | ((uint64_t) s[0]);
  67. #endif
  68. }
  69. /** => __zzip_get32
  70. * This function pushes a 32bit value at the specified address
  71. */
  72. void
  73. __zzip_set32(unsigned char *s, uint32_t v)
  74. {
  75. #if defined __ZZIP_SET32
  76. return __ZZIP_SET32(s, v);
  77. #else
  78. /* *INDENT-OFF* */
  79. s[0] = (unsigned char) (v); v >>= 8;
  80. s[1] = (unsigned char) (v); v >>= 8;
  81. s[2] = (unsigned char) (v); v >>= 8;
  82. s[3] = (unsigned char) (v);
  83. /* *INDENT-ON* */
  84. #endif
  85. }
  86. /** => __zzip_get32
  87. * This function does the same for a 16 bit value.
  88. */
  89. void
  90. __zzip_set16(unsigned char *s, uint16_t v)
  91. {
  92. #if defined __ZZIP_SET16
  93. return __ZZIP_SET16(s, v);
  94. #else
  95. /* *INDENT-OFF* */
  96. s[0] = (unsigned char) (v); v >>= 8;
  97. s[1] = (unsigned char) (v);
  98. /* *INDENT-ON* */
  99. #endif
  100. }
  101. /** => __zzip_get32
  102. * This function pushes a off64_t value at the specified address
  103. */
  104. void
  105. __zzip_set64(unsigned char *s, uint64_t v)
  106. {
  107. /* *INDENT-OFF* */
  108. s[0] = (unsigned char) (v); v >>= 8;
  109. s[1] = (unsigned char) (v); v >>= 8;
  110. s[2] = (unsigned char) (v); v >>= 8;
  111. s[3] = (unsigned char) (v); v >>= 8;
  112. s[4] = (unsigned char) (v); v >>= 8;
  113. s[5] = (unsigned char) (v); v >>= 8;
  114. s[6] = (unsigned char) (v); v >>= 8;
  115. s[7] = (unsigned char) (v);
  116. /* *INDENT-ON* */
  117. }