1 //////////////////////////////////////////////////////////////////// 2 // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine 3 // All rights reserved 4 // This file was released under the GPLv2 on June 2015. 5 //////////////////////////////////////////////////////////////////// 6 #ifndef __TOOLS_H__ 7 #define __TOOLS_H__ 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #ifndef __CROSSNT_MISC__H__ 14 15 typedef struct _FOUR_BYTE { 16 UCHAR Byte0; 17 UCHAR Byte1; 18 UCHAR Byte2; 19 UCHAR Byte3; 20 } FOUR_BYTE, *PFOUR_BYTE; 21 22 #if defined _X86_ && defined __CROSS_VERSION_LIB_NT__H__ 23 24 #define AcquireXLock(gLock, oldValue, newValue) \ 25 { \ 26 PULONG _gLock_ = &(gLock); \ 27 __asm push ebx \ 28 __asm mov eax,newValue \ 29 __asm mov ebx,_gLock_ \ 30 __asm xchg eax,[ebx] \ 31 __asm mov oldValue,eax \ 32 __asm pop ebx \ 33 } 34 35 void 36 __fastcall 37 _MOV_DD_SWP( 38 void* a, // ECX 39 void* b // EDX 40 ); 41 #define MOV_DD_SWP(a,b) _MOV_DD_SWP(&(a),&(b)) 42 /*#define MOV_DD_SWP(a,b) \ 43 { \ 44 PFOUR_BYTE _from_, _to_; \ 45 _from_ = ((PFOUR_BYTE)&(b)); \ 46 _to_ = ((PFOUR_BYTE)&(a)); \ 47 __asm mov ebx,_from_ \ 48 __asm mov eax,[ebx] \ 49 __asm bswap eax \ 50 __asm mov ebx,_to_ \ 51 __asm mov [ebx],eax \ 52 }*/ 53 54 void 55 __fastcall 56 _MOV_DW_SWP( 57 void* a, // ECX 58 void* b // EDX 59 ); 60 #define MOV_DW_SWP(a,b) _MOV_DW_SWP(&(a),&(b)) 61 /*#define MOV_DW_SWP(a,b) \ 62 { \ 63 PFOUR_BYTE _from_, _to_; \ 64 _from_ = ((PFOUR_BYTE)&(b)); \ 65 _to_ = ((PFOUR_BYTE)&(a)); \ 66 __asm push ebx \ 67 __asm mov ebx,_from_ \ 68 __asm mov ax,[ebx] \ 69 __asm rol ax,8 \ 70 __asm mov ebx,_to_ \ 71 __asm mov [ebx],ax \ 72 __asm pop ebx \ 73 }*/ 74 75 void 76 __fastcall 77 _REVERSE_DD( 78 void* a // ECX 79 ); 80 #define REVERSE_DD(a) _REVERSE_DD(&(a)) 81 /*#define REVERSE_DD(a) { \ 82 PFOUR_BYTE _from_; \ 83 _from_ = ((PFOUR_BYTE)&(a)); \ 84 __asm mov ebx,_from_ \ 85 __asm mov eax,[ebx] \ 86 __asm bswap eax \ 87 __asm mov [ebx],eax \ 88 }*/ 89 90 void 91 __fastcall 92 _REVERSE_DW( 93 void* a // ECX 94 ); 95 #define REVERSE_DW(a) _REVERSE_DW(&(a)) 96 /*#define REVERSE_DW(a) { \ 97 PFOUR_BYTE _from_; \ 98 _from_ = ((PFOUR_BYTE)&(a)); \ 99 __asm mov eax,_from_ \ 100 __asm rol word ptr [eax],8 \ 101 }*/ 102 103 void 104 __fastcall 105 _MOV_DW2DD_SWP( 106 void* a, // ECX 107 void* b // EDX 108 ); 109 #define MOV_DW2DD_SWP(a,b) _MOV_DW2DD_SWP(&(a),&(b)) 110 /*#define MOV_DW2DD_SWP(a,b) \ 111 { \ 112 PFOUR_BYTE _from_, _to_; \ 113 _from_ = ((PFOUR_BYTE)&(b)); \ 114 _to_ = ((PFOUR_BYTE)&(a)); \ 115 __asm mov ebx,_from_ \ 116 __asm mov ax,[ebx] \ 117 __asm rol ax,8 \ 118 __asm mov ebx,_to_ \ 119 __asm mov [ebx+2],ax \ 120 __asm mov [ebx],0 \ 121 }*/ 122 123 void 124 __fastcall 125 _MOV_MSF( 126 void* a, // ECX 127 void* b // EDX 128 ); 129 #define MOV_MSF(a,b) _MOV_MSF(&(a),&(b)) 130 /*#define MOV_MSF(a,b) \ 131 { \ 132 PFOUR_BYTE _from_, _to_; \ 133 _from_ = ((PFOUR_BYTE)&(b)); \ 134 _to_ = ((PFOUR_BYTE)&(a)); \ 135 __asm mov ebx,_from_ \ 136 __asm mov eax,[ebx] \ 137 __asm mov ebx,_to_ \ 138 __asm mov [ebx],ax \ 139 __asm shr eax,16 \ 140 __asm mov [ebx+2],al \ 141 }*/ 142 143 void 144 __fastcall 145 _MOV_MSF_SWP( 146 void* a, // ECX 147 void* b // EDX 148 ); 149 #define MOV_MSF_SWP(a,b) _MOV_MSF_SWP(&(a),&(b)) 150 /*#define MOV_MSF_SWP(a,b) \ 151 { \ 152 PFOUR_BYTE _from_, _to_; \ 153 _from_ = ((PFOUR_BYTE)&(b)); \ 154 _to_ = ((PFOUR_BYTE)&(a)); \ 155 __asm mov ebx,_from_ \ 156 __asm mov eax,[ebx] \ 157 __asm mov ebx,_to_ \ 158 __asm mov [ebx+2],al \ 159 __asm bswap eax \ 160 __asm shr eax,8 \ 161 __asm mov [ebx],ax \ 162 }*/ 163 164 void 165 __fastcall 166 _XCHG_DD( 167 void* a, // ECX 168 void* b // EDX 169 ); 170 #define XCHG_DD(a,b) _XCHG_DD(&(a),&(b)) 171 /*#define XCHG_DD(a,b) \ 172 { \ 173 PULONG _from_, _to_; \ 174 _from_ = ((PULONG)&(b)); \ 175 _to_ = ((PULONG)&(a)); \ 176 __asm mov ebx,_from_ \ 177 __asm mov ecx,_to_ \ 178 __asm mov eax,[ebx] \ 179 __asm xchg eax,[ecx] \ 180 __asm mov [ebx],eax \ 181 }*/ 182 183 #else // NO X86 optimization , use generic C/C++ 184 185 #define AcquireXLock(gLock, oldValue, newValue) \ 186 { \ 187 oldValue = gLock; \ 188 gLock = newValue; \ 189 } 190 191 #define MOV_DD_SWP(a,b) \ 192 { \ 193 PFOUR_BYTE _from_, _to_; \ 194 _from_ = ((PFOUR_BYTE)&(b)); \ 195 _to_ = ((PFOUR_BYTE)&(a)); \ 196 _to_->Byte0 = _from_->Byte3; \ 197 _to_->Byte1 = _from_->Byte2; \ 198 _to_->Byte2 = _from_->Byte1; \ 199 _to_->Byte3 = _from_->Byte0; \ 200 } 201 202 #define MOV_DW_SWP(a,b) \ 203 { \ 204 PFOUR_BYTE _from_, _to_; \ 205 _from_ = ((PFOUR_BYTE)&(b)); \ 206 _to_ = ((PFOUR_BYTE)&(a)); \ 207 _to_->Byte0 = _from_->Byte1; \ 208 _to_->Byte1 = _from_->Byte0; \ 209 } 210 211 #define REVERSE_DD(a) { \ 212 ULONG _i_; \ 213 MOV_DD_SWP(_i_,(a)); \ 214 *((PULONG)&(a)) = _i_; \ 215 } 216 217 #define REVERSE_DW(a) { \ 218 USHORT _i_; \ 219 MOV_DW_SWP(_i_,(a)); \ 220 *((PUSHORT)&(a)) = _i_; \ 221 } 222 223 #define MOV_DW2DD_SWP(a,b) \ 224 { \ 225 PFOUR_BYTE _from_, _to_; \ 226 _from_ = ((PFOUR_BYTE)&(b)); \ 227 _to_ = ((PFOUR_BYTE)&(a)); \ 228 *((PUSHORT)_to_) = 0; \ 229 _to_->Byte2 = _from_->Byte1; \ 230 _to_->Byte3 = _from_->Byte0; \ 231 } 232 233 #define MOV_MSF(a,b) \ 234 { \ 235 PFOUR_BYTE _from_, _to_; \ 236 _from_ = ((PFOUR_BYTE)&(b)); \ 237 _to_ = ((PFOUR_BYTE)&(a)); \ 238 _to_->Byte0 = _from_->Byte0; \ 239 _to_->Byte1 = _from_->Byte1; \ 240 _to_->Byte2 = _from_->Byte2; \ 241 } 242 243 #define MOV_MSF_SWP(a,b) \ 244 { \ 245 PFOUR_BYTE _from_, _to_; \ 246 _from_ = ((PFOUR_BYTE)&(b)); \ 247 _to_ = ((PFOUR_BYTE)&(a)); \ 248 _to_->Byte0 = _from_->Byte2; \ 249 _to_->Byte1 = _from_->Byte1; \ 250 _to_->Byte2 = _from_->Byte0; \ 251 } 252 253 #define XCHG_DD(a,b) \ 254 { \ 255 ULONG _temp_; \ 256 PULONG _from_, _to_; \ 257 _from_ = ((PULONG)&(b)); \ 258 _to_ = ((PULONG)&(a)); \ 259 _temp_ = *_from_; \ 260 *_from_ = *_to_; \ 261 *_to_ = _temp_; \ 262 } 263 264 #endif // _X86_ 265 266 #endif // __CROSSNT_MISC__H__ 267 268 #define CONV_TO_LL(a) a.Byte0 | a.Byte1 << 8 | a.Byte2 << 16 | a.Byte3 << 8 269 #define MSF_TO_LBA(Minutes,Seconds,Frames) \ 270 (ULONG)((60 * 75 * (Minutes)) + (75 * (Seconds)) + ((Frames) - 150)) 271 272 #define PacketFixed2Variable(x,ps) ( ( ( (x) / (ps) ) * (ps+7) ) + ( (x) & (ps-1) ) ) 273 #define PacketVariable2Fixed(x,ps) ( ( ( (x) / (ps+7) ) * (ps) ) + ( (((x) % (ps+7)) < (ps)) ? ((x) % (ps+7)) : (ps-1) ) ) 274 275 #define WAIT_FOR_XXX_EMU_DELAY 1000LL // 0.0001 s 276 277 #define max(a,b) (((a) > (b)) ? (a) : (b)) 278 #define min(a,b) (((a) < (b)) ? (a) : (b)) 279 280 #ifndef offsetof 281 #define offsetof(type, field) (ULONG)&(((type *)0)->field) 282 #endif //offsetof 283 284 #ifdef __cplusplus 285 }; 286 #endif 287 288 #endif // __TOOLS_H__ 289