Kea 2.5.5
io_utilities.h
Go to the documentation of this file.
1// Copyright (C) 2011-2023 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
11#include <cstddef>
12
13namespace isc {
14namespace util {
15
27inline uint16_t
28readUint16(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
54inline uint8_t*
55writeUint16(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
78inline uint32_t
79readUint32(const void* 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
104inline uint8_t*
105writeUint32(uint32_t value, void* buffer, size_t length) {
106 if (length < sizeof(uint32_t)) {
108 "Length (" << length << ") of buffer is insufficient " <<
109 "to write a uint32_t");
110 }
111
112 uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
113
114 byte_buffer[0] = static_cast<uint8_t>((value & 0xff000000U) >> 24);
115 byte_buffer[1] = static_cast<uint8_t>((value & 0x00ff0000U) >> 16);
116 byte_buffer[2] = static_cast<uint8_t>((value & 0x0000ff00U) >> 8);
117 byte_buffer[3] = static_cast<uint8_t>((value & 0x000000ffU));
118
119 return (byte_buffer + sizeof(uint32_t));
120}
121
130inline uint64_t
131readUint64(const void* buffer, size_t length) {
132 if (length < sizeof(uint64_t)) {
134 "Length (" << length << ") of buffer is insufficient " <<
135 "to read a uint64_t");
136 }
137
138 const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
139
140 uint64_t result = (static_cast<uint64_t>(byte_buffer[0])) << 56;
141 result |= (static_cast<uint64_t>(byte_buffer[1])) << 48;
142 result |= (static_cast<uint64_t>(byte_buffer[2])) << 40;
143 result |= (static_cast<uint64_t>(byte_buffer[3])) << 32;
144 result |= (static_cast<uint64_t>(byte_buffer[4])) << 24;
145 result |= (static_cast<uint64_t>(byte_buffer[5])) << 16;
146 result |= (static_cast<uint64_t>(byte_buffer[6])) << 8;
147 result |= (static_cast<uint64_t>(byte_buffer[7]));
148
149 return (result);
150}
151
160inline uint8_t*
161writeUint64(uint64_t value, void* buffer, size_t length) {
162 if (length < sizeof(uint64_t)) {
164 "Length (" << length << ") of buffer is insufficient " <<
165 "to write a uint64_t");
166 }
167
168 uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
169
170 byte_buffer[0] = static_cast<uint8_t>((value & 0xff00000000000000UL) >> 56);
171 byte_buffer[1] = static_cast<uint8_t>((value & 0x00ff000000000000UL) >> 48);
172 byte_buffer[2] = static_cast<uint8_t>((value & 0x0000ff0000000000UL) >> 40);
173 byte_buffer[3] = static_cast<uint8_t>((value & 0x000000ff00000000UL) >> 32);
174 byte_buffer[4] = static_cast<uint8_t>((value & 0x00000000ff000000UL) >> 24);
175 byte_buffer[5] = static_cast<uint8_t>((value & 0x0000000000ff0000UL) >> 16);
176 byte_buffer[6] = static_cast<uint8_t>((value & 0x000000000000ff00UL) >> 8);
177 byte_buffer[7] = static_cast<uint8_t>((value & 0x00000000000000ffUL));
178
179 return (byte_buffer + sizeof(uint64_t));
180}
181
182} // namespace util
183} // namespace isc
184
185#endif // IO_UTILITIES_H
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
#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
uint8_t * writeUint32(uint32_t value, void *buffer, size_t length)
Write Unsigned 32-Bit Integer to Buffer.
Definition: io_utilities.h:105
uint32_t readUint32(const void *buffer, size_t length)
Read Unsigned 32-Bit Integer from Buffer.
Definition: io_utilities.h:79
uint64_t readUint64(const void *buffer, size_t length)
Read Unsigned 64-Bit Integer from Buffer.
Definition: io_utilities.h:131
uint8_t * writeUint64(uint64_t value, void *buffer, size_t length)
Write Unsigned 64-Bit Integer to Buffer.
Definition: io_utilities.h:161
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Definition: io_utilities.h:28
Defines the logger used by the top-level component of kea-lfc.