Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
13 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. atkutils.h
  5. Abstract:
  6. This module contains miscellaneous support routines.
  7. Author:
  8. Jameel Hyder (jameelh@microsoft.com)
  9. Nikhil Kamkolkar (nikhilk@microsoft.com)
  10. Revision History:
  11. 19 Jun 1992 Initial Version
  12. Notes: Tab stop: 4
  13. --*/
  14. #ifndef _ATKUTILS_
  15. #define _ATKUTILS_
  16. // SpinLock Macros
  17. #if DBG
  18. #define INITIALIZE_SPIN_LOCK(_pLock) \
  19. { \
  20. KeInitializeSpinLock(&(_pLock)->SpinLock); \
  21. (_pLock)->FileLineLock = 0; \
  22. }
  23. #else // DBG
  24. #define INITIALIZE_SPIN_LOCK(_pLock) \
  25. { \
  26. KeInitializeSpinLock(&(_pLock)->SpinLock); \
  27. }
  28. #endif
  29. #if DBG
  30. #define ACQUIRE_SPIN_LOCK(_pLock, _pOldIrql) \
  31. { \
  32. KeAcquireSpinLock(&(_pLock)->SpinLock, \
  33. _pOldIrql); \
  34. (_pLock)->FileLineLock = (FILENUM | __LINE__); \
  35. }
  36. #define ACQUIRE_SPIN_LOCK_DPC(_pLock) \
  37. { \
  38. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  39. KeAcquireSpinLockAtDpcLevel(&(_pLock)->SpinLock); \
  40. (_pLock)->FileLineLock = (FILENUM | __LINE__ | 0x80000000); \
  41. }
  42. #define RELEASE_SPIN_LOCK(_pLock, _OldIrql) \
  43. { \
  44. ASSERT ((_pLock)->FileLineLock != 0); \
  45. ASSERT (((_pLock)->FileLineLock & 0x80000000) == 0); \
  46. (_pLock)->FileLineLock = 0; \
  47. (_pLock)->FileLineUnlock = (FILENUM | __LINE__); \
  48. KeReleaseSpinLock(&(_pLock)->SpinLock, \
  49. _OldIrql); \
  50. }
  51. #define RELEASE_SPIN_LOCK_DPC(_pLock) \
  52. { \
  53. ASSERT ((_pLock)->FileLineLock != 0); \
  54. ASSERT ((_pLock)->FileLineLock & 0x80000000); \
  55. (_pLock)->FileLineLock = 0; \
  56. (_pLock)->FileLineUnlock = (FILENUM | __LINE__ | 0x80000000); \
  57. KeReleaseSpinLockFromDpcLevel(&(_pLock)->SpinLock); \
  58. ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL); \
  59. }
  60. #else // DBG
  61. #define ACQUIRE_SPIN_LOCK(_pLock, _pOldIrql) \
  62. { \
  63. KeAcquireSpinLock(&(_pLock)->SpinLock, \
  64. _pOldIrql); \
  65. }
  66. #define ACQUIRE_SPIN_LOCK_DPC(_pLock) \
  67. { \
  68. KeAcquireSpinLockAtDpcLevel(&(_pLock)->SpinLock); \
  69. }
  70. #define RELEASE_SPIN_LOCK(_pLock, _OldIrql) \
  71. { \
  72. KeReleaseSpinLock(&(_pLock)->SpinLock, \
  73. (_OldIrql)); \
  74. }
  75. #define RELEASE_SPIN_LOCK_DPC(_pLock) \
  76. { \
  77. KeReleaseSpinLockFromDpcLevel(&(_pLock)->SpinLock); \
  78. } \
  79. #endif // DBG
  80. // Macros for ExInterlocked calls
  81. #define INTERLOCKED_INCREMENT_LONG(p, l) InterlockedIncrement(p)
  82. #define INTERLOCKED_DECREMENT_LONG(p, l) InterlockedDecrement(p)
  83. #define INTERLOCKED_INCREMENT_LONG_DPC(p, l) InterlockedIncrement(p)
  84. #define INTERLOCKED_DECREMENT_LONG_DPC(p, l) InterlockedDecrement(p)
  85. #define INTERLOCKED_ADD_STATISTICS(p, v, l) ExInterlockedAddLargeStatistic(p, v)
  86. #define INTERLOCKED_ADD_ULONG(p, v, l) ExInterlockedAddUlong(p, v, l)
  87. #define INTERLOCKED_ADD_LARGE_INTGR(p, v, l) ExInterlockedAddLargeInteger(p, v, l)
  88. #define INTERLOCKED_ADD_ULONG_DPC(p, v, l) ExInterlockedAddUlong(p, v, l)
  89. #define INTERLOCKED_ADD_LARGE_INTGR_DPC(p, v, l) ExInterlockedAddLargeInteger(p, v, l)
  90. #define ATALK_NODES_EQUAL(N1, N2) \
  91. ((((N1)->atn_Network == (N2)->atn_Network) || \
  92. ((N1)->atn_Network == 0) || \
  93. ((N2)->atn_Network == 0)) && \
  94. ((N1)->atn_Node == (N2)->atn_Node))
  95. #define ATALK_ADDRS_EQUAL(A1, A2) \
  96. ((((A1)->ata_Network == (A2)->ata_Network) || \
  97. ((A1)->ata_Network == 0) || \
  98. ((A2)->ata_Network == 0)) && \
  99. ((A1)->ata_Node == (A2)->ata_Node) && \
  100. ((A1)->ata_Socket == (A2)->ata_Socket))
  101. #define INVALID_ADDRESS(pAddr) \
  102. (((pAddr)->ata_Network > LAST_VALID_NETWORK) || \
  103. (((pAddr)->ata_Node > MAX_USABLE_ATALKNODE) && \
  104. ((pAddr)->ata_Node != ATALK_BROADCAST_NODE)) || \
  105. ((pAddr)->ata_Socket < FIRST_VALID_SOCKET) || \
  106. ((pAddr)->ata_Socket > LAST_VALID_SOCKET))
  107. #define ATALKADDR_TO_TDI(pTdiAddr, pAtalkAddr) \
  108. { \
  109. (pTdiAddr)->TAAddressCount = 1; \
  110. (pTdiAddr)->Address[0].AddressLength = sizeof(TDI_ADDRESS_APPLETALK); \
  111. (pTdiAddr)->Address[0].AddressType = TDI_ADDRESS_TYPE_APPLETALK; \
  112. (pTdiAddr)->Address[0].Address[0].Network = (pAtalkAddr)->ata_Network; \
  113. (pTdiAddr)->Address[0].Address[0].Node = (pAtalkAddr)->ata_Node; \
  114. (pTdiAddr)->Address[0].Address[0].Socket = (pAtalkAddr)->ata_Socket; \
  115. }
  116. #define TDI_TO_ATALKADDR(pAtalkAddr, pTdiAddr) \
  117. { \
  118. ASSERTMSG("TdiAddrCount is not 1\n", \
  119. ((pTdiAddr)->TAAddressCount == 1)); \
  120. \
  121. ASSERTMSG("TdiAddrLen invalid\n", \
  122. ((pTdiAddr)->Address[0].AddressLength >= \
  123. sizeof(TDI_ADDRESS_APPLETALK))); \
  124. \
  125. ASSERTMSG("TdiAddrType invalid\n", \
  126. ((pTdiAddr)->Address[0].AddressType == \
  127. TDI_ADDRESS_TYPE_APPLETALK)); \
  128. \
  129. (pAtalkAddr)->ata_Network = (pTdiAddr)->Address[0].Address[0].Network;\
  130. (pAtalkAddr)->ata_Node = (pTdiAddr)->Address[0].Address[0].Node; \
  131. (pAtalkAddr)->ata_Socket = (pTdiAddr)->Address[0].Address[0].Socket;\
  132. }
  133. #define IN_NETWORK_RANGE(NetworkNumber, pRte) \
  134. (((pRte)->rte_NwRange.anr_FirstNetwork == NetworkNumber) || \
  135. ((NetworkNumber >= (pRte)->rte_NwRange.anr_FirstNetwork) && \
  136. (NetworkNumber <= (pRte)->rte_NwRange.anr_LastNetwork)))
  137. #define WITHIN_NETWORK_RANGE(NetworkNumber, pRange) \
  138. (((pRange)->anr_FirstNetwork == NetworkNumber) || \
  139. ((NetworkNumber >= (pRange)->anr_FirstNetwork) && \
  140. (NetworkNumber <= (pRange)->anr_LastNetwork)))
  141. #define COPY_NETWORK_ADDR(_Dst, _Src) \
  142. { \
  143. *((ULONG UNALIGNED *)(_Dst)) = *((ULONG UNALIGNED *)(_Src)); \
  144. *((USHORT UNALIGNED *)((UCHAR *)(_Dst)+4)) = \
  145. *((USHORT UNALIGNED *)((UCHAR *)(_Src)+4)); \
  146. }
  147. // Hash functions
  148. // Make sure we're positive [thus the shift by 7 rather than 8].
  149. // Only hash node and socket; due to the "zero matches all" for
  150. // non-extended network numbers.
  151. #define HASH_ATALK_ADDR(address) \
  152. ((USHORT)(((address)->ata_Node << 7) + \
  153. ((address)->ata_Socket & 0x7F)))
  154. #define HASH_ATALK_NODE(address) \
  155. ((USHORT)((((address)->atn_Network & 0x3C) >> 2) + \
  156. (address)->atn_Node & 0x04))
  157. #define HASH_ID_SRCADDR(id, pSrcAddr) \
  158. ((id) + (((pSrcAddr)->ata_Node >> 2) + ((pSrcAddr)->ata_Network & 0xFF)))
  159. /*
  160. * The following macros deal with on-the-wire integer and long values
  161. *
  162. * On the wire format is big-endian i.e. a long value of 0x01020304 is
  163. * represented as 01 02 03 04. Similarly an int value of 0x0102 is
  164. * represented as 01 02.
  165. *
  166. * The host format is not assumed since it will vary from processor to
  167. * processor.
  168. */
  169. // Get a byte from on-the-wire format to a short in the host format
  170. #define GETBYTE2SHORT(DstPtr, SrcPtr) \
  171. *(PUSHORT)(DstPtr) = (USHORT) (*(PBYTE)(SrcPtr))
  172. // Get a byte from on-the-wire format to a dword in the host format
  173. #define GETBYTE2DWORD(DstPtr, SrcPtr) \
  174. *(PDWORD)(DstPtr) = (DWORD) (*(PBYTE)(SrcPtr))
  175. // Get a short from on-the-wire format to a dword in the host format
  176. #define GETSHORT2DWORD(DstPtr, SrcPtr) \
  177. *(PDWORD)(DstPtr) = ((*((PBYTE)(SrcPtr)+0) << 8) + \
  178. (*((PBYTE)(SrcPtr)+1)))
  179. // Get a short from on-the-wire format to a short in the host format
  180. #define GETSHORT2SHORT(DstPtr, SrcPtr) \
  181. *(PUSHORT)(DstPtr) = ((*((PBYTE)(SrcPtr)+0) << 8) + \
  182. (*((PBYTE)(SrcPtr)+1)))
  183. // Get a dword from on-the-wire format to a dword in the host format
  184. #define GETDWORD2DWORD(DstPtr, SrcPtr) \
  185. *(PDWORD)(DstPtr) = ((*((PBYTE)(SrcPtr)+0) << 24) + \
  186. (*((PBYTE)(SrcPtr)+1) << 16) + \
  187. (*((PBYTE)(SrcPtr)+2) << 8) + \
  188. (*((PBYTE)(SrcPtr)+3)))
  189. // Put a dword from the host format to a short to on-the-wire format
  190. #define PUTBYTE2BYTE(DstPtr, Src) \
  191. *((PBYTE)(DstPtr)) = (BYTE)(Src)
  192. // Put a dword from the host format to a short to on-the-wire format
  193. #define PUTSHORT2BYTE(DstPtr, Src) \
  194. *((PBYTE)(DstPtr)) = ((USHORT)(Src) % 256)
  195. // Put a dword from the host format to a short to on-the-wire format
  196. #define PUTSHORT2SHORT(DstPtr, Src) \
  197. *((PBYTE)(DstPtr)+0) = (BYTE) ((USHORT)(Src) >> 8), \
  198. *((PBYTE)(DstPtr)+1) = (BYTE)(Src)
  199. // Put a dword from the host format to a byte to on-the-wire format
  200. #define PUTDWORD2BYTE(DstPtr, Src) \
  201. *(PBYTE)(DstPtr) = (BYTE)(Src)
  202. // Put a dword from the host format to a short to on-the-wire format
  203. #define PUTDWORD2SHORT(DstPtr, Src) \
  204. *((PBYTE)(DstPtr)+0) = (BYTE) ((DWORD)(Src) >> 8), \
  205. *((PBYTE)(DstPtr)+1) = (BYTE) (Src)
  206. // Put a dword from the host format to a dword to on-the-wire format
  207. #define PUTDWORD2DWORD(DstPtr, Src) \
  208. *((PBYTE)(DstPtr)+0) = (BYTE) ((DWORD)(Src) >> 24), \
  209. *((PBYTE)(DstPtr)+1) = (BYTE) ((DWORD)(Src) >> 16), \
  210. *((PBYTE)(DstPtr)+2) = (BYTE) ((DWORD)(Src) >> 8), \
  211. *((PBYTE)(DstPtr)+3) = (BYTE) (Src)
  212. // MIN/MAX macros
  213. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  214. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  215. extern BYTE AtalkUpCaseTable[];
  216. extern
  217. VOID
  218. AtalkUpCase(
  219. IN PBYTE pSrc,
  220. IN BYTE SrcLen,
  221. OUT PBYTE pDst
  222. );
  223. extern
  224. BOOLEAN
  225. AtalkCompareCaseInsensitive(
  226. IN PBYTE s1,
  227. IN PBYTE s2
  228. );
  229. extern
  230. int
  231. AtalkOrderCaseInsensitive(
  232. IN PBYTE s1,
  233. IN PBYTE s2
  234. );
  235. #define AtalkFixedCompareCaseInsensitive(s1, l1, s2, l2) \
  236. (((l1) == (l2)) && AtalkCompareFixedCaseInsensitive(s1, s2, l1))
  237. extern
  238. BOOLEAN
  239. AtalkCompareFixedCaseInsensitive(
  240. IN PBYTE s1,
  241. IN PBYTE s2,
  242. IN int len
  243. );
  244. #define AtalkFixedCompareCaseSensitive(s1, l1, s2, l2) \
  245. ((l1 == l2) && !memcmp(s1, s2, l1))
  246. extern
  247. PBYTE
  248. AtalkSearchBuf(
  249. IN PBYTE pBuf,
  250. IN BYTE BufLen,
  251. IN BYTE SearchChar
  252. );
  253. int
  254. GetTokenLen(
  255. IN PBYTE pTokStr,
  256. IN int WildStringLen,
  257. IN BYTE NBP_WILD_CHARACTER
  258. );
  259. BOOLEAN
  260. SubStringMatch(
  261. IN PBYTE pTarget,
  262. IN PBYTE pTokStr,
  263. IN int StringLen,
  264. IN int TokStrLen
  265. );
  266. extern
  267. BOOLEAN
  268. AtalkCheckNetworkRange(
  269. IN PATALK_NETWORKRANGE NetworkRange
  270. );
  271. #define AtalkRangesOverlap(pRange1, pRange2) \
  272. (((pRange1)->anr_LastNetwork >= (pRange2)->anr_FirstNetwork) && \
  273. ((pRange1)->anr_FirstNetwork <= (pRange2)->anr_LastNetwork))
  274. extern
  275. BOOLEAN
  276. AtalkIsPrime(
  277. long Step
  278. );
  279. extern
  280. LONG
  281. AtalkRandomNumber(
  282. VOID
  283. );
  284. extern
  285. VOID
  286. AtalkDbgIncCount(
  287. IN DWORD *Value
  288. );
  289. extern
  290. VOID
  291. AtalkDbgDecCount(
  292. IN DWORD *Value
  293. );
  294. // Used for calculating round trip times using Van Jacobson algorithm
  295. typedef struct
  296. {
  297. ULONG rt_New;
  298. SHORT rt_Min;
  299. SHORT rt_Max;
  300. SHORT rt_Ave;
  301. SHORT rt_Dev;
  302. SHORT rt_Base;
  303. } RT, *PRT;
  304. #define AtalkInitializeRT(pRT, Initial, Min, Max) \
  305. { \
  306. (pRT)->rt_Min = Min; \
  307. (pRT)->rt_Max = Max; \
  308. (pRT)->rt_Base = Initial; \
  309. (pRT)->rt_Ave = Min; \
  310. (pRT)->rt_Dev = 0; \
  311. }
  312. #define AtalkCalculateNewRT(pRT) \
  313. { \
  314. SHORT baseT, error; \
  315. \
  316. /* VAN JACOBSEN Algorithm. From Internetworking with Tcp/ip (Comer). */\
  317. \
  318. if ((pRT)->rt_New == 0) \
  319. (pRT)->rt_New = 1; /* Do not let this go to zero */ \
  320. \
  321. error = (SHORT)((pRT)->rt_New) - ((pRT)->rt_Ave >> 3); \
  322. (pRT)->rt_Ave += error; \
  323. /* Make sure not too small */ \
  324. if ((pRT)->rt_Ave <= 0) \
  325. { \
  326. (pRT)->rt_Ave = (pRT)->rt_Min; \
  327. } \
  328. \
  329. if (error < 0) \
  330. error = -error; \
  331. \
  332. error -= ((pRT)->rt_Dev >> 2); \
  333. (pRT)->rt_Dev += error; \
  334. if ((pRT)->rt_Dev <= 0) \
  335. (pRT)->rt_Dev = 1; \
  336. \
  337. baseT = ((((pRT)->rt_Ave >> 2) + (pRT)->rt_Dev) >> 1); \
  338. \
  339. /* If less then min - set it */ \
  340. if (baseT < (pRT)->rt_Min) \
  341. baseT = (pRT)->rt_Min; \
  342. \
  343. /* If greater than max - set it */ \
  344. if (baseT > (pRT)->rt_Max) \
  345. baseT = (pRT)->rt_Max; \
  346. \
  347. /* Set the new value */ \
  348. (pRT)->rt_Base = baseT; \
  349. }
  350. extern
  351. BOOLEAN
  352. AtalkWaitTE(
  353. IN PKEVENT pEvent,
  354. IN ULONG TimeInMs
  355. );
  356. extern
  357. VOID
  358. AtalkSleep(
  359. IN ULONG TimeInMs
  360. );
  361. NTSTATUS
  362. AtalkGetProtocolSocketType(
  363. PATALK_DEV_CTX Context,
  364. PUNICODE_STRING RemainingFileName,
  365. PBYTE ProtocolType,
  366. PBYTE SocketType
  367. );
  368. INT
  369. AtalkIrpGetEaCreateType(
  370. IN PIRP Irp);
  371. LOCAL LONG
  372. atalkStringHash(
  373. IN PBYTE String,
  374. IN BYTE StrLen
  375. );
  376. #endif // _ATKUTILS_
  377.