Mercurial > pihelp
comparison byteordering.c @ 30:320c8cc7df5a
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 27 Jun 2013 00:12:01 +0800 |
parents | 5f9a40d6991b |
children |
comparison
equal
deleted
inserted
replaced
29:22badb6239e0 | 30:320c8cc7df5a |
---|---|
1 | |
2 /* | |
3 * Copyright (c) 2006-2012 by Roland Riegel <[email protected]> | |
4 * | |
5 * This file is free software; you can redistribute it and/or modify | |
6 * it under the terms of either the GNU General Public License version 2 | |
7 * or the GNU Lesser General Public License version 2.1, both as | |
8 * published by the Free Software Foundation. | |
9 */ | |
10 | |
11 #include "byteordering.h" | |
12 | |
13 /** | |
14 * \addtogroup byteordering | |
15 * | |
16 * Architecture-dependent handling of byte-ordering. | |
17 * | |
18 * @{ | |
19 */ | |
20 /** | |
21 * \file | |
22 * Byte-order handling implementation (license: GPLv2 or LGPLv2.1) | |
23 * | |
24 * \author Roland Riegel | |
25 */ | |
26 | |
27 #if DOXYGEN || SWAP_NEEDED | |
28 | |
29 /** | |
30 * \internal | |
31 * Swaps the bytes of a 16-bit integer. | |
32 * | |
33 * \param[in] i A 16-bit integer which to swap. | |
34 * \returns The swapped 16-bit integer. | |
35 */ | |
36 uint16_t swap16(uint16_t i) | |
37 { | |
38 return SWAP16(i); | |
39 } | |
40 | |
41 /** | |
42 * \internal | |
43 * Swaps the bytes of a 32-bit integer. | |
44 * | |
45 * \param[in] i A 32-bit integer which to swap. | |
46 * \returns The swapped 32-bit integer. | |
47 */ | |
48 uint32_t swap32(uint32_t i) | |
49 { | |
50 return SWAP32(i); | |
51 } | |
52 | |
53 #endif | |
54 | |
55 /** | |
56 * Reads a 16-bit integer from memory in little-endian byte order. | |
57 * | |
58 * \param[in] p Pointer from where to read the integer. | |
59 * \returns The 16-bit integer read from memory. | |
60 */ | |
61 uint16_t read16(const uint8_t* p) | |
62 { | |
63 return (((uint16_t) p[1]) << 8) | | |
64 (((uint16_t) p[0]) << 0); | |
65 } | |
66 | |
67 /** | |
68 * Reads a 32-bit integer from memory in little-endian byte order. | |
69 * | |
70 * \param[in] p Pointer from where to read the integer. | |
71 * \returns The 32-bit integer read from memory. | |
72 */ | |
73 uint32_t read32(const uint8_t* p) | |
74 { | |
75 return (((uint32_t) p[3]) << 24) | | |
76 (((uint32_t) p[2]) << 16) | | |
77 (((uint32_t) p[1]) << 8) | | |
78 (((uint32_t) p[0]) << 0); | |
79 } | |
80 | |
81 /** | |
82 * Writes a 16-bit integer into memory in little-endian byte order. | |
83 * | |
84 * \param[in] p Pointer where to write the integer to. | |
85 * \param[in] i The 16-bit integer to write. | |
86 */ | |
87 void write16(uint8_t* p, uint16_t i) | |
88 { | |
89 p[1] = (uint8_t) ((i & 0xff00) >> 8); | |
90 p[0] = (uint8_t) ((i & 0x00ff) >> 0); | |
91 } | |
92 | |
93 /** | |
94 * Writes a 32-bit integer into memory in little-endian byte order. | |
95 * | |
96 * \param[in] p Pointer where to write the integer to. | |
97 * \param[in] i The 32-bit integer to write. | |
98 */ | |
99 void write32(uint8_t* p, uint32_t i) | |
100 { | |
101 p[3] = (uint8_t) ((i & 0xff000000) >> 24); | |
102 p[2] = (uint8_t) ((i & 0x00ff0000) >> 16); | |
103 p[1] = (uint8_t) ((i & 0x0000ff00) >> 8); | |
104 p[0] = (uint8_t) ((i & 0x000000ff) >> 0); | |
105 } | |
106 | |
107 /** | |
108 * @} | |
109 */ | |
110 |