Kea 2.7.5
io.h
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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 KEA_UTIL_IO_H
8#define KEA_UTIL_IO_H
9
10#include <algorithm>
11#include <cstddef>
12#include <cstdint>
13
14namespace isc {
15namespace util {
16
25template <typename uint_t>
26uint_t
27readUint(void const* const buffer, size_t const length) {
28 constexpr size_t size(sizeof(uint_t));
29 if (length < size) {
30 isc_throw(OutOfRange, "Expected buffer to be long enough to read a "
31 << size << "-byte integer, but got " << length << " byte"
32 << (length == 1 ? "" : "s") << " instead");
33 }
34
35 uint8_t const* const byte_buffer(static_cast<uint8_t const*>(buffer));
36 uint_t result(0);
37
38 for (size_t i = 0; i < size; ++i) {
39 result |= (static_cast<uint_t>(byte_buffer[i])) << (8 * (size - (i + 1)));
40 }
41
42 return (result);
43}
44
54template <typename uint_t>
55uint8_t*
56writeUint(uint_t const value, void* const buffer, size_t const length) {
57 constexpr size_t size(sizeof(uint_t));
58 if (length < size) {
59 isc_throw(OutOfRange, "Expected buffer to be long enough to write a "
60 << size << "-byte integer, but got " << length << " byte"
61 << (length == 1 ? "" : "s") << " instead");
62 }
63
64 uint8_t* byte_buffer(static_cast<uint8_t*>(buffer));
65
66 for (size_t i = 0; i < size; ++i) {
67 uint8_t const shift_by(8 * (size - (i + 1)));
68 byte_buffer[i] = uint8_t((value & (uint_t(0xff) << shift_by)) >> shift_by);
69 }
70
71 return (byte_buffer + size);
72}
73
75inline uint16_t
76readUint16(void const* const buffer, size_t const length) {
77 return (readUint<uint16_t>(buffer, length));
78}
79
81inline uint32_t
82readUint32(void const* const buffer, size_t const length) {
83 return (readUint<uint32_t>(buffer, length));
84}
85
87inline uint64_t
88readUint64(void const* const buffer, size_t const length) {
89 return (readUint<uint64_t>(buffer, length));
90}
91
93inline uint8_t*
94writeUint16(uint16_t const value, void* const buffer, size_t const length) {
95 return (writeUint(value, buffer, length));
96}
97
99inline uint8_t*
100writeUint32(uint32_t const value, void* const buffer, size_t const length) {
101 return (writeUint(value, buffer, length));
102}
103
105inline uint8_t*
106writeUint64(uint64_t const value, void* const buffer, size_t const length) {
107 return (writeUint(value, buffer, length));
108}
109
110} // namespace util
111} // namespace isc
112
113#endif // KEA_UTIL_IO_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.
uint_t readUint(void const *const buffer, size_t const length)
Read an unsigned integer from given buffer.
Definition io.h:27
uint8_t * writeUint32(uint32_t const value, void *const buffer, size_t const length)
uint32_t wrapper over writeUint.
Definition io.h:100
uint8_t * writeUint(uint_t const value, void *const buffer, size_t const length)
Write the given unsigned integer to the given buffer.
Definition io.h:56
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
uint8_t * writeUint16(uint16_t const value, void *const buffer, size_t const length)
uint16_t wrapper over writeUint.
Definition io.h:94
uint8_t * writeUint64(uint64_t const value, void *const buffer, size_t const length)
uint64_t wrapper over writeUint.
Definition io.h:106
uint64_t readUint64(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:88
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition io.h:82
Defines the logger used by the top-level component of kea-lfc.