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.

513 lines
11 KiB

  1. /*
  2. * Copyright (c) 1996 Microsoft Corporation
  3. *
  4. * Module Name:
  5. *
  6. * util.cpp
  7. *
  8. * Abstract:
  9. *
  10. * This file communicates with exchange
  11. *
  12. * Author:
  13. *
  14. * Pat Styles (patst) 25-March-1997
  15. *
  16. * Environment:
  17. *
  18. * User Mode
  19. */
  20. #define _UTIL_CPP_
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <assert.h>
  25. #include <tchar.h>
  26. #include <time.h>
  27. #include "ocgen.h"
  28. #pragma hdrstop
  29. TCHAR glabel[] = TEXT("\n[OCGEN] ");
  30. // for logging
  31. #define gsLogFile TEXT("%windir%\\ocgen.log")
  32. #define gsLogCompletionMsg TEXT(" - complete\r\n")
  33. #define gsLogInitMsg TEXT("\r\n\r\nInitialize setup: OCGEN.DLL %s %s\r\n")
  34. // for trace statements
  35. #define gsTrace TEXT("OCGEN.DLL: Trace")
  36. typedef enum {
  37. nPreInit, // OC_PREINITIALIZE
  38. nInit, // OC_INIT_COMPONENT
  39. nSetLang, // OC_SET_LANGUAGE
  40. nQueryImage, // OC_QUERY_IMAGE
  41. nRequestPages, // OC_REQUEST_PAGES
  42. nQueryChangeSel, // OC_QUERY_CHANGE_SEL_STATE
  43. nCalcSpace, // OC_CALC_DISK_SPACE
  44. nQueueFile, // OC_QUEUE_FILE_OPS
  45. nQueueNot, // OC_NOTIFICATION_FROM_QUEUE
  46. nQueryStep, // OC_QUERY_STEP_COUNT
  47. nComplete, // OC_COMPLETE_INSTALLATION
  48. nCleanup, // OC_CLEANUP
  49. nQueryState, // OC_QUERY_STATE
  50. nNeedMedia, // OC_NEED_MEDIA
  51. nAboutToCommit, // OC_ABOUT_TO_COMMIT_QUEUE
  52. nQuerySkip, // OC_QUERY_SKIP_PAGE
  53. nWizardCreated, // OC_WIZARD_CREATED
  54. nExtraRoutines, // OC_EXTRA_ROUTINES
  55. nMaximum
  56. } notifications;
  57. typedef struct _OcMsgs {
  58. DWORD msg;
  59. TCHAR *desc;
  60. } OcMsgs;
  61. OcMsgs gMsgs[nMaximum] = {
  62. {OC_PREINITIALIZE, TEXT("OC_PREINITIALIZE")},
  63. {OC_INIT_COMPONENT, TEXT("OC_INIT_COMPONENT")},
  64. {OC_SET_LANGUAGE, TEXT("OC_SET_LANGUAGE")},
  65. {OC_QUERY_IMAGE, TEXT("OC_QUERY_IMAGE")},
  66. {OC_REQUEST_PAGES, TEXT("OC_REQUEST_PAGES")},
  67. {OC_QUERY_CHANGE_SEL_STATE, TEXT("OC_QUERY_CHANGE_SEL_STATE")},
  68. {OC_CALC_DISK_SPACE, TEXT("OC_CALC_DISK_SPACE")},
  69. {OC_QUEUE_FILE_OPS, TEXT("OC_QUEUE_FILE_OPS")},
  70. {OC_NOTIFICATION_FROM_QUEUE,TEXT("OC_NOTIFICATION_FROM_QUEUE")},
  71. {OC_QUERY_STEP_COUNT, TEXT("OC_QUERY_STEP_COUNT")},
  72. {OC_COMPLETE_INSTALLATION, TEXT("OC_COMPLETE_INSTALLATION")},
  73. {OC_CLEANUP, TEXT("OC_CLEANUP")},
  74. {OC_QUERY_STATE, TEXT("OC_QUERY_STATE")},
  75. {OC_NEED_MEDIA, TEXT("OC_NEED_MEDIA")},
  76. {OC_ABOUT_TO_COMMIT_QUEUE, TEXT("OC_ABOUT_TO_COMMIT_QUEUE")},
  77. {OC_QUERY_SKIP_PAGE, TEXT("OC_QUERY_SKIP_PAGE")},
  78. {OC_WIZARD_CREATED, TEXT("OC_WIZARD_CREATED")},
  79. {OC_EXTRA_ROUTINES, TEXT("OC_EXTRA_ROUTINES")}
  80. };
  81. TCHAR gUnknown[] = TEXT("Unknown Notification: ");
  82. // determines whether or not to display debug info
  83. DWORD gDebugLevel = (DWORD)-1;
  84. // forward reference
  85. TCHAR *NotificationText(DWORD msg);
  86. BOOL CheckLevel(DWORD level);
  87. /*
  88. * DebugTrace()
  89. */
  90. void DebugTrace(DWORD level, const TCHAR *text)
  91. {
  92. if (!CheckLevel(level))
  93. return;
  94. OutputDebugString(text);
  95. }
  96. /*
  97. * DebugTraceNL()
  98. *
  99. * precedes a trace statement with a newline and id prefix
  100. */
  101. void DebugTraceNL(DWORD level, const TCHAR *text)
  102. {
  103. DebugTrace(level, glabel);
  104. DebugTrace(level, text);
  105. }
  106. /*
  107. * NotificationText()
  108. */
  109. TCHAR *NotificationText(DWORD msg)
  110. {
  111. int i;
  112. static TCHAR desc[S_SIZE];
  113. for (i = 0; i < nMaximum; i++)
  114. {
  115. if (gMsgs[i].msg == msg)
  116. return gMsgs[i].desc;
  117. }
  118. wsprintf(desc, TEXT("OC_%d: "), msg);
  119. return desc;
  120. }
  121. /*
  122. * DebugTraceOCNotification()
  123. */
  124. void DebugTraceOCNotification(DWORD msg, const TCHAR *component)
  125. {
  126. DebugTraceNL(1, NotificationText(msg));
  127. DebugTrace(1, TEXT(": "));
  128. DebugTrace(1, component);
  129. DebugTrace(1, TEXT(" - "));
  130. }
  131. /*
  132. * DebugTraceFileCopy()
  133. */
  134. void DebugTraceFileCopy(const TCHAR *file)
  135. {
  136. DebugTraceNL(5, TEXT("TreeCopy: FILE="));
  137. DebugTrace(5, file);
  138. }
  139. /*
  140. * DebugTraceFileCopyError()
  141. */
  142. void DebugTraceFileCopyError()
  143. {
  144. TCHAR buf[S_SIZE];
  145. _stprintf(buf, FMT(" FAILURE CODE:[%d] "), GetLastError());
  146. DebugTrace(5, buf);
  147. }
  148. /*
  149. * DebugTraceDirCopy()
  150. */
  151. void DebugTraceDirCopy(const TCHAR *dir)
  152. {
  153. DebugTraceNL(3, TEXT("TreeCopy: DIR="));
  154. DebugTrace(3, dir);
  155. }
  156. /*
  157. * CheckLevel()
  158. */
  159. BOOL CheckLevel(DWORD level)
  160. {
  161. if (gDebugLevel == (DWORD)-1)
  162. gDebugLevel = SysGetDebugLevel();
  163. return (gDebugLevel >= level);
  164. }
  165. /*
  166. * MsgBox
  167. *
  168. */
  169. DWORD MsgBox(HWND hwnd, UINT textID, UINT type, ... )
  170. {
  171. static BOOL initialize = true;
  172. static TCHAR caption[S_SIZE];
  173. TCHAR text[S_SIZE];
  174. TCHAR format[S_SIZE];
  175. int len;
  176. va_list vaList;
  177. assert(hwnd && textID && type);
  178. if (initialize)
  179. {
  180. len = LoadString(ghinst, IDS_DIALOG_CAPTION, caption, S_SIZE);
  181. assert(len);
  182. if (!len) {
  183. _tcscpy( caption, TEXT("Setup"));
  184. }
  185. initialize = false;
  186. }
  187. len = LoadString(ghinst, textID, format, S_SIZE);
  188. assert(len);
  189. if (!len) {
  190. _tcscpy( format, TEXT("Unknown Error"));
  191. }
  192. va_start(vaList, type);
  193. tvsprintf(text, format, vaList);
  194. va_end(vaList);
  195. return MessageBox(hwnd, text, caption, type);
  196. }
  197. DWORD MsgBox(HWND hwnd, UINT textID, UINT captioniID, UINT type, ... )
  198. {
  199. TCHAR caption[S_SIZE];
  200. TCHAR text[S_SIZE];
  201. TCHAR format[S_SIZE];
  202. int len;
  203. va_list vaList;
  204. assert(hwnd && textID && type);
  205. len = LoadString(ghinst, captioniID, caption, S_SIZE);
  206. assert(len);
  207. if (!len) {
  208. _tcscpy( caption, TEXT("Setup"));
  209. }
  210. len = LoadString(ghinst, textID, format, S_SIZE);
  211. assert(len);
  212. if (!len) {
  213. _tcscpy( format, TEXT("Unknown Error"));
  214. }
  215. va_start(vaList, type);
  216. tvsprintf(text, format, vaList);
  217. va_end(vaList);
  218. return MessageBox(hwnd, text, caption, type);
  219. }
  220. DWORD MsgBox(HWND hwnd, TCHAR *fmt, TCHAR *caption, UINT type, ... )
  221. {
  222. TCHAR text[S_SIZE];
  223. va_list vaList;
  224. assert(hwnd && text && caption && type);
  225. va_start(vaList, type);
  226. tvsprintf(text, fmt, vaList);
  227. va_end(vaList);
  228. return MessageBox(hwnd, text, caption, type);
  229. }
  230. DWORD MBox(LPCTSTR fmt, LPCTSTR caption, ... )
  231. {
  232. TCHAR text[S_SIZE];
  233. va_list vaList;
  234. assert(fmt && caption);
  235. va_start(vaList, caption);
  236. tvsprintf(text, fmt, vaList);
  237. va_end(vaList);
  238. return MessageBox(ghwnd, text, caption, MB_ICONINFORMATION | MB_OK);
  239. }
  240. DWORD TMBox(LPCTSTR fmt, ... )
  241. {
  242. TCHAR text[S_SIZE];
  243. va_list vaList;
  244. assert(fmt);
  245. va_start(vaList, fmt);
  246. tvsprintf(text, fmt, vaList);
  247. va_end(vaList);
  248. return MessageBox(ghwnd, text, gsTrace, MB_ICONINFORMATION | MB_OK);
  249. }
  250. /*
  251. * SysGetDebugLevel()
  252. */
  253. DWORD SysGetDebugLevel()
  254. {
  255. DWORD rc;
  256. DWORD err;
  257. DWORD size;
  258. DWORD type;
  259. HKEY hkey;
  260. err = RegOpenKey(HKEY_LOCAL_MACHINE,
  261. TEXT("SOFTWARE\\microsoft\\windows\\currentversion\\setup"),
  262. &hkey);
  263. if (err != ERROR_SUCCESS)
  264. return 0;
  265. size = sizeof(DWORD);
  266. err = RegQueryValueEx(hkey,
  267. TEXT("OCGen Debug Level"),
  268. 0,
  269. &type,
  270. (LPBYTE)&rc,
  271. &size);
  272. if (err != ERROR_SUCCESS || type != REG_DWORD)
  273. rc = 0;
  274. RegCloseKey(hkey);
  275. return rc;
  276. }
  277. /*
  278. * TCharStringToAnsiString
  279. */
  280. DWORD TCharStringToAnsiString(TCHAR *tsz ,char *asz)
  281. {
  282. DWORD count;
  283. assert(tsz && asz);
  284. #ifdef UNICODE
  285. count = WideCharToMultiByte(CP_ACP,
  286. 0,
  287. tsz,
  288. -1,
  289. NULL,
  290. 0,
  291. NULL,
  292. NULL);
  293. if (!count || count > S_SIZE)
  294. return count;
  295. return WideCharToMultiByte(CP_ACP,
  296. 0,
  297. tsz,
  298. -1,
  299. asz,
  300. count,
  301. NULL,
  302. NULL);
  303. #else
  304. _tcscpy(asz, tsz);
  305. return _tcslen(asz);
  306. #endif
  307. }
  308. void logOCNotification(DWORD msg, const TCHAR *component)
  309. {
  310. log(FMT("[%s - %s]"), component, NotificationText(msg));
  311. }
  312. void logOCNotificationCompletion()
  313. {
  314. log(gsLogCompletionMsg);
  315. }
  316. void loginit()
  317. {
  318. HANDLE hfile;
  319. TCHAR logfile[MAX_PATH];
  320. char fmt[S_SIZE];
  321. char output[S_SIZE];
  322. char time[S_SIZE];
  323. char date[S_SIZE];
  324. DWORD bytes;
  325. //#ifdef DEBUG
  326. TCharStringToAnsiString(gsLogInitMsg, fmt);
  327. _strdate(date);
  328. _strtime(time);
  329. sprintf(output, fmt, date, time);
  330. // open the log file
  331. ExpandEnvironmentStrings(gsLogFile, logfile, MAX_PATH);
  332. hfile = CreateFile(logfile,
  333. GENERIC_WRITE,
  334. 0,
  335. NULL,
  336. OPEN_EXISTING,
  337. 0,
  338. NULL);
  339. if (hfile == INVALID_HANDLE_VALUE)
  340. hfile = CreateFile(logfile,
  341. GENERIC_WRITE,
  342. 0,
  343. NULL,
  344. CREATE_ALWAYS,
  345. 0,
  346. NULL);
  347. if (hfile != INVALID_HANDLE_VALUE)
  348. {
  349. SetFilePointer(hfile, 0, NULL, FILE_END);
  350. WriteFile(hfile, output, strlen(output) * sizeof(char), &bytes, NULL);
  351. CloseHandle(hfile);
  352. }
  353. //#endif
  354. }
  355. void log(TCHAR *fmt, ...)
  356. {
  357. TCHAR logfile[MAX_PATH];
  358. TCHAR text[S_SIZE];
  359. char output[S_SIZE];
  360. DWORD bytes;
  361. HANDLE hfile;
  362. va_list vaList;
  363. //#ifdef DEBUG
  364. assert(fmt);
  365. // create the output string
  366. va_start(vaList, fmt);
  367. tvsprintf(text, fmt, vaList);
  368. va_end(vaList);
  369. TCharStringToAnsiString(text, output);
  370. // create the log file name in the root directory
  371. ExpandEnvironmentStrings(gsLogFile, logfile, MAX_PATH);
  372. // open the log file
  373. hfile = CreateFile(logfile,
  374. GENERIC_WRITE,
  375. 0,
  376. NULL,
  377. OPEN_EXISTING,
  378. 0,
  379. NULL);
  380. if (hfile != INVALID_HANDLE_VALUE)
  381. {
  382. SetFilePointer(hfile, 0, NULL, FILE_END);
  383. WriteFile(hfile, output, strlen(output) * sizeof(char), &bytes, NULL);
  384. CloseHandle(hfile);
  385. }
  386. //#endif
  387. }
  388. void AssureTrailingBackslash(TCHAR *path)
  389. {
  390. TCHAR *p;
  391. assert(path && *path);
  392. p = path + _tcslen(path) - 1;
  393. if (*p != TEXT('\\'))
  394. _tcscat(path, TEXT("\\"));
  395. }
  396. BOOL IsNT()
  397. {
  398. DWORD dwver;
  399. dwver = GetVersion();
  400. if (dwver < 0x8000000)
  401. return TRUE;
  402. return FALSE;
  403. }