• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
put_le16(void * buf,uint16_t x)22 void put_le16(void *buf, uint16_t x)
23 {
24     uint8_t *u8ptr;
25     u8ptr = buf;
26     u8ptr[0] = (uint8_t)x;
27     u8ptr[1] = (uint8_t)(x >> 8); // 8:byte alignment
28 }
29 
put_le24(void * buf,uint32_t x)30 void put_le24(void *buf, uint32_t x)
31 {
32     uint8_t *u8ptr;
33     u8ptr = buf;
34     u8ptr[0] = (uint8_t)x;
35     u8ptr[1] = (uint8_t)(x >> 8); // 8:byte alignment
36     u8ptr[2] = (uint8_t)(x >> 16); // 2:array element, 16:byte alignment
37 }
38 
put_le32(void * buf,uint32_t x)39 void put_le32(void *buf, uint32_t x)
40 {
41     uint8_t *u8ptr;
42     u8ptr = buf;
43     u8ptr[0] = (uint8_t)x;
44     u8ptr[1] = (uint8_t)(x >> 8); // 8:byte alignment
45     u8ptr[2] = (uint8_t)(x >> 16); // 2:array element, 16:byte alignment
46     u8ptr[3] = (uint8_t)(x >> 24); // 3:array element, 24:byte alignment
47 }
48 
put_le64(void * buf,uint64_t x)49 void put_le64(void *buf, uint64_t x)
50 {
51     uint8_t *u8ptr;
52     u8ptr = buf;
53     u8ptr[0] = (uint8_t)x;
54     u8ptr[1] = (uint8_t)(x >> 8); // 8:byte alignment
55     u8ptr[2] = (uint8_t)(x >> 16); // 2:array element, 16:byte alignment
56     u8ptr[3] = (uint8_t)(x >> 24); // 3:array element, 24:byte alignment
57     u8ptr[4] = (uint8_t)(x >> 32); // 4:array element, 32:byte alignment
58     u8ptr[5] = (uint8_t)(x >> 40); // 5:array element, 40:byte alignment
59     u8ptr[6] = (uint8_t)(x >> 48); // 6:array element, 48:byte alignment
60     u8ptr[7] = (uint8_t)(x >> 56); // 7:array element, 56:byte alignment
61 }
62 
get_le16(const void * buf)63 uint16_t get_le16(const void *buf)
64 {
65     const uint8_t *u8ptr;
66     uint16_t x;
67     u8ptr = buf;
68     x = u8ptr[0];
69     x |= (uint16_t)u8ptr[1] << 8; // 8:byte alignment
70     return x;
71 }
72 
get_le24(const void * buf)73 uint32_t get_le24(const void *buf)
74 {
75     const uint8_t *u8ptr;
76     uint32_t x;
77     u8ptr = buf;
78     x = u8ptr[0];
79     x |= (uint32_t)u8ptr[1] << 8; // 8:byte alignment
80     x |= (uint32_t)u8ptr[2] << 16; // 2:array element, 16:byte alignment
81     return x;
82 }
83 
get_le32(const void * buf)84 uint32_t get_le32(const void *buf)
85 {
86     const uint8_t *u8ptr;
87     uint32_t x;
88     u8ptr = buf;
89     x = u8ptr[0];
90     x |= (uint32_t)u8ptr[1] << 8; // 8:byte alignment
91     x |= (uint32_t)u8ptr[2] << 16; // 2:array element, 16:byte alignment
92     x |= (uint32_t)u8ptr[3] << 24; // 3:array element, 24:byte alignment
93     return x;
94 }
95 
get_le64(const void * buf)96 uint64_t get_le64(const void *buf)
97 {
98     const uint8_t *u8ptr;
99     uint64_t x;
100     u8ptr = buf;
101     x = u8ptr[0];
102     x |= (uint64_t)u8ptr[1] << 8; // 8:byte alignment
103     x |= (uint64_t)u8ptr[2] << 16; // 2:array element, 16:byte alignment
104     x |= (uint64_t)u8ptr[3] << 24; // 3:array element, 24:byte alignment
105     x |= (uint64_t)u8ptr[4] << 32; // 4:array element, 32:byte alignment
106     x |= (uint64_t)u8ptr[5] << 40; // 5:array element, 40:byte alignment
107     x |= (uint64_t)u8ptr[6] << 48; // 6:array element, 48:byte alignment
108     x |= (uint64_t)u8ptr[7] << 56; // 7:array element, 56:byte alignment
109     return x;
110 }
111 
put_be16(void * buf,uint16_t x)112 void put_be16(void *buf, uint16_t x)
113 {
114     uint8_t *u8ptr;
115     u8ptr = buf;
116     u8ptr[0] = (uint8_t)(x >> 8); // 8:byte alignment
117     u8ptr[1] = (uint8_t)x;
118 }
119 
put_be24(void * buf,uint32_t x)120 void put_be24(void *buf, uint32_t x)
121 {
122     uint8_t *u8ptr;
123     u8ptr = buf;
124     u8ptr[0] = (uint8_t)(x >> 24); // 24:byte alignment
125     u8ptr[1] = (uint8_t)(x >> 16); // 16:byte alignment
126     u8ptr[2] = (uint8_t)(x >> 8); // 2:array element, 8:byte alignment
127 }
128 
put_be32(void * buf,uint32_t x)129 void put_be32(void *buf, uint32_t x)
130 {
131     uint8_t *u8ptr;
132     u8ptr = buf;
133     u8ptr[0] = (uint8_t)(x >> 24); // 24:byte alignment
134     u8ptr[1] = (uint8_t)(x >> 16); // 16:byte alignment
135     u8ptr[2] = (uint8_t)(x >> 8); // 2:array element, 8:byte alignment
136     u8ptr[3] = (uint8_t)x; // 3:array element
137 }
138 
put_be64(void * buf,uint64_t x)139 void put_be64(void *buf, uint64_t x)
140 {
141     uint8_t *u8ptr;
142     u8ptr = buf;
143     u8ptr[0] = (uint8_t)(x >> 56); // 56:byte alignment
144     u8ptr[1] = (uint8_t)(x >> 48); // 48:byte alignment
145     u8ptr[2] = (uint8_t)(x >> 40); // 2:array element, 40:byte alignment
146     u8ptr[3] = (uint8_t)(x >> 32); // 3:array element, 32:byte alignment
147     u8ptr[4] = (uint8_t)(x >> 24); // 4:array element, 24:byte alignment
148     u8ptr[5] = (uint8_t)(x >> 16); // 5:array element, 16:byte alignment
149     u8ptr[6] = (uint8_t)(x >> 8); // 6:array element, 8:byte alignment
150     u8ptr[7] = (uint8_t)x; // 7:array element
151 }
152 
get_be16(const void * buf)153 uint16_t get_be16(const void *buf)
154 {
155     const uint8_t *u8ptr;
156     uint16_t x;
157     u8ptr = buf;
158     x = (uint16_t)u8ptr[0] << 8; // 8:byte alignment
159     x |= u8ptr[1];
160     return x;
161 }
162 
get_be24(const void * buf)163 uint32_t get_be24(const void *buf)
164 {
165     const uint8_t *u8ptr;
166     uint32_t x;
167     u8ptr = buf;
168     x = (uint32_t)u8ptr[0] << 24; // 24:byte alignment
169     x |= (uint32_t)u8ptr[1] << 16; // 16:byte alignment
170     x |= (uint32_t)u8ptr[2] << 8; // 2:array element, 8:byte alignment
171     return x;
172 }
173 
get_be32(const void * buf)174 uint32_t get_be32(const void *buf)
175 {
176     const uint8_t *u8ptr;
177     uint32_t x;
178     u8ptr = buf;
179     x = (uint32_t)u8ptr[0] << 24; // 24:byte alignment
180     x |= (uint32_t)u8ptr[1] << 16; // 16:byte alignment
181     x |= (uint32_t)u8ptr[2] << 8; // 2:array element, 8:byte alignment
182     x |= u8ptr[3]; // 3:array element
183     return x;
184 }
185 
get_be64(const void * buf)186 uint64_t get_be64(const void *buf)
187 {
188     const uint8_t *u8ptr;
189     uint64_t x;
190     u8ptr = buf;
191     x = (uint64_t)u8ptr[0] << 56; // 56:byte alignment
192     x |= (uint64_t)u8ptr[1] << 48; // 48:byte alignment
193     x |= (uint64_t)u8ptr[2] << 40; // 2:array element, 40:byte alignment
194     x |= (uint64_t)u8ptr[3] << 32; // 3:array element, 32:byte alignment
195     x |= (uint64_t)u8ptr[4] << 24; // 4:array element, 24:byte alignment
196     x |= (uint64_t)u8ptr[5] << 16; // 5:array element, 16:byte alignment
197     x |= (uint64_t)u8ptr[6] << 8; // 6:array element, 8:byte alignment
198     x |= u8ptr[7]; // 7:array element
199     return x;
200 }
swap_in_place(void * buf,int len)201 void swap_in_place(void *buf, int len)
202 {
203     uint8_t *u8ptr;
204     int i;
205     int j;
206     u8ptr = buf;
207 
208     for (i = 0, j = len - 1; i < j; i++, j--) {
209         uint8_t tmp;
210         tmp = u8ptr[i];
211         u8ptr[i] = u8ptr[j];
212         u8ptr[j] = tmp;
213     }
214 }
215 
216 /* swap octets */
swap_buf(uint8_t * dst,const uint8_t * src,int len)217 void swap_buf(uint8_t *dst, const uint8_t *src, int len)
218 {
219     int i;
220 
221     for (i = 0; i < len; i++) {
222         dst[len - 1 - i] = src[i];
223     }
224 }