xref: /nrf52832-nimble/packages/NimBLE-latest/porting/nimble/src/endian.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "os/endian.h"
21 
22 void
put_le16(void * buf,uint16_t x)23 put_le16(void *buf, uint16_t x)
24 {
25     uint8_t *u8ptr;
26 
27     u8ptr = buf;
28     u8ptr[0] = (uint8_t)x;
29     u8ptr[1] = (uint8_t)(x >> 8);
30 }
31 
32 void
put_le32(void * buf,uint32_t x)33 put_le32(void *buf, uint32_t x)
34 {
35     uint8_t *u8ptr;
36 
37     u8ptr = buf;
38     u8ptr[0] = (uint8_t)x;
39     u8ptr[1] = (uint8_t)(x >> 8);
40     u8ptr[2] = (uint8_t)(x >> 16);
41     u8ptr[3] = (uint8_t)(x >> 24);
42 }
43 
44 void
put_le64(void * buf,uint64_t x)45 put_le64(void *buf, uint64_t x)
46 {
47     uint8_t *u8ptr;
48 
49     u8ptr = buf;
50     u8ptr[0] = (uint8_t)x;
51     u8ptr[1] = (uint8_t)(x >> 8);
52     u8ptr[2] = (uint8_t)(x >> 16);
53     u8ptr[3] = (uint8_t)(x >> 24);
54     u8ptr[4] = (uint8_t)(x >> 32);
55     u8ptr[5] = (uint8_t)(x >> 40);
56     u8ptr[6] = (uint8_t)(x >> 48);
57     u8ptr[7] = (uint8_t)(x >> 56);
58 }
59 
60 uint16_t
get_le16(const void * buf)61 get_le16(const void *buf)
62 {
63     const uint8_t *u8ptr;
64     uint16_t x;
65 
66     u8ptr = buf;
67     x = u8ptr[0];
68     x |= (uint16_t)u8ptr[1] << 8;
69 
70     return x;
71 }
72 
73 uint32_t
get_le32(const void * buf)74 get_le32(const void *buf)
75 {
76     const uint8_t *u8ptr;
77     uint32_t x;
78 
79     u8ptr = buf;
80     x = u8ptr[0];
81     x |= (uint32_t)u8ptr[1] << 8;
82     x |= (uint32_t)u8ptr[2] << 16;
83     x |= (uint32_t)u8ptr[3] << 24;
84 
85     return x;
86 }
87 
88 uint64_t
get_le64(const void * buf)89 get_le64(const void *buf)
90 {
91     const uint8_t *u8ptr;
92     uint64_t x;
93 
94     u8ptr = buf;
95     x = u8ptr[0];
96     x |= (uint64_t)u8ptr[1] << 8;
97     x |= (uint64_t)u8ptr[2] << 16;
98     x |= (uint64_t)u8ptr[3] << 24;
99     x |= (uint64_t)u8ptr[4] << 32;
100     x |= (uint64_t)u8ptr[5] << 40;
101     x |= (uint64_t)u8ptr[6] << 48;
102     x |= (uint64_t)u8ptr[7] << 56;
103 
104     return x;
105 }
106 
107 void
put_be16(void * buf,uint16_t x)108 put_be16(void *buf, uint16_t x)
109 {
110     uint8_t *u8ptr;
111 
112     u8ptr = buf;
113     u8ptr[0] = (uint8_t)(x >> 8);
114     u8ptr[1] = (uint8_t)x;
115 }
116 
117 void
put_be32(void * buf,uint32_t x)118 put_be32(void *buf, uint32_t x)
119 {
120     uint8_t *u8ptr;
121 
122     u8ptr = buf;
123     u8ptr[0] = (uint8_t)(x >> 24);
124     u8ptr[1] = (uint8_t)(x >> 16);
125     u8ptr[2] = (uint8_t)(x >> 8);
126     u8ptr[3] = (uint8_t)x;
127 }
128 
129 void
put_be64(void * buf,uint64_t x)130 put_be64(void *buf, uint64_t x)
131 {
132     uint8_t *u8ptr;
133 
134     u8ptr = buf;
135     u8ptr[0] = (uint8_t)(x >> 56);
136     u8ptr[1] = (uint8_t)(x >> 48);
137     u8ptr[2] = (uint8_t)(x >> 40);
138     u8ptr[3] = (uint8_t)(x >> 32);
139     u8ptr[4] = (uint8_t)(x >> 24);
140     u8ptr[5] = (uint8_t)(x >> 16);
141     u8ptr[6] = (uint8_t)(x >> 8);
142     u8ptr[7] = (uint8_t)x;
143 }
144 
145 uint16_t
get_be16(const void * buf)146 get_be16(const void *buf)
147 {
148     const uint8_t *u8ptr;
149     uint16_t x;
150 
151     u8ptr = buf;
152     x = (uint16_t)u8ptr[0] << 8;
153     x |= u8ptr[1];
154 
155     return x;
156 }
157 
158 uint32_t
get_be32(const void * buf)159 get_be32(const void *buf)
160 {
161     const uint8_t *u8ptr;
162     uint32_t x;
163 
164     u8ptr = buf;
165     x = (uint32_t)u8ptr[0] << 24;
166     x |= (uint32_t)u8ptr[1] << 16;
167     x |= (uint32_t)u8ptr[2] << 8;
168     x |= u8ptr[3];
169 
170     return x;
171 }
172 
173 uint64_t
get_be64(const void * buf)174 get_be64(const void *buf)
175 {
176     const uint8_t *u8ptr;
177     uint64_t x;
178 
179     u8ptr = buf;
180     x = (uint64_t)u8ptr[0] << 56;
181     x |= (uint64_t)u8ptr[1] << 48;
182     x |= (uint64_t)u8ptr[2] << 40;
183     x |= (uint64_t)u8ptr[3] << 32;
184     x |= (uint64_t)u8ptr[4] << 24;
185     x |= (uint64_t)u8ptr[5] << 16;
186     x |= (uint64_t)u8ptr[6] << 8;
187     x |= u8ptr[7];
188 
189     return x;
190 }
191 void
swap_in_place(void * buf,int len)192 swap_in_place(void *buf, int len)
193 {
194     uint8_t *u8ptr;
195     uint8_t tmp;
196     int i;
197     int j;
198 
199     u8ptr = buf;
200 
201     for (i = 0, j = len - 1; i < j; i++, j--) {
202         tmp = u8ptr[i];
203 
204         u8ptr[i] = u8ptr[j];
205         u8ptr[j] = tmp;
206     }
207 }
208 
209 /* swap octets */
210 void
swap_buf(uint8_t * dst,const uint8_t * src,int len)211 swap_buf(uint8_t *dst, const uint8_t *src, int len)
212 {
213     int i;
214 
215     for (i = 0; i < len; i++) {
216         dst[len - 1 - i] = src[i];
217     }
218 }
219