annotate byteordering.c @ 26:292cd17de2a4

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