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.

553 lines
13 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. wiztools.c
  5. Abstract:
  6. Implements common code for upgwiz wizard plugins.
  7. Author:
  8. Jim Schmidt (jimschm) 07-Oct-1998
  9. Revision History:
  10. <alias> <date> <comments>
  11. --*/
  12. #include "pch.h"
  13. #include "..\inc\dgdll.h"
  14. #include "..\..\w95upg\migapp\migdbp.h"
  15. HINSTANCE g_OurInst;
  16. BOOL
  17. Init (
  18. VOID
  19. )
  20. {
  21. return InitToolMode (g_OurInst);
  22. }
  23. VOID
  24. Terminate (
  25. VOID
  26. )
  27. {
  28. TerminateToolMode (g_OurInst);
  29. }
  30. BOOL
  31. WINAPI
  32. DllMain (
  33. IN HINSTANCE hInstance,
  34. IN DWORD dwReason,
  35. IN LPVOID lpReserved
  36. )
  37. {
  38. g_OurInst = hInstance;
  39. return TRUE;
  40. }
  41. VOID
  42. WizToolsMain (
  43. IN DWORD dwReason
  44. )
  45. {
  46. if (dwReason == DLL_PROCESS_ATTACH) {
  47. Init();
  48. } else if (dwReason == DLL_PROCESS_DETACH) {
  49. Terminate();
  50. }
  51. }
  52. BOOL
  53. WizardWriteRealString (
  54. IN HANDLE File,
  55. IN PCSTR String
  56. )
  57. {
  58. if (!WriteFileString (File, String)) {
  59. return FALSE;
  60. }
  61. printf ("%s", String);
  62. return TRUE;
  63. }
  64. VOID
  65. GenerateUniqueStringSectKey (
  66. IN PCSTR TwoLetterId,
  67. OUT PSTR Buffer
  68. )
  69. {
  70. SYSTEMTIME Time;
  71. DWORD TimeLow, TimeHigh;
  72. DWORD UserHash;
  73. DWORD Size;
  74. CHAR UserName[MAX_USER_NAME];
  75. PCSTR p;
  76. Size = MAX_USER_NAME;
  77. GetUserName (UserName, &Size);
  78. p = UserName;
  79. UserHash = 0;
  80. while (*p) {
  81. UserHash = _rotl (UserHash, 1) ^ (*p & 0x3f);
  82. p++;
  83. }
  84. GetLocalTime (&Time);
  85. TimeLow = Time.wMilliseconds +
  86. Time.wSecond * 1000 +
  87. Time.wMinute * 60000;
  88. TimeHigh = Time.wHour +
  89. Time.wDay * 24 +
  90. Time.wMonth * 744 +
  91. Time.wYear * 8928;
  92. wsprintf (Buffer, "%s%x%x%x", TwoLetterId, TimeLow, TimeHigh, UserHash);
  93. }
  94. BOOL
  95. WriteHeader (
  96. IN HANDLE File
  97. )
  98. {
  99. CHAR Msg[256];
  100. CHAR UserName[64];
  101. DWORD Size = 64;
  102. SYSTEMTIME SysTime;
  103. SYSTEMTIME LocTime;
  104. static PCSTR Month[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
  105. GetUserName (UserName, &Size);
  106. GetSystemTime (&SysTime);
  107. GetLocalTime (&LocTime);
  108. wsprintf (
  109. Msg,
  110. "Report From %s on %u:%02u (%u:%02u) %02u-%s-%04u:\r\n\r\n",
  111. UserName,
  112. LocTime.wHour,
  113. LocTime.wMinute,
  114. SysTime.wHour,
  115. SysTime.wMinute,
  116. LocTime.wDay,
  117. Month[LocTime.wMonth - 1],
  118. LocTime.wYear
  119. );
  120. return WizardWriteRealString (File, Msg);
  121. }
  122. BOOL
  123. pWriteAdditionalSpaces (
  124. IN HANDLE File,
  125. IN DWORD SpacesWritten,
  126. IN DWORD SpacesNeeded
  127. )
  128. {
  129. while (SpacesWritten < SpacesNeeded) {
  130. if (!WizardWriteRealString (File, " ")) {
  131. return FALSE;
  132. }
  133. SpacesWritten ++;
  134. }
  135. return TRUE;
  136. }
  137. BOOL
  138. WizardWriteInfString (
  139. IN HANDLE File,
  140. IN PCSTR String,
  141. IN BOOL Quoted,
  142. IN BOOL SkipCRLF,
  143. IN BOOL ReplaceSpace,
  144. IN CHAR SpaceReplacement,
  145. IN DWORD ColumnWidth
  146. )
  147. {
  148. PCSTR s;
  149. PSTR d;
  150. CHAR Buf[2048];
  151. BOOL quoteMode = FALSE;
  152. BOOL replaceMode = FALSE;
  153. DWORD columnWidth = 0;
  154. if (!String) {
  155. if (!WizardWriteRealString (File, "\"\"")) {
  156. return FALSE;
  157. }
  158. columnWidth +=2;
  159. return pWriteAdditionalSpaces (File, columnWidth, ColumnWidth);
  160. }
  161. if (Quoted) {
  162. if (!WizardWriteRealString (File, "\"")) {
  163. return FALSE;
  164. }
  165. columnWidth ++;
  166. }
  167. s = String;
  168. d = Buf;
  169. while (*s) {
  170. if (SkipCRLF && ((*s == '\r') || (*s == '\n'))) {
  171. s++;
  172. continue;
  173. }
  174. if (ReplaceSpace && (*s == ' ')) {
  175. *d++ = SpaceReplacement;
  176. s++;
  177. columnWidth ++;
  178. continue;
  179. }
  180. if (*s == '\"') {
  181. if (Quoted) {
  182. *d++ = '\"';
  183. columnWidth ++;
  184. } else if (!replaceMode) {
  185. quoteMode = !quoteMode;
  186. }
  187. }
  188. if (*s == '%') {
  189. if (!quoteMode && !Quoted) {
  190. replaceMode = !replaceMode;
  191. *d++ = '%';
  192. columnWidth ++;
  193. }
  194. }
  195. *d++ = *s++;
  196. columnWidth ++;
  197. }
  198. if (Quoted) {
  199. *d++ = '\"';
  200. columnWidth ++;
  201. }
  202. *d = 0;
  203. if (!WizardWriteRealString (File, Buf)) {
  204. return FALSE;
  205. }
  206. return pWriteAdditionalSpaces (File, columnWidth, ColumnWidth);
  207. }
  208. BOOL
  209. WriteStringSectKey (
  210. IN HANDLE File,
  211. IN PCSTR KeyName,
  212. IN PCSTR String
  213. )
  214. {
  215. if (!WizardWriteQuotedString (File, KeyName)) {
  216. return FALSE;
  217. }
  218. if (!WizardWriteRealString (File, " = ")) {
  219. return FALSE;
  220. }
  221. if (!WizardWriteQuotedString (File, String)) {
  222. return FALSE;
  223. }
  224. if (!WizardWriteRealString (File, "\r\n")) {
  225. return FALSE;
  226. }
  227. return TRUE;
  228. }
  229. BOOL
  230. WriteFileAttributes (
  231. IN POUTPUTARGS Args,
  232. IN PCSTR NonLocalizedName, OPTIONAL
  233. IN HANDLE FileHandle,
  234. IN PCSTR FileSpec,
  235. IN PCSTR Section OPTIONAL
  236. )
  237. {
  238. BOOL b = FALSE;
  239. WIN32_FIND_DATA fd;
  240. HANDLE FindHandle;
  241. CHAR Buf[2048];
  242. CHAR NameKey[64];
  243. CHAR TextKey[64];
  244. FindHandle = FindFirstFile (FileSpec, &fd);
  245. if (FindHandle == INVALID_HANDLE_VALUE) {
  246. return FALSE;
  247. }
  248. __try {
  249. GenerateUniqueStringSectKey ("WN", NameKey);
  250. GenerateUniqueStringSectKey ("WD", TextKey);
  251. if (Section) {
  252. if (!WizardWriteRealString (FileHandle, Section)) {
  253. __leave;
  254. }
  255. if (!WizardWriteRealString (FileHandle, "\r\n")) {
  256. __leave;
  257. }
  258. }
  259. Buf[0] = 0;
  260. if (NonLocalizedName) {
  261. wsprintf (Buf, "%s,", NonLocalizedName);
  262. NameKey[0] = 0;
  263. } else {
  264. wsprintf (Buf, "%%%s%%,", NameKey);
  265. }
  266. if (Args->OptionalText) {
  267. wsprintf (_mbschr (Buf, 0), " %%%s%%,", TextKey);
  268. } else {
  269. StringCat (Buf, ",");
  270. TextKey[0] = 0;
  271. }
  272. if (!WizardWriteColumn (FileHandle, Buf, 45)) {
  273. __leave;
  274. }
  275. if (GetFileAttributesLine (FileSpec, Buf, 2048)) {
  276. if (!WizardWriteColumn (FileHandle, Buf, 0)) {
  277. __leave;
  278. }
  279. } else {
  280. wsprintf (Buf, "%s,", GetFileNameFromPath (FileSpec));
  281. if (!WizardWriteColumn (FileHandle, Buf, 14)) {
  282. __leave;
  283. }
  284. wsprintf (Buf, "FILESIZE(%u)", fd.nFileSizeLow);
  285. if (!WizardWriteRealString (FileHandle, Buf)) {
  286. __leave;
  287. }
  288. }
  289. if (NameKey[0] || TextKey[0]) {
  290. if (!WizardWriteRealString (FileHandle, "\r\n\r\n[Strings]\r\n")) {
  291. __leave;
  292. }
  293. }
  294. if (NameKey[0]) {
  295. if (!WriteStringSectKey (FileHandle, NameKey, Args->OptionalDescription)) {
  296. __leave;
  297. }
  298. }
  299. if (TextKey[0]) {
  300. if (!WriteStringSectKey (FileHandle, TextKey, Args->OptionalText)) {
  301. __leave;
  302. }
  303. }
  304. b = TRUE;
  305. }
  306. __finally {
  307. FindClose (FindHandle);
  308. }
  309. return b;
  310. }
  311. #define ATTR_FILESIZE 0x1
  312. #define ATTR_CHECKSUM 0x2
  313. #define ATTR_COMPNAME 0x4
  314. #define ATTR_FILEDESC 0x8
  315. #define ATTR_FILEVER 0x10
  316. #define ATTR_INTNAME 0x20
  317. #define ATTR_LEGAL 0x40
  318. #define ATTR_ORIGNAME 0x80
  319. #define ATTR_PRODNAME 0x100
  320. #define ATTR_PRODVER 0x200
  321. #define ATTR_EXETYPE 0x400
  322. #define ATTR_DESCR16 0x800
  323. #define ATTR_HLPTITLE 0x1000
  324. DWORD g_ListedAttr = 0xFFFFFFFF;
  325. typedef struct _VERSION_DATA {
  326. PCSTR versionValue;
  327. PCSTR versionName;
  328. DWORD attrib;
  329. } VERSION_DATA, *PVERSION_DATA;
  330. VERSION_DATA verData [] = {{NULL, "COMPANYNAME", ATTR_COMPNAME},
  331. {NULL, "PRODUCTVERSION", ATTR_PRODVER},
  332. {NULL, "PRODUCTNAME", ATTR_PRODNAME},
  333. {NULL, "FILEDESCRIPTION", ATTR_FILEDESC},
  334. {NULL, "FILEVERSION", ATTR_FILEVER},
  335. {NULL, "ORIGINALFILENAME", ATTR_ORIGNAME},
  336. {NULL, "INTERNALNAME", ATTR_INTNAME},
  337. {NULL, "LEGALCOPYRIGHT", ATTR_LEGAL},
  338. {NULL, NULL, 0}};
  339. extern PSTR g_ExeTypes[4];
  340. BOOL
  341. GetFileAttributesLine (
  342. IN PCTSTR FileName,
  343. OUT PTSTR Buffer,
  344. IN DWORD BufferSize
  345. )
  346. {
  347. FILE_HELPER_PARAMS Params;
  348. PCTSTR extPtr;
  349. TCHAR result[MAX_TCHAR_PATH];
  350. PTSTR resultPtr;
  351. UINT checkSum;
  352. DWORD exeType;
  353. PCSTR fileDesc16;
  354. PCSTR hlpTitle;
  355. DWORD listedAttr = 0;
  356. PVERSION_DATA p;
  357. WIN32_FIND_DATA FindData;
  358. BOOL goOn = FALSE;
  359. while (!goOn) {
  360. if (!DoesFileExistEx (FileName, &FindData)) {
  361. sprintf (result, "Target file does not exist: %s", FileName);
  362. if (MessageBox (NULL, result, "Error", MB_RETRYCANCEL) != IDRETRY) {
  363. *Buffer = 0;
  364. return TRUE;
  365. }
  366. goOn = FALSE;
  367. } else {
  368. goOn = TRUE;
  369. }
  370. }
  371. Params.FindData = &FindData;
  372. Params.Handled = 0;
  373. Params.FullFileSpec = FileName;
  374. extPtr = GetFileNameFromPath (FileName);
  375. MYASSERT (extPtr);
  376. StringCopyAB (Params.DirSpec, FileName, extPtr);
  377. Params.IsDirectory = FALSE;
  378. Params.Extension = GetFileExtensionFromPath (FileName);
  379. Params.VirtualFile = FALSE;
  380. Params.CurrentDirData = NULL;
  381. _stprintf (
  382. result,
  383. TEXT("%-14s"),
  384. extPtr);
  385. resultPtr = GetEndOfString (result);
  386. listedAttr = 0;
  387. p = verData;
  388. while (p->versionName) {
  389. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & p->attrib))) {
  390. p->versionValue = QueryVersionEntry (FileName, p->versionName);
  391. if (p->versionValue) {
  392. listedAttr ++;
  393. }
  394. }
  395. else {
  396. p->versionValue = NULL;
  397. }
  398. p++;
  399. }
  400. p = verData;
  401. while (p->versionName) {
  402. if (p->versionValue) {
  403. _stprintf (
  404. resultPtr,
  405. TEXT(", %s(\"%s\")"),
  406. p->versionName,
  407. p->versionValue);
  408. resultPtr = GetEndOfString (resultPtr);
  409. }
  410. p++;
  411. }
  412. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & ATTR_DESCR16))) {
  413. fileDesc16 = Get16ModuleDescription (FileName);
  414. if (fileDesc16 != NULL) {
  415. listedAttr ++;
  416. _stprintf (
  417. resultPtr,
  418. TEXT(", DESCRIPTION(\"%s\")"),
  419. fileDesc16);
  420. resultPtr = GetEndOfString (resultPtr);
  421. FreePathString (fileDesc16);
  422. }
  423. }
  424. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & ATTR_EXETYPE))) {
  425. exeType = GetModuleType (FileName);
  426. if (exeType != UNKNOWN_MODULE) {
  427. listedAttr ++;
  428. _stprintf (
  429. resultPtr,
  430. TEXT(", EXETYPE(\"%s\")"),
  431. g_ExeTypes[exeType]);
  432. resultPtr = GetEndOfString (resultPtr);
  433. }
  434. }
  435. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & ATTR_HLPTITLE))) {
  436. hlpTitle = GetHlpFileTitle (FileName);
  437. if (hlpTitle != NULL) {
  438. listedAttr ++;
  439. _stprintf (
  440. resultPtr,
  441. TEXT(", HLPTITLE(\"%s\")"),
  442. hlpTitle);
  443. resultPtr = GetEndOfString (resultPtr);
  444. FreePathString (hlpTitle);
  445. }
  446. }
  447. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & ATTR_FILESIZE))) {
  448. listedAttr ++;
  449. _stprintf (
  450. resultPtr,
  451. TEXT(", FILESIZE(0x%08lX)"),
  452. Params.FindData->nFileSizeLow);
  453. resultPtr = GetEndOfString (resultPtr);
  454. }
  455. if (((g_ListedAttr == 0xFFFFFFFF) && (listedAttr < 2)) || ((g_ListedAttr != 0xFFFFFFFF) && (g_ListedAttr & ATTR_CHECKSUM))) {
  456. listedAttr ++;
  457. checkSum = ComputeCheckSum (&Params);
  458. _stprintf (
  459. resultPtr,
  460. TEXT(", CHECKSUM(0x%08lX)"),
  461. checkSum);
  462. resultPtr = GetEndOfString (resultPtr);
  463. }
  464. _tcssafecpy (Buffer, result, BufferSize / sizeof (TCHAR));
  465. return TRUE;
  466. }