Branch data Line data Source code
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 : :
15 : : #ifndef __IO_UTILITIES_H
16 : : #define __IO_UTILITIES_H
17 : :
18 : : #include <cstddef>
19 : :
20 : : namespace isc {
21 : : namespace util {
22 : :
23 : : /// \brief Read Unsigned 16-Bit Integer from Buffer
24 : : ///
25 : : /// This is essentially a copy of the isc::util::InputBuffer::readUint16. It
26 : : /// should really be moved into a separate library.
27 : : ///
28 : : /// \param buffer Data buffer at least two bytes long of which the first two
29 : : /// bytes are assumed to represent a 16-bit integer in network-byte
30 : : /// order.
31 : : ///
32 : : /// \return Value of 16-bit integer
33 : : inline uint16_t
34 : : readUint16(const void* buffer) {
35 : 262205 : const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
36 : :
37 : 262205 : uint16_t result = (static_cast<uint16_t>(byte_buffer[0])) << 8;
38 : 262205 : result |= static_cast<uint16_t>(byte_buffer[1]);
39 : :
40 : : return (result);
41 : : }
42 : :
43 : : /// \brief Write Unisgned 16-Bit Integer to Buffer
44 : : ///
45 : : /// This is essentially a copy of isc::util::OutputBuffer::writeUint16. It
46 : : /// should really be moved into a separate library.
47 : : ///
48 : : /// \param value 16-bit value to convert
49 : : /// \param buffer Data buffer at least two bytes long into which the 16-bit
50 : : /// value is written in network-byte order.
51 : : ///
52 : : /// \return pointer to the next byte after stored value
53 : : inline uint8_t*
54 : : writeUint16(uint16_t value, void* buffer) {
55 : 262162 : uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
56 : :
57 : 262162 : byte_buffer[0] = static_cast<uint8_t>((value & 0xff00U) >> 8);
58 : 262162 : byte_buffer[1] = static_cast<uint8_t>(value & 0x00ffU);
59 : :
60 : : return (byte_buffer + sizeof(uint16_t));
61 : : }
62 : :
63 : : /// \brief Read Unsigned 32-Bit Integer from Buffer
64 : : ///
65 : : /// \param buffer Data buffer at least four bytes long of which the first four
66 : : /// bytes are assumed to represent a 32-bit integer in network-byte
67 : : /// order.
68 : : ///
69 : : /// \return Value of 32-bit unsigned integer
70 : : inline uint32_t
71 : : readUint32(const uint8_t* buffer) {
72 : 20 : const uint8_t* byte_buffer = static_cast<const uint8_t*>(buffer);
73 : :
74 : 20 : uint32_t result = (static_cast<uint32_t>(byte_buffer[0])) << 24;
75 : 20 : result |= (static_cast<uint32_t>(byte_buffer[1])) << 16;
76 : 20 : result |= (static_cast<uint32_t>(byte_buffer[2])) << 8;
77 : 20 : result |= (static_cast<uint32_t>(byte_buffer[3]));
78 : :
79 : : return (result);
80 : : }
81 : :
82 : : /// \brief Write Unisgned 32-Bit Integer to Buffer
83 : : ///
84 : : /// \param value 32-bit value to convert
85 : : /// \param buffer Data buffer at least four bytes long into which the 32-bit
86 : : /// value is written in network-byte order.
87 : : ///
88 : : /// \return pointer to the next byte after stored value
89 : : inline uint8_t*
90 : : writeUint32(uint32_t value, uint8_t* buffer) {
91 : 20 : uint8_t* byte_buffer = static_cast<uint8_t*>(buffer);
92 : :
93 : 20 : byte_buffer[0] = static_cast<uint8_t>((value & 0xff000000U) >> 24);
94 : 20 : byte_buffer[1] = static_cast<uint8_t>((value & 0x00ff0000U) >> 16);
95 : 20 : byte_buffer[2] = static_cast<uint8_t>((value & 0x0000ff00U) >> 8);
96 : 20 : byte_buffer[3] = static_cast<uint8_t>((value & 0x000000ffU));
97 : :
98 : : return (byte_buffer + sizeof(uint32_t));
99 : : }
100 : :
101 : : } // namespace util
102 : : } // namespace isc
103 : :
104 : : #endif // __ASIOLINK_UTILITIES_H
|