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.

346 lines
7.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. misc
  5. Abstract:
  6. This module contains an interesting collection of routines that are
  7. generally useful, but don't seem to fit anywhere else.
  8. Author:
  9. Doug Barlow (dbarlow) 11/14/1996
  10. Environment:
  11. Win32, C++ w/ Exceptions
  12. Notes:
  13. ?Notes?
  14. --*/
  15. #ifndef WIN32_LEAN_AND_MEAN
  16. #define WIN32_LEAN_AND_MEAN
  17. #endif
  18. #include <Windows.h>
  19. #include <stdarg.h>
  20. #include <tchar.h>
  21. #include "cspUtils.h"
  22. /*++
  23. GetPlatform:
  24. This routine determines, to the best of its ability, the underlying
  25. operating system.
  26. Arguments:
  27. None
  28. Return Value:
  29. A DWORD, formatted as follows:
  30. +-------------------------------------------------------------------+
  31. | OpSys Id | Major Version | Minor Version |
  32. +-------------------------------------------------------------------+
  33. Bit 31 16 15 8 7 0
  34. Predefined values are:
  35. PLATFORM_UNKNOWN - The platform cannot be determined
  36. PLATFORM_WIN95 - The platform is Windows 95
  37. PLATFORM_WIN98 - The platform is Windows 98
  38. PLATFORM_WINNT40 - The platform is Windows NT V4.0
  39. PLATFORM_WIN2K - The platform is Windows 2000 Professional
  40. Throws:
  41. None
  42. Author:
  43. Doug Barlow (dbarlow) 1/16/1997
  44. Taken from a collection of common routines with no authorship
  45. information.
  46. --*/
  47. DWORD
  48. GetPlatform(
  49. void)
  50. {
  51. static DWORD dwPlatform = PLATFORM_UNKNOWN;
  52. if (PLATFORM_UNKNOWN == dwPlatform)
  53. {
  54. OSVERSIONINFO osVer;
  55. memset(&osVer, 0, sizeof(OSVERSIONINFO));
  56. osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  57. if (GetVersionEx(&osVer))
  58. dwPlatform =
  59. (osVer.dwPlatformId << 16)
  60. + (osVer.dwMajorVersion << 8)
  61. + osVer.dwMinorVersion;
  62. }
  63. return dwPlatform;
  64. }
  65. /*++
  66. SelectString:
  67. This routine compares a given string to a list of possible strings, and
  68. returns the index of the string that matches. The comparison is done case
  69. insensitive, and abbreviations are allowed, as long as they're unique.
  70. Arguments:
  71. szSource supplies the string to be compared against all other strings.
  72. Following strings supply a list of strings against which the source string
  73. can be compared. The last parameter must be NULL.
  74. Return Value:
  75. 0 - No match, or ambiguous match.
  76. 1-n - The source string matches the indexed template string.
  77. Throws:
  78. None
  79. Remarks:
  80. Author:
  81. Doug Barlow (dbarlow) 8/27/1998
  82. --*/
  83. DWORD
  84. SelectString(
  85. LPCTSTR szSource,
  86. ...)
  87. {
  88. va_list vaArgs;
  89. DWORD cchSourceLen;
  90. DWORD dwReturn = 0;
  91. DWORD dwIndex = 1;
  92. LPCTSTR szTpl;
  93. va_start(vaArgs, szSource);
  94. //
  95. // Step through each input parameter until we find an exact match.
  96. //
  97. cchSourceLen = lstrlen(szSource);
  98. if (0 == cchSourceLen)
  99. return 0; // Empty strings don't match anything.
  100. szTpl = va_arg(vaArgs, LPCTSTR);
  101. while (NULL != szTpl)
  102. {
  103. if (0 == _tcsncicmp(szTpl, szSource, cchSourceLen))
  104. {
  105. if (0 != dwReturn)
  106. {
  107. dwReturn = 0;
  108. break;
  109. }
  110. dwReturn = dwIndex;
  111. }
  112. szTpl = va_arg(vaArgs, LPCTSTR);
  113. dwIndex += 1;
  114. }
  115. va_end(vaArgs);
  116. return dwReturn;
  117. }
  118. /*++
  119. StringFromGuid:
  120. This routine converts a GUID into its corresponding string representation.
  121. It's here so that it's not necessary to link all of OleBase.
  122. Otherwise, we'd just use StringFromCLSID.
  123. Arguments:
  124. pguidSource supplies the GUID to convert.
  125. szGuid receives the GUID as a string. This string is assumed to be at
  126. least 39 characters long.
  127. Return Value:
  128. None
  129. Throws:
  130. Errors are thrown as DWORD status codes.
  131. Author:
  132. Doug Barlow (dbarlow) 1/20/1998
  133. --*/
  134. void
  135. StringFromGuid(
  136. IN LPCGUID pguidResult,
  137. OUT LPTSTR szGuid)
  138. {
  139. //
  140. // The following placement assumes Little Endianness.
  141. // {1D92589A-91E4-11d1-93AA-00C04FD91402}
  142. // 0123456789012345678901234567890123456789
  143. // 1 2 3
  144. //
  145. static const WORD wPlace[sizeof(GUID)]
  146. = { 8, 6, 4, 2, 13, 11, 18, 16, 21, 23, 26, 28, 30, 32, 34, 36 };
  147. static const WORD wPunct[]
  148. = { 0, 9, 14, 19, 24, 37, 38 };
  149. static const TCHAR chPunct[]
  150. = { TEXT('{'), TEXT('-'), TEXT('-'), TEXT('-'), TEXT('-'), TEXT('}'), TEXT('\000') };
  151. DWORD dwI, dwJ;
  152. TCHAR ch;
  153. LPTSTR pch;
  154. LPBYTE pbGuid = (LPBYTE)pguidResult;
  155. BYTE bVal;
  156. for (dwI = 0; dwI < sizeof(GUID); dwI += 1)
  157. {
  158. bVal = pbGuid[dwI];
  159. pch = &szGuid[wPlace[dwI]];
  160. for (dwJ = 0; dwJ < 2; dwJ += 1)
  161. {
  162. ch = (TCHAR)(bVal & 0x000f);
  163. ch += TEXT('0');
  164. if (ch > TEXT('9'))
  165. ch += TEXT('A') - (TEXT('9') + 1);
  166. *pch-- = ch;
  167. bVal >>= 4;
  168. }
  169. }
  170. dwI = 0;
  171. do
  172. {
  173. szGuid[wPunct[dwI]] = chPunct[dwI];
  174. } while (0 != chPunct[dwI++]);
  175. }
  176. /*++
  177. GuidFromString:
  178. This routine converts a string representation of a GUID into an actual GUID.
  179. It tries not to be picky about the systax, as long as it can get a GUID out
  180. of the string. It's here so that it's not necessary to link all of OleBase
  181. into WinSCard. Otherwise, we'd just use CLSIDFromString.
  182. Arguments:
  183. szGuid supplies the GUID as a string. For this routine, a GUID consists of
  184. hex digits, and some collection of braces and dashes.
  185. pguidResult receives the converted GUID. If an error occurs during
  186. conversion, the contents of this parameter are indeterminant.
  187. Return Value:
  188. TRUE - Successful completion
  189. FALSE - That's not a GUID
  190. Author:
  191. Doug Barlow (dbarlow) 1/20/1998
  192. --*/
  193. BOOL
  194. GuidFromString(
  195. IN LPCTSTR szGuid,
  196. OUT LPGUID pguidResult)
  197. {
  198. // The following placement assumes Little Endianness.
  199. static const WORD wPlace[sizeof(GUID)]
  200. = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
  201. DWORD dwI, dwJ;
  202. LPCTSTR pch = szGuid;
  203. LPBYTE pbGuid = (LPBYTE)pguidResult;
  204. BYTE bVal;
  205. for (dwI = 0; dwI < sizeof(GUID); dwI += 1)
  206. {
  207. bVal = 0;
  208. for (dwJ = 0; dwJ < 2;)
  209. {
  210. switch (*pch)
  211. {
  212. case TEXT('0'):
  213. case TEXT('1'):
  214. case TEXT('2'):
  215. case TEXT('3'):
  216. case TEXT('4'):
  217. case TEXT('5'):
  218. case TEXT('6'):
  219. case TEXT('7'):
  220. case TEXT('8'):
  221. case TEXT('9'):
  222. bVal = (BYTE)((bVal << 4) + (*pch - TEXT('0')));
  223. dwJ += 1;
  224. break;
  225. case TEXT('A'):
  226. case TEXT('B'):
  227. case TEXT('C'):
  228. case TEXT('D'):
  229. case TEXT('E'):
  230. case TEXT('F'):
  231. bVal = (BYTE)((bVal << 4) + (10 + *pch - TEXT('A')));
  232. dwJ += 1;
  233. break;
  234. case TEXT('a'):
  235. case TEXT('b'):
  236. case TEXT('c'):
  237. case TEXT('d'):
  238. case TEXT('e'):
  239. case TEXT('f'):
  240. bVal = (BYTE)((bVal << 4) + (10 + *pch - TEXT('a')));
  241. dwJ += 1;
  242. break;
  243. case TEXT('['):
  244. case TEXT(']'):
  245. case TEXT('{'):
  246. case TEXT('}'):
  247. case TEXT('-'):
  248. break;
  249. default:
  250. return FALSE;
  251. }
  252. pch += 1;
  253. }
  254. pbGuid[wPlace[dwI]] = bVal;
  255. }
  256. return TRUE;
  257. }