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.

360 lines
10 KiB

  1. /*----------------------------------------------------------------------------
  2. util.cpp
  3. utility functions for phone book server
  4. Copyright (c) 1997-1998 Microsoft Corporation
  5. All rights reserved.
  6. Authors:
  7. byao Baogang Yao
  8. History:
  9. 1/23/97 byao -- Created
  10. --------------------------------------------------------------------------*/
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <assert.h>
  14. #include <winreg.h>
  15. #include "util.h"
  16. #include "common.h"
  17. // comment the following line if not debug
  18. //#ifdef DEBUG
  19. //#define _LOG_DEBUG_MESSAGE
  20. //#endif
  21. #ifdef _LOG_DEBUG_MESSAGE
  22. HANDLE g_hDbgFile = INVALID_HANDLE_VALUE;
  23. #endif
  24. ////////////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Name: GetWord
  27. //
  28. // Synopsis: Get the first word from a line, using a given separator character
  29. //
  30. // Parameters:
  31. // pszWord[out] The first word from the line
  32. // pszLine[in] The byte line
  33. // cStopChar[in] The separator character
  34. // nMaxWordLen [in] The max length of the word (not counting terminating null)
  35. //
  36. void GetWord(char *pszWord, char *pszLine, char cStopChar, int nMaxWordLen)
  37. {
  38. int i = 0, j;
  39. for(i = 0; pszLine[i] && (pszLine[i] != cStopChar) && (i < nMaxWordLen); i++)
  40. {
  41. pszWord[i] = pszLine[i];
  42. }
  43. pszWord[i] = '\0';
  44. if(pszLine[i]) ++i;
  45. j = 0;
  46. while(pszLine[j++] = pszLine[i++]);
  47. }
  48. //////////////////////////////////////////////////////////////////////
  49. //
  50. // Name: Decrypt the URL_escaped code '%xx' characters
  51. //
  52. // Synopsis: HTTPd generated
  53. //
  54. // Return: Original special character, such as '*', '?', etc.
  55. //
  56. // Parameters:
  57. //
  58. // pszEscapedSequence[in] escaped sequence, such as 3F (%3F)
  59. //
  60. static char HexToChar(char *pszEscapedSequence)
  61. {
  62. register char cCh;
  63. cCh = (pszEscapedSequence[0] >= 'A' ? ((pszEscapedSequence[0] & 0xdf) - 'A')+10 : (pszEscapedSequence[0] - '0'));
  64. cCh *= 16;
  65. cCh += (pszEscapedSequence[1] >= 'A' ? ((pszEscapedSequence[1] & 0xdf) - 'A')+10 : (pszEscapedSequence[1] - '0'));
  66. return cCh;
  67. }
  68. //////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Name: UnEscapeURL
  71. //
  72. // Synopsis: convert the after-escaped URL string back to normal ASCII string
  73. //
  74. // Parameter:
  75. // pszURL[in/out] The pointer to the URL, the string will be converted
  76. //
  77. //
  78. void UnEscapeURL(char *pszURL)
  79. {
  80. register int i,j;
  81. for(i = 0, j = 0; pszURL[j]; ++i, ++j)
  82. {
  83. if ((pszURL[i] = pszURL[j]) == '%')
  84. {
  85. pszURL[i] = HexToChar(&pszURL[j + 1]);
  86. j += 2;
  87. }
  88. else
  89. {
  90. if ('+' == pszURL[i])
  91. {
  92. pszURL[i] = ' ';
  93. }
  94. }
  95. }
  96. pszURL[i] = '\0';
  97. }
  98. // Log debug information to a debug file
  99. // very useful utility function
  100. void LogDebugMessage(const char * pszFormat, ...)
  101. {
  102. #if DBG
  103. char szBuffer[2048];
  104. char * pszBufferRemaining;
  105. size_t cchBufferRemaining;
  106. va_list vaArgs;
  107. HRESULT hr;
  108. #ifdef _LOG_DEBUG_MESSAGE
  109. DWORD dwBytesWritten;
  110. if (INVALID_HANDLE_VALUE == g_hDbgFile)
  111. {
  112. SYSTEMTIME st;
  113. char szLogFileName[1024];
  114. GetLocalTime(&st);
  115. if (GetTempPath(CELEMS(szBuffer), szBuffer))
  116. {
  117. if (S_OK == StringCchPrintf(szLogFileName, CELEMS(szLogFileName),
  118. "%s\\isapidbg%04u%02u%02u%02u%02u%02u",
  119. szBuffer,
  120. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond))
  121. {
  122. g_hDbgFile = CreateFile(szLogFileName,
  123. GENERIC_WRITE,
  124. FILE_SHARE_READ | FILE_SHARE_WRITE,
  125. NULL,
  126. CREATE_NEW,
  127. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  128. NULL);
  129. }
  130. }
  131. }
  132. if (INVALID_HANDLE_VALUE == g_hDbgFile)
  133. {
  134. DWORD dwErrorCode = GetLastError();
  135. return;
  136. }
  137. #endif
  138. va_start(vaArgs, pszFormat);
  139. // ignore return values here since termination is guaranteed. The hr is there for debugging.
  140. pszBufferRemaining = szBuffer;;
  141. cchBufferRemaining = CELEMS(szBuffer);
  142. hr = StringCchPrintfEx(pszBufferRemaining, cchBufferRemaining, &pszBufferRemaining, &cchBufferRemaining, STRSAFE_IGNORE_NULLS,
  143. "%d\t%d\t", GetTickCount(), GetCurrentThreadId());
  144. hr = StringCchVPrintfEx(pszBufferRemaining, cchBufferRemaining, &pszBufferRemaining, &cchBufferRemaining, STRSAFE_IGNORE_NULLS,
  145. pszFormat, vaArgs);
  146. hr = StringCchCat(pszBufferRemaining, cchBufferRemaining, "\r\n");
  147. #ifdef _LOG_DEBUG_MESSAGE
  148. WriteFile(g_hDbgFile, (LPCVOID) &szBuffer[0], strlen(szBuffer), &dwBytesWritten, NULL);
  149. // assert(dwBytesWritten == strlen(szBuffer);
  150. #endif
  151. OutputDebugString(szBuffer);
  152. va_end(vaArgs);
  153. #endif // DBG
  154. return;
  155. }
  156. #if 0
  157. /*
  158. //+---------------------------------------------------------------------------
  159. //
  160. // Function: GetRegEntry
  161. //
  162. // Synopsis: Gets the value of specified registry key
  163. //
  164. // Arguments: hKeyType [Key Type - HKEY_LOCAL_MACHINE,...]
  165. // pszSubKey [Subkey under hKeyType]
  166. // pszKeyName [Key name whose value should be retrieved]
  167. // dwRegType [Type of the registry key - REG_SZ,...]
  168. // lpbDataIn [Default value for the reg key]
  169. // cbDataIn [Size of lpbDataIn]
  170. // lpbDataOut [Value of the registry key || Default Value ]
  171. // pcbDataIn [Size of lpbDataOut]
  172. //
  173. // Returns: TRUE if successful, FALSE otherwise
  174. //
  175. // History: VetriV Created 2/6/96
  176. //
  177. //----------------------------------------------------------------------------
  178. BOOL GetRegEntry(HKEY hKeyType,
  179. const char* pszSubkey,
  180. const char* pszKeyName,
  181. DWORD dwRegType,
  182. const BYTE* lpbDataIn,
  183. DWORD cbDataIn,
  184. BYTE * lpbDataOut,
  185. LPDWORD pcbDataOut)
  186. {
  187. HKEY hKey;
  188. DWORD dwResult;
  189. LONG retcode;
  190. assert(pszSubkey && pszKeyName);
  191. if (!pszSubkey)
  192. {
  193. return FALSE;
  194. }
  195. if (!pszKeyName)
  196. {
  197. return FALSE;
  198. }
  199. // create the specified key; If the key already exists, open it
  200. retcode = RegCreateKeyEx(hKeyType,
  201. (LPCTSTR)pszSubkey,
  202. 0,
  203. 0,
  204. REG_OPTION_NON_VOLATILE,
  205. KEY_QUERY_VALUE,
  206. NULL,
  207. &hKey,
  208. &dwResult);
  209. if (ERROR_SUCCESS != retcode)
  210. {
  211. SetLastError(retcode);
  212. return FALSE;
  213. }
  214. // get the data and type for a value name
  215. retcode = RegQueryValueEx(hKey,
  216. (LPTSTR)pszKeyName,
  217. 0,
  218. NULL,
  219. lpbDataOut,
  220. pcbDataOut);
  221. if (ERROR_SUCCESS != retcode)
  222. {
  223. SetLastError(retcode);
  224. RegCloseKey(hKey);
  225. return FALSE;
  226. }
  227. RegCloseKey(hKey);
  228. return TRUE;
  229. }
  230. //+---------------------------------------------------------------------------
  231. //
  232. // Function: GetRegEntryStr()
  233. //
  234. // Synopsis: Gets the value of specified registry key using the key name
  235. // An easier way than GetRegEntry()
  236. //
  237. // Arguments: pszBuffer [buffer for the key value]
  238. // dwBufferSize [size of the buffer]
  239. // pszKeyName [Key name whose value should be retrieved]
  240. //
  241. // History: t-byao Created 6/10/96
  242. //
  243. //----------------------------------------------------------------------------
  244. BOOL GetRegEntryStr(unsigned char *pszBuffer,
  245. DWORD dwBufferSize,
  246. const char *pszKeyName)
  247. {
  248. return GetRegEntry(HKEY_LOCAL_MACHINE,
  249. "SOFTWARE\\MICROSOFT\\NAMESERVICE\\MAPPING", pszKeyName,
  250. REG_SZ,NULL,0,pszBuffer,&dwBufferSize);
  251. }
  252. //+---------------------------------------------------------------------------
  253. //
  254. // Function: GetRegEntryInt()
  255. //
  256. // Synopsis: Gets the value of specified registry key using the key name
  257. // An easier way than GetRegEntry()
  258. //
  259. // Arguments: cstrKeyName [Key name whose value should be retrieved]
  260. //
  261. // Returns: Value of the Key (type: int)
  262. //
  263. // History: t-byao Created 6/17/96
  264. //
  265. //----------------------------------------------------------------------------
  266. BOOL GetRegEntryInt(int *pdValue, const char *pszKeyName)
  267. {
  268. DWORD dwSize=sizeof(int);
  269. DWORD dwValue;
  270. BOOL ret;
  271. ret = GetRegEntry(HKEY_LOCAL_MACHINE,
  272. "SOFTWARE\\MICROSOFT\\NAMESERVICE\\MAPPING",
  273. pszKeyName,
  274. REG_DWORD,NULL,0,(BYTE *)&dwValue,&dwSize);
  275. if (ret)
  276. {
  277. *pdValue = dwValue;
  278. }
  279. return ret;
  280. }
  281. //+---------------------------------------------------------------------------
  282. //
  283. // Function: GetRegEntryDWord()
  284. //
  285. // Synopsis: Gets the value of specified DWORD registry key using the key name
  286. // An easier way than using GetRegEntry() directly
  287. //
  288. // Arguments: cstrKeyName [Key name whose value should be retrieved]
  289. //
  290. // Returns: Value of the Key (type: DWORD)
  291. //
  292. // History: t-byao Created 6/19/96
  293. //
  294. //----------------------------------------------------------------------------
  295. BOOL GetRegEntryDWord(DWORD *pdValue, const char *pszKeyName)
  296. {
  297. DWORD dwSize = sizeof(int);
  298. return GetRegEntry(HKEY_LOCAL_MACHINE,
  299. "SOFTWARE\\MICROSOFT\\NAMESERVICE\\MAPPING",
  300. pszKeyName,
  301. REG_DWORD,NULL,
  302. 0,
  303. (BYTE *)pdValue,
  304. &dwSize);
  305. }
  306. */
  307. #endif