io_utilities.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __IO_UTILITIES_H
  15. #define __IO_UTILITIES_H
  16. #include <cstddef>
  17. namespace isc {
  18. namespace util {
  19. /// \brief Read Unsigned 16-Bit Integer from Buffer
  20. ///
  21. /// This is essentially a copy of the isc::util::InputBuffer::readUint16. It
  22. /// should really be moved into a separate library.
  23. ///
  24. /// \param buffer Data buffer at least two bytes long of which the first two
  25. /// bytes are assumed to represent a 16-bit integer in network-byte
  26. /// order.
  27. ///
  28. /// \return Value of 16-bit integer
  29. inline uint16_t
  30. readUint16(const void* buffer) {
  31. const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
  32. uint16_t result = (static_cast<uint16_t>(byte_buffer[0])) << 8;
  33. result |= static_cast<uint16_t>(byte_buffer[1]);
  34. return (result);
  35. }
  36. /// \brief Write Unisgned 16-Bit Integer to Buffer
  37. ///
  38. /// This is essentially a copy of isc::util::OutputBuffer::writeUint16. It
  39. /// should really be moved into a separate library.
  40. ///
  41. /// \param value 16-bit value to convert
  42. /// \param buffer Data buffer at least two bytes long into which the 16-bit
  43. /// value is written in network-byte order.
  44. ///
  45. /// \return pointer to the next byte after stored value
  46. inline uint8_t*
  47. writeUint16(uint16_t value, void* buffer) {
  48. uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
  49. byte_buffer[0] = static_cast<uint8_t>((value & 0xff00U) >> 8);
  50. byte_buffer[1] = static_cast<uint8_t>(value & 0x00ffU);
  51. return (byte_buffer + sizeof(uint16_t));
  52. }
  53. /// \brief Read Unsigned 32-Bit Integer from Buffer
  54. ///
  55. /// \param buffer Data buffer at least two bytes long of which the first two
  56. /// bytes are assumed to represent a 32-bit integer in network-byte
  57. /// order.
  58. ///
  59. /// \return Value of 32-bit unsigned integer
  60. inline uint32_t
  61. readUint32(const uint8_t* buffer) {
  62. const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
  63. uint32_t result = (static_cast<uint32_t>(byte_buffer[0])) << 24;
  64. result |= (static_cast<uint32_t>(byte_buffer[1])) << 16;
  65. result |= (static_cast<uint32_t>(byte_buffer[2])) << 8;
  66. result |= (static_cast<uint32_t>(byte_buffer[3]));
  67. return (result);
  68. }
  69. /// \brief Write Unisgned 32-Bit Integer to Buffer
  70. ///
  71. /// \param value 32-bit value to convert
  72. /// \param buffer Data buffer at least two bytes long into which the 16-bit
  73. /// value is written in network-byte order.
  74. ///
  75. /// \return pointer to the next byte after stored value
  76. inline uint8_t*
  77. writeUint32(uint32_t value, uint8_t* buffer) {
  78. uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
  79. byte_buffer[0] = static_cast<uint8_t>((value & 0xff000000U) >> 24);
  80. byte_buffer[1] = static_cast<uint8_t>((value & 0x00ff0000U) >> 16);
  81. byte_buffer[2] = static_cast<uint8_t>((value & 0x0000ff00U) >> 8);
  82. byte_buffer[3] = static_cast<uint8_t>((value & 0x000000ffU));
  83. return (byte_buffer + sizeof(uint32_t));
  84. }
  85. } // namespace util
  86. } // namespace isc
  87. #endif // __ASIOLINK_UTILITIES_H