Source code of Windows XP (NT5)
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.

201 lines
4.8 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Philips Semiconductors-CSU 1999
  4. // All rights are reserved. Reproduction in whole or in part is prohibited
  5. // without the written consent of the copyright owner.
  6. //
  7. // Philips reserves the right to make changes without notice at any time.
  8. // Philips makes no warranty, expressed, implied or statutory, including but
  9. // not limited to any implied warranty of merchantibility or fitness for any
  10. // particular purpose, or that the use will not infringe any third party
  11. // patent, copyright or trademark. Philips must not be liable for any loss
  12. // or damage arising from its use.
  13. //
  14. // UTIL.CPP
  15. //////////////////////////////////////////////////////////////////////////////
  16. #define DECLARE_PURECALL
  17. #include "common.h"
  18. #define NO_GLOBAL_FUNCTION
  19. #include "wdmdrv.h"
  20. #include "util.h"
  21. // global space functions definition
  22. // Thread related functions
  23. BOOL StartThread(HANDLE hHandle, void(*p_Function)(void *), PVOID p_Context);
  24. BOOL StopThread();
  25. void Delay(int iTime);
  26. // Memory related functions
  27. PVOID AllocateFixedMemory(UINT uiSize);
  28. void FreeFixedMemory(PVOID p_vBuffer);
  29. void MemoryCopy(VOID *p_Destination, CONST VOID *p_Source, ULONG ulLength);
  30. // Registry related functions
  31. NTSTATUS GetRegistryValue(IN HANDLE Handle, IN PWCHAR KeyNameString,
  32. IN ULONG KeyNameStringLength, IN PWCHAR Data, IN ULONG DataLength);
  33. BOOL StringsEqual(PWCHAR pwc1, PWCHAR pwc2);
  34. BOOL ConvertToNumber(PWCHAR sLine, PULONG pulNumber);
  35. BOOL ConvertNumberToString(PWCHAR sLine, ULONG ulNumber) ;
  36. /*
  37. * StringsEqual()
  38. * Input: PWCHAR pwc1 - First string to compare.
  39. PWCHAR pwc2 - Second string to compare.
  40. * Output: TRUE - if equal else FALSE
  41. * Description: Compares to UNICODE srings and checks if they are equal
  42. */
  43. BOOL StringsEqual(PWCHAR pwc1, PWCHAR pwc2)
  44. {
  45. UNICODE_STRING us1, us2;
  46. RtlInitUnicodeString(&us1, pwc1);
  47. RtlInitUnicodeString(&us2, pwc2);
  48. // case INsensitive
  49. return (RtlEqualUnicodeString(&us1, &us2, TRUE));
  50. }
  51. /*
  52. * ConvertToNumber()
  53. * Input: PWCHAR sLine - String to parse.
  54. * PULONG pulNumber - Reference to variable to store the value.
  55. * Output: TRUE if operation succeeded else FALSE
  56. * Description: Converts UNICODE string to integer
  57. */
  58. BOOL ConvertToNumber(PWCHAR sLine, PULONG pulNumber)
  59. {
  60. UNICODE_STRING usLine;
  61. RtlInitUnicodeString(&usLine, sLine);
  62. if (!(NT_SUCCESS(RtlUnicodeStringToInteger(&usLine, 0, pulNumber))))
  63. return FALSE;
  64. return TRUE;
  65. }
  66. /*
  67. * ConvertNumberToString()
  68. * Input: PWCHAR sLine - String to store.
  69. * ULONG ulNumber - Number to convert.
  70. *
  71. * Output: TRUE if operation succeeded else FALSE
  72. * Description: Converts integer to UNICODE string
  73. */
  74. BOOL ConvertNumberToString(PWCHAR sLine, ULONG ulNumber)
  75. {
  76. UNICODE_STRING UnicodeString;
  77. RtlInitUnicodeString(&UnicodeString, sLine);
  78. if (!(NT_SUCCESS(RtlIntegerToUnicodeString(ulNumber, 10, &UnicodeString))))
  79. return FALSE;
  80. return TRUE;
  81. }
  82. /*
  83. * Delay()
  84. * Input: int iTime : its the delay required in musec
  85. * Output:
  86. * Description: Introduces delay corresponding to parameter passed
  87. */
  88. void Delay(int iTime)
  89. {
  90. LARGE_INTEGER liTime;
  91. liTime.QuadPart = 0 - (iTime * 10);
  92. // uses time in units of 100ns
  93. KeDelayExecutionThread(KernelMode, FALSE, &liTime);
  94. }
  95. BOOL StartThread(HANDLE hHandle, void(*p_Function)(void *), PVOID p_Context)
  96. {
  97. if(PsCreateSystemThread( &hHandle, 0L, NULL, NULL, NULL, p_Function,
  98. p_Context) == STATUS_SUCCESS)
  99. return TRUE;
  100. else
  101. return FALSE;
  102. }
  103. BOOL StopThread()
  104. {
  105. PsTerminateSystemThread(STATUS_SUCCESS);
  106. return TRUE;
  107. }
  108. PVOID AllocateFixedMemory(UINT uiSize)
  109. {
  110. return ExAllocatePool(NonPagedPool, uiSize);
  111. }
  112. void FreeFixedMemory(PVOID p_vBuffer)
  113. {
  114. ExFreePool(p_vBuffer);
  115. }
  116. void MemoryCopy(VOID *p_Destination, CONST VOID *p_Source, ULONG ulLength)
  117. {
  118. RtlCopyMemory(p_Destination, p_Source, ulLength);
  119. }
  120. /********************* TIMER RELATED FUNCTIONS ********************/
  121. CPhilTimer::CPhilTimer()
  122. {
  123. }
  124. CPhilTimer::~CPhilTimer()
  125. {
  126. KeCancelTimer(&m_Timer);
  127. }
  128. BOOL CPhilTimer::Set(int iTimePeriod)
  129. {
  130. // Initialize and start timer
  131. KeInitializeTimerEx(&m_Timer, SynchronizationTimer);
  132. int iTime = iTimePeriod/1000;
  133. // Set timer to occur every "uiTimePeriod" microseconds
  134. LARGE_INTEGER liTime;
  135. liTime.QuadPart = (LONGLONG)(0-(iTimePeriod * 10));
  136. if(!KeSetTimerEx(&m_Timer, liTime, iTime, NULL))
  137. return FALSE;
  138. return TRUE;
  139. }
  140. void CPhilTimer::Cancel()
  141. {
  142. KeCancelTimer(&m_Timer);
  143. }
  144. BOOL CPhilTimer::Wait(int iTimeOut)
  145. {
  146. LARGE_INTEGER liTime;
  147. liTime.QuadPart = (LONGLONG)(0-(iTimeOut * 10));
  148. if (KeWaitForSingleObject(&m_Timer, Executive, KernelMode,
  149. FALSE, &liTime) == STATUS_TIMEOUT)
  150. return FALSE;
  151. else
  152. return TRUE;
  153. }