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.

203 lines
6.2 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: random.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * This module contains a random collection of support routines for the User
  7. * API functions. Many of these functions will be moved to more appropriate
  8. * files once we get our act together.
  9. *
  10. * History:
  11. * 10-17-90 DarrinM Created.
  12. * 02-06-91 IanJa HWND revalidation added (none required)
  13. \***************************************************************************/
  14. /***************************************************************************\
  15. * RtlGetExpWinVer
  16. *
  17. * Returns the expected windows version, in the same format as Win3.1's
  18. * GetExpWinVer(). This takes it out of the module header. As such, this
  19. * api cannot be called from the server context to get version info for
  20. * a client process - instead that information needs to be queried ahead
  21. * of time and passed with any client/server call.
  22. *
  23. * 03-14-92 ScottLu Created.
  24. \***************************************************************************/
  25. DWORD RtlGetExpWinVer(
  26. HANDLE hmod)
  27. {
  28. PIMAGE_NT_HEADERS pnthdr;
  29. DWORD dwMajor = 3;
  30. DWORD dwMinor = 0xA;
  31. /*
  32. * If it doesn't look like a valid 32bit hmod, use the default
  33. * (i.e., assuming all 16bit hmods are 0x30a)
  34. */
  35. if ((hmod != NULL) && (LOWORD(HandleToUlong(hmod)) == 0)) {
  36. try {
  37. pnthdr = RtlImageNtHeader((PVOID)hmod);
  38. // If for some reason we can't get the header information,
  39. // just return the default
  40. if(pnthdr == NULL) {
  41. goto NO_HEADER;
  42. }
  43. dwMajor = pnthdr->OptionalHeader.MajorSubsystemVersion;
  44. /*
  45. * Still need this hack 'cuz the linker still puts
  46. * version 1.00 in the header of some things.
  47. */
  48. if (dwMajor == 1) {
  49. dwMajor = 0x3;
  50. } else {
  51. dwMinor = pnthdr->OptionalHeader.MinorSubsystemVersion;
  52. }
  53. } except (W32ExceptionHandler(FALSE, RIP_WARNING)) {
  54. dwMajor = 3; // just to be safe
  55. dwMinor = 0xA;
  56. }
  57. }
  58. /*
  59. * Return this is a win3.1 compatible format:
  60. *
  61. * 0x030A == win3.1
  62. * 0x0300 == win3.0
  63. * 0x0200 == win2.0, etc.
  64. */
  65. // dwMajor and dwMinor are initialized where they're declared
  66. NO_HEADER:
  67. return (DWORD)MAKELONG(MAKEWORD((BYTE)dwMinor, (BYTE)dwMajor), 0);
  68. }
  69. /***************************************************************************\
  70. * FindCharPosition
  71. *
  72. * Finds position of character ch in lpString. If not found, the length
  73. * of the string is returned.
  74. *
  75. * History:
  76. * 11-13-90 JimA Created.
  77. \***************************************************************************/
  78. DWORD FindCharPosition(
  79. LPWSTR lpString,
  80. WCHAR ch)
  81. {
  82. DWORD dwPos = 0L;
  83. while (*lpString && *lpString != ch) {
  84. ++lpString;
  85. ++dwPos;
  86. }
  87. return dwPos;
  88. }
  89. /***************************************************************************\
  90. * TextCopy
  91. *
  92. * Returns: number of characters copied not including the NULL
  93. *
  94. * History:
  95. * 10-25-90 MikeHar Wrote.
  96. * 11-09-90 DarrinM Rewrote with a radically new algorithm.
  97. * 01-25-91 MikeHar Fixed the radically new algorithm.
  98. * 02-01-91 DarrinM Bite me.
  99. * 11-26-91 DarrinM Ok, this time it's perfect (except NLS, probably).
  100. * 01-13-92 GregoryW Now it's okay for Unicode.
  101. \***************************************************************************/
  102. UINT TextCopy(
  103. PLARGE_UNICODE_STRING pstr,
  104. LPWSTR pszDst,
  105. UINT cchMax)
  106. {
  107. if (cchMax != 0) {
  108. cchMax = min(pstr->Length / sizeof(WCHAR), cchMax - 1);
  109. RtlCopyMemory(pszDst, KPVOID_TO_PVOID(pstr->Buffer), cchMax * sizeof(WCHAR));
  110. pszDst[cchMax] = 0;
  111. }
  112. return cchMax;
  113. }
  114. /***************************************************************************\
  115. * DWORD wcsncpycch(dest, source, count) - copy no more than n wide chars
  116. *
  117. * Purpose:
  118. * Copies no more than count characters from the source string to the
  119. * destination. If count is less than the length of source,
  120. * NO NULL CHARACTER is put onto the end of the copied string.
  121. * If count is greater than the length of sources, dest is NOT padded
  122. * with more than 1 null character.
  123. *
  124. *
  125. * Entry:
  126. * LPWSTR dest - pointer to destination
  127. * LPWSTR source - source string for copy
  128. * DWORD count - max number of characters to copy
  129. *
  130. * Exit:
  131. * returns number of characters copied into dest, including the null
  132. * terminator, if any.
  133. *
  134. * Exceptions:
  135. *
  136. ****************************************************************************/
  137. DWORD wcsncpycch (
  138. LPWSTR dest,
  139. LPCWSTR source,
  140. DWORD count
  141. )
  142. {
  143. LPWSTR start = dest;
  144. while (count && (*dest++ = *source++)) /* copy string */
  145. count--;
  146. return (DWORD)(dest - start);
  147. }
  148. /***************************************************************************\
  149. * DWORD strncpycch(dest, source, count) - copy no more than n characters
  150. *
  151. * Purpose:
  152. * Copies no more than count characters from the source string to the
  153. * destination. If count is less than the length of source,
  154. * NO NULL CHARACTER is put onto the end of the copied string.
  155. * If count is greater than the length of sources, dest is NOT padded
  156. * with more than 1 null character.
  157. *
  158. *
  159. * Entry:
  160. * LPSTR dest - pointer to destination
  161. * LPSTR source - source string for copy
  162. * DWORD count - max number of characters to copy
  163. *
  164. * Exit:
  165. * returns number of characters copied into dest, including the null
  166. * terminator, if any.
  167. *
  168. * Exceptions:
  169. *
  170. *******************************************************************************/
  171. DWORD strncpycch (
  172. LPSTR dest,
  173. LPCSTR source,
  174. DWORD count
  175. )
  176. {
  177. LPSTR start = dest;
  178. while (count && (*dest++ = *source++)) /* copy string */
  179. count--;
  180. return (DWORD)(dest - start);
  181. }