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.

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