summaryrefslogtreecommitdiffstats
path: root/private/inc/byteordr.hxx
blob: 009f56c5e0998f8a1aa331bb90bf937ad47be8ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//+--------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 1992 - 1996.
//
//  File:       byteordr.hxx
//
//  Contents:   Byte swapping functions.
//              These functions can swap all of the base types,
//              and some special cases.  There is always an
//              overloaded version which accepts a pointer to the
//              data, and performs a swap in place.  Where possible,
//              there is also an overload which returns the swapped
//              value without modifying the input.
//
//---------------------------------------------------------------

#ifndef __BYTEORDER_HXX_
#define __BYTEORDER_HXX_

#include <limits.h> 

//
// BYTE Byte-Swap
//

// These exist so that you can call ByteSwap(OLECHAR) without
// knowning if OLECHAR is a CHAR or a WCHAR.

inline BYTE ByteSwap( BYTE b )
{
    return(b);
}

inline VOID ByteSwap( BYTE *pb )
{
    *pb = ByteSwap(*pb);
}

//
// WORD Byte-Swap
//

#ifndef LOBYTE

#   define LOBYTE(a)  (unsigned char)((a) & ((unsigned)~0 >> sizeof(BYTE)*8 ))
#   define HIBYTE(a)  (unsigned char)((unsigned)(a) >> sizeof(BYTE)*8 )

#   define LOWORD(a)  (WORD)( (a) & ( (WORD)~0 >> sizeof(WORD)*8 ))
#   define HIWORD(a)  (WORD)( (WORD)(a) >> sizeof(WORD)*8 )

#   define LODWORD(a) (DWORD)( (a) & ( (DWORD)~0 >> sizeof(DWORD)*8 ))
#   define HIDWORD(a) (DWORD)( (DWORD)(a) >> sizeof(DWORD)*8 )

#endif // #ifndef LOBYTE

inline WORD ByteSwap( WORD w )
{
    return( (USHORT) ( (LOBYTE(w) << 8 )
                     |
                     HIBYTE(w)) );
}

inline VOID ByteSwap( WORD *pw )
{
    *pw = ByteSwap(*pw);
}

//
// DWORD Byte-Swap
//

#define BYTE_MASK_A_C_  0xff00ff00
#define BYTE_MASK__B_D  0x00ff00ff
#define BYTE_MASK_AB__  0xffff0000
#define BYTE_MASK___CD  0x0000ffff

inline DWORD ByteSwap( DWORD dwOriginal )
{
    ULONG dwSwapped;

    // ABCD => BADC

    dwSwapped = (( (dwOriginal) & BYTE_MASK_A_C_ ) >> 8 )
                |
                (( (dwOriginal) & BYTE_MASK__B_D ) << 8 );

    // BADC => DCBA

    dwSwapped = (( dwSwapped & BYTE_MASK_AB__ ) >> 16 )
                |
                (( dwSwapped & BYTE_MASK___CD ) << 16 );

    return( dwSwapped );
}

inline VOID ByteSwap( DWORD *pdw )
{
    *pdw = ByteSwap(*pdw);
}


//
// LONGLONG Byte-Swap
//

#define BYTE_MASK_A_C_E_G_  0xff00ff00ff00ff00
#define BYTE_MASK__B_D_F_H  0x00ff00ff00ff00ff
#define BYTE_MASK_AB__EF__  0xffff0000ffff0000
#define BYTE_MASK___CD__GH  0x0000ffff0000ffff
#define BYTE_MASK_ABCD____  0xffffffff00000000
#define BYTE_MASK_____EFGH  0x00000000ffffffff

inline LONGLONG ByteSwap( LONGLONG llOriginal )
{
    LONGLONG llSwapped;

#ifdef _MAC

    *((DWORD*) &llSwapped) = *((DWORD*) &llOriginal + 1);
    *((DWORD*) &llSwapped + 1) = *((DWORD*) &llOriginal);

    ByteSwap( (DWORD*) &llSwapped );
    ByteSwap( (DWORD*) &llSwapped + 1 );

#else

    // ABCDEFGH => BADCFEHG

    llSwapped = (( (llOriginal) & BYTE_MASK_A_C_E_G_ ) >> 8 )
                |
                (( (llOriginal) & BYTE_MASK__B_D_F_H ) << 8 );

    // BADCFEHG => DCBAHGFE

    llSwapped = (( llSwapped & BYTE_MASK_AB__EF__ ) >> 16 )
                |
                (( llSwapped & BYTE_MASK___CD__GH ) << 16 );

    // DCBAHGFE => HGFEDCBA

    llSwapped = (( llSwapped & BYTE_MASK_ABCD____ ) >> 32 )
                |
                (( llSwapped & BYTE_MASK_____EFGH ) << 32 );

#endif

    return( llSwapped );
}

inline VOID ByteSwap( LONGLONG *pll )
{
    *pll = ByteSwap( *pll );
}

//
// GUID Byte-swap
//

inline VOID ByteSwap( GUID *pguid)
{
    ByteSwap(&pguid->Data1);
    ByteSwap(&pguid->Data2);
    ByteSwap(&pguid->Data3);
}

//
// FILETIME Byte-Swap
//

inline VOID ByteSwap(FILETIME *pfileTime)
{
    ByteSwap(&pfileTime->dwLowDateTime);
    ByteSwap(&pfileTime->dwHighDateTime);
}


#endif  // !__BYTEORDER_HXX_