]> FriiDump Source - friidump.git/blob - libfriidump/byteorder.h
PFES-REPO-008: Import FriiDump 0.5.3.5 Project Frankenstein baseline
[friidump.git] / libfriidump / byteorder.h
1 /***************************************************************************
2  *   Copyright (C) 2007 by Arep                                            *
3  *   Support is provided through the forums at                             *
4  *   http://wii.console-tribe.com                                          *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22 #include "misc.h"
23 #include <sys/types.h>
24
25 /*************************** BYTE SWAPPING MACROS ***************************/
26 // WORDS_BIGENDIAN is defined by the AC_C_BIGENDIAN autoconf macro, in case. On Windows please (un)define manually.
27 #ifdef WORDS_BIGENDIAN          //!< Swapping macros on big endian systems (Where host b. o. = network b. o.)
28
29 /* This machine's order to big endian */
30 #define bo_my2big_16(a) ((u_int16_t)(a))
31 #define bo_my2big_32(a) ((u_int32_t)(a))
32
33 /* This machine's order to little endian */
34 #define bo_my2little_16(a) ( \
35                 ((((u_int16_t) (a)) & 0x00FF) << 8) + \
36                 (((u_int16_t) (a)) >> 8) \
37         )
38 #define bo_my2little_32(a) ( \
39                 ((((u_int32_t) (a)) & 0x000000FF) << 24) + \
40                 ((((u_int32_t) (a)) & 0x0000FF00) << 8) + \
41                 ((((u_int32_t) (a)) & 0x00FF0000) >> 8) + \
42                 (((u_int32_t) (a)) >> 24) \
43         )
44 #else                           //!< Swapping macros on little endian systems
45         /* This machine's order to big endian */
46 #define bo_my2big_16(a) ( \
47                 ((((u_int16_t) (a)) & 0x00FF) << 8) + \
48                 (((u_int16_t) (a)) >> 8) \
49         )
50 #define bo_my2big_32(a) ( \
51                 ((((u_int32_t) (a)) & 0x000000FF) << 24) + \
52                 ((((u_int32_t) (a)) & 0x0000FF00) << 8) + \
53                 ((((u_int32_t) (a)) & 0x00FF0000) >> 8) + \
54                 (((u_int32_t) (a)) >> 24) \
55         )
56
57 /* This machine's order to little endian */
58 #define bo_my2little_16(a) ((u_int16_t)(a))
59 #define bo_my2little_32(a) ((u_int32_t)(a))
60 #endif
61
62 /* These will be handy */
63 /* Big endian to this machine's order */
64 #define bo_big2my_16(x) bo_my2big_16(x)
65 #define bo_big2my_32(x) bo_my2big_32(x)
66
67 /* Little endian to this machine's order */
68 #define bo_little2my_16(x) bo_my2little_16(x)
69 #define bo_little2my_32(x) bo_my2little_32(x)
70
71 /* There are the most useful ones */
72 #define my_htons(x)     bo_my2big_16(x)
73 #define my_htonl(x)     bo_my2big_32(x)
74 #define my_ntohs(x)     my_htons(x)
75 #define my_ntohl(x)     my_htonl(x)
76
77 /************************ END OF BYTE SWAPPING MACROS ***********************/