io_utilities.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. namespace io {
  20. /// \brief Read Unsigned 16-Bit Integer from Buffer
  21. ///
  22. /// This is essentially a copy of the isc::util::InputBuffer::readUint16. It
  23. /// should really be moved into a separate library.
  24. ///
  25. /// \param buffer Data buffer at least two bytes long of which the first two
  26. /// bytes are assumed to represent a 16-bit integer in network-byte
  27. /// order.
  28. ///
  29. /// \return Value of 16-bit integer
  30. inline uint16_t
  31. readUint16(const void* buffer) {
  32. const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
  33. uint16_t result = (static_cast<uint16_t>(byte_buffer[0])) << 8;
  34. result |= static_cast<uint16_t>(byte_buffer[1]);
  35. return (result);
  36. }
  37. /// \brief Write Unisgned 16-Bit Integer to Buffer
  38. ///
  39. /// This is essentially a copy of isc::util::OutputBuffer::writeUint16. It
  40. /// should really be moved into a separate library.
  41. ///
  42. /// \param value 16-bit value to convert
  43. /// \param buffer Data buffer at least two bytes long into which the 16-bit
  44. /// value is written in network-byte order.
  45. inline void
  46. writeUint16(uint16_t value, void* buffer) {
  47. uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
  48. byte_buffer[0] = static_cast<uint8_t>((value & 0xff00U) >> 8);
  49. byte_buffer[1] = static_cast<uint8_t>(value & 0x00ffU);
  50. }
  51. } // namespace io
  52. } // namespace util
  53. } // namespace isc
  54. #endif // __ASIOLINK_UTILITIES_H