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.

481 lines
12 KiB

  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. #define MAX_KEYNAME_SIZE 2048
  5. #define MAX_VALUENAME_SIZE 512
  6. //
  7. // Verison number for the registry file format
  8. //
  9. #define REGISTRY_FILE_VERSION 1
  10. //
  11. // File signature
  12. //
  13. #define REGFILE_SIGNATURE 0x67655250
  14. BOOL DisplayRegistryData (LPTSTR lpRegistry);
  15. int __cdecl main( int argc, char *argv[])
  16. {
  17. WCHAR szPath[MAX_PATH * 2];
  18. if (argc != 2) {
  19. _tprintf(TEXT("usage: regview <pathname>\registry.pol"));
  20. _tprintf(TEXT("example: regview d:\registry.pol"));
  21. return 1;
  22. }
  23. if (!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[1], -1, szPath,
  24. (MAX_PATH * 2))) {
  25. _tprintf(TEXT("Failed to convert path to unicode"));
  26. return 1;
  27. }
  28. DisplayRegistryData(szPath);
  29. return 0;
  30. }
  31. //*************************************************************
  32. //
  33. // DisplayRegistryData()
  34. //
  35. // Purpose: Displays the registry data
  36. //
  37. // Parameters: lpRegistry - Path to registry.pol
  38. //
  39. //
  40. // Return: TRUE if successful
  41. // FALSE if an error occurs
  42. //
  43. //*************************************************************
  44. BOOL DisplayRegistryData (LPTSTR lpRegistry)
  45. {
  46. HANDLE hFile;
  47. BOOL bResult = FALSE;
  48. DWORD dwTemp, dwBytesRead, dwType, dwDataLength, dwIndex, dwCount;
  49. LPWSTR lpKeyName, lpValueName, lpTemp;
  50. LPBYTE lpData = NULL, lpIndex;
  51. WCHAR chTemp;
  52. INT i;
  53. CHAR szString[20];
  54. //
  55. // Open the registry file
  56. //
  57. hFile = CreateFile (lpRegistry, GENERIC_READ, FILE_SHARE_READ, NULL,
  58. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  59. NULL);
  60. if (hFile == INVALID_HANDLE_VALUE) {
  61. if (GetLastError() == ERROR_FILE_NOT_FOUND)
  62. {
  63. return TRUE;
  64. }
  65. else
  66. {
  67. _tprintf(TEXT("DisplayRegistryData: CreateFile failed with %d"),
  68. GetLastError());
  69. return FALSE;
  70. }
  71. }
  72. //
  73. // Allocate buffers to hold the keyname, valuename, and data
  74. //
  75. lpKeyName = (LPWSTR) LocalAlloc (LPTR, MAX_KEYNAME_SIZE * sizeof(WCHAR));
  76. if (!lpKeyName)
  77. {
  78. _tprintf(TEXT("DisplayRegistryData: Failed to allocate memory with %d"),
  79. GetLastError());
  80. return FALSE;
  81. }
  82. lpValueName = (LPWSTR) LocalAlloc (LPTR, MAX_VALUENAME_SIZE * sizeof(WCHAR));
  83. if (!lpValueName)
  84. {
  85. _tprintf(TEXT("DisplayRegistryData: Failed to allocate memory with %d"),
  86. GetLastError());
  87. LocalFree (lpKeyName);
  88. return FALSE;
  89. }
  90. //
  91. // Read the header block
  92. //
  93. // 2 DWORDS, signature (PReg) and version number and 2 newlines
  94. //
  95. if (!ReadFile (hFile, &dwTemp, sizeof(dwTemp), &dwBytesRead, NULL) ||
  96. dwBytesRead != sizeof(dwTemp))
  97. {
  98. _tprintf(TEXT("DisplayRegistryData: Failed to read signature with %d"),
  99. GetLastError());
  100. goto Exit;
  101. }
  102. if (dwTemp != REGFILE_SIGNATURE)
  103. {
  104. _tprintf(TEXT("DisplayRegistryData: Invalid file signature"));
  105. goto Exit;
  106. }
  107. if (!ReadFile (hFile, &dwTemp, sizeof(dwTemp), &dwBytesRead, NULL) ||
  108. dwBytesRead != sizeof(dwTemp))
  109. {
  110. _tprintf(TEXT("DisplayRegistryData: Failed to read version number with %d"),
  111. GetLastError());
  112. goto Exit;
  113. }
  114. if (dwTemp != REGISTRY_FILE_VERSION)
  115. {
  116. _tprintf(TEXT("DisplayRegistryData: Invalid file version"));
  117. goto Exit;
  118. }
  119. //
  120. // Read the data
  121. //
  122. while (TRUE)
  123. {
  124. //
  125. // Read the first character. It will either be a [ or the end
  126. // of the file.
  127. //
  128. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  129. {
  130. if (GetLastError() != ERROR_HANDLE_EOF)
  131. {
  132. _tprintf(TEXT("DisplayRegistryData: Failed to read first character with %d"),
  133. GetLastError());
  134. goto Exit;
  135. }
  136. break;
  137. }
  138. if ((dwBytesRead == 0) || (chTemp != L'['))
  139. {
  140. break;
  141. }
  142. //
  143. // Read the keyname
  144. //
  145. lpTemp = lpKeyName;
  146. while (TRUE)
  147. {
  148. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  149. {
  150. _tprintf(TEXT("DisplayRegistryData: Failed to read keyname character with %d"),
  151. GetLastError());
  152. goto Exit;
  153. }
  154. *lpTemp++ = chTemp;
  155. if (chTemp == TEXT('\0'))
  156. break;
  157. }
  158. //
  159. // Read the semi-colon
  160. //
  161. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  162. {
  163. if (GetLastError() != ERROR_HANDLE_EOF)
  164. {
  165. _tprintf(TEXT("DisplayRegistryData: Failed to read first character with %d"),
  166. GetLastError());
  167. goto Exit;
  168. }
  169. break;
  170. }
  171. if ((dwBytesRead == 0) || (chTemp != L';'))
  172. {
  173. break;
  174. }
  175. //
  176. // Read the valuename
  177. //
  178. lpTemp = lpValueName;
  179. while (TRUE)
  180. {
  181. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  182. {
  183. _tprintf(TEXT("DisplayRegistryData: Failed to read valuename character with %d"),
  184. GetLastError());
  185. goto Exit;
  186. }
  187. *lpTemp++ = chTemp;
  188. if (chTemp == TEXT('\0'))
  189. break;
  190. }
  191. //
  192. // Read the semi-colon
  193. //
  194. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  195. {
  196. if (GetLastError() != ERROR_HANDLE_EOF)
  197. {
  198. _tprintf(TEXT("DisplayRegistryData: Failed to read first character with %d"),
  199. GetLastError());
  200. goto Exit;
  201. }
  202. break;
  203. }
  204. if ((dwBytesRead == 0) || (chTemp != L';'))
  205. {
  206. break;
  207. }
  208. //
  209. // Read the type
  210. //
  211. if (!ReadFile (hFile, &dwType, sizeof(DWORD), &dwBytesRead, NULL))
  212. {
  213. _tprintf(TEXT("DisplayRegistryData: Failed to read type with %d"),
  214. GetLastError());
  215. goto Exit;
  216. }
  217. //
  218. // Skip semicolon
  219. //
  220. if (!ReadFile (hFile, &dwTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  221. {
  222. _tprintf(TEXT("DisplayRegistryData: Failed to skip semicolon with %d"),
  223. GetLastError());
  224. goto Exit;
  225. }
  226. //
  227. // Read the data length
  228. //
  229. if (!ReadFile (hFile, &dwDataLength, sizeof(DWORD), &dwBytesRead, NULL))
  230. {
  231. _tprintf(TEXT("DisplayRegistryData: Failed to data length with %d"),
  232. GetLastError());
  233. goto Exit;
  234. }
  235. //
  236. // Skip semicolon
  237. //
  238. if (!ReadFile (hFile, &dwTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  239. {
  240. _tprintf(TEXT("DisplayRegistryData: Failed to skip semicolon with %d"),
  241. GetLastError());
  242. goto Exit;
  243. }
  244. //
  245. // Allocate memory for data
  246. //
  247. lpData = (LPBYTE) LocalAlloc (LPTR, dwDataLength);
  248. if (!lpData)
  249. {
  250. _tprintf(TEXT("DisplayRegistryData: Failed to allocate memory for data with %d"),
  251. GetLastError());
  252. goto Exit;
  253. }
  254. //
  255. // Read data
  256. //
  257. if (!ReadFile (hFile, lpData, dwDataLength, &dwBytesRead, NULL))
  258. {
  259. _tprintf(TEXT("DisplayRegistryData: Failed to read data with %d"),
  260. GetLastError());
  261. goto Exit;
  262. }
  263. //
  264. // Skip closing bracket
  265. //
  266. if (!ReadFile (hFile, &chTemp, sizeof(WCHAR), &dwBytesRead, NULL))
  267. {
  268. _tprintf(TEXT("DisplayRegistryData: Failed to skip closing bracket with %d"),
  269. GetLastError());
  270. goto Exit;
  271. }
  272. if (chTemp != L']')
  273. {
  274. _tprintf(TEXT("DisplayRegistryData: Expected to find ], but found %c"),
  275. chTemp);
  276. goto Exit;
  277. }
  278. //
  279. // Print out the entry
  280. //
  281. _tprintf (TEXT("\nKeyName:\t%s\n"), lpKeyName);
  282. _tprintf (TEXT("ValueName:\t%s\n"), lpValueName);
  283. switch (dwType) {
  284. case REG_DWORD:
  285. _tprintf (TEXT("ValueType:\tREG_DWORD\n"));
  286. _tprintf (TEXT("Value:\t\t0x%08x\n"), *((LPDWORD)lpData));
  287. break;
  288. case REG_SZ:
  289. _tprintf (TEXT("ValueType:\tREG_SZ\n"));
  290. _tprintf (TEXT("Value:\t%s\n"), (LPTSTR)lpData);
  291. break;
  292. case REG_EXPAND_SZ:
  293. _tprintf (TEXT("ValueType:\tREG_EXPAND_SZ\n"));
  294. _tprintf (TEXT("Value:\t%s\n"), (LPTSTR)lpData);
  295. break;
  296. case REG_MULTI_SZ:
  297. _tprintf (TEXT("ValueType:\tREG_MULTI_SZ\n"));
  298. _tprintf (TEXT("Value:\n\t\t"));
  299. lpTemp = (LPWSTR) lpData;
  300. while (*lpTemp) {
  301. _tprintf (TEXT("%s\n\t\t"), lpTemp);
  302. lpTemp += lstrlen(lpTemp) + 1;
  303. }
  304. break;
  305. case REG_BINARY:
  306. _tprintf (TEXT("ValueType:\tREG_BINARY\n"));
  307. _tprintf (TEXT("Value:\n\t"));
  308. dwIndex = 0;
  309. dwCount = 0;
  310. lpIndex = lpData;
  311. ZeroMemory(szString, sizeof(szString));
  312. while (dwIndex <= dwDataLength) {
  313. _tprintf (TEXT("%02x "), *lpIndex);
  314. if ((*lpIndex > 32) && (*lpIndex < 127)) {
  315. szString[dwCount] = *lpIndex;
  316. } else {
  317. szString[dwCount] = '.';
  318. }
  319. if (dwCount < 15) {
  320. dwCount++;
  321. } else {
  322. printf (" %s", szString);
  323. _tprintf (TEXT("\n\t"));
  324. ZeroMemory(szString, sizeof(szString));
  325. dwCount = 0;
  326. }
  327. dwIndex++;
  328. lpIndex++;
  329. }
  330. if (dwCount > 0) {
  331. while (dwCount < 16) {
  332. _tprintf (TEXT(" "));
  333. dwCount++;
  334. }
  335. printf (" %s\n", szString);
  336. }
  337. _tprintf (TEXT("\n"));
  338. break;
  339. case REG_NONE:
  340. _tprintf (TEXT("ValueType:\tREG_NONE\n"));
  341. _tprintf (TEXT("Value:\t\tThis key contains no values\n"), *lpData);
  342. break;
  343. default:
  344. _tprintf (TEXT("ValueType:\tUnknown\n"));
  345. _tprintf (TEXT("ValueSize:\t%d\n"), dwDataLength);
  346. break;
  347. }
  348. LocalFree (lpData);
  349. lpData = NULL;
  350. }
  351. bResult = TRUE;
  352. Exit:
  353. //
  354. // Finished
  355. //
  356. if (lpData) {
  357. LocalFree (lpData);
  358. }
  359. CloseHandle (hFile);
  360. LocalFree (lpKeyName);
  361. LocalFree (lpValueName);
  362. return bResult;
  363. }