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.

355 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name :
  4. isutils.cxx
  5. Abstract:
  6. This module defines functions that are generic for Internet servers
  7. Author:
  8. Murali R. Krishnan ( MuraliK ) 15-Nov-1995
  9. Environment:
  10. Win32 - User Mode
  11. Project:
  12. Internet Servers Common DLL
  13. Functions Exported:
  14. IsLargeIntegerToChar();
  15. Revision History:
  16. --*/
  17. #include "precomp.hxx"
  18. #include <inetsvcs.h>
  19. #include <mbstring.h>
  20. DWORD
  21. IsLargeIntegerToDecimalChar(
  22. IN const LARGE_INTEGER * pliValue,
  23. OUT LPSTR pchBuffer
  24. )
  25. /*++
  26. Routine Description:
  27. Maps a Large Integer to be a displayable string.
  28. Arguments:
  29. pliValue - The LARGE INTEGER to be mapped.
  30. pchBuffer - pointer to character buffer to store the result
  31. (This buffer should be at least about 32 bytes long to hold
  32. entire large integer value as well as null character)
  33. Return Value:
  34. Win32 Error code. NO_ERROR on success
  35. --*/
  36. {
  37. PSTR p1;
  38. PSTR p2;
  39. BOOL negative;
  40. LONGLONG Value;
  41. if ( pchBuffer == NULL || pliValue == NULL) {
  42. return ( ERROR_INVALID_PARAMETER);
  43. }
  44. //
  45. // Handling zero specially makes everything else a bit easier.
  46. //
  47. if( pliValue->QuadPart == 0 ) {
  48. // store the value 0 and return.
  49. pchBuffer[0] = '0';
  50. pchBuffer[1] = '\0';
  51. return (NO_ERROR);
  52. }
  53. Value = pliValue->QuadPart; // cache the value.
  54. //
  55. // Remember if the value is negative.
  56. //
  57. if( Value < 0 ) {
  58. negative = TRUE;
  59. Value = -Value;
  60. } else {
  61. negative = FALSE;
  62. }
  63. //
  64. // Pull the least signifigant digits off the value and store them
  65. // into the buffer. Note that this will store the digits in the
  66. // reverse order.
  67. // p1 is used for storing the digits as they are computed
  68. // p2 is used during the reversing stage.
  69. //
  70. p1 = p2 = pchBuffer;
  71. for ( p1 = pchBuffer; Value != 0; ) {
  72. int digit = (int)( Value % 10 );
  73. Value = Value / 10;
  74. *p1++ = '0' + digit;
  75. } // for
  76. //
  77. // Tack on a '-' if necessary.
  78. //
  79. if( negative ) {
  80. *p1++ = '-';
  81. }
  82. // terminate the string
  83. *p1-- = '\0';
  84. //
  85. // Reverse the digits in the buffer.
  86. //
  87. for( p2 = pchBuffer; ( p1 > p2 ); p1--, p2++) {
  88. CHAR ch = *p1;
  89. *p1 = *p2;
  90. *p2 = ch;
  91. } // for
  92. return ( NO_ERROR);
  93. } // IsLargeIntegerToDecimalChar()
  94. BOOL
  95. ZapRegistryKey(
  96. IN HKEY hKey,
  97. IN LPCSTR pszRegPath
  98. )
  99. /*++
  100. Description:
  101. Zaps the reg key starting from pszRegPath down
  102. Arguments:
  103. hkey - handle for parent
  104. pszRegPath - Key to zap
  105. Returns:
  106. FALSE if there is any error.
  107. TRUE when the reg key was successfully zapped.
  108. --*/
  109. {
  110. DWORD err = NO_ERROR;
  111. DWORD i = 0;
  112. HKEY hKeyParam;
  113. if ( hKey == NULL ) {
  114. hKey = HKEY_LOCAL_MACHINE;
  115. }
  116. //
  117. // Loop through instance keys
  118. //
  119. err = RegOpenKeyEx( hKey,
  120. pszRegPath,
  121. 0,
  122. KEY_ALL_ACCESS,
  123. &hKeyParam );
  124. if( err != NO_ERROR ) {
  125. return(TRUE);
  126. }
  127. while ( TRUE ) {
  128. CHAR szKeyName[MAX_PATH+1];
  129. DWORD cbKeyName = sizeof( szKeyName );
  130. FILETIME ft;
  131. BOOL fRet;
  132. DWORD dwInstance;
  133. CHAR szRegKey[MAX_PATH+1];
  134. err = RegEnumKeyEx( hKeyParam,
  135. i,
  136. szKeyName,
  137. &cbKeyName,
  138. NULL,
  139. NULL,
  140. NULL,
  141. &ft );
  142. if ( err == ERROR_NO_MORE_ITEMS ) {
  143. err = NO_ERROR;
  144. break;
  145. }
  146. //
  147. // Zap this key
  148. //
  149. ZapRegistryKey(hKeyParam, szKeyName);
  150. }
  151. RegCloseKey(hKeyParam);
  152. err = RegDeleteKey(
  153. hKey,
  154. pszRegPath
  155. );
  156. if ( err != NO_ERROR ) {
  157. return(FALSE);
  158. }
  159. return(TRUE);
  160. } // ZapRegistryKey
  161. HKEY
  162. CreateKey(
  163. IN HKEY RootKey,
  164. IN LPCSTR KeyName,
  165. IN LPCSTR KeyValue
  166. )
  167. {
  168. HKEY hKey = NULL;
  169. DWORD dwDisp;
  170. if ( RegCreateKeyExA(RootKey,
  171. KeyName,
  172. NULL,
  173. "",
  174. REG_OPTION_NON_VOLATILE,
  175. KEY_ALL_ACCESS,
  176. NULL,
  177. &hKey,
  178. &dwDisp) != ERROR_SUCCESS ) {
  179. goto exit;
  180. }
  181. if ( KeyValue != NULL ) {
  182. if (RegSetValueExA(hKey,
  183. "",
  184. NULL,
  185. REG_SZ,
  186. (LPBYTE)KeyValue,
  187. lstrlen(KeyValue)+1)!=ERROR_SUCCESS) {
  188. RegCloseKey(hKey);
  189. hKey = NULL;
  190. }
  191. }
  192. exit:
  193. return hKey;
  194. } // CreateKey
  195. BOOL
  196. IISCreateDirectory(
  197. IN LPCSTR DirectoryName,
  198. IN BOOL fAllowNetDrive
  199. )
  200. {
  201. PCHAR p;
  202. DWORD len;
  203. len = strlen(DirectoryName);
  204. if ( (len < 3) ||
  205. (DirectoryName[1] != ':') ||
  206. (DirectoryName[2] != '\\') ) {
  207. SetLastError(ERROR_INVALID_NAME);
  208. return(FALSE);
  209. }
  210. if ( !fAllowNetDrive ) {
  211. UINT driveType;
  212. CHAR path[4];
  213. CopyMemory(path, DirectoryName, 3);
  214. path[3] = '\0';
  215. driveType = GetDriveType(path);
  216. if ( driveType == DRIVE_REMOTE ) {
  217. DBGPRINTF((DBG_CONTEXT,
  218. "%s is a remote directory. Not allowed\n", path));
  219. SetLastError(ERROR_INVALID_NAME);
  220. return(FALSE);
  221. }
  222. }
  223. p = (PCHAR)DirectoryName+3;
  224. do {
  225. p = (PCHAR)_mbschr((PUCHAR)p,'\\');
  226. if ( p != NULL ) {
  227. *p = '\0';
  228. }
  229. if ( !CreateDirectoryA(DirectoryName,NULL) ) {
  230. DWORD err = GetLastError();
  231. if ( err != ERROR_ALREADY_EXISTS ) {
  232. DBGPRINTF((DBG_CONTEXT,
  233. "Error %d in CreateDirectory [%s]\n",
  234. err, DirectoryName));
  235. if ( p != NULL ) {
  236. *p = '\\';
  237. }
  238. SetLastError(err);
  239. return(FALSE);
  240. }
  241. }
  242. if ( p != NULL ) {
  243. *p = '\\';
  244. p++;
  245. }
  246. } while ( p != NULL );
  247. return(TRUE);
  248. } // IISCreateDirectory