1 /*++
2
3 Copyright (c) 2002-2012 Alexandr A. Telyatnikov (Alter)
4
5 Module Name:
6 tools.h
7
8 Abstract:
9 This header contains some useful definitions for data manipulation.
10
11 Author:
12 Alexander A. Telyatnikov (Alter)
13
14 Environment:
15 kernel mode only
16
17 Notes:
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 Revision History:
31
32 Licence:
33 GPLv2
34
35 --*/
36
37 #ifndef __TOOLS_H__
38 #define __TOOLS_H__
39
40 #pragma pack(push, 1)
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif //__cplusplus
45
46 //----------------
47
48 #ifndef USER_MODE
49 #include <ntddk.h> // various NT definitions
50 #endif //USER_MODE
51
52 //----------------
53
54 #ifndef FOUR_BYTE_DEFINED
55 #define FOUR_BYTE_DEFINED
56 typedef struct _FOUR_BYTE {
57 UCHAR Byte0;
58 UCHAR Byte1;
59 UCHAR Byte2;
60 UCHAR Byte3;
61 } FOUR_BYTE, *PFOUR_BYTE;
62 #endif //FOUR_BYTE_DEFINED
63
64 #ifdef _DEBUG
65
66 #ifndef DBG
67 #define DBG
68 #endif // DBG
69
70 #else // _DEBUG
71
72 #ifdef DBG
73 #undef DBG
74 #endif // DBG
75
76 #endif // _DEBUG
77
78 // This macro has the effect of Bit = log2(Data)
79
80 #define WHICH_BIT(Data, Bit) { \
81 for (Bit = 0; Bit < 32; Bit++) { \
82 if ((Data >> Bit) == 1) { \
83 break; \
84 } \
85 } \
86 }
87
88 #define PAGE_MASK (PAGE_SIZE-1)
89
90 #define ntohs(x) ( (((USHORT)x[0])<<8) | x[1] )
91 #define PLAY_ACTIVE(DeviceExtension) (((PCDROM_DATA)(DeviceExtension + 1))->PlayActive)
92 #define MSF_TO_LBA(Minutes,Seconds,Frames) \
93 (ULONG)((60 * 75 * (Minutes)) + (75 * (Seconds)) + ((Frames) - 150))
94 #define LBA_TO_MSF(Lba,Minutes,Seconds,Frames) \
95 { \
96 (Minutes) = (UCHAR)(Lba / (60 * 75)); \
97 (Seconds) = (UCHAR)((Lba % (60 * 75)) / 75); \
98 (Frames) = (UCHAR)((Lba % (60 * 75)) % 75); \
99 }
100 #define DEC_TO_BCD(x) (((x / 10) << 4) + (x % 10))
101
102
103 #ifdef DBG
104
105 #define KdDump(a,b) \
106 if((a)!=NULL) { \
107 ULONG i; \
108 for(i=0; i<(b); i++) { \
109 ULONG c; \
110 c = (ULONG)(*(((PUCHAR)(a))+i)); \
111 KdPrint(("%2.2x ",c)); \
112 if ((i & 0x0f) == 0x0f) KdPrint(("\n")); \
113 } \
114 KdPrint(("\n")); \
115 }
116
117 #define DbgDumpRegTranslation(chan, idx) \
118 KdPrint2((PRINT_PREFIX \
119 " IO_%#x (%#x), %s:\n", \
120 idx, \
121 chan->RegTranslation[idx].Addr, \
122 chan->RegTranslation[idx].Proc ? "Proc" : ( \
123 chan->RegTranslation[idx].MemIo ? "Mem" : "IO"))); \
124
125 #define BrutePoint() { ASSERT(0); }
126
127 #define DbgAllocatePool(x,y) ExAllocatePool(x,y)
128 #define DbgFreePool(x) ExFreePool(x)
129 #define DbgAllocatePoolWithTag(a,b,c) ExAllocatePoolWithTag(a,b,c)
130
131 #else
132
133 #define KdDump(a,b) {}
134
135 #define DbgDumpRegTranslation(chan, idx) {}
136
137 #define DbgAllocatePool(x,y) ExAllocatePool(x,y)
138 #define DbgFreePool(x) ExFreePool(x)
139 #define DbgAllocatePoolWithTag(a,b,c) ExAllocatePoolWithTag(a,b,c)
140
141 #define BrutePoint() {}
142
143 #endif //DBG
144
145
146
147 #define WAIT_FOR_XXX_EMU_DELAY DEF_I64(5000000) // 0.5 s
148
149 // neat little hacks to count number of bits set efficiently
CountOfSetBitsUChar(UCHAR _X)150 __inline ULONG CountOfSetBitsUChar(UCHAR _X)
151 { ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
CountOfSetBitsULong(ULONG _X)152 __inline ULONG CountOfSetBitsULong(ULONG _X)
153 { ULONG i = 0; while (_X) { _X &= _X - 1; i++; } return i; }
154
155 #ifndef max
156 #define max(a,b) (((a) > (b)) ? (a) : (b))
157 #endif //max
158
159 #ifndef min
160 #define min(a,b) (((a) < (b)) ? (a) : (b))
161 #endif //min
162 //#define abs(a) (((a) < 0) ? (-(a)) : (a))
163
164 /*
165 extern ULONG MajorVersion;
166 extern ULONG MinorVersion;
167 extern ULONG BuildNumber;
168 extern ULONG SPVersion;
169 */
170
171 #ifdef __cplusplus
172 };
173 #endif //__cplusplus
174
175 #ifndef USER_MODE
176 extern UNICODE_STRING SavedSPString;
177 #endif //USER_MODE
178
179 /*
180 #define WinVer_Is351 (MajorVersion==0x03)
181 #define WinVer_IsNT (MajorVersion==0x04)
182 #define WinVer_Is2k (MajorVersion==0x05 && MinorVersion==0x00)
183 #define WinVer_IsXP (MajorVersion==0x05 && MinorVersion==0x01)
184 #define WinVer_IsdNET (MajorVersion==0x05 && MinorVersion==0x02)
185
186 #define WinVer_Id() ((MajorVersion << 8) | MinorVersion)
187
188 #define WinVer_351 (0x0351)
189 #define WinVer_NT (0x0400)
190 #define WinVer_2k (0x0500)
191 #define WinVer_XP (0x0501)
192 #define WinVer_dNET (0x0502)
193 */
194
195 #define PtrOffset(BASE,OFFSET) ((ULONG)((ULONG)(OFFSET) - (ULONG)(BASE)))
196
197 #ifndef offsetof
198 #define offsetof(type, field) (ULONG)&(((type *)0)->field)
199 #endif //offsetof
200
201 #pragma pack(pop)
202
203 #endif // __TOOLS_H__
204