util.hh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2011 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of libzipper.
  4. //
  5. // libzipper is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // libzipper is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with libzipper. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef zipper_util_hh
  18. #define zipper_util_hh
  19. namespace zipper
  20. {
  21. template <typename T>
  22. struct
  23. dummy_delete
  24. {
  25. void operator()(T*) {}
  26. };
  27. template<typename T>
  28. uint32_t
  29. read32_le(const T& inArray, size_t pos = 0)
  30. {
  31. // Read 4 bytes in little-endian order.
  32. // Return results in host-endian.
  33. return uint32_t(
  34. inArray[pos] |
  35. (uint32_t(inArray[pos+1]) << 8) |
  36. (uint32_t(inArray[pos+2]) << 16) |
  37. (uint32_t(inArray[pos+3]) << 24)
  38. );
  39. }
  40. template<typename T>
  41. uint16_t
  42. read16_le(const T& inArray, size_t pos = 0)
  43. {
  44. // Read 2 bytes in little-endian order.
  45. // Return results in host-endian.
  46. return uint16_t(
  47. inArray[pos] |
  48. (uint16_t(inArray[pos+1]) << 8)
  49. );
  50. }
  51. template<typename T>
  52. void
  53. write32_le(uint32_t value, T& outArray, size_t pos = 0)
  54. {
  55. // Write 4 bytes in little-endian order.
  56. outArray[pos] = value & 0xff;
  57. outArray[pos + 1] = (value >> 8) & 0xff;
  58. outArray[pos + 2] = (value >> 16) & 0xff;
  59. outArray[pos + 3] = (value >> 24) & 0xff;
  60. }
  61. template<typename T>
  62. void
  63. write32_le(uint32_t value, T* outArray, size_t pos = 0)
  64. {
  65. // Write 4 bytes in little-endian order.
  66. outArray[pos] = value & 0xff;
  67. outArray[pos + 1] = (value >> 8) & 0xff;
  68. outArray[pos + 2] = (value >> 16) & 0xff;
  69. outArray[pos + 3] = (value >> 24) & 0xff;
  70. }
  71. template<typename T>
  72. void
  73. write16_le(uint16_t value, T& outArray, size_t pos = 0)
  74. {
  75. // Write 4 bytes in little-endian order.
  76. outArray[pos] = value & 0xff;
  77. outArray[pos + 1] = (value >> 8);
  78. }
  79. template<typename T>
  80. void
  81. write16_le(uint16_t value, T* outArray, size_t pos = 0)
  82. {
  83. // Write 4 bytes in little-endian order.
  84. outArray[pos] = value & 0xff;
  85. outArray[pos + 1] = (value >> 8);
  86. }
  87. }
  88. #endif