1 /**
2 ****************************************************************************************
3 *
4 * @file utility.c
5 *
6 * @brief utility Implementation.
7 *
8 ****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * 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 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 *****************************************************************************************
36 */
37
38 /*
39 * INCLUDE FILES
40 ****************************************************************************************
41 */
42 #include "utility.h"
43
44 /*
45 * DEFINES
46 *****************************************************************************************
47 */
48
49 #define ITEM_0 0
50 #define ITEM_1 1
51 #define ITEM_2 2
52 #define ITEM_3 3
53 #define ITEM_4 4
54 #define ITEM_5 5
55 #define ITEM_6 6
56 #define ITEM_7 7
57
58 #define BIT_8 8
59 #define BIT_16 16
60 #define BIT_24 24
61 #define BIT_32 32
62 #define BIT_40 40
63 #define BIT_48 48
64 #define BIT_56 56
65
66 #define OFFSET_0 0
67 #define OFFSET_1 1
68 #define OFFSET_2 2
69 #define OFFSET_3 3
70 #define OFFSET_4 4
71
72 /*
73 * GLOBAL FUNCTION DEFINITIONS
74 ****************************************************************************************
75 */
76
htole16(uint8_t * p_buf,uint16_t x)77 void htole16(uint8_t *p_buf, uint16_t x)
78 {
79 uint8_t *u8ptr;
80 u8ptr = p_buf;
81 u8ptr[ITEM_0] = (uint8_t) x;
82 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_8);
83 }
84
htole32(uint8_t * p_buf,uint32_t x)85 void htole32(uint8_t *p_buf, uint32_t x)
86 {
87 uint8_t *u8ptr;
88 u8ptr = p_buf;
89 u8ptr[ITEM_0] = (uint8_t) x;
90 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_8);
91 u8ptr[ITEM_2] = (uint8_t)(x >> BIT_16);
92 u8ptr[ITEM_3] = (uint8_t)(x >> BIT_24);
93 }
94
htole64(uint8_t * p_buf,uint64_t x)95 void htole64(uint8_t *p_buf, uint64_t x)
96 {
97 uint8_t *u8ptr;
98 u8ptr = p_buf;
99 u8ptr[ITEM_0] = (uint8_t) x;
100 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_8);
101 u8ptr[ITEM_2] = (uint8_t)(x >> BIT_16);
102 u8ptr[ITEM_3] = (uint8_t)(x >> BIT_24);
103 u8ptr[ITEM_4] = (uint8_t)(x >> BIT_32);
104 u8ptr[ITEM_5] = (uint8_t)(x >> BIT_40);
105 u8ptr[ITEM_6] = (uint8_t)(x >> BIT_48);
106 u8ptr[ITEM_7] = (uint8_t)(x >> BIT_56);
107 }
108
le16toh(const uint8_t * p_buf)109 uint16_t le16toh(const uint8_t *p_buf)
110 {
111 const uint8_t *u8ptr;
112 uint16_t x;
113 u8ptr = p_buf;
114 x = u8ptr[ITEM_0];
115 x |= (uint16_t) u8ptr[ITEM_1] << BIT_8;
116 return x;
117 }
118
le32toh(const uint8_t * p_buf)119 uint32_t le32toh(const uint8_t *p_buf)
120 {
121 const uint8_t *u8ptr;
122 uint32_t x;
123 u8ptr = p_buf;
124 x = u8ptr[ITEM_0];
125 x |= (uint32_t) u8ptr[ITEM_1] << BIT_8;
126 x |= (uint32_t) u8ptr[ITEM_2] << BIT_16;
127 x |= (uint32_t) u8ptr[ITEM_3] << BIT_24;
128 return x;
129 }
130
le64toh(const uint8_t * p_buf)131 uint64_t le64toh(const uint8_t *p_buf)
132 {
133 const uint8_t *u8ptr;
134 uint64_t x;
135 u8ptr = p_buf;
136 x = u8ptr[ITEM_0];
137 x |= (uint64_t) u8ptr[ITEM_1] << BIT_8;
138 x |= (uint64_t) u8ptr[ITEM_2] << BIT_16;
139 x |= (uint64_t) u8ptr[ITEM_3] << BIT_24;
140 x |= (uint64_t) u8ptr[ITEM_4] << BIT_32;
141 x |= (uint64_t) u8ptr[ITEM_5] << BIT_40;
142 x |= (uint64_t) u8ptr[ITEM_6] << BIT_48;
143 x |= (uint64_t) u8ptr[ITEM_7] << BIT_56;
144 return x;
145 }
146
htobe16(uint8_t * p_buf,uint16_t x)147 void htobe16(uint8_t *p_buf, uint16_t x)
148 {
149 uint8_t *u8ptr;
150 u8ptr = p_buf;
151 u8ptr[ITEM_0] = (uint8_t)(x >> BIT_8);
152 u8ptr[ITEM_1] = (uint8_t) x;
153 }
154
htobe32(uint8_t * p_buf,uint32_t x)155 void htobe32(uint8_t *p_buf, uint32_t x)
156 {
157 uint8_t *u8ptr;
158 u8ptr = p_buf;
159 u8ptr[ITEM_0] = (uint8_t)(x >> BIT_24);
160 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_16);
161 u8ptr[ITEM_2] = (uint8_t)(x >> BIT_8);
162 u8ptr[ITEM_3] = (uint8_t) x;
163 }
164
htobe64(uint8_t * p_buf,uint64_t x)165 void htobe64(uint8_t *p_buf, uint64_t x)
166 {
167 uint8_t *u8ptr;
168 u8ptr = p_buf;
169 u8ptr[ITEM_0] = (uint8_t)(x >> BIT_56);
170 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_48);
171 u8ptr[ITEM_2] = (uint8_t)(x >> BIT_40);
172 u8ptr[ITEM_3] = (uint8_t)(x >> BIT_32);
173 u8ptr[ITEM_4] = (uint8_t)(x >> BIT_24);
174 u8ptr[ITEM_5] = (uint8_t)(x >> BIT_16);
175 u8ptr[ITEM_6] = (uint8_t)(x >> BIT_8);
176 u8ptr[ITEM_7] = (uint8_t) x;
177 }
178
be16toh(const uint8_t * p_buf)179 uint16_t be16toh(const uint8_t *p_buf)
180 {
181 const uint8_t *u8ptr;
182 uint16_t x;
183 u8ptr = p_buf;
184 x = (uint16_t) u8ptr[ITEM_0] << BIT_8;
185 x |= u8ptr[ITEM_1];
186 return x;
187 }
188
be32toh(const uint8_t * p_buf)189 uint32_t be32toh(const uint8_t *p_buf)
190 {
191 const uint8_t *u8ptr;
192 uint32_t x;
193 u8ptr = p_buf;
194 x = (uint32_t) u8ptr[ITEM_0] << BIT_24;
195 x |= (uint32_t) u8ptr[ITEM_1] << BIT_16;
196 x |= (uint32_t) u8ptr[ITEM_2] << BIT_8;
197 x |= u8ptr[ITEM_3];
198 return x;
199 }
200
be64toh(const uint8_t * p_buf)201 uint64_t be64toh(const uint8_t *p_buf)
202 {
203 const uint8_t *u8ptr;
204 uint64_t x;
205 u8ptr = p_buf;
206 x = (uint64_t) u8ptr[ITEM_0] << BIT_56;
207 x |= (uint64_t) u8ptr[ITEM_1] << BIT_48;
208 x |= (uint64_t) u8ptr[ITEM_2] << BIT_40;
209 x |= (uint64_t) u8ptr[ITEM_3] << BIT_32;
210 x |= (uint64_t) u8ptr[ITEM_4] << BIT_24;
211 x |= (uint64_t) u8ptr[ITEM_5] << BIT_16;
212 x |= (uint64_t) u8ptr[ITEM_6] << BIT_8;
213 x |= u8ptr[ITEM_7];
214 return x;
215 }
216
get_u8_inc(const uint8_t ** pp_buf)217 uint8_t get_u8_inc(const uint8_t **pp_buf)
218 {
219 const uint8_t *u8ptr;
220 uint8_t x;
221 u8ptr = *pp_buf;
222 x = u8ptr[ITEM_0];
223 *pp_buf += OFFSET_1;
224 return x;
225 }
226
get_u16_inc(const uint8_t ** pp_buf)227 uint16_t get_u16_inc(const uint8_t **pp_buf)
228 {
229 const uint8_t *u8ptr;
230 uint16_t x;
231 u8ptr = *pp_buf;
232 x = u8ptr[ITEM_0];
233 x |= (uint16_t) u8ptr[ITEM_1] << BIT_8;
234 *pp_buf += OFFSET_2;
235 return x;
236 }
237
get_u32_inc(const uint8_t ** pp_buf)238 uint32_t get_u32_inc(const uint8_t **pp_buf)
239 {
240 const uint8_t *u8ptr;
241 uint32_t x;
242 u8ptr = *pp_buf;
243 x = u8ptr[ITEM_0];
244 x |= (uint32_t) u8ptr[ITEM_1] << BIT_8;
245 x |= (uint32_t) u8ptr[ITEM_2] << BIT_16;
246 x |= (uint32_t) u8ptr[ITEM_3] << BIT_24;
247 *pp_buf += OFFSET_4;
248 return x;
249 }
250
put_u8_inc(uint8_t ** pp_buf,uint8_t x)251 void put_u8_inc(uint8_t **pp_buf, uint8_t x)
252 {
253 uint8_t *u8ptr;
254 u8ptr = *pp_buf;
255 u8ptr[ITEM_0] = x;
256 *pp_buf += OFFSET_1;
257 }
258
put_u16_inc(uint8_t ** pp_buf,uint16_t x)259 void put_u16_inc(uint8_t **pp_buf, uint16_t x)
260 {
261 uint8_t *u8ptr;
262 u8ptr = *pp_buf;
263 u8ptr[ITEM_0] = (uint8_t) x;
264 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_8);
265 *pp_buf += OFFSET_2;
266 }
267
put_u32_inc(uint8_t ** pp_buf,uint32_t x)268 void put_u32_inc(uint8_t **pp_buf, uint32_t x)
269 {
270 uint8_t *u8ptr;
271 u8ptr = *pp_buf;
272 u8ptr[ITEM_0] = (uint8_t) x;
273 u8ptr[ITEM_1] = (uint8_t)(x >> BIT_8);
274 u8ptr[ITEM_2] = (uint8_t)(x >> BIT_16);
275 u8ptr[ITEM_3] = (uint8_t)(x >> BIT_24);
276 *pp_buf += OFFSET_4;
277 }
278
279