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.

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