libgphoto2 photo camera library (libgphoto2) Internals  2.5.26
gphoto2-endian.h
Go to the documentation of this file.
1 /* This file is generated automatically by configure */
2 /* It is valid only for the system type x86_64-w64-mingw32 */
3 
4 #ifndef __BYTEORDER_H
5 #define __BYTEORDER_H
6 
7 /* Use these as generic byteswapping macros on this little endian system */
8 /* on windows we might not have ntohs / ntohl without including winsock.dll,
9  * so use generic macros */
10 #ifdef __HAVE_NTOHL
11 # define swap16(x) htons(x)
12 # define swap32(x) htonl(x)
13 #else
14 # define swap16(x) ((uint16_t)(((x) << 8) | ((uint16_t)(x) >> 8)))
15 # define swap32(x) ((uint32_t)((((uint32_t)(x) << 24) & 0xff000000UL) | \
16  (((uint32_t)(x) << 8) & 0x00ff0000UL) | \
17  (((x) >> 8) & 0x0000ff00UL) | \
18  (((x) >> 24) & 0x000000ffUL)))
19 #endif
20 /* No optimized 64 bit byte swapping macro is available */
21 #define swap64(x) ((uint64_t)((((uint64_t)(x) << 56) & 0xff00000000000000ULL) | \
22  (((uint64_t)(x) << 40) & 0x00ff000000000000ULL) | \
23  (((uint64_t)(x) << 24) & 0x0000ff0000000000ULL) | \
24  (((uint64_t)(x) << 8) & 0x000000ff00000000ULL) | \
25  (((x) >> 8) & 0x00000000ff000000ULL) | \
26  (((x) >> 24) & 0x0000000000ff0000ULL) | \
27  (((x) >> 40) & 0x000000000000ff00ULL) | \
28  (((x) >> 56) & 0x00000000000000ffULL)))
29 
30 /* The byte swapping macros have the form: */
31 /* EENN[a]toh or htoEENN[a] where EE is be (big endian) or */
32 /* le (little-endian), NN is 16 or 32 (number of bits) and a, */
33 /* if present, indicates that the endian side is a pointer to an */
34 /* array of uint8_t bytes instead of an integer of the specified length. */
35 /* h refers to the host's ordering method. */
36 
37 /* So, to convert a 32-bit integer stored in a buffer in little-endian */
38 /* format into a uint32_t usable on this machine, you could use: */
39 /* uint32_t value = le32atoh(&buf[3]); */
40 /* To put that value back into the buffer, you could use: */
41 /* htole32a(&buf[3], value); */
42 
43 /* Define aliases for the standard byte swapping macros */
44 /* Arguments to these macros must be properly aligned on natural word */
45 /* boundaries in order to work properly on all architectures */
46 #ifndef htobe16
47 # ifdef __HAVE_NTOHL
48 # define htobe16(x) htons(x)
49 # else
50 # ifdef WORDS_BIGENDIAN
51 # define htobe16(x) (x)
52 # else
53 # define htobe16(x) swap16(x)
54 # endif
55 # endif
56 #endif
57 #ifndef htobe32
58 # ifdef __HAVE_NTOHL
59 # define htobe32(x) htonl(x)
60 # else
61 # ifdef WORDS_BIGENDIAN
62 # define htobe32(x) (x)
63 # else
64 # define htobe32(x) swap32(x)
65 # endif
66 # endif
67 #endif
68 #ifndef be16toh
69 # define be16toh(x) htobe16(x)
70 #endif
71 #ifndef be32toh
72 # define be32toh(x) htobe32(x)
73 #endif
74 
75 #define HTOBE16(x) (x) = htobe16(x)
76 #define HTOBE32(x) (x) = htobe32(x)
77 #define BE32TOH(x) (x) = be32toh(x)
78 #define BE16TOH(x) (x) = be16toh(x)
79 
80 /* On little endian machines, these macros are null */
81 #ifndef htole16
82 # define htole16(x) (x)
83 #endif
84 #ifndef htole32
85 # define htole32(x) (x)
86 #endif
87 #ifndef htole64
88 # define htole64(x) (x)
89 #endif
90 #ifndef le16toh
91 # define le16toh(x) (x)
92 #endif
93 #ifndef le32toh
94 # define le32toh(x) (x)
95 #endif
96 #ifndef le64toh
97 # define le64toh(x) (x)
98 #endif
99 
100 #define HTOLE16(x) (void) (x)
101 #define HTOLE32(x) (void) (x)
102 #define HTOLE64(x) (void) (x)
103 #define LE16TOH(x) (void) (x)
104 #define LE32TOH(x) (void) (x)
105 #define LE64TOH(x) (void) (x)
106 
107 /* These don't have standard aliases */
108 #ifndef htobe64
109 # define htobe64(x) swap64(x)
110 #endif
111 #ifndef be64toh
112 # define be64toh(x) swap64(x)
113 #endif
114 
115 #define HTOBE64(x) (x) = htobe64(x)
116 #define BE64TOH(x) (x) = be64toh(x)
117 
118 /* Define the C99 standard length-specific integer types */
119 #include <_stdint.h>
120 
121 /* Here are some macros to create integers from a byte array */
122 /* These are used to get and put integers from/into a uint8_t array */
123 /* with a specific endianness. This is the most portable way to generate */
124 /* and read messages to a network or serial device. Each member of a */
125 /* packet structure must be handled separately. */
126 
127 /* Non-optimized but portable macros */
128 #define be16atoh(x) ((uint16_t)(((x)[0]<<8)|(x)[1]))
129 #define be32atoh(x) ((uint32_t)(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]))
130 #define be64atoh_x(x,off,shift) (((uint64_t)((x)[off]))<<shift)
131 #define be64atoh(x) ((uint64_t)(be64atoh_x(x,0,56)|be64atoh_x(x,1,48)|be64atoh_x(x,2,40)| \
132  be64atoh_x(x,3,32)|be64atoh_x(x,4,24)|be64atoh_x(x,5,16)|be64atoh_x(x,6,8)|((x)[7])))
133 #define le16atoh(x) ((uint16_t)(((x)[1]<<8)|(x)[0]))
134 #define le32atoh(x) ((uint32_t)(((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
135 #define le64atoh_x(x,off,shift) (((uint64_t)(x)[off])<<shift)
136 #define le64atoh(x) ((uint64_t)(le64atoh_x(x,7,56)|le64atoh_x(x,6,48)|le64atoh_x(x,5,40)| \
137  le64atoh_x(x,4,32)|le64atoh_x(x,3,24)|le64atoh_x(x,2,16)|le64atoh_x(x,1,8)|((x)[0])))
138 
139 #define htobe16a(a,x) (a)[0]=(uint8_t)((x)>>8), (a)[1]=(uint8_t)(x)
140 #define htobe32a(a,x) (a)[0]=(uint8_t)((x)>>24), (a)[1]=(uint8_t)((x)>>16), \
141  (a)[2]=(uint8_t)((x)>>8), (a)[3]=(uint8_t)(x)
142 #define htobe64a(a,x) (a)[0]=(uint8_t)((x)>>56), (a)[1]=(uint8_t)((x)>>48), \
143  (a)[2]=(uint8_t)((x)>>40), (a)[3]=(uint8_t)((x)>>32), \
144  (a)[4]=(uint8_t)((x)>>24), (a)[5]=(uint8_t)((x)>>16), \
145  (a)[6]=(uint8_t)((x)>>8), (a)[7]=(uint8_t)(x)
146 #define htole16a(a,x) (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
147 #define htole32a(a,x) (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
148  (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
149 #define htole64a(a,x) (a)[7]=(uint8_t)((x)>>56), (a)[6]=(uint8_t)((x)>>48), \
150  (a)[5]=(uint8_t)((x)>>40), (a)[4]=(uint8_t)((x)>>32), \
151  (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
152  (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
153 
154 #endif /*__BYTEORDER_H*/