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.

2152 lines
50 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. inf.c
  5. Abstract:
  6. Provides wrappers for commonly used INF file handling routines. The wrappers provide, amount
  7. other things, easy memory allocation using a user supplied GROWBUFFER or POOLHANDLE
  8. Author:
  9. 09-Jul-1997 Marc R. Whitten (marcw) - File creation.
  10. Revision History:
  11. 07-Feb-2001 ovidiut Revised the replace/append capability.
  12. 22-Oct-1998 marcw Added capability to replace/append inf files.
  13. 08-Oct-1997 jimschm OEM version of SetupGetStringField
  14. --*/
  15. #include "pch.h"
  16. #define INF_REPLACE 1
  17. #define INF_APPEND 2
  18. #define S_VERSION_A "Version"
  19. #define S_TARGETINF_A "TargetInf_2"
  20. #define S_VERSION_W L"Version"
  21. #define S_LANGUAGE_W L"Language"
  22. #define S_STRINGS_W L"Strings"
  23. #define S_INFDIR_A "inf"
  24. #define S_TAG_A "Tag"
  25. #define INF_INVALID_VERSION 0xffff
  26. #define INF_ANY_LANGUAGE 0
  27. #define ASSERT_VALID_INF(handle) MYASSERT((handle) != INVALID_HANDLE_VALUE && (handle) != NULL)
  28. UINT pGetLanguage (IN PCSTR File);
  29. typedef struct _tagINFMOD {
  30. struct _tagINFMOD *Next;
  31. PCSTR TargetInf;
  32. DWORD Language;
  33. DWORD Version;
  34. PCSTR Tag;
  35. BOOL ReplacementFile;
  36. PCSTR PatchInf;
  37. } INFMOD, *PINFMOD;
  38. PINFMOD g_RootInfMod;
  39. POOLHANDLE g_InfModPool;
  40. VOID
  41. InfGlobalInit (
  42. IN BOOL Terminate
  43. )
  44. {
  45. if (!Terminate) {
  46. g_InfModPool = PoolMemInitNamedPool ("INF Modifications");
  47. } else {
  48. MYASSERT(g_InfModPool);
  49. PoolMemDestroyPool (g_InfModPool);
  50. g_RootInfMod = NULL;
  51. }
  52. }
  53. /*++
  54. Routine Description:
  55. pAllocateSpace is a private function that allocates space using the user specified allocator.
  56. Arguments:
  57. Context - A valid INFSTRUCT which has been initialized either by a call to InitInfStruct or
  58. by using one of the static initializers (INITINFSTRUCT_GROWBUFFER or
  59. INITINFSTRUCT_POOLHANDLE)
  60. Size - The size (in bytes) to allocate.
  61. Return Value:
  62. A pointer to the successfully allocated memory or NULL if no memory could be allocated.
  63. --*/
  64. PBYTE
  65. pAllocateSpaceA (
  66. IN PINFSTRUCTA Context,
  67. IN UINT Size
  68. )
  69. {
  70. PBYTE rBytes = NULL;
  71. switch (Context -> Allocator) {
  72. case INF_USE_POOLHANDLE:
  73. MYASSERT(Context);
  74. MYASSERT(Size);
  75. //
  76. // Allocate space using Poolmem.
  77. //
  78. rBytes = PoolMemGetMemory(Context -> PoolHandle, Size);
  79. break;
  80. case INF_USE_GROWBUFFER:
  81. case INF_USE_PRIVATE_GROWBUFFER:
  82. MYASSERT(Context);
  83. MYASSERT(Size);
  84. //
  85. // Allocate space using Growbuf.
  86. //
  87. Context->GrowBuffer.End = 0;
  88. rBytes = GrowBuffer(&(Context -> GrowBuffer), Size);
  89. break;
  90. case INF_USE_PRIVATE_POOLHANDLE:
  91. MYASSERT(Context);
  92. MYASSERT(Size);
  93. //
  94. // Allocate space using private growbuffer.
  95. //
  96. if (!Context -> PoolHandle) {
  97. Context -> PoolHandle = PoolMemInitNamedPool ("INF Pool");
  98. }
  99. if (Context -> PoolHandle) {
  100. rBytes = PoolMemGetMemory(Context -> PoolHandle, Size);
  101. }
  102. break;
  103. default:
  104. return NULL;
  105. }
  106. MYASSERT(rBytes);
  107. return rBytes;
  108. }
  109. PBYTE
  110. pAllocateSpaceW (
  111. IN PINFSTRUCTW Context,
  112. IN UINT Size
  113. )
  114. {
  115. PBYTE rBytes = NULL;
  116. switch (Context -> Allocator) {
  117. case INF_USE_POOLHANDLE:
  118. MYASSERT(Context);
  119. MYASSERT(Size);
  120. //
  121. // Allocate space using Poolmem.
  122. //
  123. rBytes = PoolMemGetMemory(Context -> PoolHandle, Size);
  124. break;
  125. case INF_USE_GROWBUFFER:
  126. case INF_USE_PRIVATE_GROWBUFFER:
  127. MYASSERT(Context);
  128. MYASSERT(Size);
  129. //
  130. // Allocate space using Growbuf.
  131. //
  132. Context->GrowBuffer.End = 0;
  133. rBytes = GrowBuffer(&(Context -> GrowBuffer), Size);
  134. break;
  135. case INF_USE_PRIVATE_POOLHANDLE:
  136. MYASSERT(Context);
  137. MYASSERT(Size);
  138. //
  139. // Allocate space using private growbuffer.
  140. //
  141. if (!Context -> PoolHandle) {
  142. Context -> PoolHandle = PoolMemInitNamedPool ("INF Pool");
  143. }
  144. if (Context -> PoolHandle) {
  145. rBytes = PoolMemGetMemory(Context -> PoolHandle, Size);
  146. }
  147. break;
  148. default:
  149. return NULL;
  150. }
  151. MYASSERT(rBytes);
  152. return rBytes;
  153. }
  154. /*++
  155. Routine Description:
  156. This function initializes an INFSTRUCT with the user supplied allocator. It is used when
  157. user of the INF wrapper routines wishes to manage his own memory (i.e. such as when he
  158. already has a suitable allocator with sufficient scope created, etc.)
  159. There is no need to call this function if the user wishes to have the INF wrapper routines
  160. manage there own memory. Initialize your Init structure with one of either
  161. INITINFSTRUCT_POOLMEM or INITINFSTRUCT_GROWBUFFER, depending on your preference and needs
  162. for an allocator.
  163. Arguments:
  164. Context - Recieves the initialized INFSTRUCT.
  165. GrowBuffer - An optional parameter containing a user supplied and initialized GROWBUFFER.
  166. If this parameter is non-NULL, then PoolHandle should be NULL.
  167. PoolHandle - An optional parameter containing a user supplied and initialized POOLHANDLE.
  168. If this parameter is non-NULL, then GrowBuffer should be NULL.
  169. One of either GrowBuffer or PoolHandle *must* be specified.
  170. Return Value:
  171. None.
  172. --*/
  173. VOID
  174. InitInfStructA (
  175. OUT PINFSTRUCTA Context,
  176. IN PGROWBUFFER GrowBuffer, OPTIONAL
  177. IN POOLHANDLE PoolHandle OPTIONAL
  178. )
  179. {
  180. if(!Context){
  181. MYASSERT(Context);
  182. return;
  183. }
  184. ZeroMemory(Context,sizeof(INFSTRUCTA));
  185. if (!PoolHandle && !GrowBuffer) {
  186. Context -> Allocator = INF_USE_PRIVATE_POOLHANDLE;
  187. }
  188. if (PoolHandle) {
  189. Context -> PoolHandle = PoolHandle;
  190. Context -> Allocator = INF_USE_POOLHANDLE;
  191. }
  192. if (GrowBuffer) {
  193. Context -> GrowBuffer = *GrowBuffer;
  194. Context -> Allocator = INF_USE_GROWBUFFER;
  195. }
  196. }
  197. VOID
  198. InitInfStructW (
  199. OUT PINFSTRUCTW Context,
  200. IN PGROWBUFFER GrowBuffer, OPTIONAL
  201. IN POOLHANDLE PoolHandle OPTIONAL
  202. )
  203. {
  204. if(!Context){
  205. MYASSERT(Context);
  206. return;
  207. }
  208. ZeroMemory(Context,sizeof(INFSTRUCTW));
  209. if (!PoolHandle && !GrowBuffer) {
  210. Context -> Allocator = INF_USE_PRIVATE_POOLHANDLE;
  211. }
  212. if (PoolHandle) {
  213. Context -> PoolHandle = PoolHandle;
  214. Context -> Allocator = INF_USE_POOLHANDLE;
  215. }
  216. if (GrowBuffer) {
  217. Context -> GrowBuffer = *GrowBuffer;
  218. Context -> Allocator = INF_USE_GROWBUFFER;
  219. }
  220. }
  221. /*++
  222. Routine Description:
  223. InfCleanupInfStruct is responsible for cleaning up the data associated
  224. with an INFSTRUCT. This is a mandatory call, unless the INFSTRUCT
  225. was initialized with InitInfStruct, called with a non-NULL grow buffer or
  226. pool handle.
  227. This routine can be called no matter how the INFSTRUCT was initialized.
  228. However, it will NOT free caller-owned grow buffers or pools.
  229. Arguments:
  230. Context - Receives the properly cleaned up INFSTRUCT, ready to be
  231. reused.
  232. Return Value:
  233. none
  234. --*/
  235. VOID
  236. InfCleanUpInfStructA (
  237. IN OUT PINFSTRUCTA Context
  238. )
  239. {
  240. if(!Context){
  241. MYASSERT(Context);
  242. return;
  243. }
  244. if (Context -> Allocator == INF_USE_PRIVATE_GROWBUFFER) {
  245. FreeGrowBuffer (&(Context -> GrowBuffer));
  246. }
  247. else if (Context -> Allocator == INF_USE_PRIVATE_POOLHANDLE && Context -> PoolHandle) {
  248. PoolMemDestroyPool(Context -> PoolHandle);
  249. }
  250. InitInfStructA (Context, NULL, NULL);
  251. }
  252. VOID
  253. InfCleanUpInfStructW (
  254. IN OUT PINFSTRUCTW Context
  255. )
  256. {
  257. if(!Context){
  258. MYASSERT(Context);
  259. return;
  260. }
  261. if (Context -> Allocator == INF_USE_PRIVATE_GROWBUFFER) {
  262. FreeGrowBuffer (&(Context -> GrowBuffer));
  263. }
  264. else if (Context -> Allocator == INF_USE_PRIVATE_POOLHANDLE && Context -> PoolHandle) {
  265. PoolMemDestroyPool(Context -> PoolHandle);
  266. }
  267. InitInfStructW (Context, NULL, NULL);
  268. }
  269. /*++
  270. Routine Description:
  271. InfResetInfStruct resets the pool so memory can be recycled. The intent is
  272. to allow a caller to reset the INFSTRUCT in order to release the memory
  273. obtained from getting INF fields. This is useful in a loop of InfFindFirstLine/
  274. InfFindNextLine, where two or more fields are processed for each line.
  275. If only one field is processed in an InfFindFirstLine/InfFindNextLine loop,
  276. a grow buffer should be used instead.
  277. This routine empties the active pool block, a block that is 8K by default. If
  278. more than the block size has been allocated, other memory blocks besides the
  279. active block will exist. Because only the active block is reset, the pool will
  280. grow.
  281. If the caller expects more than the block size during one iteration, it should call
  282. InfCleanupInfStruct to free the pool completely.
  283. Arguments:
  284. Context - Specifies the struct to reset
  285. Return Value:
  286. none
  287. --*/
  288. VOID
  289. InfResetInfStructA (
  290. IN OUT PINFSTRUCTA Context
  291. )
  292. {
  293. if(!Context){
  294. MYASSERT(Context);
  295. return;
  296. }
  297. switch (Context -> Allocator) {
  298. case INF_USE_POOLHANDLE:
  299. case INF_USE_PRIVATE_POOLHANDLE:
  300. if (Context->PoolHandle) {
  301. PoolMemEmptyPool (Context->PoolHandle);
  302. }
  303. break;
  304. }
  305. }
  306. VOID
  307. InfResetInfStructW (
  308. IN OUT PINFSTRUCTW Context
  309. )
  310. {
  311. if(!Context){
  312. MYASSERT(Context);
  313. return;
  314. }
  315. switch (Context -> Allocator) {
  316. case INF_USE_POOLHANDLE:
  317. case INF_USE_PRIVATE_POOLHANDLE:
  318. if (Context->PoolHandle) {
  319. PoolMemEmptyPool (Context->PoolHandle);
  320. }
  321. break;
  322. }
  323. }
  324. VOID
  325. pDeleteNode (
  326. IN PINFMOD Node
  327. )
  328. {
  329. if (Node) {
  330. if (Node->TargetInf) {
  331. PoolMemReleaseMemory (g_InfModPool, (PVOID)Node->TargetInf);
  332. }
  333. if (Node->Tag) {
  334. PoolMemReleaseMemory (g_InfModPool, (PVOID)Node->Tag);
  335. }
  336. if (Node->PatchInf) {
  337. PoolMemReleaseMemory (g_InfModPool, (PVOID)Node->PatchInf);
  338. }
  339. PoolMemReleaseMemory (g_InfModPool, Node);
  340. }
  341. }
  342. PINFMOD
  343. pCreateInfMod (
  344. IN PCSTR TargetInf,
  345. IN DWORD Language,
  346. IN DWORD Version,
  347. IN PCSTR Tag, OPTIONAL
  348. IN BOOL ReplacementFile,
  349. IN PCSTR PatchInf
  350. )
  351. {
  352. PINFMOD node;
  353. node = (PINFMOD) PoolMemGetAlignedMemory (g_InfModPool, sizeof (INFMOD));
  354. if (node) {
  355. node->Next = NULL;
  356. node->TargetInf = PoolMemDuplicateString (g_InfModPool, TargetInf);
  357. node->Language = Language;
  358. node->Version = Version;
  359. node->Tag = Tag ? PoolMemDuplicateString (g_InfModPool, Tag) : NULL;
  360. node->ReplacementFile = ReplacementFile;
  361. node->PatchInf = PoolMemDuplicateString (g_InfModPool, PatchInf);
  362. }
  363. return node;
  364. }
  365. BOOL
  366. pAddReplacementInfToTable (
  367. IN PSTR InfToPatch,
  368. IN UINT Version,
  369. IN UINT Language,
  370. IN PCSTR Tag, OPTIONAL
  371. IN DWORD Operation,
  372. IN PCSTR PatchInf
  373. )
  374. {
  375. PINFMOD node;
  376. node = pCreateInfMod (InfToPatch, Language, Version, Tag, Operation & INF_REPLACE, PatchInf);
  377. if (!node) {
  378. return FALSE;
  379. }
  380. node->Next = g_RootInfMod;
  381. g_RootInfMod = node;
  382. return TRUE;
  383. }
  384. BOOL
  385. pGetInfModificationList (
  386. IN PCSTR TargetInf,
  387. IN UINT TargetLanguage,
  388. IN UINT TargetVersion,
  389. IN PCSTR Tag, OPTIONAL
  390. OUT PCSTR* TargetReplacementFile, OPTIONAL
  391. OUT PGROWBUFFER TargetAppendList OPTIONAL
  392. )
  393. {
  394. PINFMOD node;
  395. UINT version;
  396. PCSTR patchInf;
  397. BOOL b = FALSE;
  398. if (TargetReplacementFile) {
  399. *TargetReplacementFile = NULL;
  400. }
  401. if (TargetAppendList) {
  402. TargetAppendList->End = 0;
  403. }
  404. if (TargetVersion == INF_INVALID_VERSION) {
  405. return FALSE;
  406. }
  407. version = TargetVersion;
  408. patchInf = NULL;
  409. for (node = g_RootInfMod; node; node = node->Next) {
  410. if (node->Version > version &&
  411. (node->Language == TargetLanguage || node->Language == INF_ANY_LANGUAGE) &&
  412. (!Tag || !node->Tag || StringIMatchA (node->Tag, Tag)) &&
  413. StringIMatchA (node->TargetInf, TargetInf)
  414. ) {
  415. if (node->ReplacementFile) {
  416. //
  417. // rev the version#; new minimum version will be that of the replacement file
  418. //
  419. version = node->Version;
  420. patchInf = node->PatchInf;
  421. b = TRUE;
  422. }
  423. }
  424. }
  425. if (TargetReplacementFile) {
  426. *TargetReplacementFile = patchInf;
  427. }
  428. //
  429. // for append nodes, add to the list only those that have a higher version than the
  430. // target or the replacement file
  431. //
  432. for (node = g_RootInfMod; node; node = node->Next) {
  433. if (node->Version > version &&
  434. (node->Language == TargetLanguage || node->Language == INF_ANY_LANGUAGE) &&
  435. (!Tag || !node->Tag || StringIMatchA (node->Tag, Tag)) &&
  436. StringIMatchA (node->TargetInf, TargetInf) &&
  437. !node->ReplacementFile
  438. ) {
  439. if (TargetAppendList) {
  440. MultiSzAppendA (TargetAppendList, node->PatchInf);
  441. }
  442. b = TRUE;
  443. }
  444. }
  445. if (TargetAppendList && TargetAppendList->End) {
  446. MultiSzAppendA (TargetAppendList, "");
  447. }
  448. return b;
  449. }
  450. VOID
  451. pDestroyInfModList (
  452. IN PINFMOD List
  453. )
  454. {
  455. PINFMOD node, next;
  456. node = List;
  457. while (node) {
  458. next = node->Next;
  459. pDeleteNode (node);
  460. node = next;
  461. }
  462. }
  463. /*++
  464. Routine Description:
  465. InfOpenInfFileA and InfOpenInfFileW are wrappers for the SetupOpenInfFile function.
  466. They cut down the number of parameters necessary to open an INF file by supplying
  467. the most common options for non-user specified parameters.
  468. A call to one of these functions is equivelant to
  469. SetupOpenInfFile(<FileName>,NULL,INF_STYLE_WIN4,NULL)
  470. Arguments:
  471. FileName - Contains the name of the INF file to open. See the help for SetupOpenInfFile
  472. for special details concerning this parameter.
  473. Return Value:
  474. If the INF file is successfully opened, a valid HINF is returned, otherwise,
  475. INVALID_HANDLE_VALUE is returned. See the documentation for SetupOpenInfFile for more
  476. details.
  477. --*/
  478. HINF
  479. RealInfOpenInfFileA (
  480. IN PCSTR FileSpec /*,*/
  481. ALLOCATION_TRACKING_DEF
  482. )
  483. {
  484. PCSTR p;
  485. HINF rInf;
  486. UINT language;
  487. GROWBUFFER AppendList = GROWBUF_INIT;
  488. MULTISZ_ENUM e;
  489. UINT version;
  490. PCSTR replacementFile;
  491. CHAR windir[MAX_MBCHAR_PATH];
  492. CHAR buf[MAX_MBCHAR_PATH];
  493. PCSTR tag;
  494. PCSTR fullPath = NULL;
  495. if(!FileSpec){
  496. MYASSERT(FileSpec);
  497. return INVALID_HANDLE_VALUE;
  498. }
  499. //
  500. // if FileSpec is incomplete, make the full path first
  501. //
  502. if (!_mbschr (FileSpec, '\\')) {
  503. if (GetWindowsDirectoryA (windir, MAX_MBCHAR_PATH)) {
  504. WIN32_FIND_DATAA fd;
  505. p = JoinPathsA (windir, S_INFDIR_A);
  506. fullPath = JoinPathsA (p, FileSpec);
  507. FreePathStringA (p);
  508. if (!DoesFileExistExA (fullPath, &fd) ||
  509. (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  510. ) {
  511. FreePathStringA (fullPath);
  512. if (GetSystemDirectoryA (windir, MAX_MBCHAR_PATH)) {
  513. fullPath = JoinPathsA (windir, FileSpec);
  514. if (!DoesFileExistExA (fullPath, &fd) ||
  515. (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  516. ) {
  517. FreePathStringA (fullPath);
  518. fullPath = NULL;
  519. }
  520. }
  521. }
  522. }
  523. if (fullPath) {
  524. FileSpec = fullPath;
  525. }
  526. }
  527. //
  528. // gather info we'll need to determine if there are infs to replace/append
  529. // this inf.
  530. //
  531. p = GetFileNameFromPathA (FileSpec);
  532. language = pGetLanguage (FileSpec);
  533. version = GetPrivateProfileIntA (
  534. S_VERSION_A,
  535. S_VERSION_A,
  536. INF_INVALID_VERSION,
  537. FileSpec
  538. );
  539. if (GetPrivateProfileStringA (
  540. S_VERSION_A,
  541. S_TAG_A,
  542. TEXT(""),
  543. buf,
  544. MAX_MBCHAR_PATH,
  545. FileSpec
  546. )) {
  547. tag = buf;
  548. } else {
  549. tag = NULL;
  550. }
  551. if (!pGetInfModificationList (p, language, version, tag, &replacementFile, &AppendList)) {
  552. replacementFile = FileSpec;
  553. } else {
  554. if (replacementFile) {
  555. LOGA ((LOG_INFORMATION, "Using replacement file %s for %s", replacementFile, FileSpec));
  556. } else {
  557. replacementFile = FileSpec;
  558. }
  559. }
  560. //
  561. // Open the main inf.
  562. //
  563. rInf = SetupOpenInfFileA (
  564. replacementFile,
  565. NULL,
  566. INF_STYLE_WIN4 | INF_STYLE_OLDNT,
  567. NULL
  568. );
  569. //
  570. // Append language and non-language-specific .add files.
  571. //
  572. if (rInf != INVALID_HANDLE_VALUE) {
  573. if (EnumFirstMultiSzA (&e, (PCSTR) AppendList.Buf)) {
  574. do {
  575. if (!SetupOpenAppendInfFileA (e.CurrentString, rInf, NULL)) {
  576. DEBUGMSGA ((
  577. DBG_ERROR,
  578. "Unable to append %s to %s.",
  579. e.CurrentString,
  580. FileSpec
  581. ));
  582. } else {
  583. LOGA ((LOG_INFORMATION, "Using append file %s for %s", e.CurrentString, FileSpec));
  584. }
  585. } while (EnumNextMultiSzA (&e));
  586. }
  587. }
  588. FreeGrowBuffer (&AppendList);
  589. if (rInf != INVALID_HANDLE_VALUE) {
  590. DebugRegisterAllocation (INF_HANDLE, (PVOID) rInf, File, Line);
  591. }
  592. if (fullPath) {
  593. FreePathStringA (fullPath);
  594. }
  595. return rInf;
  596. }
  597. HINF
  598. RealInfOpenInfFileW (
  599. IN PCWSTR FileSpec /*,*/
  600. ALLOCATION_TRACKING_DEF
  601. )
  602. {
  603. PCSTR AnsiFileSpec;
  604. HINF rInf;
  605. if(!FileSpec){
  606. MYASSERT(FileSpec);
  607. return INVALID_HANDLE_VALUE;
  608. }
  609. AnsiFileSpec = ConvertWtoA (FileSpec);
  610. MYASSERT (AnsiFileSpec);
  611. rInf = InfOpenInfFileA (AnsiFileSpec);
  612. FreeConvertedStr (AnsiFileSpec);
  613. return rInf;
  614. }
  615. VOID
  616. InfCloseInfFile (
  617. HINF Inf
  618. )
  619. {
  620. ASSERT_VALID_INF(Inf);
  621. DebugUnregisterAllocation (INF_HANDLE, Inf);
  622. SetupCloseInfFile (Inf);
  623. }
  624. /*++
  625. Routine Description:
  626. InfOpenInfInAllSourcesA and InfOpenInfInAllSourcesW are special inf open routines that
  627. are capable of opening multiple versions of the same inf file that may be spread out across
  628. installation directories. The first INF file found will be opened with a call to
  629. SetupOpenInfFile. Additional files will be opened with SetupOpenAppendInfFile.
  630. Arguments:
  631. InfSpecifier - Contains the source directory indepent portion of the path to a particular inf file.
  632. For files located in the root of the source directory, this will simply be the name
  633. of the file. For files located in a sub-directory of the source directory, this will
  634. be a partial path.
  635. SourceCount - Contains the number of source directories
  636. SourceDirectories - Contains an array of all the source directories.
  637. Return Value:
  638. If any INF file is successfully opened, a valid HINF is returned, otherwise,
  639. INVALID_HANDLE_VALUE is returned. See the documentation for SetupOpenInfFile for more
  640. details.
  641. --*/
  642. HINF
  643. InfOpenInfInAllSourcesA (
  644. IN PCSTR InfSpecifier,
  645. IN DWORD SourceCount,
  646. IN PCSTR * SourceDirectories
  647. )
  648. {
  649. DWORD index;
  650. HINF rInf = INVALID_HANDLE_VALUE;
  651. PSTR curPath;
  652. MYASSERT(InfSpecifier && SourceDirectories);
  653. //
  654. // Open all available inf files in the source directories.
  655. //
  656. for (index = 0;index < SourceCount; index++) {
  657. //
  658. // Create a path to the INF in the current source directory.
  659. //
  660. curPath = JoinPathsA(SourceDirectories[index],InfSpecifier);
  661. //
  662. // See if the INF file exists there...
  663. //
  664. if (DoesFileExistA (curPath)) {
  665. //
  666. // Open the INF file.
  667. //
  668. rInf = InfOpenInfFileA(curPath);
  669. if (rInf == INVALID_HANDLE_VALUE) {
  670. LOGA ((LOG_ERROR, "Error opening INF %s.", curPath));
  671. }
  672. }
  673. //
  674. // Free this string.
  675. //
  676. FreePathStringA(curPath);
  677. if (rInf != INVALID_HANDLE_VALUE) {
  678. //
  679. // done
  680. //
  681. break;
  682. }
  683. }
  684. return rInf;
  685. }
  686. HINF
  687. InfOpenInfInAllSourcesW (
  688. IN PCWSTR InfSpecifier,
  689. IN DWORD SourceCount,
  690. IN PCWSTR *SourceDirectories
  691. )
  692. {
  693. DWORD index;
  694. HINF rInf = INVALID_HANDLE_VALUE;
  695. PWSTR curPath;
  696. MYASSERT(InfSpecifier && SourceDirectories);
  697. //
  698. // Open all available inf files in the source directories.
  699. //
  700. for (index = 0;index < SourceCount; index++) {
  701. //
  702. // Create a path to the INF in the current source directory.
  703. //
  704. curPath = JoinPathsW(SourceDirectories[index],InfSpecifier);
  705. //
  706. // See if the INF file exists there...
  707. //
  708. if (DoesFileExistW (curPath)) {
  709. //
  710. // Open the INF file.
  711. //
  712. rInf = InfOpenInfFileW(curPath);
  713. if (rInf == INVALID_HANDLE_VALUE) {
  714. LOGW ((LOG_ERROR, "OpenInfInAllSources: Error opening INF %s.", curPath));
  715. }
  716. }
  717. //
  718. // Free this string.
  719. //
  720. FreePathStringW(curPath);
  721. if (rInf != INVALID_HANDLE_VALUE) {
  722. //
  723. // done
  724. //
  725. break;
  726. }
  727. }
  728. return rInf;
  729. }
  730. /*++
  731. Routine Description:
  732. InfGetLineTextA and InfGetLineTextW are wrappers for the SetupGetLineText function.
  733. They both reduce the number of parameters required to get the line text and
  734. take care of allocating and filling a buffer with the data returned from the API.
  735. Arguments:
  736. Context - A valid InfStruct. The INFCONTEXT member of the structure must point to a valid
  737. line to retrieve (i.e. through the use of InfFindFirstLine/InfFindNextLine.
  738. Return Value:
  739. A pointer to the allocated line or NULL if there was an error. Consult GetLastError() for
  740. extended error information.
  741. --*/
  742. PSTR
  743. InfGetLineTextA (
  744. IN OUT PINFSTRUCTA Context
  745. )
  746. {
  747. PSTR rLine = NULL;
  748. UINT requiredSize;
  749. if(!Context){
  750. MYASSERT(Context);
  751. return NULL;
  752. }
  753. //
  754. // Get the size necessary for holding the field.
  755. //
  756. if (SetupGetLineTextA(
  757. &(Context -> Context),
  758. NULL,
  759. NULL,
  760. NULL,
  761. NULL,
  762. 0,
  763. &requiredSize
  764. )) {
  765. //
  766. // Create a string big enough.
  767. //
  768. rLine = (PSTR) pAllocateSpaceA(Context,requiredSize);
  769. if (rLine) {
  770. //
  771. // Get the field.
  772. //
  773. if (!SetupGetLineTextA(
  774. &(Context -> Context),
  775. NULL,
  776. NULL,
  777. NULL,
  778. rLine,
  779. requiredSize,
  780. NULL
  781. )) {
  782. //
  783. // If we did not successfully get the field, reset the string to NULL.
  784. //
  785. DEBUGMSGA((DBG_ERROR,"InfGetLineTextA: Error retrieving field from INF file."));
  786. rLine = NULL;
  787. }
  788. }
  789. }
  790. return rLine;
  791. }
  792. PWSTR
  793. InfGetLineTextW (
  794. IN OUT PINFSTRUCTW Context
  795. )
  796. {
  797. PWSTR rLine = NULL;
  798. UINT requiredSize;
  799. if(!Context){
  800. MYASSERT(Context);
  801. return NULL;
  802. }
  803. //
  804. // Get the size necessary for holding the field.
  805. //
  806. if (SetupGetLineTextW(
  807. &(Context -> Context),
  808. NULL,
  809. NULL,
  810. NULL,
  811. NULL,
  812. 0,
  813. &requiredSize
  814. )) {
  815. //
  816. // Create a string big enough.
  817. //
  818. rLine = (PWSTR) pAllocateSpaceW(Context,requiredSize*sizeof(WCHAR));
  819. if (rLine) {
  820. //
  821. // Get the field.
  822. //
  823. if (!SetupGetLineTextW(
  824. &(Context -> Context),
  825. NULL,
  826. NULL,
  827. NULL,
  828. rLine,
  829. requiredSize,
  830. NULL
  831. )) {
  832. //
  833. // If we did not successfully get the field, reset the string to NULL.
  834. //
  835. DEBUGMSGW((DBG_ERROR,"InfGetLineTextW: Error retrieving field from INF file."));
  836. rLine = NULL;
  837. }
  838. }
  839. }
  840. return rLine;
  841. }
  842. /*++
  843. Routine Description:
  844. InfGetMultiSzFieldA and InfGetMultiSzFieldW are wrappers for the SetupGetMultiSzField function.
  845. They both reduce the number of parameters required to get the line text and
  846. take care of allocating and filling a buffer with the data returned from the API.
  847. Arguments:
  848. Context - A valid InfStruct. The INFCONTEXT member of the structure must point to a valid
  849. line to retrieve (i.e. through the use of InfFindFirstLine/InfFindNextLine.
  850. FieldIndex - The index within the line to retrieve a string field.
  851. Return Value:
  852. A pointer to the allocated fields or NULL if there was an error. Consult GetLastError() for
  853. extended error information.
  854. --*/
  855. PSTR
  856. InfGetMultiSzFieldA (
  857. IN OUT PINFSTRUCTA Context,
  858. IN UINT FieldIndex
  859. )
  860. {
  861. UINT requiredSize;
  862. PSTR rFields = NULL;
  863. if(!Context){
  864. MYASSERT(Context);
  865. return NULL;
  866. }
  867. //
  868. // Get the size necessary for holding the field.
  869. //
  870. if (SetupGetMultiSzFieldA(
  871. &(Context -> Context),
  872. FieldIndex,
  873. NULL,
  874. 0,
  875. &requiredSize
  876. )) {
  877. //
  878. // Create a string big enough.
  879. //
  880. rFields = (PSTR) pAllocateSpaceA(Context,requiredSize);
  881. if (rFields) {
  882. //
  883. // Get the field.
  884. //
  885. if (!SetupGetMultiSzFieldA(
  886. &(Context -> Context),
  887. FieldIndex,
  888. rFields,
  889. requiredSize,
  890. NULL
  891. )) {
  892. //
  893. // If we did not successfully get the field, reset the string to NULL.
  894. //
  895. DEBUGMSGA((DBG_ERROR,"InfGetMultiSzFieldA: Error retrieving field from INF file."));
  896. rFields = NULL;
  897. }
  898. }
  899. }
  900. return rFields;
  901. }
  902. PWSTR
  903. InfGetMultiSzFieldW (
  904. IN OUT PINFSTRUCTW Context,
  905. IN UINT FieldIndex
  906. )
  907. {
  908. UINT requiredSize;
  909. PWSTR rFields = NULL;
  910. if(!Context){
  911. MYASSERT(Context);
  912. return NULL;
  913. }
  914. //
  915. // Get the size necessary for holding the field.
  916. //
  917. if (SetupGetMultiSzFieldW(
  918. &(Context -> Context),
  919. FieldIndex,
  920. NULL,
  921. 0,
  922. &requiredSize
  923. )) {
  924. //
  925. // Create a string big enough.
  926. //
  927. rFields = (PWSTR) pAllocateSpaceW(Context,requiredSize*sizeof(WCHAR));
  928. if (rFields) {
  929. //
  930. // Get the field.
  931. //
  932. if (!SetupGetMultiSzFieldW(
  933. &(Context -> Context),
  934. FieldIndex,
  935. rFields,
  936. requiredSize,
  937. NULL
  938. )) {
  939. //
  940. // If we did not successfully get the field, reset the string to NULL.
  941. //
  942. DEBUGMSGW((DBG_ERROR,"InfGetMultiSzFieldW: Error retrieving field from INF file."));
  943. rFields = NULL;
  944. }
  945. }
  946. }
  947. return rFields;
  948. }
  949. /*++
  950. Routine Description:
  951. InfGetStringFieldA and InfGetStringFieldW are wrappers for the SetupGetStringField function.
  952. They both reduce the number of parameters required to get the line text and
  953. take care of allocating and filling a buffer with the data returned from the API.
  954. Arguments:
  955. Context - A valid InfStruct. The INFCONTEXT member of the structure must point to a valid
  956. line to retrieve (i.e. through the use of InfFindFirstLine/InfFindNextLine.
  957. FieldIndex - The index within the line to retrieve a string field.
  958. Return Value:
  959. A pointer to the allocated line or NULL if there was an error. Consult GetLastError() for
  960. extended error information.
  961. --*/
  962. PSTR
  963. InfGetStringFieldA (
  964. IN OUT PINFSTRUCTA Context,
  965. IN UINT FieldIndex
  966. )
  967. {
  968. UINT requiredSize;
  969. PSTR rField = NULL;
  970. if(!Context){
  971. MYASSERT(Context);
  972. return NULL;
  973. }
  974. //
  975. // Get the size necessary for holding the field.
  976. //
  977. if (SetupGetStringFieldA(
  978. &(Context -> Context),
  979. FieldIndex,
  980. NULL,
  981. 0,
  982. &requiredSize
  983. )) {
  984. //
  985. // Create a string big enough.
  986. //
  987. rField = (PSTR) pAllocateSpaceA(Context,requiredSize);
  988. if (rField) {
  989. //
  990. // Get the field.
  991. //
  992. if (!SetupGetStringFieldA(
  993. &(Context -> Context),
  994. FieldIndex,
  995. rField,
  996. requiredSize,
  997. NULL
  998. )) {
  999. //
  1000. // If we did not successfully get the field, reset the string to NULL.
  1001. //
  1002. DEBUGMSGA((DBG_ERROR,"InfGetStringFieldA: Error retrieving field from INF file."));
  1003. rField = NULL;
  1004. }
  1005. }
  1006. }
  1007. return rField;
  1008. }
  1009. PWSTR
  1010. InfGetStringFieldW (
  1011. IN OUT PINFSTRUCTW Context,
  1012. IN UINT FieldIndex
  1013. )
  1014. {
  1015. UINT requiredSize;
  1016. PWSTR rField = NULL;
  1017. if(!Context){
  1018. MYASSERT(Context);
  1019. return NULL;
  1020. }
  1021. //
  1022. // Get the size necessary for holding the field.
  1023. //
  1024. if (SetupGetStringFieldW(
  1025. &(Context -> Context),
  1026. FieldIndex,
  1027. NULL,
  1028. 0,
  1029. &requiredSize
  1030. )) {
  1031. //
  1032. // Create a string big enough.
  1033. //
  1034. rField = (PWSTR) pAllocateSpaceW(Context,requiredSize*sizeof(WCHAR));
  1035. if (rField) {
  1036. //
  1037. // Get the field.
  1038. //
  1039. if (!SetupGetStringFieldW(
  1040. &(Context -> Context),
  1041. FieldIndex,
  1042. rField,
  1043. requiredSize,
  1044. NULL
  1045. )) {
  1046. //
  1047. // If we did not successfully get the field, reset the string to NULL.
  1048. //
  1049. DEBUGMSGW((DBG_ERROR,"InfGetStringFieldW: Error retrieving field from INF file."));
  1050. rField = NULL;
  1051. }
  1052. }
  1053. }
  1054. return rField;
  1055. }
  1056. /*++
  1057. Routine Description:
  1058. InfGetIntField is a wrapper for SetupGetIntField. It is virtually identical to this function
  1059. except that it takes care of getting the INFCONTEXT out of the INFSTRUCT structure.
  1060. Arguments:
  1061. Context - A valid InfStruct. The INFCONTEXT member of the structure must point to a valid
  1062. line to retrieve (i.e. through the use of InfFindFirstLine/InfFindNextLine.
  1063. FieldIndex - The index within the line from which to retrieve the field.
  1064. Value - Recieves the value of the requested Int field.
  1065. Return Value:
  1066. TRUE if the field was successfully retrieved, FALSE otherwise. Use GetLastError() To receive
  1067. extended error information.
  1068. --*/
  1069. BOOL
  1070. InfGetIntFieldA (
  1071. IN PINFSTRUCTA Context,
  1072. IN UINT FieldIndex,
  1073. IN PINT Value
  1074. )
  1075. {
  1076. if(!Context){
  1077. MYASSERT(Context);
  1078. return FALSE;
  1079. }
  1080. return SetupGetIntField (&(Context -> Context), FieldIndex, Value);
  1081. }
  1082. BOOL
  1083. InfGetIntFieldW (
  1084. IN PINFSTRUCTW Context,
  1085. IN UINT FieldIndex,
  1086. IN PINT Value
  1087. )
  1088. {
  1089. if(!Context){
  1090. MYASSERT(Context);
  1091. return FALSE;
  1092. }
  1093. return SetupGetIntField (&(Context -> Context), FieldIndex, Value);
  1094. }
  1095. /*++
  1096. Routine Description:
  1097. InfGetBinaryField is a wrapper for the SetupGetBinaryField function. It reduces
  1098. the number of parameters required to get the line text and takes care of
  1099. allocating and filling a buffer with the data returned from the API.
  1100. Arguments:
  1101. Context - A valid InfStruct. The INFCONTEXT member of the structure must point to a valid
  1102. line to retrieve (i.e. through the use of InfFindFirstLine/InfFindNextLine.
  1103. FieldIndex - the index within the line of the desired binary information.
  1104. Return Value:
  1105. A pointer to the allocated line or NULL if there was an error. Consult GetLastError() for
  1106. extended error information.
  1107. --*/
  1108. PBYTE
  1109. InfGetBinaryFieldA (
  1110. IN PINFSTRUCTA Context,
  1111. IN UINT FieldIndex
  1112. )
  1113. {
  1114. UINT requiredSize;
  1115. PBYTE rField = NULL;
  1116. if(!Context){
  1117. MYASSERT(Context);
  1118. return NULL;
  1119. }
  1120. //
  1121. // Get the size necessary for holding the field.
  1122. //
  1123. if (SetupGetBinaryField(
  1124. &(Context -> Context),
  1125. FieldIndex,
  1126. NULL,
  1127. 0,
  1128. &requiredSize
  1129. )) {
  1130. //
  1131. // Create a string big enough.
  1132. //
  1133. rField = pAllocateSpaceA(Context,requiredSize);
  1134. if (rField) {
  1135. //
  1136. // Get the field.
  1137. //
  1138. if (!SetupGetBinaryField(
  1139. &(Context -> Context),
  1140. FieldIndex,
  1141. rField,
  1142. requiredSize,
  1143. NULL
  1144. )) {
  1145. //
  1146. // If we did not successfully get the field, reset the string to NULL.
  1147. //
  1148. DEBUGMSGA((DBG_ERROR,"InfGetBinaryFieldA: Error retrieving field from INF file."));
  1149. rField = NULL;
  1150. }
  1151. }
  1152. }
  1153. return rField;
  1154. }
  1155. PBYTE
  1156. InfGetBinaryFieldW (
  1157. IN PINFSTRUCTW Context,
  1158. IN UINT FieldIndex
  1159. )
  1160. {
  1161. UINT requiredSize;
  1162. PBYTE rField = NULL;
  1163. if(!Context){
  1164. MYASSERT(Context);
  1165. return NULL;
  1166. }
  1167. //
  1168. // Get the size necessary for holding the field.
  1169. //
  1170. if (SetupGetBinaryField(
  1171. &(Context -> Context),
  1172. FieldIndex,
  1173. NULL,
  1174. 0,
  1175. &requiredSize
  1176. )) {
  1177. //
  1178. // Create a string big enough.
  1179. //
  1180. rField = pAllocateSpaceW(Context,requiredSize);
  1181. if (rField) {
  1182. //
  1183. // Get the field.
  1184. //
  1185. if (!SetupGetBinaryField(
  1186. &(Context -> Context),
  1187. FieldIndex,
  1188. rField,
  1189. requiredSize,
  1190. NULL
  1191. )) {
  1192. //
  1193. // If we did not successfully get the field, reset the string to NULL.
  1194. //
  1195. DEBUGMSGW((DBG_ERROR,"InfGetBinaryFieldW: Error retrieving field from INF file."));
  1196. rField = NULL;
  1197. }
  1198. }
  1199. }
  1200. return rField;
  1201. }
  1202. /*++
  1203. Routine Description:
  1204. InfGetIndexByLine is a straight wrapper for SetupGetLineByIndex. The only
  1205. difference is the use of an PINFSTRUCT instead of a PINFCONTEXT.
  1206. Arguments:
  1207. InfHandle - Contains a valid HINF.
  1208. Section - Contains the name of the section within the InfFile.
  1209. Index - Contains the index within the section of the line in question.
  1210. Context - A valid InfStruct that is updated with the result of these calls.
  1211. Return Value:
  1212. TRUE if the function was called successfully, FALSE otherwise.
  1213. --*/
  1214. BOOL
  1215. InfGetLineByIndexA (
  1216. IN HINF InfHandle,
  1217. IN PCSTR Section,
  1218. IN DWORD Index,
  1219. OUT PINFSTRUCTA Context
  1220. )
  1221. {
  1222. ASSERT_VALID_INF(InfHandle);
  1223. if(!Context){
  1224. MYASSERT(Context);
  1225. return FALSE;
  1226. }
  1227. return SetupGetLineByIndexA(InfHandle,Section,Index,&(Context -> Context));
  1228. }
  1229. BOOL
  1230. InfGetLineByIndexW (
  1231. IN HINF InfHandle,
  1232. IN PCWSTR Section,
  1233. IN DWORD Index,
  1234. OUT PINFSTRUCTW Context
  1235. )
  1236. {
  1237. ASSERT_VALID_INF(InfHandle);
  1238. if(!Context){
  1239. MYASSERT(Context);
  1240. return FALSE;
  1241. }
  1242. return SetupGetLineByIndexW(InfHandle,Section,Index,&(Context -> Context));
  1243. }
  1244. /*++
  1245. Routine Description:
  1246. InfFindFirstLineA and InfFindFirstLineW are wrappers for the SetupFindFirstLine function.
  1247. They are virtually identical except that they operate on INFSTRUCTs instead of INFCONTEXTS.
  1248. Arguments:
  1249. InfHandle - Contains a valid HINF.
  1250. Section - Contains the name of the section within the InfFile.
  1251. Key - An optional parameter containing the name of the key within the section to find.
  1252. If NULL, these routines will return the first line in the section.
  1253. Context - A valid InfStruct that is updated with the result of these calls.
  1254. Return Value:
  1255. TRUE if lines exist in the section, FALSE otherwise.
  1256. --*/
  1257. BOOL
  1258. InfFindFirstLineA (
  1259. IN HINF InfHandle,
  1260. IN PCSTR Section,
  1261. IN PCSTR Key, OPTIONAL
  1262. OUT PINFSTRUCTA Context
  1263. )
  1264. {
  1265. ASSERT_VALID_INF(InfHandle);
  1266. if(!Context){
  1267. MYASSERT(Context);
  1268. return FALSE;
  1269. }
  1270. if (Key) {
  1271. Context->KeyName = (PCSTR) pAllocateSpaceA (Context, SizeOfStringA (Key));
  1272. StringCopyA ((PSTR)Context->KeyName, Key);
  1273. } else {
  1274. Context->KeyName = NULL;
  1275. }
  1276. return SetupFindFirstLineA (
  1277. InfHandle,
  1278. Section,
  1279. Context->KeyName,
  1280. &(Context -> Context)
  1281. );
  1282. }
  1283. BOOL
  1284. InfFindFirstLineW (
  1285. IN HINF InfHandle,
  1286. IN PCWSTR Section,
  1287. IN PCWSTR Key, OPTIONAL
  1288. OUT PINFSTRUCTW Context
  1289. )
  1290. {
  1291. ASSERT_VALID_INF(InfHandle);
  1292. if(!Context){
  1293. MYASSERT(Context);
  1294. return FALSE;
  1295. }
  1296. if (Key) {
  1297. Context->KeyName = (PCWSTR) pAllocateSpaceW (Context, SizeOfStringW (Key));
  1298. StringCopyW ((PWSTR)Context->KeyName, Key);
  1299. } else {
  1300. Context->KeyName = NULL;
  1301. }
  1302. return SetupFindFirstLineW (
  1303. InfHandle,
  1304. Section,
  1305. Context->KeyName,
  1306. &(Context -> Context)
  1307. );
  1308. }
  1309. /*++
  1310. Routine Description:
  1311. InfFindNextLineA and InfFindNextLineW are wrappers for the SetupFindNextMatchLine function.
  1312. They are virtually identical except that they operate on INFSTRUCTs instead of INFCONTEXTS and
  1313. need only one INFSTRUCT parameter.
  1314. Arguments:
  1315. Context - A valid InfStruct that is updated with the result of these calls.
  1316. Return Value:
  1317. TRUE if there is another line in the section, FALSE otherwise.
  1318. --*/
  1319. BOOL
  1320. InfFindNextLineA (
  1321. IN OUT PINFSTRUCTA Context
  1322. )
  1323. {
  1324. if(!Context){
  1325. MYASSERT(Context);
  1326. return FALSE;
  1327. }
  1328. return SetupFindNextMatchLineA (&(Context -> Context), Context->KeyName, &(Context -> Context));
  1329. }
  1330. BOOL
  1331. InfFindNextLineW (
  1332. IN OUT PINFSTRUCTW Context
  1333. )
  1334. {
  1335. if(!Context){
  1336. MYASSERT(Context);
  1337. return FALSE;
  1338. }
  1339. return SetupFindNextMatchLineW (&(Context -> Context), Context->KeyName, &(Context -> Context));
  1340. }
  1341. UINT
  1342. InfGetFieldCountA (
  1343. IN PINFSTRUCTA Context
  1344. )
  1345. {
  1346. if (!Context) {
  1347. MYASSERT(Context);
  1348. return 0;
  1349. }
  1350. return SetupGetFieldCount(&(Context -> Context));
  1351. }
  1352. UINT
  1353. InfGetFieldCountW (
  1354. IN PINFSTRUCTW Context
  1355. )
  1356. {
  1357. if (!Context) {
  1358. MYASSERT(Context);
  1359. return 0;
  1360. }
  1361. return SetupGetFieldCount(&(Context -> Context));
  1362. }
  1363. PCSTR
  1364. InfGetOemStringFieldA (
  1365. IN PINFSTRUCTA Context,
  1366. IN UINT Field
  1367. )
  1368. /*++
  1369. Routine Description:
  1370. InfGetOemStringField returns a string field in the OEM character set.
  1371. This routine is used when accessing txtsetup.sif. It is implemented
  1372. only in the A version because UNICODE does not have a concept of OEM
  1373. characters.
  1374. Arguments:
  1375. Context - Specifies the initialized INF structure that points to the
  1376. line to read from
  1377. Field - Specifies the field number
  1378. Return Value:
  1379. A pointer to the OEM string, or NULL if an error occurred.
  1380. --*/
  1381. {
  1382. PCSTR Text;
  1383. PSTR OemText;
  1384. INT Size;
  1385. Text = InfGetStringFieldA (Context, Field);
  1386. if (!Text) {
  1387. return NULL;
  1388. }
  1389. Size = SizeOfStringA (Text);
  1390. OemText = (PSTR) pAllocateSpaceA (Context, Size);
  1391. if (!OemText) {
  1392. return NULL;
  1393. }
  1394. //
  1395. // We leave Text allocated because the caller will free everything
  1396. // when they clean up Context. Note the assumption that the conversion
  1397. // doesn't change string length.
  1398. //
  1399. OemToCharBuffA (Text, OemText, Size);
  1400. return OemText;
  1401. }
  1402. BOOL
  1403. SetupGetOemStringFieldA (
  1404. IN PINFCONTEXT Context,
  1405. IN DWORD Index,
  1406. IN PTSTR ReturnBuffer, OPTIONAL
  1407. IN DWORD ReturnBufferSize,
  1408. OUT PDWORD RequiredSize OPTIONAL
  1409. )
  1410. /*++
  1411. Routine Description:
  1412. SetupGetOemStringFieldA is a SetupGetStringField that converts the
  1413. return text to the OEM character set.
  1414. Arguments:
  1415. Context - Specifies the initialized INF structure that points to the
  1416. line to read from
  1417. Index - Specifies the field number
  1418. ReturnBuffer - Specifies the buffer to fill the text into
  1419. ReturnBufferSize - Specifies the size of ReturnBuffer in bytes
  1420. RequiredSize - Receives the size of the buffer needed
  1421. Return Value:
  1422. TRUE if successful, FALSE if failure.
  1423. --*/
  1424. {
  1425. PSTR OemBuf;
  1426. INT Size;
  1427. if (!SetupGetStringFieldA (
  1428. Context,
  1429. Index,
  1430. ReturnBuffer,
  1431. ReturnBufferSize,
  1432. RequiredSize
  1433. )) {
  1434. return FALSE;
  1435. }
  1436. if (!ReturnBuffer) {
  1437. return TRUE;
  1438. }
  1439. Size = SizeOfStringA (ReturnBuffer);
  1440. //
  1441. // BUGBUG - why not use in-place conversion like the function above?
  1442. // OemToCharBuff supports in-place conversion as well
  1443. //
  1444. OemBuf = (PSTR) MemAlloc (g_hHeap, 0, Size);
  1445. OemToCharBuffA (ReturnBuffer, OemBuf, Size);
  1446. StringCopyA (ReturnBuffer, OemBuf);
  1447. MemFree (g_hHeap, 0, OemBuf);
  1448. return TRUE;
  1449. }
  1450. UINT
  1451. pGetLanguage (
  1452. IN PCSTR File
  1453. )
  1454. {
  1455. HINF inf = INVALID_HANDLE_VALUE;
  1456. PINFSECTION section;
  1457. PINFLINE line;
  1458. PWSTR start, end;
  1459. UINT rLanguage = INF_INVALID_VERSION;
  1460. WCHAR envvar[MAX_MBCHAR_PATH];
  1461. *envvar = 0;
  1462. MYASSERT(File);
  1463. //
  1464. // Use the infparse rourtines to get this information. They
  1465. // are more reliable than the *privateprofile* apis.
  1466. //
  1467. inf = OpenInfFileExA (File, "version, strings", FALSE);
  1468. if (inf == INVALID_HANDLE_VALUE) {
  1469. return rLanguage;
  1470. }
  1471. section = FindInfSectionInTableW (inf, S_VERSION_W);
  1472. if (section) {
  1473. line = FindLineInInfSectionW (inf, section, S_LANGUAGE_W);
  1474. if (line && line->Data) {
  1475. start = wcschr (line->Data, L'%');
  1476. if (start) {
  1477. end = wcschr (start + 1, L'%');
  1478. if (end) {
  1479. if(ARRAYSIZE(envvar) <= (end - start - 1)){
  1480. MYASSERT(ARRAYSIZE(envvar) > (end - start - 1));
  1481. envvar[0] = '\0';
  1482. }
  1483. else{
  1484. StringCopyABW(envvar, start+1, end);
  1485. }
  1486. }
  1487. }
  1488. else {
  1489. if (*line->Data == L'*') {
  1490. rLanguage = INF_ANY_LANGUAGE;
  1491. }
  1492. else {
  1493. MYASSERT(line->Data);
  1494. rLanguage = _wcsnum (line->Data);
  1495. }
  1496. }
  1497. }
  1498. }
  1499. if (*envvar) {
  1500. //
  1501. // Get the data from the strings section.
  1502. //
  1503. section = FindInfSectionInTableW (inf, S_STRINGS_W);
  1504. if (section) {
  1505. line = FindLineInInfSectionW (inf, section, envvar);
  1506. if (line && line->Data) {
  1507. if (*line->Data == L'*') {
  1508. rLanguage = INF_ANY_LANGUAGE;
  1509. }
  1510. else {
  1511. rLanguage = _wcsnum (line->Data);
  1512. }
  1513. }
  1514. }
  1515. }
  1516. if (inf != INVALID_HANDLE_VALUE) {
  1517. CloseInfFile (inf);
  1518. }
  1519. return rLanguage;
  1520. }
  1521. BOOL
  1522. pInitInfReplaceTableA (
  1523. IN PCSTR UpginfsDir OPTIONAL
  1524. )
  1525. {
  1526. CHAR systemPath[MAX_MBCHAR_PATH];
  1527. CHAR buffer[MAX_MBCHAR_PATH];
  1528. BOOL validFile;
  1529. TREE_ENUMA e;
  1530. INT version;
  1531. INT language;
  1532. DWORD operation;
  1533. BOOL bReplace, bAdd;
  1534. CHAR buf[MAX_MBCHAR_PATH];
  1535. PCSTR tag;
  1536. BOOL b;
  1537. // pDestroyInfModList (g_RootInfMod);
  1538. PoolMemEmptyPool (g_InfModPool);
  1539. g_RootInfMod = NULL;
  1540. if (!(UpginfsDir && *UpginfsDir)) {
  1541. return TRUE;
  1542. }
  1543. if (!EnumFirstFileInTreeA (&e, UpginfsDir, NULL, FALSE)) {
  1544. DEBUGMSGA ((
  1545. DBG_VERBOSE,
  1546. "InfInitialize: No infs in %s or not a directory (rc=%u)",
  1547. UpginfsDir,
  1548. GetLastError ()
  1549. ));
  1550. return FALSE;
  1551. }
  1552. b = FALSE;
  1553. do {
  1554. //
  1555. // we only care about *.rep and *.add files. Ignore everything
  1556. // else.
  1557. //
  1558. if (e.Directory) {
  1559. continue;
  1560. }
  1561. bReplace = IsPatternMatchA ("*.rep", e.Name);
  1562. bAdd = IsPatternMatchA ("*.add", e.Name);
  1563. if (bAdd || bReplace) {
  1564. __try {
  1565. validFile = FALSE;
  1566. operation = bReplace ? INF_REPLACE : INF_APPEND;
  1567. GetPrivateProfileStringA (
  1568. S_VERSION_A,
  1569. S_TARGETINF_A,
  1570. "",
  1571. buffer,
  1572. MAX_MBCHAR_PATH,
  1573. e.FullPath
  1574. );
  1575. if (!*buffer) {
  1576. DEBUGMSGA ((DBG_WARNING, "%s not found in %s [%s]", S_TARGETINF_A, e.FullPath, S_VERSION_A));
  1577. __leave;
  1578. }
  1579. //
  1580. // CAUTION: as a result of the security review,
  1581. // require a new key called Version2 in the [Version]
  1582. // section of the patch INF
  1583. //
  1584. version = GetPrivateProfileIntA (
  1585. S_VERSION_A,
  1586. S_VERSION_A,
  1587. INF_INVALID_VERSION,
  1588. e.FullPath
  1589. );
  1590. //
  1591. // version is ALWAYS needed
  1592. //
  1593. if (version == INF_INVALID_VERSION) {
  1594. DEBUGMSGA ((DBG_WARNING, "%s not found in %s [%s]", S_VERSION_A, e.FullPath, S_VERSION_A));
  1595. __leave;
  1596. }
  1597. language = pGetLanguage (e.FullPath);
  1598. if (language == INF_INVALID_VERSION) {
  1599. __leave;
  1600. }
  1601. if (GetPrivateProfileStringA (
  1602. S_VERSION_A,
  1603. S_TAG_A,
  1604. TEXT(""),
  1605. buf,
  1606. MAX_MBCHAR_PATH,
  1607. e.FullPath
  1608. )) {
  1609. tag = buf;
  1610. } else {
  1611. tag = NULL;
  1612. }
  1613. validFile = TRUE;
  1614. }
  1615. __finally {
  1616. if (!validFile || !pAddReplacementInfToTable (buffer, version, language, tag, operation, e.FullPath)) {
  1617. DEBUGMSGA ((DBG_WARNING,"Invalid Replace or Add file found in %s.", UpginfsDir));
  1618. } else {
  1619. //
  1620. // at least one patch was successfully added to the table
  1621. //
  1622. b = TRUE;
  1623. }
  1624. }
  1625. } else {
  1626. DEBUGMSGA ((
  1627. DBG_WARNING,
  1628. "Non .rep or .add file found in %s directory! Unexpected.",
  1629. UpginfsDir
  1630. ));
  1631. }
  1632. } while (EnumNextFileInTreeA (&e));
  1633. return b;
  1634. }
  1635. /*++
  1636. Routine Description:
  1637. InitInfReplaceTable [re]initializes the table used by this module
  1638. with patches for inbox INFs
  1639. Arguments:
  1640. UpginfsDir - Specifies the (full path) directory to search for patches;
  1641. if NULL, clears the table
  1642. Return Value:
  1643. None
  1644. --*/
  1645. BOOL
  1646. InitInfReplaceTableA (
  1647. IN PCSTR UpginfsDir OPTIONAL
  1648. )
  1649. {
  1650. return pInitInfReplaceTableA (UpginfsDir);
  1651. }
  1652. BOOL
  1653. InitInfReplaceTableW (
  1654. IN PCWSTR UpginfsDir OPTIONAL
  1655. )
  1656. {
  1657. BOOL b;
  1658. PCSTR ansiDir;
  1659. ansiDir = UpginfsDir ? ConvertWtoA (UpginfsDir) : NULL;
  1660. b = InitInfReplaceTableA (ansiDir);
  1661. if (ansiDir) {
  1662. FreeConvertedStr (ansiDir);
  1663. }
  1664. return b;
  1665. }