0.01.00
encoding.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef ENCODING_HPP_
35 #define ENCODING_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <limits.h>
40 #include "utils/wrap_stdint.h"
41 
42 namespace ot {
43 namespace Encoding {
44 
45 inline uint16_t Swap16(uint16_t v)
46 {
47  return
48  (((v & 0x00ffU) << 8) & 0xff00) |
49  (((v & 0xff00U) >> 8) & 0x00ff);
50 }
51 
52 inline uint32_t Swap32(uint32_t v)
53 {
54  return
55  ((v & static_cast<uint32_t>(0x000000ffUL)) << 24) |
56  ((v & static_cast<uint32_t>(0x0000ff00UL)) << 8) |
57  ((v & static_cast<uint32_t>(0x00ff0000UL)) >> 8) |
58  ((v & static_cast<uint32_t>(0xff000000UL)) >> 24);
59 }
60 
61 inline uint64_t Swap64(uint64_t v)
62 {
63  return
64  ((v & static_cast<uint64_t>(0x00000000000000ffULL)) << 56) |
65  ((v & static_cast<uint64_t>(0x000000000000ff00ULL)) << 40) |
66  ((v & static_cast<uint64_t>(0x0000000000ff0000ULL)) << 24) |
67  ((v & static_cast<uint64_t>(0x00000000ff000000ULL)) << 8) |
68  ((v & static_cast<uint64_t>(0x000000ff00000000ULL)) >> 8) |
69  ((v & static_cast<uint64_t>(0x0000ff0000000000ULL)) >> 24) |
70  ((v & static_cast<uint64_t>(0x00ff000000000000ULL)) >> 40) |
71  ((v & static_cast<uint64_t>(0xff00000000000000ULL)) >> 56);
72 }
73 
74 inline uint32_t Reverse32(uint32_t v)
75 {
76  v = ((v & 0x55555555) << 1) | ((v & 0xaaaaaaaa) >> 1);
77  v = ((v & 0x33333333) << 2) | ((v & 0xcccccccc) >> 2);
78  v = ((v & 0x0f0f0f0f) << 4) | ((v & 0xf0f0f0f0) >> 4);
79  v = ((v & 0x00ff00ff) << 8) | ((v & 0xff00ff00) >> 8);
80  v = ((v & 0x0000ffff) << 16) | ((v & 0xffff0000) >> 16);
81 
82  return v;
83 }
84 
85 #define BitVectorBytes(x) \
86  (((x) + (CHAR_BIT-1)) / CHAR_BIT)
87 
88 namespace BigEndian {
89 
90 #if BYTE_ORDER_BIG_ENDIAN
91 
92 inline uint16_t HostSwap16(uint16_t v) { return v; }
93 inline uint32_t HostSwap32(uint32_t v) { return v; }
94 inline uint64_t HostSwap64(uint64_t v) { return v; }
95 
96 #else /* BYTE_ORDER_LITTLE_ENDIAN */
97 
98 inline uint16_t HostSwap16(uint16_t v) { return Swap16(v); }
99 inline uint32_t HostSwap32(uint32_t v) { return Swap32(v); }
100 inline uint64_t HostSwap64(uint64_t v) { return Swap64(v); }
101 
102 #endif // LITTLE_ENDIAN
103 
112 inline uint16_t ReadUint16(const uint8_t *aBuffer)
113 {
114  return static_cast<uint16_t>((aBuffer[0] << 8) | aBuffer[1]);
115 }
116 
125 inline uint32_t ReadUint32(const uint8_t *aBuffer)
126 {
127  return ((static_cast<uint32_t>(aBuffer[0]) << 24) |
128  (static_cast<uint32_t>(aBuffer[1]) << 16) |
129  (static_cast<uint32_t>(aBuffer[2]) << 8) |
130  (static_cast<uint32_t>(aBuffer[3]) << 0));
131 }
132 
140 inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer)
141 {
142  aBuffer[0] = (aValue >> 8) & 0xff;
143  aBuffer[1] = (aValue >> 0) & 0xff;
144 }
145 
153 inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer)
154 {
155  aBuffer[0] = (aValue >> 24) & 0xff;
156  aBuffer[1] = (aValue >> 16) & 0xff;
157  aBuffer[2] = (aValue >> 8) & 0xff;
158  aBuffer[3] = (aValue >> 0) & 0xff;
159 }
160 
161 } // namespace BigEndian
162 
163 namespace LittleEndian {
164 
165 #if BYTE_ORDER_BIG_ENDIAN
166 
167 inline uint16_t HostSwap16(uint16_t v) { return Swap16(v); }
168 inline uint32_t HostSwap32(uint32_t v) { return Swap32(v); }
169 inline uint64_t HostSwap64(uint64_t v) { return Swap64(v); }
170 
171 #else /* BYTE_ORDER_LITTLE_ENDIAN */
172 
173 inline uint16_t HostSwap16(uint16_t v) { return v; }
174 inline uint32_t HostSwap32(uint32_t v) { return v; }
175 inline uint64_t HostSwap64(uint64_t v) { return v; }
176 
177 #endif
178 
187 inline uint16_t ReadUint16(const uint8_t *aBuffer)
188 {
189  return static_cast<uint16_t>(aBuffer[0] | (aBuffer[1] << 8));
190 }
191 
200 inline uint32_t ReadUint32(const uint8_t *aBuffer)
201 {
202  return ((static_cast<uint32_t>(aBuffer[0]) << 0) |
203  (static_cast<uint32_t>(aBuffer[1]) << 8) |
204  (static_cast<uint32_t>(aBuffer[2]) << 16) |
205  (static_cast<uint32_t>(aBuffer[3]) << 24));
206 }
207 
215 inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer)
216 {
217  aBuffer[0] = (aValue >> 0) & 0xff;
218  aBuffer[1] = (aValue >> 8) & 0xff;
219 }
220 
228 inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer)
229 {
230  aBuffer[0] = (aValue >> 0) & 0xff;
231  aBuffer[1] = (aValue >> 8) & 0xff;
232  aBuffer[2] = (aValue >> 16) & 0xff;
233  aBuffer[3] = (aValue >> 24) & 0xff;
234 }
235 
236 } // namespace LittleEndian
237 } // namespace Encoding
238 } // namespace ot
239 
240 #endif // ENCODING_HPP_
void WriteUint32(uint32_t aValue, uint8_t *aBuffer)
This function writes a uint32_t value to a given buffer using big-ending encoding.
Definition: encoding.hpp:153
Definition: cli.cpp:90
uint16_t ReadUint16(const uint8_t *aBuffer)
This function reads a uint16_t value from a given buffer assuming big-ending encoding.
Definition: encoding.hpp:112
void WriteUint16(uint16_t aValue, uint8_t *aBuffer)
This function writes a uint16_t value to a given buffer using big-ending encoding.
Definition: encoding.hpp:140
uint32_t ReadUint32(const uint8_t *aBuffer)
This function reads a uint32_t value from a given buffer assuming big-ending encoding.
Definition: encoding.hpp:125
This file includes compile-time configuration constants for OpenThread.