Line data Source code
1 : // fielddecode.c was generated by ProtoGen version 3.5.c
2 :
3 : /*
4 : * This file is free software: you can redistribute it and/or modify it
5 : * under the terms of the GNU General Public License as published by the
6 : * Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This file is distributed in the hope that it will be useful, but
10 : * WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 : * See the GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License along
15 : * with this program. If not, see <http://www.gnu.org/licenses/>.
16 : *
17 : * Author: Oliver Walters / Currawong Engineering Pty Ltd
18 : */
19 :
20 : #include "fielddecode.h"
21 :
22 : /*!
23 : * Decode a null terminated string from a byte stream
24 : * \param string receives the deocded null-terminated string.
25 : * \param bytes is a pointer to the byte stream to be decoded.
26 : * \param index gives the location of the first byte in the byte stream, and
27 : * will be incremented by the number of bytes decoded when this function
28 : * is complete.
29 : * \param maxLength is the maximum number of bytes that can be decoded.
30 : * maxLength includes the null terminator, which is always applied.
31 : * \param fixedLength should be 1 to force the number of bytes decoded to be
32 : * exactly equal to maxLength.
33 : */
34 0 : void stringFromBytes(char* string, const uint8_t* bytes, int* index, int maxLength, int fixedLength)
35 : {
36 : int i;
37 :
38 : // increment byte pointer for starting point
39 0 : bytes += *index;
40 :
41 0 : for(i = 0; i < maxLength - 1; i++)
42 : {
43 0 : if(bytes[i] == 0)
44 0 : break;
45 : else
46 0 : string[i] = (char)bytes[i];
47 : }
48 :
49 : // Make sure we include null terminator
50 0 : string[i++] = 0;
51 :
52 0 : if(fixedLength)
53 0 : (*index) += maxLength;
54 : else
55 0 : (*index) += i;
56 :
57 0 : }// stringFromBytes
58 :
59 :
60 : /*!
61 : * Copy an array of bytes from a byte stream without changing the order.
62 : * \param data receives the copied bytes
63 : * \param bytes is a pointer to the byte stream to be copied from.
64 : * \param index gives the location of the first byte in the byte stream, and
65 : * will be incremented by num when this function is complete.
66 : * \param num is the number of bytes to copy
67 : */
68 0 : void bytesFromBeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num)
69 : {
70 : // increment byte pointer for starting point
71 0 : bytes += (*index);
72 :
73 : // Increment byte index to indicate number of bytes copied
74 0 : (*index) += num;
75 :
76 : // Copy the bytes without changing the order
77 0 : while(num > 0)
78 : {
79 0 : *(data++) = *(bytes++);
80 0 : num--;
81 : }
82 :
83 0 : }// bytesFromBeBytes
84 :
85 :
86 : /*!
87 : * Copy an array of bytes from a byte stream, reversing the order.
88 : * \param data receives the copied bytes
89 : * \param bytes is a pointer to the byte stream to be copied.
90 : * \param index gives the location of the first byte in the byte stream, and
91 : * will be incremented by num when this function is complete.
92 : * \param num is the number of bytes to copy
93 : */
94 0 : void bytesFromLeBytes(uint8_t* data, const uint8_t* bytes, int* index, int num)
95 : {
96 : // increment byte pointer for starting point
97 0 : bytes += (*index);
98 :
99 : // Increment byte index to indicate number of bytes copied
100 0 : (*index) += num;
101 :
102 : // To encode as "little endian bytes", (a nonsensical statement), reverse the byte order
103 0 : bytes += (num - 1);
104 :
105 : // Copy the bytes, reversing the order
106 0 : while(num > 0)
107 : {
108 0 : *(data++) = *(bytes--);
109 0 : num--;
110 : }
111 :
112 0 : }// bytesFromLeBytes
113 :
114 : /*!
115 : * Decode a 4 byte float from a big endian byte stream.
116 : * \param bytes is a pointer to the byte stream which contains the encoded data.
117 : * \param index gives the location of the first byte in the byte stream, and
118 : * will be incremented by 4 when this function is complete.
119 : * \return the number decoded from the byte stream
120 : */
121 0 : float float32FromBeBytes(const uint8_t* bytes, int* index)
122 : {
123 : union
124 : {
125 : float floatValue;
126 : uint32_t integerValue;
127 : }field;
128 :
129 0 : field.integerValue = uint32FromBeBytes(bytes, index);
130 :
131 0 : return field.floatValue;
132 : }
133 :
134 : /*!
135 : * Decode a 4 byte float from a little endian byte stream.
136 : * \param bytes is a pointer to the byte stream which contains the encoded data.
137 : * \param index gives the location of the first byte in the byte stream, and
138 : * will be incremented by 4 when this function is complete.
139 : * \return the number decoded from the byte stream
140 : */
141 0 : float float32FromLeBytes(const uint8_t* bytes, int* index)
142 : {
143 : union
144 : {
145 : float floatValue;
146 : uint32_t integerValue;
147 : }field;
148 :
149 0 : field.integerValue = uint32FromLeBytes(bytes, index);
150 :
151 0 : return field.floatValue;
152 : }
153 :
154 : /*!
155 : * Decode a unsigned 4 byte integer from a big endian byte stream.
156 : * \param bytes is a pointer to the byte stream which contains the encoded data.
157 : * \param index gives the location of the first byte in the byte stream, and
158 : * will be incremented by 4 when this function is complete.
159 : * \return the number decoded from the byte stream
160 : */
161 0 : uint32_t uint32FromBeBytes(const uint8_t* bytes, int* index)
162 : {
163 : uint32_t number;
164 :
165 : // increment byte pointer for starting point
166 0 : bytes += *index;
167 :
168 0 : number = *(bytes++);
169 0 : number = (number << 8) | *(bytes++);
170 0 : number = (number << 8) | *(bytes++);
171 0 : number = (number << 8) | *bytes;
172 :
173 0 : (*index) += 4;
174 :
175 0 : return number;
176 : }
177 :
178 : /*!
179 : * Decode a unsigned 4 byte integer from a little endian byte stream.
180 : * \param bytes is a pointer to the byte stream which contains the encoded data.
181 : * \param index gives the location of the first byte in the byte stream, and
182 : * will be incremented by 4 when this function is complete.
183 : * \return the number decoded from the byte stream
184 : */
185 0 : uint32_t uint32FromLeBytes(const uint8_t* bytes, int* index)
186 : {
187 : uint32_t number;
188 :
189 : // increment byte pointer for starting point
190 0 : bytes += (*index) + 3;
191 :
192 0 : number = *(bytes--);
193 0 : number = (number << 8) | *(bytes--);
194 0 : number = (number << 8) | *(bytes--);
195 0 : number = (number << 8) | *bytes;
196 :
197 0 : (*index) += 4;
198 :
199 0 : return number;
200 : }
201 :
202 : /*!
203 : * Decode a signed 4 byte integer from a big endian byte stream.
204 : * \param bytes is a pointer to the byte stream which contains the encoded data.
205 : * \param index gives the location of the first byte in the byte stream, and
206 : * will be incremented by 4 when this function is complete.
207 : * \return the number decoded from the byte stream
208 : */
209 0 : int32_t int32FromBeBytes(const uint8_t* bytes, int* index)
210 : {
211 : int32_t number;
212 :
213 : // increment byte pointer for starting point
214 0 : bytes += *index;
215 :
216 0 : number = *(bytes++);
217 0 : number = (number << 8) | *(bytes++);
218 0 : number = (number << 8) | *(bytes++);
219 0 : number = (number << 8) | *bytes;
220 :
221 0 : (*index) += 4;
222 :
223 0 : return number;
224 : }
225 :
226 : /*!
227 : * Decode a signed 4 byte integer from a little endian byte stream.
228 : * \param bytes is a pointer to the byte stream which contains the encoded data.
229 : * \param index gives the location of the first byte in the byte stream, and
230 : * will be incremented by 4 when this function is complete.
231 : * \return the number decoded from the byte stream
232 : */
233 0 : int32_t int32FromLeBytes(const uint8_t* bytes, int* index)
234 : {
235 : int32_t number;
236 :
237 : // increment byte pointer for starting point
238 0 : bytes += (*index) + 3;
239 :
240 0 : number = *(bytes--);
241 0 : number = (number << 8) | *(bytes--);
242 0 : number = (number << 8) | *(bytes--);
243 0 : number = (number << 8) | *bytes;
244 :
245 0 : (*index) += 4;
246 :
247 0 : return number;
248 : }
249 :
250 : /*!
251 : * Decode a unsigned 3 byte integer from a big endian byte stream.
252 : * \param bytes is a pointer to the byte stream which contains the encoded data.
253 : * \param index gives the location of the first byte in the byte stream, and
254 : * will be incremented by 3 when this function is complete.
255 : * \return the number decoded from the byte stream
256 : */
257 0 : uint32_t uint24FromBeBytes(const uint8_t* bytes, int* index)
258 : {
259 : uint32_t number;
260 :
261 : // increment byte pointer for starting point
262 0 : bytes += *index;
263 :
264 0 : number = *(bytes++);
265 0 : number = (number << 8) | *(bytes++);
266 0 : number = (number << 8) | *bytes;
267 :
268 0 : (*index) += 3;
269 :
270 0 : return number;
271 : }
272 :
273 : /*!
274 : * Decode a unsigned 3 byte integer from a little endian byte stream.
275 : * \param bytes is a pointer to the byte stream which contains the encoded data.
276 : * \param index gives the location of the first byte in the byte stream, and
277 : * will be incremented by 3 when this function is complete.
278 : * \return the number decoded from the byte stream
279 : */
280 0 : uint32_t uint24FromLeBytes(const uint8_t* bytes, int* index)
281 : {
282 : uint32_t number;
283 :
284 : // increment byte pointer for starting point
285 0 : bytes += (*index) + 2;
286 :
287 0 : number = *(bytes--);
288 0 : number = (number << 8) | *(bytes--);
289 0 : number = (number << 8) | *bytes;
290 :
291 0 : (*index) += 3;
292 :
293 0 : return number;
294 : }
295 :
296 : /*!
297 : * Decode a signed 3 byte integer from a big endian byte stream.
298 : * \param bytes is a pointer to the byte stream which contains the encoded data.
299 : * \param index gives the location of the first byte in the byte stream, and
300 : * will be incremented by 3 when this function is complete.
301 : * \return the number decoded from the byte stream
302 : */
303 0 : int32_t int24FromBeBytes(const uint8_t* bytes, int* index)
304 : {
305 : // Signed value in non-native size, requires sign extension. Algorithm
306 : // courtesty of: https://graphics.stanford.edu/~seander/bithacks.html
307 0 : const int32_t m = 0x00800000;
308 : int32_t number;
309 :
310 : // increment byte pointer for starting point
311 0 : bytes += *index;
312 :
313 0 : number = *(bytes++);
314 0 : number = (number << 8) | *(bytes++);
315 0 : number = (number << 8) | *bytes;
316 :
317 0 : (*index) += 3;
318 :
319 0 : return (number ^ m) - m;
320 : }
321 :
322 : /*!
323 : * Decode a signed 3 byte integer from a little endian byte stream.
324 : * \param bytes is a pointer to the byte stream which contains the encoded data.
325 : * \param index gives the location of the first byte in the byte stream, and
326 : * will be incremented by 3 when this function is complete.
327 : * \return the number decoded from the byte stream
328 : */
329 0 : int32_t int24FromLeBytes(const uint8_t* bytes, int* index)
330 : {
331 : // Signed value in non-native size, requires sign extension. Algorithm
332 : // courtesty of: https://graphics.stanford.edu/~seander/bithacks.html
333 0 : const int32_t m = 0x00800000;
334 : int32_t number;
335 :
336 : // increment byte pointer for starting point
337 0 : bytes += (*index) + 2;
338 :
339 0 : number = *(bytes--);
340 0 : number = (number << 8) | *(bytes--);
341 0 : number = (number << 8) | *bytes;
342 :
343 0 : (*index) += 3;
344 :
345 0 : return (number ^ m) - m;
346 : }
347 :
348 : /*!
349 : * Decode a unsigned 2 byte integer from a big endian byte stream.
350 : * \param bytes is a pointer to the byte stream which contains the encoded data.
351 : * \param index gives the location of the first byte in the byte stream, and
352 : * will be incremented by 2 when this function is complete.
353 : * \return the number decoded from the byte stream
354 : */
355 0 : uint16_t uint16FromBeBytes(const uint8_t* bytes, int* index)
356 : {
357 : uint16_t number;
358 :
359 : // increment byte pointer for starting point
360 0 : bytes += *index;
361 :
362 0 : number = *(bytes++);
363 0 : number = (number << 8) | *bytes;
364 :
365 0 : (*index) += 2;
366 :
367 0 : return number;
368 : }
369 :
370 : /*!
371 : * Decode a unsigned 2 byte integer from a little endian byte stream.
372 : * \param bytes is a pointer to the byte stream which contains the encoded data.
373 : * \param index gives the location of the first byte in the byte stream, and
374 : * will be incremented by 2 when this function is complete.
375 : * \return the number decoded from the byte stream
376 : */
377 0 : uint16_t uint16FromLeBytes(const uint8_t* bytes, int* index)
378 : {
379 : uint16_t number;
380 :
381 : // increment byte pointer for starting point
382 0 : bytes += (*index) + 1;
383 :
384 0 : number = *(bytes--);
385 0 : number = (number << 8) | *bytes;
386 :
387 0 : (*index) += 2;
388 :
389 0 : return number;
390 : }
391 :
392 : /*!
393 : * Decode a signed 2 byte integer from a big endian byte stream.
394 : * \param bytes is a pointer to the byte stream which contains the encoded data.
395 : * \param index gives the location of the first byte in the byte stream, and
396 : * will be incremented by 2 when this function is complete.
397 : * \return the number decoded from the byte stream
398 : */
399 0 : int16_t int16FromBeBytes(const uint8_t* bytes, int* index)
400 : {
401 : int16_t number;
402 :
403 : // increment byte pointer for starting point
404 0 : bytes += *index;
405 :
406 0 : number = *(bytes++);
407 0 : number = (number << 8) | *bytes;
408 :
409 0 : (*index) += 2;
410 :
411 0 : return number;
412 : }
413 :
414 : /*!
415 : * Decode a signed 2 byte integer from a little endian byte stream.
416 : * \param bytes is a pointer to the byte stream which contains the encoded data.
417 : * \param index gives the location of the first byte in the byte stream, and
418 : * will be incremented by 2 when this function is complete.
419 : * \return the number decoded from the byte stream
420 : */
421 0 : int16_t int16FromLeBytes(const uint8_t* bytes, int* index)
422 : {
423 : int16_t number;
424 :
425 : // increment byte pointer for starting point
426 0 : bytes += (*index) + 1;
427 :
428 0 : number = *(bytes--);
429 0 : number = (number << 8) | *bytes;
430 :
431 0 : (*index) += 2;
432 :
433 0 : return number;
434 : }
435 :
436 : // end of fielddecode.c
|