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.

3012 lines
100 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. oemmint.cpp
  5. Abstract:
  6. Simple tool to create a Mini NT image
  7. from a regular NT image
  8. Author:
  9. Vijay Jayaseelan (vijayj) Apr-23-2000
  10. Revision History:
  11. Aug-08-2000 - Major rewrite using setupapi wrapper
  12. class library.
  13. Nov-10-2000 - Made the utility work with actual
  14. distribution CD.
  15. Jan-23-2001 - Add support for version checking.
  16. Feb-09-2002 - Add support for multiple driver cab
  17. file extraction.
  18. Apr-15-2002 - Modify tool to work on both layouts (placement)
  19. of SxS assemblies on release share. It used to be in
  20. ASMS folder but now is in asms*.cab file.
  21. NOTE: This tool needs to be updated on changes to
  22. entries to the disk ordinals for WOW64 files.!!!!
  23. Change needs to go in IsWow64File(..)
  24. --*/
  25. #include <oemmint.h>
  26. #include <iostream>
  27. #include <io.h>
  28. #include <sxsapi.h>
  29. #include "msg.h"
  30. #include <libmsg.h>
  31. using namespace std;
  32. //
  33. // static constant data members
  34. //
  35. const std::basic_string<TCHAR> DriverIndexInfFile<TCHAR>::VersionSectionName = TEXT("version");
  36. const std::basic_string<TCHAR> DriverIndexInfFile<TCHAR>::CabsSectionName = TEXT("cabs");
  37. const std::basic_string<TCHAR> DriverIndexInfFile<TCHAR>::CabsSearchOrderKeyName = TEXT("cabfiles");
  38. //
  39. // Constants
  40. //
  41. const std::wstring REGIONAL_SECTION_NAME = TEXT("regionalsettings");
  42. const std::wstring LANGUAGE_GROUP_KEY = TEXT("languagegroup");
  43. const std::wstring LANGUAGE_KEY = TEXT("language");
  44. const std::wstring LANGGROUP_SECTION_PREFIX = TEXT("lg_install_");
  45. const std::wstring DEFAULT_LANGGROUP_NAME = TEXT("lg_install_1");
  46. const std::wstring LOCALES_SECTION_NAME = TEXT("locales");
  47. const std::wstring FONT_CP_REGSECTION_FMT_STR = TEXT("font.cp%s.%d");
  48. const std::wstring X86_PLATFORM_DIR = TEXT("i386");
  49. const std::wstring IA64_PLATFORM_DIR = TEXT("ia64");
  50. const std::wstring INFCHANGES_SECTION_NAME = TEXT("infchanges");
  51. const DWORD LANG_GROUP1_INDEX = 2;
  52. const DWORD OEM_CP_INDEX = 1;
  53. const DWORD DEFAULT_FONT_SIZE = 96;
  54. //
  55. // Global variables used to get formatted message for this program.
  56. //
  57. HMODULE ThisModule = NULL;
  58. WCHAR Message[4096];
  59. //
  60. // Main entry point
  61. //
  62. int
  63. __cdecl
  64. wmain(int Argc, wchar_t* Argv[])
  65. {
  66. int Result = 0;
  67. ThisModule = GetModuleHandle(NULL);
  68. try{
  69. //
  70. // parse the arguments
  71. //
  72. UnicodeArgs Args(Argc, Argv);
  73. //
  74. // Check to see if we are using this utility to check the version
  75. //
  76. if (!Args.CheckVersion) {
  77. if (Args.Verbose) {
  78. cout << GetFormattedMessage(ThisModule,
  79. FALSE,
  80. Message,
  81. sizeof(Message)/sizeof(Message[0]),
  82. MSG_CREATING_WINPE_FILE_LIST) << endl;
  83. }
  84. //
  85. // open the config.inf file
  86. //
  87. if (Args.Verbose) {
  88. cout << GetFormattedMessage(ThisModule,
  89. FALSE,
  90. Message,
  91. sizeof(Message)/sizeof(Message[0]),
  92. MSG_PARSING_FILE,
  93. Args.ConfigInfFileName.c_str()) << endl;
  94. }
  95. InfFileW ConfigInfFile(Args.ConfigInfFileName);
  96. //
  97. // open the layout.inf file
  98. //
  99. if (Args.Verbose) {
  100. cout << GetFormattedMessage(ThisModule,
  101. FALSE,
  102. Message,
  103. sizeof(Message)/sizeof(Message[0]),
  104. MSG_PARSING_FILE,
  105. Args.LayoutName.c_str()) << endl;
  106. }
  107. InfFileW InputFile(Args.LayoutName);
  108. //
  109. // open the drvindex.inf file
  110. //
  111. if (Args.Verbose) {
  112. cout << GetFormattedMessage(ThisModule,
  113. FALSE,
  114. Message,
  115. sizeof(Message)/sizeof(Message[0]),
  116. MSG_PARSING_FILE,
  117. Args.DriverIndexName.c_str()) << endl;
  118. }
  119. DriverIndexInfFile<WCHAR> DriverIdxFile(Args.DriverIndexName);
  120. //
  121. // open the intl.inf file
  122. //
  123. if (Args.Verbose) {
  124. cout << GetFormattedMessage(ThisModule,
  125. FALSE,
  126. Message,
  127. sizeof(Message)/sizeof(Message[0]),
  128. MSG_PARSING_FILE,
  129. Args.IntlInfFileName.c_str()) << endl;
  130. }
  131. InfFileW IntlInfFile(Args.IntlInfFileName);
  132. //
  133. // open the font.inf file
  134. //
  135. if (Args.Verbose) {
  136. cout << GetFormattedMessage(ThisModule,
  137. FALSE,
  138. Message,
  139. sizeof(Message)/sizeof(Message[0]),
  140. MSG_PARSING_FILE,
  141. Args.FontInfFileName.c_str()) << endl;
  142. }
  143. InfFileW FontInfFile(Args.FontInfFileName);
  144. map<std::basic_string<wchar_t>, Section<wchar_t>* > Sections;
  145. //
  146. // get hold of the sections in the layout file
  147. //
  148. InputFile.GetSections(Sections);
  149. //
  150. // get hold of "[SourceDisksFiles] section
  151. //
  152. map<basic_string<wchar_t>, Section<wchar_t> * >::iterator iter = Sections.find(L"sourcedisksfiles");
  153. Section<wchar_t> *SDSection = 0;
  154. Section<wchar_t> *DirSection = 0;
  155. Section<wchar_t> *PlatSection = 0;
  156. if (iter != Sections.end()) {
  157. SDSection = (*iter).second;
  158. }
  159. //
  160. // get hold of the [WinntDirectories] section
  161. //
  162. iter = Sections.find(L"winntdirectories");
  163. if (iter != Sections.end()) {
  164. DirSection = (*iter).second;
  165. }
  166. //
  167. // get hold of the platform specific source files section
  168. //
  169. basic_string<wchar_t> PlatformSection = SDSection->GetName() + L"." + Args.PlatformSuffix;
  170. iter = Sections.find(PlatformSection);
  171. if (iter != Sections.end()) {
  172. PlatSection = (*iter).second;
  173. }
  174. //
  175. // Merge the platform and common source files section
  176. //
  177. if (PlatSection) {
  178. if (Args.Verbose) {
  179. cout << GetFormattedMessage( ThisModule,
  180. FALSE,
  181. Message,
  182. sizeof(Message)/sizeof(Message[0]),
  183. MSG_MERGING_PLATFORM_AND_COMMON_SRC_FILES,
  184. PlatSection->GetName().c_str(),
  185. SDSection->GetName().c_str()) << endl;
  186. }
  187. *SDSection += *PlatSection;
  188. }
  189. //
  190. // Iterate through each file in the common merged section
  191. // creating a file list of minint image
  192. //
  193. FileListCreatorContext<wchar_t> fl(Args, SDSection,
  194. DirSection, ConfigInfFile,
  195. IntlInfFile, FontInfFile,
  196. DriverIdxFile);
  197. //
  198. // Create the list of files to be copied
  199. //
  200. SDSection->DoForEach(FileListCreator, &fl);
  201. //
  202. // Process Nls files
  203. //
  204. ULONG NlsFileCount = fl.ProcessNlsFiles();
  205. if (Args.Verbose) {
  206. std::cout << GetFormattedMessage( ThisModule,
  207. FALSE,
  208. Message,
  209. sizeof(Message)/sizeof(Message[0]),
  210. MSG_DUMP_PROCESSED_NLS_FILE_COUNT,
  211. NlsFileCount) << std::endl;
  212. }
  213. //
  214. // Process WinSxS files
  215. //
  216. ULONG SxSFileCount = ProcessWinSxSFiles(fl);
  217. if (Args.Verbose) {
  218. std::cout << GetFormattedMessage( ThisModule,
  219. FALSE,
  220. Message,
  221. sizeof(Message)/sizeof(Message[0]),
  222. MSG_DUMP_PROCESSED_SXS_FILE_COUNT,
  223. SxSFileCount) << std::endl;
  224. }
  225. //
  226. // If there are extra files specified then process them and
  227. // add to the file list for minint image
  228. //
  229. if (Args.ExtraFileName.length() > 0) {
  230. ULONG ExtraFiles = ProcessExtraFiles(fl);
  231. if (Args.Verbose) {
  232. cout << GetFormattedMessage(ThisModule,
  233. FALSE,
  234. Message,
  235. sizeof(Message)/sizeof(Message[0]),
  236. MSG_DUMP_PROCESSED_EXTRA_FILES,
  237. ExtraFiles,
  238. fl.Args.ExtraFileName.c_str()) << endl;
  239. }
  240. }
  241. //
  242. // Create all the required destination directories
  243. //
  244. ULONG DirsCreated = PreCreateDirs(fl);
  245. //
  246. // Ok, now copy the list of files
  247. //
  248. ULONG FilesToCopy = fl.GetSourceCount();
  249. if (FilesToCopy) {
  250. ULONG Count = CopyFileList(fl);
  251. Result = FilesToCopy - Count;
  252. if (Result || Args.Verbose) {
  253. cout << GetFormattedMessage(ThisModule,
  254. FALSE,
  255. Message,
  256. sizeof(Message)/sizeof(Message[0]),
  257. MSG_NUMBER_OF_FILES_COPIED,
  258. Count,
  259. FilesToCopy ) << endl;
  260. }
  261. }
  262. //
  263. // Now process the required inf changes
  264. //
  265. wstring ControlInf = Args.CurrentDirectory + L"config.inf";
  266. if (!IsFilePresent(ControlInf)) {
  267. throw new W32Exception<wchar_t>(ERROR_FILE_NOT_FOUND);
  268. }
  269. ProcessInfChanges(Args, ControlInf);
  270. } else {
  271. //
  272. // Check the version of the current OS and the install media
  273. // to make sure that they match
  274. //
  275. Result = CheckMediaVersion(Args) ? 0 : 1;
  276. }
  277. }
  278. catch (InvalidArguments *InvArgs) {
  279. cerr << GetFormattedMessage(ThisModule,
  280. FALSE,
  281. Message,
  282. sizeof(Message)/sizeof(Message[0]),
  283. MSG_PGM_USAGE) << endl;
  284. delete InvArgs;
  285. Result = 1;
  286. }
  287. catch (BaseException<wchar_t> *Exp) {
  288. Exp->Dump(std::cout);
  289. delete Exp;
  290. Result = 1;
  291. }
  292. catch (...) {
  293. Result = 1;
  294. }
  295. return Result;
  296. }
  297. //
  298. // Processes all the inf files and adds them to the copy list
  299. // to copy to the destination\inf directory
  300. // NOTE : This routine only processes net*.inf file automatically.
  301. // Other inf needs to be marked specifically
  302. //
  303. template <class T>
  304. BOOLEAN
  305. InfFileListCreator(
  306. SectionValues<T> &Values,
  307. FileListCreatorContext<T> &Context
  308. )
  309. {
  310. //
  311. // Note : All the inf files in layout always end with ".inf"
  312. // lowercase characters
  313. //
  314. basic_string<T> Key = Values.GetName();
  315. basic_string<T>::size_type InfIdx = Key.find(L".inf");
  316. BOOLEAN Result = FALSE;
  317. static bool DirAdded = false;
  318. basic_string<T>::size_type NetIdx = Key.find(L"net");
  319. if (!Context.SkipInfFiles && (InfIdx != basic_string<T>::npos) && (NetIdx == 0)) {
  320. Result = TRUE;
  321. if (sizeof(T) == sizeof(CHAR)) {
  322. _strlwr((PSTR)Key.c_str());
  323. } else {
  324. _wcslwr((PWSTR)Key.c_str());
  325. }
  326. basic_string<T> SrcFile = Context.Args.SourceDirectory + Key;
  327. basic_string<T> DestFile;
  328. bool DestDirPresent = false;
  329. if (Values.Count() > 12) {
  330. basic_string<T> DestDir = Values.GetValue(12);
  331. //
  332. // remove trailing white spaces
  333. //
  334. unsigned int DestDirLength = DestDir.length();
  335. while (DestDirLength) {
  336. if (DestDir[DestDirLength] != L' ') {
  337. break;
  338. }
  339. DestDir[DestDirLength] = 0;
  340. DestDirLength--;
  341. }
  342. //
  343. // if the destination directory ID is 0 then skip
  344. // the file
  345. //
  346. if (DestDir == L"0") {
  347. return TRUE;
  348. }
  349. if (DestDir.length()) {
  350. basic_string<T> DestDirCode = DestDir;
  351. DestDir = Context.DirsSection->GetValue(DestDir).GetValue(0);
  352. if (DestDir.length()) {
  353. if (DestDir[DestDir.length() - 1] != L'\\') {
  354. DestDir += L"\\";
  355. }
  356. DestDir = Context.Args.DestinationDirectory + DestDir;
  357. //
  358. // Cache the directory, if not already done
  359. //
  360. if (Context.DestDirs.find(DestDirCode) ==
  361. Context.DestDirs.end()) {
  362. Context.DestDirs[DestDirCode] = DestDir;
  363. }
  364. DestDirPresent = true;
  365. DestFile = DestDir;
  366. }
  367. }
  368. }
  369. if (!DestDirPresent) {
  370. DestFile = Context.Args.DestinationDirectory + L"Inf\\";
  371. if (!DirAdded) {
  372. //
  373. // Inf directory's code is 20
  374. //
  375. basic_string<T> DestDirCode(L"20");
  376. //
  377. // Cache the directory, if not already done
  378. //
  379. if (Context.DestDirs.find(DestDirCode) ==
  380. Context.DestDirs.end()) {
  381. Context.DestDirs[DestDirCode] = DestFile;
  382. }
  383. DirAdded = true;
  384. }
  385. }
  386. if (Values.Count() > 10) {
  387. const basic_string<T> &DestName = Values.GetValue(10);
  388. if (DestName.length()) {
  389. DestFile += DestName;
  390. } else {
  391. DestFile += Key;
  392. }
  393. } else {
  394. DestFile += Key;
  395. }
  396. bool AlternateFound = false;
  397. if (Context.Args.OptionalSrcDirectory.length()) {
  398. basic_string<T> OptionalSrcFile = Context.Args.OptionalSrcDirectory + Key;
  399. if (IsFilePresent(OptionalSrcFile)) {
  400. SrcFile = OptionalSrcFile;
  401. AlternateFound = true;
  402. }
  403. }
  404. const basic_string<T> &DriverCabFileName = Context.GetDriverCabFileName(Key);
  405. if (!AlternateFound && DriverCabFileName.length()) {
  406. SrcFile = Key;
  407. }
  408. if (DriverCabFileName.length()) {
  409. Context.AddFileToCabFileList(DriverCabFileName, SrcFile, DestFile);
  410. } else if (Context.ProcessingExtraFiles) {
  411. Context.ExtraFileList[SrcFile] = DestFile;
  412. } else {
  413. Context.FileList[SrcFile] = DestFile;
  414. }
  415. }
  416. return Result;
  417. }
  418. //
  419. // Parses the value to determine, if this file needs
  420. // to be in minint and adds the file to the file list
  421. // if this file is needed
  422. //
  423. template <class T>
  424. void
  425. FileListCreator(SectionValues<T> &Values, void *Context) {
  426. FileListCreatorContext<T> *FlContext = (FileListCreatorContext<T> *)(Context);
  427. unsigned int Count = Values.Count() ;
  428. bool Compressed = false;
  429. if (FlContext && !IsFileSkipped(Values, *FlContext) &&
  430. !InfFileListCreator(Values, *FlContext) && (Count > 12)) {
  431. basic_string<T> SrcDir = Values.GetValue(11);
  432. basic_string<T> DestDir = Values.GetValue(12);
  433. basic_string<T> Key = Values.GetName();
  434. if (sizeof(T) == sizeof(CHAR)) {
  435. _strlwr((PSTR)Key.c_str());
  436. } else {
  437. _wcslwr((PWSTR)Key.c_str());
  438. }
  439. //
  440. // remove trailing white spaces
  441. //
  442. unsigned int DestDirLength = DestDir.length();
  443. while (DestDirLength) {
  444. if (DestDir[DestDirLength] != L' ') {
  445. break;
  446. }
  447. DestDir[DestDirLength] = 0;
  448. DestDirLength--;
  449. }
  450. //
  451. // if the destination directory ID is 0 then skip
  452. // the file
  453. //
  454. if (DestDir == L"0") {
  455. return;
  456. }
  457. basic_string<T> SrcSubDir = FlContext->DirsSection->GetValue(SrcDir).GetValue(0);
  458. basic_string<T> DestSubDir = FlContext->DirsSection->GetValue(DestDir).GetValue(0);
  459. basic_string<T> DestDirCode = DestDir;
  460. //
  461. // Fix up diretory names
  462. //
  463. if (SrcSubDir.length() && (SrcSubDir[SrcSubDir.length() - 1] != L'\\')) {
  464. SrcSubDir += L"\\";
  465. }
  466. if (DestSubDir.length() && (DestSubDir[DestSubDir.length() - 1] != L'\\')) {
  467. DestSubDir += L"\\";
  468. }
  469. basic_string<T> OptSrcDir = FlContext->Args.OptionalSrcDirectory;
  470. SrcDir = FlContext->Args.SourceDirectory;
  471. if (SrcSubDir != L"\\") {
  472. SrcDir += SrcSubDir;
  473. if (OptSrcDir.length()) {
  474. OptSrcDir += SrcSubDir;
  475. }
  476. }
  477. DestDir = FlContext->Args.DestinationDirectory;
  478. if (DestSubDir != L"\\") {
  479. DestDir += DestSubDir;
  480. }
  481. //
  482. // Cache the directory, if not already done
  483. //
  484. if (FlContext->DestDirs.find(DestDirCode) ==
  485. FlContext->DestDirs.end()) {
  486. FlContext->DestDirs[DestDirCode] = DestDir;
  487. }
  488. basic_string<T> SrcFile, DestFile;
  489. bool AltSrcDir = false;
  490. if (OptSrcDir.length()) {
  491. SrcFile = OptSrcDir + Key;
  492. AltSrcDir = IsFilePresent(SrcFile);
  493. }
  494. const basic_string<T> &DriverCabFileName = FlContext->GetDriverCabFileName(Key);
  495. bool DriverCabFile = false;
  496. if (!AltSrcDir) {
  497. SrcFile = SrcDir + Key;
  498. basic_string<T> CompressedSrcName = SrcFile;
  499. CompressedSrcName[CompressedSrcName.length() - 1] = TEXT('_');
  500. if (!IsFilePresent(SrcFile) && !IsFilePresent(CompressedSrcName)) {
  501. if (DriverCabFileName.length()) {
  502. SrcFile = Key;
  503. DriverCabFile = true;
  504. }
  505. }
  506. }
  507. DestFile = Values.GetValue(10);
  508. if (!DestFile.length()) {
  509. DestFile = Key;
  510. }
  511. DestFile = DestDir + DestFile;
  512. if (DriverCabFile) {
  513. FlContext->AddFileToCabFileList(DriverCabFileName, SrcFile, DestFile);
  514. } else if (FlContext->ProcessingExtraFiles) {
  515. FlContext->ExtraFileList[SrcFile] = DestFile;
  516. } else {
  517. FlContext->FileList[SrcFile] = DestFile;
  518. }
  519. }
  520. }
  521. //
  522. // CAB file callback routine, which does the actual
  523. // check of whether to extract the file or skip the
  524. // file
  525. //
  526. template <class T>
  527. UINT
  528. CabinetCallback(
  529. PVOID Context,
  530. UINT Notification,
  531. UINT_PTR Param1,
  532. UINT_PTR Param2
  533. )
  534. {
  535. UINT ReturnCode = NO_ERROR;
  536. FileListCreatorContext<T> *FlContext = (FileListCreatorContext<T> *)Context;
  537. PFILE_IN_CABINET_INFO FileInfo = NULL;
  538. PFILEPATHS FilePaths = NULL;
  539. basic_string<T> &FileName = FlContext->CurrentFileName;
  540. map<basic_string<T>, basic_string<T> >::iterator Iter;
  541. map<basic_string<T>, basic_string<T> >::iterator FlIter;
  542. switch (Notification) {
  543. case SPFILENOTIFY_FILEINCABINET:
  544. {
  545. ReturnCode = FILEOP_SKIP;
  546. FileInfo = (PFILE_IN_CABINET_INFO)Param1;
  547. if (sizeof(T) == sizeof(CHAR)) {
  548. FileName = (const T *)(FileInfo->NameInCabinet);
  549. _strlwr((PSTR)(FileName.c_str()));
  550. } else {
  551. FileName = (const T *)(FileInfo->NameInCabinet);
  552. _wcslwr((PWSTR)(FileName.c_str()));
  553. }
  554. Iter = FlContext->CabFileListMap[FlContext->CurrentCabFileIdx]->find(FileName);
  555. if (Iter != FlContext->CabFileListMap[FlContext->CurrentCabFileIdx]->end()) {
  556. if (!FlContext->Args.SkipFileCopy) {
  557. if (sizeof(T) == sizeof(CHAR)) {
  558. (VOID)StringCchCopyA((PSTR)(FileInfo->FullTargetName),
  559. ARRAY_SIZE(FileInfo->FullTargetName),
  560. (PCSTR)((*Iter).second).c_str());
  561. } else {
  562. (VOID)StringCchCopyW((PWSTR)(FileInfo->FullTargetName),
  563. ARRAY_SIZE(FileInfo->FullTargetName),
  564. (PCWSTR)((*Iter).second).c_str());
  565. }
  566. ReturnCode = FILEOP_DOIT;
  567. } else {
  568. ReturnCode = FILEOP_SKIP;
  569. FlContext->FileCount++;
  570. if (FlContext->Args.Verbose) {
  571. std::cout << GetFormattedMessage(ThisModule,
  572. FALSE,
  573. Message,
  574. sizeof(Message)/sizeof(Message[0]),
  575. MSG_EXTRACT_FILES_FROM_CAB_NOTIFICATION,
  576. FlContext->CurrentCabFileIdx.c_str(),
  577. FileName.c_str(),
  578. (*Iter).second.c_str()) << std::endl;
  579. }
  580. }
  581. }
  582. }
  583. break;
  584. case SPFILENOTIFY_FILEEXTRACTED:
  585. FilePaths = (PFILEPATHS)Param1;
  586. if (FilePaths->Win32Error) {
  587. std::cout << GetFormattedMessage(ThisModule,
  588. FALSE,
  589. Message,
  590. sizeof(Message)/sizeof(Message[0]),
  591. MSG_ERROR_EXTRACTING_FILES,
  592. FilePaths->Win32Error,
  593. FilePaths->Source,
  594. FileName.c_str(),
  595. FilePaths->Target) << std::endl;
  596. } else {
  597. FlContext->FileCount++;
  598. if (FlContext->Args.Verbose) {
  599. std::cout << GetFormattedMessage(ThisModule,
  600. FALSE,
  601. Message,
  602. sizeof(Message)/sizeof(Message[0]),
  603. MSG_EXTRACTED_FILES_FROM_CAB_NOTIFICATION,
  604. FilePaths->Source,
  605. FileName.c_str(),
  606. FilePaths->Target) << std::endl;
  607. }
  608. }
  609. break;
  610. default:
  611. break;
  612. }
  613. return ReturnCode;
  614. }
  615. //
  616. // Copies all the required files in given CAB file to the specified
  617. // destination directory
  618. //
  619. template <class T>
  620. ULONG
  621. CopyCabFileList(
  622. FileListCreatorContext<T> &Context,
  623. const std::basic_string<T> &CabFileName
  624. )
  625. {
  626. ULONG Count = Context.FileCount;
  627. if (Context.CabFileListMap.size()) {
  628. BOOL Result = FALSE;
  629. if (sizeof(T) == sizeof(CHAR)) {
  630. Result = SetupIterateCabinetA((PCSTR)CabFileName.c_str(),
  631. NULL,
  632. (PSP_FILE_CALLBACK_A)CabinetCallback<char>,
  633. &Context);
  634. } else {
  635. Result = SetupIterateCabinetW((PCWSTR)CabFileName.c_str(),
  636. NULL,
  637. (PSP_FILE_CALLBACK_W)CabinetCallback<wchar_t>,
  638. &Context);
  639. }
  640. if (!Result) {
  641. cout << GetFormattedMessage(ThisModule,
  642. FALSE,
  643. Message,
  644. sizeof(Message)/sizeof(Message[0]),
  645. MSG_ERROR_ITERATING_CAB_FILE,
  646. GetLastError(),
  647. CabFileName.c_str()) << endl;
  648. }
  649. }
  650. return Context.FileCount - Count;
  651. }
  652. template <class T>
  653. ULONG
  654. CopySingleFileList(
  655. FileListCreatorContext<T> &Context,
  656. map<basic_string<T>, basic_string<T> > &FileList
  657. )
  658. {
  659. ULONG Count = 0;
  660. map<basic_string<T>, basic_string<T> >::iterator Iter = FileList.begin();
  661. while (Iter != FileList.end()) {
  662. DWORD ErrorCode = 0;
  663. if (!Context.Args.SkipFileCopy) {
  664. if (sizeof(T) == sizeof(CHAR)) {
  665. ErrorCode = SetupDecompressOrCopyFileA(
  666. (PCSTR)((*Iter).first.c_str()),
  667. (PCSTR)((*Iter).second.c_str()),
  668. NULL);
  669. } else {
  670. ErrorCode = SetupDecompressOrCopyFileW(
  671. (PCWSTR)((*Iter).first.c_str()),
  672. (PCWSTR)((*Iter).second.c_str()),
  673. NULL);
  674. }
  675. }
  676. if (!ErrorCode) {
  677. Count++;
  678. if (sizeof(T) == sizeof(CHAR)) {
  679. ErrorCode = SetFileAttributesA((LPCSTR)((*Iter).second.c_str()),
  680. FILE_ATTRIBUTE_NORMAL);
  681. } else {
  682. ErrorCode = SetFileAttributesW((LPCWSTR)((*Iter).second.c_str()),
  683. FILE_ATTRIBUTE_NORMAL);
  684. }
  685. if (Context.Args.SkipFileCopy) {
  686. std::cout << GetFormattedMessage(ThisModule,
  687. FALSE,
  688. Message,
  689. sizeof(Message)/sizeof(Message[0]),
  690. MSG_WILL_COPY) ;
  691. }
  692. if (Context.Args.Verbose) {
  693. std::cout << GetFormattedMessage(ThisModule,
  694. FALSE,
  695. Message,
  696. sizeof(Message)/sizeof(Message[0]),
  697. MSG_FILE_NAME,
  698. (*Iter).first.c_str(),
  699. (*Iter).second.c_str()) << std::endl;
  700. }
  701. } else {
  702. std::cout << GetFormattedMessage(ThisModule,
  703. FALSE,
  704. Message,
  705. sizeof(Message)/sizeof(Message[0]),
  706. MSG_ERROR_COPYING_FILES,
  707. ErrorCode,
  708. (*Iter).first.c_str(),
  709. (*Iter).second.c_str()) << std::endl;
  710. }
  711. Iter++;
  712. }
  713. return Count;
  714. }
  715. //
  716. // Iterates through a file list and copies the files
  717. // from the specified source directory to the destination
  718. // directory
  719. //
  720. template <class T>
  721. ULONG
  722. CopyFileList(
  723. FileListCreatorContext<T> &Context
  724. )
  725. {
  726. ULONG Count = 0;
  727. std::map<std::basic_string<T>,
  728. std::map<std::basic_string<T>, std::basic_string<T> > * >::iterator Iter;
  729. for (Iter = Context.CabFileListMap.begin();
  730. Iter != Context.CabFileListMap.end();
  731. Iter++) {
  732. basic_string<T> FullCabFileName = Context.Args.SourceDirectory + (*Iter).first;
  733. Context.CurrentCabFileIdx = (*Iter).first;
  734. Count += CopyCabFileList(Context, FullCabFileName);
  735. }
  736. Count += CopySingleFileList(Context, Context.FileList);
  737. Context.FileCount += Count;
  738. Count += CopySingleFileList(Context, Context.NlsFileMap);
  739. Context.FileCount += Count;
  740. Count += CopySingleFileList(Context, Context.WinSxSFileList);
  741. Context.FileCount += Count;
  742. Count += CopySingleFileList(Context, Context.ExtraFileList);
  743. Context.FileCount += Count;
  744. return Count;
  745. }
  746. //
  747. // Processes the extra files from the specified file name
  748. // other than those present in the layout.inf file.
  749. // Adds the files to the file list for MiniNT image
  750. //
  751. template <class T>
  752. ULONG
  753. ProcessExtraFiles(FileListCreatorContext<T> &Context) {
  754. ULONG Count = 0;
  755. InfFile<T> ExtraFile(Context.Args.ExtraFileName);
  756. basic_string<T> ExtraSecName = TEXT("extrafiles");
  757. basic_string<T> PlatExtraSecName = ExtraSecName + TEXT(".") + Context.Args.PlatformSuffix;
  758. Section<T> *ExtraFilesSec = ExtraFile.GetSection(ExtraSecName.c_str());
  759. Section<T> *PlatExtraFilesSec = ExtraFile.GetSection(PlatExtraSecName.c_str());
  760. if (ExtraFilesSec) {
  761. Context.ProcessingExtraFiles = true;
  762. ExtraFilesSec->DoForEach(FileListCreator, &Context);
  763. Context.ProcessingExtraFiles = false;
  764. Count += Context.ExtraFileList.size();
  765. }
  766. if (PlatExtraFilesSec) {
  767. Context.ProcessingExtraFiles = true;
  768. PlatExtraFilesSec->DoForEach(FileListCreator, &Context);
  769. Context.ProcessingExtraFiles = false;
  770. Count += (Context.ExtraFileList.size() - Count);
  771. }
  772. return Count;
  773. }
  774. //
  775. // Goes through the list of desination directories and precreates
  776. // them
  777. //
  778. template <class T>
  779. ULONG
  780. PreCreateDirs(
  781. FileListCreatorContext<T> &Context
  782. )
  783. {
  784. ULONG Count = 0;
  785. std::map< std::basic_string<T>, std::basic_string<T> >::iterator
  786. Iter = Context.DestDirs.begin();
  787. while (Iter != Context.DestDirs.end()) {
  788. if (CreateDirectories((*Iter).second, NULL)) {
  789. if (Context.Args.Verbose) {
  790. std::cout << GetFormattedMessage(ThisModule,
  791. FALSE,
  792. Message,
  793. sizeof(Message)/sizeof(Message[0]),
  794. MSG_CREATING_DIRECTORIES,
  795. (*Iter).second.c_str()) << std::endl;
  796. }
  797. Count++;
  798. }
  799. Iter++;
  800. }
  801. return Count;
  802. }
  803. //
  804. // Creates the directory (including subdirectories)
  805. //
  806. template <class T>
  807. bool
  808. CreateDirectories(const basic_string<T> &DirName,
  809. LPSECURITY_ATTRIBUTES SecurityAttrs) {
  810. bool Result = false;
  811. std::vector<std::basic_string<T> > Dirs;
  812. std::basic_string<T> Delimiters((T *)TEXT("\\"));
  813. std::basic_string<T> NextDir;
  814. if (Tokenize(DirName, Delimiters, Dirs)) {
  815. std::vector<std::basic_string<T> >::iterator Iter = Dirs.begin();
  816. while (Iter != Dirs.end()) {
  817. NextDir += (*Iter);
  818. if (sizeof(T) == sizeof(CHAR)) {
  819. if (_access((PCSTR)NextDir.c_str(), 0)) {
  820. Result = (CreateDirectoryA((PCSTR)NextDir.c_str(),
  821. SecurityAttrs) == TRUE);
  822. }
  823. } else {
  824. if (_waccess((PCWSTR)NextDir.c_str(), 0)) {
  825. Result = (CreateDirectoryW((PCWSTR)NextDir.c_str(),
  826. SecurityAttrs) == TRUE);
  827. }
  828. }
  829. Iter++;
  830. NextDir += (T *)TEXT("\\");
  831. }
  832. }
  833. return Result;
  834. }
  835. //
  836. // Determines if the given file (or directory) is present
  837. //
  838. template <class T>
  839. bool
  840. IsFilePresent(const basic_string<T> &FileName) {
  841. bool Result = false;
  842. if (sizeof(T) == sizeof(CHAR)) {
  843. Result = (::_access((PCSTR)FileName.c_str(), 0) == 0);
  844. } else {
  845. Result = (::_waccess((PCWSTR)FileName.c_str(), 0) == 0);
  846. }
  847. return Result;
  848. }
  849. //
  850. // Determines if the file is Wow64 file (only valid in IA64) case
  851. //
  852. template <class T>
  853. bool
  854. IsWow64File(
  855. SectionValues<T> &Values,
  856. FileListCreatorContext<T> &Context
  857. )
  858. {
  859. bool Result = false;
  860. if (Values.Count() > 0) {
  861. //
  862. // NOTE : DiskID == 55 for wowfiles. In XPSP1 it is 155.
  863. //
  864. Result = ((Values.GetValue(0) == L"55")||
  865. (Values.GetValue(0) == L"155"));
  866. }
  867. return Result;
  868. }
  869. //
  870. // Determines if the record (file) needs to be skipped or not
  871. //
  872. template <class T>
  873. bool
  874. IsFileSkipped(
  875. SectionValues<T> &Values,
  876. FileListCreatorContext<T> &Context
  877. )
  878. {
  879. bool Result = false;
  880. if (Context.Args.WowFilesPresent && Context.Args.SkipWowFiles) {
  881. Result = IsWow64File(Values, Context);
  882. }
  883. return Result;
  884. }
  885. //
  886. // InfProcessing context
  887. //
  888. template <class T>
  889. struct InfProcessingErrors {
  890. vector<basic_string<T> > FileList;
  891. Arguments<T> &Args;
  892. InfProcessingErrors(Arguments<T> &TempArgs) : Args(TempArgs){}
  893. };
  894. //
  895. // Inf processing worker routine
  896. //
  897. template <class T>
  898. VOID
  899. InfFileChangeWorker(
  900. SectionValues<T> &Values,
  901. PVOID CallbackContext
  902. )
  903. {
  904. InfProcessingErrors<T> *ProcessingContext =
  905. (InfProcessingErrors<T> *)CallbackContext;
  906. if (ProcessingContext) {
  907. InfProcessingErrors<T> &Context = *ProcessingContext;
  908. T Buffer[4096] = {0};
  909. DWORD CharsCopied = 0;
  910. BOOL WriteResult = FALSE;
  911. basic_string<T> FileName;
  912. FileName = Context.Args.DestinationDirectory;
  913. FileName += Values.GetValue(0);
  914. basic_string<T> Value = Values.GetValue(2);
  915. if (Value.find(L' ') != Value.npos) {
  916. Value = L"\"" + Value + L"\"";
  917. }
  918. if (sizeof(T) == sizeof(CHAR)) {
  919. WriteResult = WritePrivateProfileStringA((PCSTR)Values.GetValue(1).c_str(),
  920. (PCSTR)Values.GetName().c_str(),
  921. (PCSTR)Value.c_str(),
  922. (PCSTR)FileName.c_str());
  923. } else {
  924. WriteResult = WritePrivateProfileStringW((PCWSTR)Values.GetValue(1).c_str(),
  925. (PCWSTR)Values.GetName().c_str(),
  926. (PCWSTR)Value.c_str(),
  927. (PCWSTR)FileName.c_str());
  928. }
  929. if (!WriteResult) {
  930. Context.FileList.push_back(Values.GetName());
  931. }
  932. }
  933. }
  934. //
  935. // Given the control inf, reads the [infchanges] section
  936. // and changes each of the specified value of the specified
  937. // inf in destination directory to given value
  938. //
  939. // The format for the [infchanges] section is
  940. // <[sub-directory]\><inf-name>=<section-name>,<key-name>,<new-value>
  941. //
  942. template <class T>
  943. bool
  944. ProcessInfChanges(
  945. Arguments<T> &Args,
  946. const basic_string<T> &InfName
  947. )
  948. {
  949. bool Result = false;
  950. try{
  951. InfFile<T> ControlInf(InfName);
  952. Section<T> *ChangeSection = ControlInf.GetSection(INFCHANGES_SECTION_NAME);
  953. T SectionStringBuffer[16] = {0};
  954. if (sizeof(T) == sizeof(CHAR)) {
  955. (VOID)StringCchPrintfA((PSTR)SectionStringBuffer,
  956. ARRAY_SIZE(SectionStringBuffer),
  957. "%d",
  958. Args.MajorBuildNumber);
  959. } else {
  960. (VOID)StringCchPrintfW((PWSTR)SectionStringBuffer,
  961. ARRAY_SIZE(SectionStringBuffer),
  962. TEXT("%d"),
  963. Args.MajorBuildNumber);
  964. }
  965. basic_string<T> BuildSpecificInfChangeSecName = INFCHANGES_SECTION_NAME +
  966. TEXT(".") +
  967. SectionStringBuffer;
  968. Section<T> *BuildSpecificInfChangeSection = ControlInf.GetSection(BuildSpecificInfChangeSecName.c_str());
  969. InfProcessingErrors<T> ProcessingErrors(Args);
  970. //
  971. // There needs to be atleast one entry with "/minint" load option change
  972. // for txtsetup.sif
  973. //
  974. if (!ChangeSection) {
  975. throw new InvalidInfSection<T>(L"infchanges", InfName);
  976. }
  977. else {
  978. ChangeSection->DoForEach(InfFileChangeWorker, &ProcessingErrors);
  979. if (BuildSpecificInfChangeSection){
  980. BuildSpecificInfChangeSection->DoForEach(InfFileChangeWorker, &ProcessingErrors);
  981. }
  982. }
  983. if (ProcessingErrors.FileList.size()) {
  984. vector<basic_string<T> >::iterator Iter = ProcessingErrors.FileList.begin();
  985. cout << GetFormattedMessage(ThisModule,
  986. FALSE,
  987. Message,
  988. sizeof(Message)/sizeof(Message[0]),
  989. MSG_ERROR_PROCESSING_INF_FILES) << endl;
  990. while (Iter != ProcessingErrors.FileList.end()) {
  991. cout << GetFormattedMessage(ThisModule,
  992. FALSE,
  993. Message,
  994. sizeof(Message)/sizeof(Message[0]),
  995. MSG_FILE,
  996. (*Iter).c_str()) << endl;
  997. Iter++;
  998. }
  999. } else {
  1000. Result = true;
  1001. }
  1002. }
  1003. catch (BaseException<wchar_t> *Exp) {
  1004. Exp->Dump(std::cout);
  1005. delete Exp;
  1006. Result = false;
  1007. }
  1008. catch(...) {
  1009. Result = false;
  1010. }
  1011. return Result;
  1012. }
  1013. //
  1014. // Arguments (constructor)
  1015. //
  1016. template <class T>
  1017. Arguments<T>::Arguments(int Argc, T *Argv[]) : Verbose(false) {
  1018. bool ValidArguments = false;
  1019. SkipWowFiles = true;
  1020. WowFilesPresent = false;
  1021. SkipFileCopy = false;
  1022. CheckVersion = false;
  1023. IA64Image = false;
  1024. MajorBuildNumber = 0;
  1025. MajorVersionNumber = 0;
  1026. MinorVersionNumber = 0;
  1027. T Buffer[MAX_PATH] = {0};
  1028. DWORD CharsCopied = 0;
  1029. if (sizeof(T) == sizeof(CHAR)) {
  1030. CharsCopied = GetCurrentDirectoryA(sizeof(Buffer)/sizeof(T),
  1031. (PSTR)Buffer);
  1032. } else {
  1033. CharsCopied = GetCurrentDirectoryW(sizeof(Buffer)/sizeof(T),
  1034. (PWSTR)Buffer);
  1035. }
  1036. if (!CharsCopied) {
  1037. throw new W32Exception<T>();
  1038. }
  1039. if (Buffer[CharsCopied - 1] != L'\\') {
  1040. Buffer[CharsCopied] = L'\\';
  1041. Buffer[CharsCopied + 1] = NULL;
  1042. }
  1043. CurrentDirectory = Buffer;
  1044. if (Argc >= 2) {
  1045. for (int Index = 0; Index < Argc; Index++) {
  1046. if (wcsstr(Argv[Index], L"/s:")) {
  1047. SourceDirectory = Argv[Index] + 3;
  1048. } else if (wcsstr(Argv[Index], L"/d:")) {
  1049. DestinationDirectory = Argv[Index] + 3;
  1050. } else if (wcsstr(Argv[Index], L"/m:")) {
  1051. OptionalSrcDirectory = Argv[Index] + 3;
  1052. } else if (wcsstr(Argv[Index], L"/e:")) {
  1053. ExtraFileName = Argv[Index] + 3;
  1054. } else if (wcsstr(Argv[Index], L"/l:")) {
  1055. LayoutName = Argv[Index] + 3;
  1056. } else if (wcsstr(Argv[Index], L"/p:")) {
  1057. PlatformSuffix = Argv[Index] + 3;
  1058. } else if (wcsstr(Argv[Index], L"/v")) {
  1059. Verbose = true;
  1060. } else if (!_wcsicmp(Argv[Index], L"/#u:nocopy")) {
  1061. SkipFileCopy = true;
  1062. } else if (!_wcsicmp(Argv[Index], L"/#u:checkversion")) {
  1063. CheckVersion = true;
  1064. }
  1065. }
  1066. if (SourceDirectory.length() &&
  1067. SourceDirectory[SourceDirectory.length() - 1] != L'\\') {
  1068. SourceDirectory += L"\\";
  1069. SourceDirectoryRoot = SourceDirectory;
  1070. std::basic_string<T> ia64Dir = SourceDirectory + L"ia64";
  1071. std::basic_string<T> x86Dir = SourceDirectory + L"i386";
  1072. if (IsFilePresent(ia64Dir)) {
  1073. PlatformSuffix = L"ia64";
  1074. SourceDirectory += L"ia64\\";
  1075. WowFilesPresent = true;
  1076. IA64Image = true;
  1077. } else if (IsFilePresent(x86Dir)) {
  1078. PlatformSuffix = L"x86";
  1079. SourceDirectory += L"i386\\";
  1080. }
  1081. }
  1082. if (DestinationDirectory.length() &&
  1083. DestinationDirectory[DestinationDirectory.length() - 1] != L'\\') {
  1084. DestinationDirectory += L'\\';
  1085. }
  1086. if (!LayoutName.length()) {
  1087. LayoutName = SourceDirectory + L"layout.inf";
  1088. }
  1089. if (OptionalSrcDirectory.length() &&
  1090. OptionalSrcDirectory[OptionalSrcDirectory.length() - 1] != L'\\') {
  1091. OptionalSrcDirectory += L"\\";
  1092. }
  1093. DriverIndexName = SourceDirectory + L"drvindex.inf";
  1094. if (OptionalSrcDirectory.length()) {
  1095. IntlInfFileName = OptionalSrcDirectory + L"intl.inf";
  1096. FontInfFileName = OptionalSrcDirectory + L"font.inf";
  1097. ConfigInfFileName = OptionalSrcDirectory + L"config.inf";
  1098. } else {
  1099. IntlInfFileName = SourceDirectory + L"intl.inf";
  1100. FontInfFileName = SourceDirectory + L"font.inf";
  1101. ConfigInfFileName = SourceDirectory + L"config.inf";
  1102. }
  1103. DosNetFileName = SourceDirectory + L"dosnet.inf";
  1104. //
  1105. // Get the SxS assembly layout (in ASMS directory or CAB).
  1106. //
  1107. IdentifySxSLayout();
  1108. if (!CheckVersion) {
  1109. ValidArguments = SourceDirectory.length() && DestinationDirectory.length() &&
  1110. LayoutName.length() &&
  1111. ((PlatformSuffix == L"x86") || (PlatformSuffix == L"ia64"));
  1112. } else {
  1113. ValidArguments = (SourceDirectory.length() > 0) &&
  1114. IsFilePresent(DosNetFileName);
  1115. }
  1116. }
  1117. if (!ValidArguments) {
  1118. throw new InvalidArguments();
  1119. }
  1120. }
  1121. template <class T>
  1122. VOID
  1123. Arguments<T>::IdentifySxSLayout(
  1124. VOID
  1125. )
  1126. /*++
  1127. Routine Description:
  1128. This routine determines the file layout for SXS files.
  1129. Arguments:
  1130. None.
  1131. Return Value:
  1132. None.
  1133. --*/
  1134. {
  1135. WCHAR DriverVer[MAX_PATH] = {0};
  1136. WinSxSLayout = SXS_LAYOUT_TYPE_CAB; // by default assumes latest layout
  1137. if (GetPrivateProfileString(L"Version",
  1138. L"DriverVer",
  1139. NULLSTR,
  1140. DriverVer,
  1141. sizeof(DriverVer)/sizeof(DriverVer[0]),
  1142. DosNetFileName.c_str())){
  1143. basic_string<T> DriverVerStr = DriverVer;
  1144. basic_string<T>::size_type VerStartPos = DriverVerStr.find(L',');
  1145. basic_string<T> VersionStr = DriverVerStr.substr(VerStartPos + 1);
  1146. vector<basic_string<T> > VersionTokens;
  1147. if (Tokenize(VersionStr, basic_string<T>(L"."), VersionTokens) > 2) {
  1148. T *EndChar;
  1149. MajorVersionNumber = wcstoul(VersionTokens[0].c_str(),
  1150. &EndChar, 10);
  1151. MinorVersionNumber = wcstoul(VersionTokens[1].c_str(),
  1152. &EndChar, 10);
  1153. MajorBuildNumber = wcstoul(VersionTokens[2].c_str(),
  1154. &EndChar, 10);
  1155. //
  1156. // This can be expanded in future for more products.
  1157. //
  1158. if ((MajorVersionNumber == 5) && (MajorBuildNumber < SXS_CAB_LAYOUT_BUILD_NUMBER)) {
  1159. WinSxSLayout = SXS_LAYOUT_TYPE_DIRECTORY;
  1160. }
  1161. } else {
  1162. throw new InvalidInfSection<T>(L"Version", DosNetFileName.c_str());
  1163. }
  1164. } else {
  1165. throw new W32Exception<T>();
  1166. }
  1167. }
  1168. //
  1169. // Checks the media version against the current OS version
  1170. //
  1171. template <class T>
  1172. bool
  1173. CheckMediaVersion(
  1174. Arguments<T> &Args
  1175. )
  1176. {
  1177. bool Result = false;
  1178. #ifdef _IA64_
  1179. bool IA64Build = true;
  1180. #else
  1181. bool IA64Build = false;
  1182. #endif
  1183. try {
  1184. WCHAR DriverVer[MAX_PATH] = {0};
  1185. WCHAR ProductType[MAX_PATH] = {0};
  1186. if (GetPrivateProfileString(L"Version",
  1187. L"DriverVer",
  1188. NULLSTR,
  1189. DriverVer,
  1190. sizeof(DriverVer)/sizeof(DriverVer[0]),
  1191. Args.DosNetFileName.c_str()) &&
  1192. GetPrivateProfileString(L"Miscellaneous",
  1193. L"ProductType",
  1194. NULLSTR,
  1195. ProductType,
  1196. sizeof(ProductType)/sizeof(ProductType[0]),
  1197. Args.DosNetFileName.c_str())) {
  1198. basic_string<T> DriverVerStr = DriverVer;
  1199. basic_string<T> ProductTypeStr = ProductType;
  1200. basic_string<T>::size_type VerStartPos = DriverVerStr.find(L',');
  1201. T *EndPtr;
  1202. DWORD ProductType = wcstoul(ProductTypeStr.c_str(), &EndPtr, 10);
  1203. //
  1204. // For the time being only worry about CD type
  1205. // Allow only from Pro, Server, Blade and ADS SKU's.
  1206. //
  1207. Result = ((0 == ProductType) ||
  1208. (1 == ProductType) ||
  1209. (5 == ProductType) ||
  1210. (2 == ProductType));
  1211. /*
  1212. //
  1213. // make sure that the CD is pro CD and the version is the same
  1214. // version as we are running from
  1215. //
  1216. if ((ProductType == 0) && (VerStartPos != basic_string<T>::npos)) {
  1217. basic_string<T> VersionStr = DriverVerStr.substr(VerStartPos + 1);
  1218. vector<basic_string<T> > VersionTokens;
  1219. if (Tokenize(VersionStr, basic_string<T>(L"."), VersionTokens) >= 3) {
  1220. T *EndChar;
  1221. DWORD MajorVer = wcstoul(VersionTokens[0].c_str(),
  1222. &EndChar, 10);
  1223. DWORD MinorVer = wcstoul(VersionTokens[1].c_str(),
  1224. &EndChar, 10);
  1225. DWORD BuildNumber = wcstoul(VersionTokens[2].c_str(),
  1226. &EndChar, 10);
  1227. OSVERSIONINFO VersionInfo;
  1228. ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
  1229. VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1230. if (MajorVer && MinorVer && BuildNumber &&
  1231. ::GetVersionEx(&VersionInfo)) {
  1232. Result = (VersionInfo.dwMajorVersion == MajorVer) &&
  1233. (VersionInfo.dwMinorVersion == MinorVer) &&
  1234. (VersionInfo.dwBuildNumber == BuildNumber);
  1235. }
  1236. }
  1237. }
  1238. */
  1239. }
  1240. } catch (...) {
  1241. Result = false;
  1242. }
  1243. return Result;
  1244. }
  1245. //
  1246. // Computes a SxS string hash, used in creating assembly identity
  1247. //
  1248. template <class T>
  1249. bool
  1250. ComputeStringHash(
  1251. const std::basic_string<T> &String,
  1252. ULONG &HashValue
  1253. )
  1254. {
  1255. bool Result = false;
  1256. ULONG TmpHashValue = 0;
  1257. if (String.length()) {
  1258. std::basic_string<T>::const_iterator Iter = String.begin();
  1259. while (Iter != String.end()) {
  1260. TmpHashValue = (TmpHashValue * 65599) + toupper(*Iter);
  1261. Iter++;
  1262. }
  1263. HashValue = TmpHashValue;
  1264. Result = true;
  1265. }
  1266. return Result;
  1267. }
  1268. //
  1269. // Computes an assembly identity hash for the specified
  1270. // Name and attribute pairs
  1271. //
  1272. template <class T>
  1273. bool
  1274. ComputeWinSxSHash(
  1275. IN std::map<std::basic_string<T>, std::basic_string<T> > &Attributes,
  1276. ULONG &Hash
  1277. )
  1278. {
  1279. bool Result = false;
  1280. std::map<std::basic_string<T>, std::basic_string<T> >::iterator Iter = Attributes.begin();
  1281. Hash = 0;
  1282. while (Iter != Attributes.end()) {
  1283. ULONG NameHash = 0;
  1284. ULONG ValueHash = 0;
  1285. ULONG AttrHash = 0;
  1286. if (ComputeStringHash((*Iter).first, NameHash) &&
  1287. ComputeStringHash((*Iter).second, ValueHash)) {
  1288. Result = true;
  1289. AttrHash = (NameHash * 65599) + ValueHash;
  1290. Hash = (Hash * 65599) + AttrHash;
  1291. }
  1292. Iter++;
  1293. }
  1294. return Result;
  1295. }
  1296. //
  1297. // Given a manifest file name generates a unique
  1298. // assmebly name (with ID) to be used as destination
  1299. // directory for the assembly
  1300. //
  1301. template <class T>
  1302. bool
  1303. GenerateWinSxSName(
  1304. IN std::basic_string<T> &ManifestName,
  1305. IN ULONG FileSize,
  1306. OUT std::basic_string<T> &SxSName
  1307. )
  1308. {
  1309. bool Result = false;
  1310. if (FileSize) {
  1311. bool Read = false;
  1312. PUCHAR Buffer = new UCHAR[FileSize + 1];
  1313. PWSTR UnicodeBuffer = new WCHAR[FileSize + 1];
  1314. std::wstring FileContent;
  1315. if (Buffer && UnicodeBuffer) {
  1316. HANDLE FileHandle;
  1317. //
  1318. // Open the manifest file
  1319. //
  1320. FileHandle = CreateFile(ManifestName.c_str(),
  1321. GENERIC_READ,
  1322. FILE_SHARE_READ,
  1323. NULL,
  1324. OPEN_EXISTING,
  1325. FILE_ATTRIBUTE_NORMAL,
  1326. NULL);
  1327. if (FileHandle != INVALID_HANDLE_VALUE) {
  1328. DWORD BytesRead = 0;
  1329. //
  1330. // Read the entire contents of the file
  1331. //
  1332. if (ReadFile(FileHandle,
  1333. Buffer,
  1334. FileSize,
  1335. &BytesRead,
  1336. NULL)) {
  1337. Read = (BytesRead == FileSize);
  1338. }
  1339. CloseHandle(FileHandle);
  1340. }
  1341. if (Read) {
  1342. //
  1343. // null terminate the buffer
  1344. //
  1345. Buffer[FileSize] = NULL;
  1346. //
  1347. // Convert the string to unicode string
  1348. //
  1349. if (MultiByteToWideChar(CP_UTF8,
  1350. 0,
  1351. (LPCSTR)Buffer,
  1352. FileSize + 1,
  1353. UnicodeBuffer,
  1354. FileSize + 1)) {
  1355. FileContent = UnicodeBuffer;
  1356. }
  1357. }
  1358. delete []Buffer;
  1359. delete []UnicodeBuffer;
  1360. } else {
  1361. if (Buffer) {
  1362. delete []Buffer;
  1363. }
  1364. if (UnicodeBuffer)
  1365. delete []UnicodeBuffer;
  1366. }
  1367. if (FileContent.length()) {
  1368. std::wstring IdentityKey = L"<" SXS_ASSEMBLY_MANIFEST_STD_ELEMENT_NAME_ASSEMBLY_IDENTITY;
  1369. std::wstring::size_type IdentityStartPos = FileContent.find(IdentityKey);
  1370. std::wstring::size_type IdentityEndPos = FileContent.find(L"/>", IdentityStartPos);
  1371. //
  1372. // Create name, value pairs for all the identity attributes specified
  1373. // in the manifest
  1374. //
  1375. if ((IdentityStartPos != IdentityKey.npos) &&
  1376. (IdentityEndPos != IdentityKey.npos)) {
  1377. std::map<std::wstring, std::wstring> IdentityPairs;
  1378. WCHAR *KeyNames[] = { SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_NAME,
  1379. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_VERSION,
  1380. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_LANGUAGE,
  1381. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_PUBLIC_KEY_TOKEN,
  1382. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_PROCESSOR_ARCHITECTURE,
  1383. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_TYPE,
  1384. NULL };
  1385. for (ULONG Index = 0; KeyNames[Index]; Index++) {
  1386. std::wstring::size_type ValueStartPos;
  1387. std::wstring::size_type ValueEndPos;
  1388. std::wstring KeyName = KeyNames[Index];
  1389. KeyName += L"=\"";
  1390. ValueStartPos = FileContent.find(KeyName, IdentityStartPos);
  1391. if (ValueStartPos != std::wstring::npos) {
  1392. ValueStartPos += KeyName.length();
  1393. ValueEndPos = FileContent.find(L"\"", ValueStartPos);
  1394. if ((ValueEndPos != std::wstring::npos) &&
  1395. (ValueEndPos > ValueStartPos) &&
  1396. (ValueEndPos <= IdentityEndPos)) {
  1397. IdentityPairs[KeyNames[Index]] = FileContent.substr(ValueStartPos,
  1398. ValueEndPos - ValueStartPos);
  1399. }
  1400. }
  1401. }
  1402. ULONG Hash = 0;
  1403. //
  1404. // Compute the assembly identity hash
  1405. //
  1406. if (ComputeWinSxSHash(IdentityPairs, Hash)) {
  1407. WCHAR *KeyValues[] = {
  1408. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_PROCESSOR_ARCHITECTURE,
  1409. NULL,
  1410. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_NAME,
  1411. NULL,
  1412. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_PUBLIC_KEY_TOKEN,
  1413. NULL,
  1414. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_VERSION,
  1415. NULL,
  1416. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_NAME_LANGUAGE,
  1417. SXS_ASSEMBLY_IDENTITY_STD_ATTRIBUTE_LANGUAGE_MISSING_VALUE,
  1418. NULL};
  1419. std::wstring Name;
  1420. Result = true;
  1421. //
  1422. // Generate the unique assembly name based on
  1423. // its identity attribute name, value pairs
  1424. //
  1425. for (Index = 0; KeyValues[Index]; Index += 2) {
  1426. std::wstring Key(KeyValues[Index]);
  1427. std::wstring Value(IdentityPairs[Key]);
  1428. //
  1429. // Use default value, if none specified
  1430. //
  1431. if ((Value.length() == 0) && KeyValues[Index + 1]) {
  1432. Value = KeyValues[Index + 1];
  1433. }
  1434. if (Value.length()) {
  1435. Name += Value;
  1436. if (KeyValues[Index + 2]) {
  1437. Name += TEXT("_");
  1438. }
  1439. } else {
  1440. Result = false;
  1441. break; // required value is missing
  1442. }
  1443. }
  1444. if (Result) {
  1445. WCHAR Buffer[32] = {0};
  1446. (VOID)StringCchPrintfW(Buffer,
  1447. ARRAY_SIZE(Buffer),
  1448. L"%x",
  1449. Hash);
  1450. SxSName = Name + TEXT("_") + Buffer;
  1451. }
  1452. }
  1453. }
  1454. }
  1455. }
  1456. return Result;
  1457. }
  1458. //
  1459. // Processes the fusion assembly in the specified directory
  1460. //
  1461. template <class T>
  1462. ULONG
  1463. ProcessWinSxSFilesInDirectory(
  1464. IN FileListCreatorContext<T> &Context,
  1465. IN std::basic_string<T> &DirName
  1466. )
  1467. {
  1468. //
  1469. // persistent state
  1470. //
  1471. static basic_string<T> WinSxSDirCode = TEXT("124");
  1472. static basic_string<T> WinSxSManifestDirCode = TEXT("125");
  1473. static basic_string<T> WinSxSDir = Context.DirsSection->GetValue(WinSxSDirCode).GetValue(0);
  1474. static basic_string<T> WinSxSManifestDir = Context.DirsSection->GetValue(WinSxSManifestDirCode).GetValue(0);
  1475. static ULONG NextDirIndex = 123456; // some random number not used in layout.inx
  1476. ULONG FileCount = 0;
  1477. WIN32_FIND_DATA FindData = {0};
  1478. std::basic_string<T> SearchName = DirName + TEXT("\\*.MAN");
  1479. HANDLE SearchHandle;
  1480. //
  1481. // Search for the *.man file in the specfied directory
  1482. //
  1483. SearchHandle = FindFirstFile(SearchName.c_str(), &FindData);
  1484. if (SearchHandle != INVALID_HANDLE_VALUE) {
  1485. std::basic_string<T> ManifestName = DirName + TEXT("\\") + FindData.cFileName;
  1486. std::basic_string<T> WinSxSName;
  1487. bool NameGenerated = false;
  1488. //
  1489. // Generate the WinSxS destination name for the manifest
  1490. //
  1491. NameGenerated = GenerateWinSxSName(ManifestName,
  1492. FindData.nFileSizeLow,
  1493. WinSxSName);
  1494. FindClose(SearchHandle);
  1495. if (NameGenerated) {
  1496. T NextDirCode[64] = {0};
  1497. std::basic_string<T> SxSDirName = Context.Args.DestinationDirectory + WinSxSDir + TEXT("\\");
  1498. std::basic_string<T> ManifestDirName = Context.Args.DestinationDirectory + WinSxSManifestDir + TEXT("\\");
  1499. //
  1500. // Cache the directory, if not already done
  1501. //
  1502. if (Context.DestDirs.find(WinSxSDirCode) == Context.DestDirs.end()) {
  1503. Context.DestDirs[WinSxSDirCode] = SxSDirName;
  1504. }
  1505. if (Context.DestDirs.find(WinSxSManifestDirCode) == Context.DestDirs.end()) {
  1506. Context.DestDirs[WinSxSManifestDirCode] = ManifestDirName;
  1507. }
  1508. ZeroMemory(&FindData, sizeof(WIN32_FIND_DATA));
  1509. //
  1510. // Search for all the files in the specified directory
  1511. //
  1512. SearchName = DirName + TEXT("\\*");
  1513. SearchHandle = FindFirstFile(SearchName.c_str(), &FindData);
  1514. if (SearchHandle != INVALID_HANDLE_VALUE) {
  1515. std::basic_string<T> SrcFileName, DestFileName;
  1516. std::basic_string<T> ManifestDirCode;
  1517. if (sizeof(T) == sizeof(CHAR)) {
  1518. (VOID)StringCchPrintfA((PSTR)NextDirCode,
  1519. ARRAY_SIZE(NextDirCode),
  1520. "%d",
  1521. NextDirIndex++);
  1522. } else {
  1523. (VOID)StringCchPrintfW((PWSTR)NextDirCode,
  1524. ARRAY_SIZE(NextDirCode),
  1525. TEXT("%d"),
  1526. NextDirIndex++);
  1527. }
  1528. ManifestDirCode = NextDirCode;
  1529. do {
  1530. if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  1531. SrcFileName = DirName + TEXT("\\") + FindData.cFileName;
  1532. DestFileName = SxSDirName;
  1533. std::basic_string<T> FileName(FindData.cFileName);
  1534. std::basic_string<T>::size_type DotPos = FileName.find(TEXT("."));
  1535. std::basic_string<T> Extension;
  1536. if (DotPos != FileName.npos) {
  1537. Extension = FileName.substr(DotPos + 1);
  1538. }
  1539. //
  1540. // *.man and *.cat go to the WinSxS\Manifest directory
  1541. //
  1542. if ((Extension == TEXT("man")) ||
  1543. (Extension == TEXT("MAN")) ||
  1544. (Extension == TEXT("cat")) ||
  1545. (Extension == TEXT("CAT"))) {
  1546. DestFileName = ManifestDirName;
  1547. DestFileName += WinSxSName;
  1548. if ((Extension == TEXT("man")) ||
  1549. (Extension == TEXT("MAN"))) {
  1550. DestFileName += TEXT(".Manifest");
  1551. } else {
  1552. DestFileName += TEXT(".");
  1553. DestFileName += Extension;
  1554. }
  1555. } else {
  1556. //
  1557. // Cache the directory, if not already done
  1558. //
  1559. if (Context.DestDirs.find(ManifestDirCode) == Context.DestDirs.end()) {
  1560. Context.DestDirs[ManifestDirCode] = SxSDirName + WinSxSName;
  1561. }
  1562. //
  1563. // Each file other than *.man & *.cat go the unique
  1564. // assembly directory created
  1565. //
  1566. DestFileName += WinSxSName;
  1567. DestFileName += TEXT("\\");
  1568. DestFileName += FileName;
  1569. }
  1570. //
  1571. // Queue this file for copying
  1572. //
  1573. Context.WinSxSFileList[SrcFileName] = DestFileName;
  1574. FileCount++;
  1575. }
  1576. }
  1577. while (FindNextFile(SearchHandle, &FindData));
  1578. FindClose(SearchHandle);
  1579. }
  1580. }
  1581. }
  1582. return FileCount;
  1583. }
  1584. template<class T>
  1585. bool
  1586. WinSxsExtractVersionInfo(
  1587. IN basic_string<T> ManifestName,
  1588. OUT basic_string<T> &Version
  1589. )
  1590. /*++
  1591. Routine Description:
  1592. Extracts the version information string (like 1.0.0.1) from
  1593. the given manifest name.
  1594. NOTE: Assumes that version information is the third-last (third
  1595. from the last) value in the assembly Id.
  1596. Arguments:
  1597. ManifestName - full manifest name
  1598. Version - placeholder for extracted version information
  1599. Return Value:
  1600. true on success, otherwise false
  1601. --*/
  1602. {
  1603. bool Result = false;
  1604. basic_string<T>::size_type VersionEnd = ManifestName.rfind((T)TEXT('_'));
  1605. if (VersionEnd != ManifestName.npos) {
  1606. VersionEnd = ManifestName.rfind((T)TEXT('_'), VersionEnd - 1);
  1607. if (VersionEnd != ManifestName.npos) {
  1608. basic_string<T>::size_type VersionStart = ManifestName.rfind((T)TEXT('_'), VersionEnd - 1);
  1609. VersionEnd--;
  1610. if (VersionStart != ManifestName.npos) {
  1611. Version = ManifestName.substr(VersionStart + 1, VersionEnd - VersionStart);
  1612. Result = (Version.length() > 0);
  1613. }
  1614. }
  1615. }
  1616. return Result;
  1617. }
  1618. template <class T>
  1619. bool
  1620. WinSxsFixFilePaths(
  1621. IN FileListCreatorContext<T> &Context,
  1622. IN OUT FILE_IN_CABINET_INFO &FileInfo,
  1623. OUT bool &rSkipFile
  1624. )
  1625. /*++
  1626. Routine Description:
  1627. This routine fixes the destination path in the
  1628. FileInfo argument. The new format of this cabinet (build 364?+) is
  1629. that it contains direct paths underneath winsxs, no wierd finding-and-
  1630. placing files into different directories required. All that has to
  1631. happen is the winsxs root gets prepended to the cab path name.
  1632. Arguments:
  1633. Context - FileListCreatorContext instance as PVOID.
  1634. FileInfo - Cab file iteration FileInfo instance
  1635. Return Value:
  1636. true if the destination name was fixed otherwise false.
  1637. --*/
  1638. {
  1639. bool Result = true;
  1640. basic_string<T> SourceName;
  1641. static basic_string<T> SetupPoliciesPrefix = (T*)TEXT("setuppolicies");
  1642. static basic_string<T> WinSxSDirCode((T *)TEXT("124"));
  1643. static basic_string<T> WinSxSDir = Context.Args.DestinationDirectory +
  1644. Context.DirsSection->GetValue(WinSxSDirCode).GetValue(0)
  1645. + (T *)TEXT("\\") ;
  1646. basic_string<T> DestinationName;
  1647. rSkipFile = false;
  1648. if (sizeof(T) == sizeof(CHAR)) {
  1649. SourceName = (T *)(_strlwr((PSTR)(FileInfo.NameInCabinet)));
  1650. } else {
  1651. SourceName = (T *)(_wcslwr((PWSTR)(FileInfo.NameInCabinet)));
  1652. }
  1653. //
  1654. // Cab contains "setuppolicies" files that should not appear on winpe cds.
  1655. // Skip them with abandon.
  1656. //
  1657. if (sizeof(T) == sizeof(CHAR)) {
  1658. if (_strnicmp((PCSTR)SourceName.c_str(), (PCSTR)SetupPoliciesPrefix.c_str(), SetupPoliciesPrefix.length()) == 0) {
  1659. rSkipFile = true;
  1660. return true;
  1661. }
  1662. }
  1663. else {
  1664. if (_wcsnicmp((PCWSTR)SourceName.c_str(), (PCWSTR)SetupPoliciesPrefix.c_str(), SetupPoliciesPrefix.length()) == 0) {
  1665. rSkipFile = true;
  1666. return true;
  1667. }
  1668. }
  1669. DestinationName = WinSxSDir;
  1670. DestinationName += TEXT('\\');
  1671. DestinationName += SourceName;
  1672. if (sizeof(T) == sizeof(CHAR)) {
  1673. Result = SUCCEEDED(StringCchCopyA((PSTR)(FileInfo.FullTargetName),
  1674. ARRAY_SIZE(FileInfo.FullTargetName),
  1675. (PCSTR)DestinationName.c_str()));
  1676. } else {
  1677. Result = SUCCEEDED(StringCchCopyW((PWSTR)(FileInfo.FullTargetName),
  1678. ARRAY_SIZE(FileInfo.FullTargetName),
  1679. (PCWSTR)DestinationName.c_str()));
  1680. }
  1681. if (Result) {
  1682. if (sizeof(T) == sizeof(CHAR)) {
  1683. (VOID)StringCchCopyA((PSTR)(FileInfo.FullTargetName),
  1684. ARRAY_SIZE(FileInfo.FullTargetName),
  1685. (PCSTR)DestinationName.c_str());
  1686. } else {
  1687. (VOID)StringCchCopyW((PWSTR)(FileInfo.FullTargetName),
  1688. ARRAY_SIZE(FileInfo.FullTargetName),
  1689. (PCWSTR)DestinationName.c_str());
  1690. }
  1691. }
  1692. return Result;
  1693. }
  1694. template <class T>
  1695. UINT
  1696. WinSxsCabinetCallback(
  1697. IN PVOID Context,
  1698. IN UINT Notification,
  1699. IN UINT_PTR Param1,
  1700. IN UINT_PTR Param2
  1701. )
  1702. /*++
  1703. Routine Description:
  1704. This routine processes WinSxS files in Cabinet.
  1705. Arguments:
  1706. Context - FileListCreatorContext instance as PVOID.
  1707. Notification - CAB Iteration Code
  1708. Param1 - First parameter for Notification.
  1709. Param2 - Second parameter for Notification.
  1710. Return Value:
  1711. Appropriate return code to continue iterating, copy the file or skip
  1712. the file in cab.
  1713. --*/
  1714. {
  1715. UINT ReturnCode = NO_ERROR;
  1716. FileListCreatorContext<T> *FlContext = (FileListCreatorContext<T> *)Context;
  1717. PFILE_IN_CABINET_INFO FileInfo = NULL;
  1718. PFILEPATHS FilePaths = NULL;
  1719. basic_string<T> &FileName = FlContext->CurrentFileName;
  1720. bool fSkipFile = false;
  1721. switch (Notification) {
  1722. case SPFILENOTIFY_FILEINCABINET:
  1723. ReturnCode = FILEOP_SKIP;
  1724. FileInfo = (PFILE_IN_CABINET_INFO)Param1;
  1725. if (WinSxsFixFilePaths(*FlContext, *FileInfo, fSkipFile)) {
  1726. if (sizeof(T) == sizeof(CHAR)) {
  1727. FileName = (const T *)(FileInfo->NameInCabinet);
  1728. } else {
  1729. FileName = (const T *)(FileInfo->NameInCabinet);
  1730. }
  1731. if (!FlContext->Args.SkipFileCopy && !fSkipFile) {
  1732. //
  1733. // create the destination directory if it doesnot exist
  1734. //
  1735. basic_string<T> DestinationName = (T *)(FileInfo->FullTargetName);
  1736. basic_string<T> DestinationDir = DestinationName.substr(0, DestinationName.rfind((T *)TEXT("\\")));
  1737. if (sizeof(T) == sizeof(CHAR)) {
  1738. if (_access((PCSTR)(DestinationDir.c_str()), 0)) {
  1739. CreateDirectories(DestinationDir, NULL);
  1740. }
  1741. } else {
  1742. if (_waccess((PCWSTR)(DestinationDir.c_str()), 0)) {
  1743. CreateDirectories(DestinationDir, NULL);
  1744. }
  1745. }
  1746. ReturnCode = FILEOP_DOIT;
  1747. } else {
  1748. ReturnCode = FILEOP_SKIP;
  1749. FlContext->FileCount++;
  1750. if (FlContext->Args.Verbose) {
  1751. std::cout << GetFormattedMessage(ThisModule,
  1752. FALSE,
  1753. Message,
  1754. sizeof(Message)/sizeof(Message[0]),
  1755. MSG_EXTRACT_FILES_FROM_CAB_NOTIFICATION,
  1756. FlContext->WinSxsCabinetFileName.c_str(),
  1757. FileInfo->NameInCabinet,
  1758. FileInfo->FullTargetName) << std::endl;
  1759. }
  1760. }
  1761. } else {
  1762. ReturnCode = FILEOP_ABORT;
  1763. }
  1764. break;
  1765. case SPFILENOTIFY_FILEEXTRACTED:
  1766. FilePaths = (PFILEPATHS)Param1;
  1767. if (FilePaths->Win32Error) {
  1768. std::cout << GetFormattedMessage(ThisModule,
  1769. FALSE,
  1770. Message,
  1771. sizeof(Message)/sizeof(Message[0]),
  1772. MSG_ERROR_EXTRACTING_FILES,
  1773. FilePaths->Win32Error,
  1774. FilePaths->Source,
  1775. FileName.c_str(),
  1776. FilePaths->Target) << std::endl;
  1777. } else {
  1778. FlContext->FileCount++;
  1779. if (FlContext->Args.Verbose) {
  1780. std::cout << GetFormattedMessage(ThisModule,
  1781. FALSE,
  1782. Message,
  1783. sizeof(Message)/sizeof(Message[0]),
  1784. MSG_EXTRACTED_FILES_FROM_CAB_NOTIFICATION,
  1785. FilePaths->Source,
  1786. FileName.c_str(),
  1787. FilePaths->Target) << std::endl;
  1788. }
  1789. }
  1790. break;
  1791. default:
  1792. break;
  1793. }
  1794. return ReturnCode;
  1795. }
  1796. //
  1797. // Copies all the required files in given CAB file to the specified
  1798. // destination directory
  1799. //
  1800. template <class T>
  1801. ULONG
  1802. ProcessWinSxsCabFiles(
  1803. IN FileListCreatorContext<T> &Context,
  1804. IN const std::basic_string<T> &CabFileName
  1805. )
  1806. /*++
  1807. Routine Description:
  1808. This routine processes the given CAB file for WinSxS. It extracts
  1809. the required manifest, catalog and policy files and installs them
  1810. to the the appropriate assembly on the destination.
  1811. Arguments:
  1812. Context - FileListCreatorContext instance as PVOID.
  1813. CabFileName - Fully qualitifed cab file name that needs to be processed.
  1814. Return Value:
  1815. Number of files processed.
  1816. --*/
  1817. {
  1818. ULONG Count = Context.FileCount;
  1819. BOOL Result = FALSE;
  1820. Context.WinSxsCabinetFileName = CabFileName;
  1821. if (sizeof(T) == sizeof(CHAR)) {
  1822. Result = SetupIterateCabinetA((PCSTR)CabFileName.c_str(),
  1823. NULL,
  1824. (PSP_FILE_CALLBACK_A)WinSxsCabinetCallback<char>,
  1825. &Context);
  1826. } else {
  1827. Result = SetupIterateCabinetW((PCWSTR)CabFileName.c_str(),
  1828. NULL,
  1829. (PSP_FILE_CALLBACK_W)WinSxsCabinetCallback<wchar_t>,
  1830. &Context);
  1831. }
  1832. if (!Result) {
  1833. cout << GetFormattedMessage(ThisModule,
  1834. FALSE,
  1835. Message,
  1836. sizeof(Message)/sizeof(Message[0]),
  1837. MSG_ERROR_ITERATING_CAB_FILE,
  1838. GetLastError(),
  1839. CabFileName.c_str()) << endl;
  1840. }
  1841. return Context.FileCount - Count;
  1842. }
  1843. template <class T>
  1844. ULONG
  1845. ProcessWinSxSFilesForCabLayout(
  1846. IN FileListCreatorContext<T> &Context,
  1847. IN std::basic_string<T> &SearchPattern
  1848. )
  1849. /*++
  1850. Routine Description:
  1851. Processes Win SXS files for CAB layout.
  1852. Arguments:
  1853. Context : Current Processing Context.
  1854. SearchPattern : The search pattern for cab files.
  1855. Return Value:
  1856. The number of files which were processed.
  1857. --*/
  1858. {
  1859. ULONG FileCount = 0;
  1860. WIN32_FIND_DATA FindData = {0};
  1861. std::basic_string<T> SearchName = Context.Args.SourceDirectory + SearchPattern;
  1862. HANDLE SearchHandle;
  1863. SearchHandle = FindFirstFile(SearchName.c_str(), &FindData);
  1864. if (SearchHandle != INVALID_HANDLE_VALUE) {
  1865. do {
  1866. if (!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  1867. basic_string<T> FullCabFileName = Context.Args.SourceDirectory + FindData.cFileName;
  1868. //
  1869. // Process any manifests present in the current directory
  1870. //
  1871. FileCount += ProcessWinSxsCabFiles(Context, FullCabFileName);
  1872. }
  1873. }
  1874. while (FindNextFile(SearchHandle, &FindData));
  1875. FindClose(SearchHandle);
  1876. }
  1877. return FileCount;
  1878. }
  1879. template <class T>
  1880. ULONG
  1881. ProcessWinSxSFilesForDirectoryLayout(
  1882. IN FileListCreatorContext<T> &Context,
  1883. IN std::basic_string<T> &DirName
  1884. )
  1885. /*++
  1886. Routine Description:
  1887. Processes Win SXS files for flat/directory layout.
  1888. Arguments:
  1889. Context: Current Processing context.
  1890. DirName: Current directory to be processed.
  1891. Return Value:
  1892. The number of files which were processed.
  1893. --*/
  1894. {
  1895. WIN32_FIND_DATA FindData = {0};
  1896. std::basic_string<T> SearchName;
  1897. static std::basic_string<T> CurrDir = TEXT(".");
  1898. static std::basic_string<T> ParentDir = TEXT("..");
  1899. ULONG FileCount = 0;
  1900. HANDLE SearchHandle;
  1901. SearchName = DirName + TEXT("\\*");
  1902. SearchHandle = FindFirstFile(SearchName.c_str(), &FindData);
  1903. if (SearchHandle != INVALID_HANDLE_VALUE) {
  1904. do {
  1905. if ((CurrDir != FindData.cFileName) && (ParentDir != FindData.cFileName)) {
  1906. //
  1907. // If we hit a directory then search again in that directory
  1908. //
  1909. if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  1910. std::basic_string<T> NewDirName = DirName + TEXT("\\") + FindData.cFileName;
  1911. FileCount += ProcessWinSxSFilesForDirectoryLayout(Context, NewDirName);
  1912. } else {
  1913. //
  1914. // Process any manifests present in the current directory
  1915. //
  1916. FileCount += ProcessWinSxSFilesInDirectory(Context, DirName);
  1917. //
  1918. // done with this directory and sub-directories
  1919. //
  1920. break;
  1921. }
  1922. }
  1923. }
  1924. while (FindNextFile(SearchHandle, &FindData));
  1925. FindClose(SearchHandle);
  1926. }
  1927. return FileCount;
  1928. }
  1929. //
  1930. // Processes the asms directory and installs fusion assemblies in an
  1931. // offline fashion
  1932. //
  1933. template <class T>
  1934. ULONG
  1935. ProcessWinSxSFiles(
  1936. IN FileListCreatorContext<T> &Context
  1937. )
  1938. {
  1939. ULONG FileCount = 0;
  1940. if (Context.Args.WinSxSLayout == SXS_LAYOUT_TYPE_DIRECTORY) {
  1941. basic_string<T> AsmsDir = Context.Args.SourceDirectory + TEXT("asms");
  1942. FileCount = ProcessWinSxSFilesForDirectoryLayout(Context, AsmsDir);
  1943. } else {
  1944. basic_string<T> SearchPattern = TEXT("asms*.cab");
  1945. FileCount = ProcessWinSxSFilesForCabLayout(Context, SearchPattern);
  1946. }
  1947. return FileCount;
  1948. }
  1949. //
  1950. // Process NLS specific files
  1951. //
  1952. template <class T>
  1953. FileListCreatorContext<T>::FileListCreatorContext(
  1954. Arguments<T> &PrgArgs,
  1955. Section<T> *Curr,
  1956. Section<T> *Dirs,
  1957. InfFile<T> &ConfigInf,
  1958. InfFile<T> &IntlInf,
  1959. InfFile<T> &FontInf,
  1960. DriverIndexInfFile<T> &DrvIdxFile
  1961. ): Args(PrgArgs),
  1962. ConfigInfFile(ConfigInf),
  1963. IntlInfFile(IntlInf),
  1964. FontInfFile(FontInf),
  1965. DriverIdxFile(DrvIdxFile)
  1966. /*++
  1967. Routine Description:
  1968. Constructor
  1969. Arguments:
  1970. Bunch of them.
  1971. Return Value:
  1972. FileListCreatorContext object instance.
  1973. --*/
  1974. {
  1975. CurrentSection = Curr;
  1976. DirsSection = Dirs;
  1977. SkipInfFiles = false;
  1978. FileCount = 0;
  1979. ProcessingExtraFiles = false;
  1980. DummyDirectoryId = 50000; // we start with 50000 and count upwards
  1981. //
  1982. // get hold of the windows directory which we need to prune the NLS
  1983. // copy file list
  1984. //
  1985. DWORD Length;
  1986. T WindowsDirBuffer[MAX_PATH] = {0};
  1987. if (sizeof(T) == sizeof(CHAR)) {
  1988. Length = GetWindowsDirectoryA((PSTR)WindowsDirBuffer, sizeof(WindowsDirBuffer)/sizeof(T));
  1989. if (Length){
  1990. if (((PSTR)WindowsDirBuffer)[Length] != '\\') {
  1991. (VOID)StringCchCatA((PSTR)WindowsDirBuffer,
  1992. ARRAY_SIZE(WindowsDirBuffer),
  1993. "\\");
  1994. }
  1995. _strlwr((PSTR)WindowsDirBuffer);
  1996. WindowsDirectory = basic_string<T>((const T*)WindowsDirBuffer);
  1997. }
  1998. } else {
  1999. Length = GetWindowsDirectoryW((PWSTR)WindowsDirBuffer, sizeof(WindowsDirBuffer)/sizeof(T));
  2000. if (Length) {
  2001. if (((PWSTR)WindowsDirBuffer)[Length] != L'\\') {
  2002. (VOID)StringCchCatW((PWSTR)WindowsDirBuffer,
  2003. ARRAY_SIZE(WindowsDirBuffer),
  2004. L"\\");
  2005. }
  2006. _wcslwr((PWSTR)WindowsDirBuffer);
  2007. WindowsDirectory = basic_string<T>((const T*)WindowsDirBuffer);
  2008. }
  2009. }
  2010. if (!WindowsDirBuffer[0]) {
  2011. throw new W32Exception<T>();
  2012. }
  2013. }
  2014. template <class T>
  2015. ULONG
  2016. FileListCreatorContext<T>::ProcessNlsFiles(
  2017. VOID
  2018. )
  2019. /*++
  2020. Routine Description:
  2021. Does the necessary work to process the NLS files from INTL.INF
  2022. & FONT.INF files.
  2023. NOTE : For all locales language group 1 (LG_INSTALL_1 section) is
  2024. processed.
  2025. Arguments:
  2026. None.
  2027. Return Value:
  2028. Number of NLS files that were added to the files to copy list.
  2029. --*/
  2030. {
  2031. ULONG FileCount = 0;
  2032. //
  2033. // get hold of the necessary copy file sections
  2034. //
  2035. Section<T> *RegionalSection = ConfigInfFile.GetSection(REGIONAL_SECTION_NAME);
  2036. if (!RegionalSection) {
  2037. throw new InvalidInfSection<T>(REGIONAL_SECTION_NAME,
  2038. ConfigInfFile.GetName());
  2039. }
  2040. SectionValues<T> *LangGroups;
  2041. //
  2042. // [LanguageGroup] section is optional
  2043. //
  2044. try {
  2045. LangGroups = &(RegionalSection->GetValue(LANGUAGE_GROUP_KEY));
  2046. } catch (...) {
  2047. LangGroups = NULL;
  2048. }
  2049. SectionValues<T> &Language = RegionalSection->GetValue(LANGUAGE_KEY);
  2050. ULONG LangGroupCount = LangGroups ? LangGroups->Count() : 0;
  2051. //
  2052. // go through all language group sections and create a list of unique
  2053. // language group sections that need to be processed.
  2054. //
  2055. std::map< std::basic_string<T>, std::basic_string<T> > RegSectionsToProcess;
  2056. for (ULONG Index = 0; Index < LangGroupCount; Index++) {
  2057. //
  2058. // get the language group section
  2059. //
  2060. std::basic_string<T> LangGroupName = LANGGROUP_SECTION_PREFIX;
  2061. LangGroupName += LangGroups->GetValue(Index);
  2062. //std::cout << LangGroupName << std::endl;
  2063. if (sizeof(T) == sizeof(CHAR)) {
  2064. _strlwr((PSTR)LangGroupName.c_str());
  2065. } else {
  2066. _wcslwr((PWSTR)LangGroupName.c_str());
  2067. }
  2068. //
  2069. // if the section is not already there then add it
  2070. //
  2071. if (RegSectionsToProcess.find(LangGroupName) == RegSectionsToProcess.end()) {
  2072. // std::cout << "Adding : " << LangGroupName << std::endl;
  2073. RegSectionsToProcess[LangGroupName] = LangGroupName;
  2074. }
  2075. }
  2076. //
  2077. // process the language section
  2078. //
  2079. T LanguageIdStr[64];
  2080. T *EndPtr;
  2081. DWORD LanguageId;
  2082. if (sizeof(T) == sizeof(CHAR)) {
  2083. LanguageId = strtoul((PSTR)Language.GetValue(0).c_str(),
  2084. (PSTR *)&EndPtr,
  2085. 16);
  2086. (VOID)StringCchPrintfA((PSTR)LanguageIdStr,
  2087. ARRAY_SIZE(LanguageIdStr),
  2088. "%08x",
  2089. LanguageId);
  2090. _strlwr((PSTR)LanguageIdStr);
  2091. } else {
  2092. LanguageId = wcstoul((PWSTR)Language.GetValue(0).c_str(),
  2093. (PWSTR *)&EndPtr,
  2094. 16);
  2095. (VOID)StringCchPrintfW((PWSTR)LanguageIdStr,
  2096. ARRAY_SIZE(LanguageIdStr),
  2097. L"%08x",
  2098. LanguageId);
  2099. _wcslwr((PWSTR)LanguageIdStr);
  2100. }
  2101. std::basic_string<T> LangSectionName = LanguageIdStr;
  2102. RegSectionsToProcess[LangSectionName] = LangSectionName;
  2103. //
  2104. // make sure the required language groups for this
  2105. // language are also processed
  2106. //
  2107. Section<T> *LocaleSection = IntlInfFile.GetSection(LOCALES_SECTION_NAME);
  2108. if (!LocaleSection) {
  2109. throw new InvalidInfSection<T>(LOCALES_SECTION_NAME,
  2110. IntlInfFile.GetName());
  2111. }
  2112. SectionValues<T> &LocaleValues = LocaleSection->GetValue(LangSectionName);
  2113. std::basic_string<T> NeededLangGroup = LANGGROUP_SECTION_PREFIX + LocaleValues.GetValue(LANG_GROUP1_INDEX);
  2114. RegSectionsToProcess[NeededLangGroup] = NeededLangGroup;
  2115. //
  2116. // add the font registry entries also
  2117. //
  2118. T FontSectionName[MAX_PATH];
  2119. if (sizeof(T) == sizeof(CHAR)) {
  2120. (VOID)StringCchPrintfA((PSTR)FontSectionName,
  2121. ARRAY_SIZE(FontSectionName),
  2122. (PSTR)FONT_CP_REGSECTION_FMT_STR.c_str(),
  2123. (PSTR)LocaleValues.GetValue(OEM_CP_INDEX).c_str(),
  2124. DEFAULT_FONT_SIZE);
  2125. } else {
  2126. (VOID)StringCchPrintfW((PWSTR)FontSectionName,
  2127. ARRAY_SIZE(FontSectionName),
  2128. (PWSTR)FONT_CP_REGSECTION_FMT_STR.c_str(),
  2129. (PWSTR)LocaleValues.GetValue(OEM_CP_INDEX).c_str(),
  2130. DEFAULT_FONT_SIZE);
  2131. }
  2132. RegSectionsToProcess[FontSectionName] = FontSectionName;
  2133. std::map< std::wstring, std::wstring >::iterator Iter = RegSectionsToProcess.find(DEFAULT_LANGGROUP_NAME);
  2134. if (Iter == RegSectionsToProcess.end()) {
  2135. RegSectionsToProcess[DEFAULT_LANGGROUP_NAME] = DEFAULT_LANGGROUP_NAME;
  2136. }
  2137. //
  2138. // NOTE : Rather than parsing INTL.INF and FONT.INF files manually
  2139. // we use file queue to populate the queue and then later use the file
  2140. // queue to initialize our copy list map data structure.
  2141. //
  2142. //
  2143. // Initialize file queue
  2144. //
  2145. HINF IntlInfHandle = (HINF)IntlInfFile.GetInfHandle();
  2146. HINF FontInfHandle = (HINF)FontInfFile.GetInfHandle();
  2147. if (sizeof(T) == sizeof(CHAR)) {
  2148. if (!SetupOpenAppendInfFileA((PSTR)Args.LayoutName.c_str(),
  2149. IntlInfHandle,
  2150. NULL)) {
  2151. throw new W32Exception<T>();
  2152. }
  2153. if (!SetupOpenAppendInfFileA((PSTR)Args.LayoutName.c_str(),
  2154. FontInfHandle,
  2155. NULL)) {
  2156. throw new W32Exception<T>();
  2157. }
  2158. } else {
  2159. if (!SetupOpenAppendInfFileW((PWSTR)Args.LayoutName.c_str(),
  2160. IntlInfHandle,
  2161. NULL)) {
  2162. throw new W32Exception<T>();
  2163. }
  2164. if (!SetupOpenAppendInfFileW((PWSTR)Args.LayoutName.c_str(),
  2165. FontInfHandle,
  2166. NULL)) {
  2167. throw new W32Exception<T>();
  2168. }
  2169. }
  2170. HSPFILEQ FileQueueHandle = SetupOpenFileQueue();
  2171. if (FileQueueHandle == INVALID_HANDLE_VALUE) {
  2172. throw new W32Exception<T>();
  2173. }
  2174. //
  2175. // add copy file sections to the queue
  2176. //
  2177. BOOL Result;
  2178. Iter = RegSectionsToProcess.begin();
  2179. while (Iter != RegSectionsToProcess.end()) {
  2180. // cout << (*Iter).first << endl;
  2181. //
  2182. // process each section
  2183. //
  2184. if (sizeof(T) == sizeof(CHAR)) {
  2185. Result = SetupInstallFilesFromInfSectionA(IntlInfHandle,
  2186. NULL,
  2187. FileQueueHandle,
  2188. (PCSTR)(*Iter).first.c_str(),
  2189. (PCSTR)Args.SourceDirectoryRoot.c_str(),
  2190. 0);
  2191. } else {
  2192. Result = SetupInstallFilesFromInfSectionW(IntlInfHandle,
  2193. NULL,
  2194. FileQueueHandle,
  2195. (PCWSTR)(*Iter).first.c_str(),
  2196. (PCWSTR)Args.SourceDirectoryRoot.c_str(),
  2197. 0);
  2198. }
  2199. if (!Result) {
  2200. throw new W32Exception<T>();
  2201. }
  2202. Iter++;
  2203. }
  2204. //
  2205. // scan the queue and populate FileListCreatorContext<T> copy list
  2206. // data structure
  2207. //
  2208. DWORD ScanResult = 0;
  2209. if (sizeof(T) == sizeof(CHAR)) {
  2210. Result = SetupScanFileQueueA(FileQueueHandle,
  2211. SPQ_SCAN_USE_CALLBACKEX,
  2212. NULL,
  2213. (PSP_FILE_CALLBACK_A)NlsFileQueueScanWorker,
  2214. this,
  2215. &ScanResult);
  2216. } else {
  2217. Result = SetupScanFileQueueW(FileQueueHandle,
  2218. SPQ_SCAN_USE_CALLBACKEX,
  2219. NULL,
  2220. (PSP_FILE_CALLBACK_W)NlsFileQueueScanWorker,
  2221. this,
  2222. &ScanResult);
  2223. }
  2224. SetupCloseFileQueue(FileQueueHandle);
  2225. //
  2226. // Add the Nls directory entries to main directory map
  2227. //
  2228. ProcessNlsDirMapEntries();
  2229. //
  2230. // Remove duplicate Nls file entries
  2231. //
  2232. RemoveDuplicateNlsEntries();
  2233. //
  2234. // Move the driver cab files to driver cab list
  2235. //
  2236. MoveDriverCabNlsFiles();
  2237. //
  2238. // After all this work, how many NLS files do we actually
  2239. // want to copy ?
  2240. //
  2241. return NlsFileMap.size();
  2242. }
  2243. template <class T>
  2244. void
  2245. FileListCreatorContext<T>::MoveDriverCabNlsFiles(
  2246. void
  2247. )
  2248. /*++
  2249. Routine Description:
  2250. Takes each NLS file entry to be copied and moves it to
  2251. the driver cab file copy list if the file is present
  2252. in driver cab so that we can extract the file from
  2253. driver cab.
  2254. Arguments:
  2255. None.
  2256. Return Value:
  2257. None.
  2258. --*/
  2259. {
  2260. std::map<std::basic_string<T>, std::basic_string<T> >::iterator NlsIter, DelIter;
  2261. T Slash;
  2262. if (sizeof(T) == sizeof(CHAR)) {
  2263. Slash = (T)'\\';
  2264. } else {
  2265. Slash = (T)L'\\';
  2266. }
  2267. for (NlsIter = NlsFileMap.begin(); NlsIter != NlsFileMap.end();) {
  2268. const std::basic_string<T> &Key = (*NlsIter).first;
  2269. std::basic_string<T>::size_type KeyStart = Key.rfind(Slash);
  2270. std::basic_string<T> FileKey;
  2271. DelIter = NlsFileMap.end();
  2272. if (KeyStart != Key.npos) {
  2273. FileKey = Key.substr(Key.rfind(Slash) + 1);
  2274. }
  2275. if (FileKey.length()) {
  2276. if (sizeof(T) == sizeof(CHAR)) {
  2277. _strlwr((PSTR)FileKey.c_str());
  2278. } else {
  2279. _wcslwr((PWSTR)FileKey.c_str());
  2280. }
  2281. const basic_string<T> &DriverCabFileName = GetDriverCabFileName(FileKey);
  2282. if (DriverCabFileName.length()) {
  2283. // std::cout << "Moved to driver cab list : (" << FileKey << ")" << std::endl;
  2284. AddFileToCabFileList(DriverCabFileName,
  2285. FileKey,
  2286. (*NlsIter).second);
  2287. DelIter = NlsIter;
  2288. } else {
  2289. // std::cout << "Not present in driver cab list : (" << FileKey << ")" << std::endl;
  2290. }
  2291. }
  2292. NlsIter++;
  2293. if (DelIter != NlsFileMap.end()) {
  2294. NlsFileMap.erase(DelIter);
  2295. }
  2296. }
  2297. }
  2298. template <class T>
  2299. UINT
  2300. FileListCreatorContext<T>::NlsFileQueueScanWorker(
  2301. PVOID Context,
  2302. UINT Notification,
  2303. UINT_PTR Param1,
  2304. UINT_PTR Param2
  2305. )
  2306. /*++
  2307. Routine Description:
  2308. The callback routine for the file queue scan. Takes each
  2309. node and copies the relevant information to Nls file copy
  2310. list and caches the directory names in Nls directory map.
  2311. Arguments:
  2312. Context - FileListCreatorContext in disguise.
  2313. Notification - Type of notification.
  2314. Param1 & Param2 - Polymorphic arguments based on type of
  2315. notification.
  2316. Return Value:
  2317. 0 to continue the scan or 1 to stop the scan.
  2318. --*/
  2319. {
  2320. UINT Result = 0; // continue on
  2321. // cout << "Scanning (" << std::hex << Notification << ")" << endl;
  2322. if (Notification == SPFILENOTIFY_QUEUESCAN_EX) {
  2323. FileListCreatorContext<T> &fl = *(FileListCreatorContext<T> *)Context;
  2324. std::basic_string<T> SrcFileName, DestFileName, SrcFileKey, DestFileKey;
  2325. T TargetFileNameBuffer[MAX_PATH];
  2326. bool ProcessEntry = false;
  2327. if (sizeof(T) == sizeof(CHAR)) {
  2328. PFILEPATHS_A FileNodeInfo = (PFILEPATHS_A)Param1;
  2329. if (FileNodeInfo) {
  2330. SrcFileName = std::basic_string<T>((const T*)FileNodeInfo->Source);
  2331. DestFileName = std::basic_string<T>((const T*)FileNodeInfo->Target);
  2332. _strlwr((PSTR)SrcFileName.c_str());
  2333. _strlwr((PSTR)DestFileName.c_str());
  2334. basic_string<T>::size_type SlashPos = SrcFileName.rfind((T)'\\');
  2335. if (SlashPos != SrcFileName.npos) {
  2336. SrcFileKey = SrcFileName.substr(SlashPos + 1);
  2337. SlashPos = DestFileName.rfind((T)L'\\');
  2338. if (SlashPos != DestFileName.npos) {
  2339. DestFileKey = DestFileName.substr(SlashPos + 1);
  2340. DestFileName[fl.WindowsDirectory.length()] = 0;
  2341. if (_stricmp((PCSTR)DestFileName.c_str(), (PCSTR)fl.WindowsDirectory.c_str()) == 0) {
  2342. (VOID)StringCchCopyA((PSTR)TargetFileNameBuffer,
  2343. ARRAY_SIZE(TargetFileNameBuffer),
  2344. (PCSTR)fl.Args.DestinationDirectory.c_str());
  2345. (VOID)StringCchCatA((PSTR)TargetFileNameBuffer,
  2346. ARRAY_SIZE(TargetFileNameBuffer),
  2347. ((PCSTR)(FileNodeInfo->Target)) +
  2348. fl.WindowsDirectory.length());
  2349. DestFileName = (const T *)TargetFileNameBuffer;
  2350. ProcessEntry = true;
  2351. }
  2352. }
  2353. }
  2354. }
  2355. } else {
  2356. PFILEPATHS_W FileNodeInfo = (PFILEPATHS_W)Param1;
  2357. if (FileNodeInfo) {
  2358. SrcFileName = std::basic_string<T>((const T*)FileNodeInfo->Source);
  2359. DestFileName = std::basic_string<T>((const T*)FileNodeInfo->Target);
  2360. _wcslwr((PWSTR)SrcFileName.c_str());
  2361. _wcslwr((PWSTR)DestFileName.c_str());
  2362. basic_string<T>::size_type SlashPos = SrcFileName.rfind((T)L'\\');
  2363. if (SlashPos != SrcFileName.npos) {
  2364. SrcFileKey = SrcFileName.substr(SlashPos + 1);
  2365. SlashPos = DestFileName.rfind((T)L'\\');
  2366. if (SlashPos != DestFileName.npos) {
  2367. DestFileKey = DestFileName.substr(SlashPos + 1);
  2368. DestFileName[fl.WindowsDirectory.length()] = 0;
  2369. if (_wcsicmp((PCWSTR)DestFileName.c_str(), (PCWSTR)fl.WindowsDirectory.c_str()) == 0) {
  2370. (VOID)StringCchCopyW((PWSTR)TargetFileNameBuffer,
  2371. ARRAY_SIZE(TargetFileNameBuffer),
  2372. (PCWSTR)fl.Args.DestinationDirectory.c_str());
  2373. (VOID)StringCchCatW((PWSTR)TargetFileNameBuffer,
  2374. ARRAY_SIZE(TargetFileNameBuffer),
  2375. ((PCWSTR)(FileNodeInfo->Target)) +
  2376. fl.WindowsDirectory.length());
  2377. DestFileName = (const T *)TargetFileNameBuffer;
  2378. ProcessEntry = true;
  2379. }
  2380. }
  2381. }
  2382. }
  2383. }
  2384. if (ProcessEntry) {
  2385. bool SkipFileEntry = false;
  2386. if (fl.CurrentSection && fl.Args.IA64Image) {
  2387. SectionValues<T> *Values = NULL;
  2388. try {
  2389. Values = &(fl.CurrentSection->GetValue(SrcFileKey));
  2390. }
  2391. catch(...) {
  2392. }
  2393. if (Values) {
  2394. SkipFileEntry = IsWow64File(*Values, fl);
  2395. }
  2396. if (!SkipFileEntry) {
  2397. if (sizeof(T) == sizeof(CHAR)) {
  2398. SkipFileEntry = ( 0 == _stricmp((PCSTR)SrcFileKey.c_str() + 1, (PCSTR)DestFileKey.c_str())) &&
  2399. (((T)SrcFileKey[0] == (T)'w') || ((T)SrcFileKey[0] == (T)'W'));
  2400. } else {
  2401. SkipFileEntry = ( 0 == _wcsicmp((PCWSTR)SrcFileKey.c_str() + 1, (PCWSTR)DestFileKey.c_str())) &&
  2402. (((T)SrcFileKey[0] == (T)L'w') || ((T)SrcFileKey[0] == (T)L'W'));
  2403. }
  2404. }
  2405. }
  2406. if (!SkipFileEntry) {
  2407. if (fl.Args.IA64Image) {
  2408. basic_string<T>::size_type PlatDirPos = SrcFileName.find(X86_PLATFORM_DIR.c_str());
  2409. if (PlatDirPos != SrcFileName.npos) {
  2410. basic_string<T> NewSrcFileName = SrcFileName.substr(0, PlatDirPos);
  2411. NewSrcFileName += IA64_PLATFORM_DIR;
  2412. NewSrcFileName += SrcFileName.substr(PlatDirPos + X86_PLATFORM_DIR.length());
  2413. // std::cout << "Remapping " << SrcFileName << "->" << NewSrcFileName << std::endl;
  2414. SrcFileName = NewSrcFileName;
  2415. }
  2416. }
  2417. fl.NlsFileMap[SrcFileName] = DestFileName;
  2418. fl.AddDirectoryToNlsDirMap(DestFileName);
  2419. } else {
  2420. // std::cout << "Skipping " << SrcFileName << " WOW64 file" << std::endl;
  2421. }
  2422. }
  2423. }
  2424. return Result;
  2425. }