Kea  1.9.9-git
io_utilities.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef IO_UTILITIES_H
8 #define IO_UTILITIES_H
9 
10 #include <exceptions/exceptions.h>
11 #include <cstddef>
12 
13 namespace isc {
14 namespace util {
15 
27 inline uint16_t
28 readUint16(const void* buffer, size_t length) {
29  if (length < sizeof(uint16_t)) {
31  "Length (" << length << ") of buffer is insufficient " <<
32  "to read a uint16_t");
33  }
34 
35  const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
36 
37  uint16_t result = (static_cast<uint16_t>(byte_buffer[0])) << 8;
38  result |= static_cast<uint16_t>(byte_buffer[1]);
39 
40  return (result);
41 }
42 
54 inline uint8_t*
55 writeUint16(uint16_t value, void* buffer, size_t length) {
56  if (length < sizeof(uint16_t)) {
58  "Length (" << length << ") of buffer is insufficient " <<
59  "to write a uint16_t");
60  }
61 
62  uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
63 
64  byte_buffer[0] = static_cast<uint8_t>((value & 0xff00U) >> 8);
65  byte_buffer[1] = static_cast<uint8_t>(value & 0x00ffU);
66 
67  return (byte_buffer + sizeof(uint16_t));
68 }
69 
78 inline uint32_t
79 readUint32(const uint8_t* buffer, size_t length) {
80  if (length < sizeof(uint32_t)) {
82  "Length (" << length << ") of buffer is insufficient " <<
83  "to read a uint32_t");
84  }
85 
86  const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
87 
88  uint32_t result = (static_cast<uint32_t>(byte_buffer[0])) << 24;
89  result |= (static_cast<uint32_t>(byte_buffer[1])) << 16;
90  result |= (static_cast<uint32_t>(byte_buffer[2])) << 8;
91  result |= (static_cast<uint32_t>(byte_buffer[3]));
92 
93  return (result);
94 }
95 
104 inline uint64_t
105 readUint64(const uint8_t* buffer, size_t length) {
106  if (length < sizeof(uint64_t)) {
108  "Length (" << length << ") of buffer is insufficient " <<
109  "to read a uint64_t");
110  }
111 
112  const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
113 
114  uint64_t result = (static_cast<uint64_t>(byte_buffer[0])) << 56;
115  result |= (static_cast<uint64_t>(byte_buffer[1])) << 48;
116  result |= (static_cast<uint64_t>(byte_buffer[2])) << 40;
117  result |= (static_cast<uint64_t>(byte_buffer[3])) << 32;
118  result |= (static_cast<uint64_t>(byte_buffer[4])) << 24;
119  result |= (static_cast<uint64_t>(byte_buffer[5])) << 16;
120  result |= (static_cast<uint64_t>(byte_buffer[6])) << 8;
121  result |= (static_cast<uint64_t>(byte_buffer[7]));
122 
123 
124  return (result);
125 }
126 
135 inline uint8_t*
136 writeUint32(uint32_t value, uint8_t* buffer, size_t length) {
137  if (length < sizeof(uint32_t)) {
139  "Length (" << length << ") of buffer is insufficient " <<
140  "to write a uint32_t");
141  }
142 
143  uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
144 
145  byte_buffer[0] = static_cast<uint8_t>((value & 0xff000000U) >> 24);
146  byte_buffer[1] = static_cast<uint8_t>((value & 0x00ff0000U) >> 16);
147  byte_buffer[2] = static_cast<uint8_t>((value & 0x0000ff00U) >> 8);
148  byte_buffer[3] = static_cast<uint8_t>((value & 0x000000ffU));
149 
150  return (byte_buffer + sizeof(uint32_t));
151 }
152 
153 } // namespace util
154 } // namespace isc
155 
156 #endif // IO_UTILITIES_H
uint8_t * writeUint32(uint32_t value, uint8_t *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
Definition: io_utilities.h:136
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
uint8_t * writeUint16(uint16_t value, void *buffer, size_t length)
Write Unsigned 16-Bit Integer to Buffer.
Definition: io_utilities.h:55
uint32_t readUint32(const uint8_t *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
Defines the logger used by the top-level component of kea-dhcp-ddns.
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
uint64_t readUint64(const uint8_t *buffer, size_t length)
Read Unsigned 64-Bit Integer from Buffer.
Definition: io_utilities.h:105