RTEMS 6.1-rc1
endian.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
9/*
10 * COPYRIGHT (C) 1989-1999 On-Line Applications Research Corporation (OAR).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef _RTEMS_ENDIAN_H
35#define _RTEMS_ENDIAN_H
36
37#include <stdint.h>
38
39#include <machine/endian.h>
40
41#ifndef _BYTEORDER_FUNC_DEFINED
42#define _BYTEORDER_FUNC_DEFINED
43#define htonl(x) __htonl(x)
44#define htons(x) __htons(x)
45#define ntohl(x) __ntohl(x)
46#define ntohs(x) __ntohs(x)
47#endif
48
49#define NTOHS(x) (x) = ntohs(x)
50#define HTONS(x) (x) = htons(x)
51#define NTOHL(x) (x) = ntohl(x)
52#define HTONL(x) (x) = htonl(x)
53
54static inline uint16_t rtems_uint16_from_little_endian( const uint8_t *data)
55{
56 uint16_t value = 0;
57 int i;
58
59 for (i = 1; i >= 0; --i) {
60 value = (uint16_t) ((value << 8) + data [i]);
61 }
62
63 return value;
64}
65
66static inline uint32_t rtems_uint32_from_little_endian( const uint8_t *data)
67{
68 uint32_t value = 0;
69 int i;
70
71 for (i = 3; i >= 0; --i) {
72 value = (value << 8) + data [i];
73 }
74
75 return value;
76}
77
78static inline uint64_t rtems_uint64_from_little_endian( const uint8_t *data)
79{
80 uint64_t value = 0;
81 int i;
82
83 for (i = 7; i >= 0; --i) {
84 value = (value << 8) + (uint64_t) data [i];
85 }
86
87 return value;
88}
89
90static inline uint16_t rtems_uint16_from_big_endian( const uint8_t *data)
91{
92 uint16_t value = 0;
93 int i;
94
95 for (i = 0; i < 2; ++i) {
96 value = (uint16_t) ((value << 8) + data [i]);
97 }
98
99 return value;
100}
101
102static inline uint32_t rtems_uint32_from_big_endian( const uint8_t *data)
103{
104 uint32_t value = 0;
105 int i;
106
107 for (i = 0; i < 4; ++i) {
108 value = (value << 8) + (uint32_t) data [i];
109 }
110
111 return value;
112}
113
114static inline uint64_t rtems_uint64_from_big_endian( const uint8_t *data)
115{
116 uint64_t value = 0;
117 int i;
118
119 for (i = 0; i < 8; ++i) {
120 value = (value << 8) + (uint64_t) data [i];
121 }
122
123 return value;
124}
125
126static inline void rtems_uint16_to_little_endian( uint16_t value, uint8_t *data)
127{
128 int i;
129
130 for (i = 0; i < 2; ++i) {
131 data [i] = (uint8_t) value;
132 value >>= 8;
133 }
134}
135
136static inline void rtems_uint32_to_little_endian( uint32_t value, uint8_t *data)
137{
138 int i;
139
140 for (i = 0; i < 4; ++i) {
141 data [i] = (uint8_t) value;
142 value >>= 8;
143 }
144}
145
146static inline void rtems_uint64_to_little_endian( uint64_t value, uint8_t *data)
147{
148 int i;
149
150 for (i = 0; i < 8; ++i) {
151 data [i] = (uint8_t) value;
152 value >>= 8;
153 }
154}
155
156static inline void rtems_uint16_to_big_endian( uint16_t value, uint8_t *data)
157{
158 int i;
159
160 for (i = 1; i >= 0; --i) {
161 data [i] = (uint8_t) value;
162 value >>= 8;
163 }
164}
165
166static inline void rtems_uint32_to_big_endian( uint32_t value, uint8_t *data)
167{
168 int i;
169
170 for (i = 3; i >= 0; --i) {
171 data [i] = (uint8_t) value;
172 value >>= 8;
173 }
174}
175
176static inline void rtems_uint64_to_big_endian( uint64_t value, uint8_t *data)
177{
178 int i;
179
180 for (i = 7; i >= 0; --i) {
181 data [i] = (uint8_t) value;
182 value >>= 8;
183 }
184}
185
186#endif /* _RTEMS_ENDIAN_H */