Source code of Windows XP (NT5)
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.

2093 lines
45 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. infparse.c
  5. Abstract:
  6. The code in this file read in an INF file, organizing it into a data
  7. structure that can be manipulated.
  8. The entry points are:
  9. OpenInfFile - Parses the INF associated with the STF file.
  10. InfParse_WriteInfToDisk - Writes the INF memory structure to disk
  11. AddInfSectionToTable - Adds a new section to the INF memory structure
  12. AddInfLineToTable - Adds a new line to a section's memory structure
  13. FindInfSectionInTable - Performs a sequential search for a specific
  14. section name
  15. FindLineInInfSection - Locates a line given a specific key
  16. DeleteLineInInfSection - Removes a line from an INF section
  17. DeleteSectionInInfFile - Removes a complete section from the INF memory
  18. structure
  19. GetInfSectionLineCount - Returns the number of lines in a section
  20. GetFirstLineInSectionStruct - Begins a line enumeration given an INF
  21. section ptr
  22. GetFirstLineInSectionStr - Begins a line enumeration given an INF
  23. section string
  24. GetNextLineInSection - Continues a line enumeration
  25. Author:
  26. Jim Schmidt (jimschm) 20-Sept-1997
  27. Revision History:
  28. --*/
  29. #include "pch.h"
  30. //
  31. // Globals to manage INF file reading
  32. //
  33. static PBYTE g_Buf1, g_Buf2;
  34. static DWORD g_Buf1Start, g_Buf2Start;
  35. static DWORD g_Buf1End, g_Buf2End;
  36. #define INF_BUFFER_SIZE 32768
  37. WCHAR
  38. pGetInfFileWchar (
  39. IN HANDLE File,
  40. IN DWORD Pos,
  41. OUT PBOOL Error
  42. );
  43. PCWSTR
  44. pGetNextInfLine (
  45. IN HANDLE File,
  46. IN PGROWBUFFER LineBuf,
  47. IN OUT PDWORD Pos,
  48. IN BOOL UnicodeMode
  49. );
  50. typedef struct {
  51. HANDLE SourceInfFile;
  52. HANDLE DestInfFile;
  53. POOLHANDLE InfPool; // A pool for appended INF data
  54. PINFSECTION FirstInfSection; // The first section of the parsed INF
  55. PINFSECTION LastInfSection; // The last section of the parsed INF
  56. BOOL InfIsUnicode;
  57. } INFFILE, *PINFFILE;
  58. BOOL
  59. pReadInfIntoTable (
  60. IN OUT PINFFILE InfFile,
  61. IN PWSTR SectionList,
  62. IN BOOL KeepComments
  63. )
  64. /*++
  65. Routine Description:
  66. Reads the specified file into memory, parsing the lines according to basic
  67. INF structure.
  68. Arguments:
  69. InfFile - Specifies the structure initilized with the INF file handle.
  70. Receives the complete INF structure.
  71. Return Value:
  72. TRUE if parsing was successful, or FALSE if parsing failed.
  73. --*/
  74. {
  75. WCHAR ch;
  76. BOOL Error;
  77. GROWBUFFER LineBuf = GROWBUF_INIT;
  78. PCWSTR Text;
  79. DWORD Pos;
  80. PCWSTR Key, Data;
  81. PWSTR p, q;
  82. DWORD i;
  83. PINFSECTION Section = NULL;
  84. DWORD LineFlags;
  85. BOOL Result = FALSE;
  86. HASHTABLE ht = NULL;
  87. BOOL neededSection = FALSE;
  88. PWSTR list;
  89. Section = AddInfSectionToTableW (InfFile, L"");
  90. if (!Section) {
  91. LOG ((LOG_ERROR, "Read Inf Into Table: Could not add comment section"));
  92. return FALSE;
  93. }
  94. //
  95. // If we have a list of sections to fill, add them to a ht, for faster retrieval.
  96. //
  97. if (SectionList) {
  98. list = PoolMemDuplicateStringW (InfFile->InfPool, SectionList);
  99. ht = HtAllocW ();
  100. if (ht) {
  101. while (list) {
  102. p = wcschr (list, L',');
  103. if (p) {
  104. *p = 0;
  105. }
  106. HtAddStringW (ht, SkipSpaceW(list));
  107. if (p) {
  108. *p = L',';
  109. list = p + 1;
  110. }
  111. else {
  112. list = p;
  113. }
  114. }
  115. }
  116. else {
  117. LOG ((LOG_ERROR, "Read Inf Into Table: Could not allocate section hash table."));
  118. return FALSE;
  119. }
  120. }
  121. g_Buf1Start = 0;
  122. g_Buf2Start = 0;
  123. g_Buf1End = 0;
  124. g_Buf2End = 0;
  125. g_Buf1 = (PBYTE) MemAlloc (g_hHeap, 0, INF_BUFFER_SIZE);
  126. g_Buf2 = (PBYTE) MemAlloc (g_hHeap, 0, INF_BUFFER_SIZE);
  127. __try {
  128. //
  129. // Determine if this file is UNICODE
  130. //
  131. ch = pGetInfFileWchar (InfFile->SourceInfFile, 0, &Error);
  132. InfFile->InfIsUnicode = (ch == 0xfeff) && !Error;
  133. //
  134. // Parse each line.
  135. //
  136. Pos = 0;
  137. for (;;) {
  138. //
  139. // Get the line
  140. //
  141. Text = pGetNextInfLine (
  142. InfFile->SourceInfFile,
  143. &LineBuf,
  144. &Pos,
  145. InfFile->InfIsUnicode
  146. );
  147. if (!Text) {
  148. break;
  149. }
  150. //
  151. // If a comment line or blank line, skip it
  152. //
  153. p = (PWSTR) SkipSpaceW (Text);
  154. if (!p[0] || p[0] == L';') {
  155. if (KeepComments && !AddInfLineToTableW (InfFile, Section, NULL, Text, LINEFLAG_ALL_COMMENTS)) {
  156. LOG ((LOG_ERROR, "Read Inf Into Table: Can't add line comments to table", Text));
  157. __leave;
  158. }
  159. continue;
  160. }
  161. //
  162. // If a section line, start the new section
  163. //
  164. if (p[0] == L'[') {
  165. p++;
  166. q = wcschr (p, L']');
  167. if (!q) {
  168. q = GetEndOfStringW (p);
  169. } else {
  170. *q = 0;
  171. }
  172. if (!ht || HtFindStringW (ht, p)) {
  173. Section = AddInfSectionToTableW (InfFile, p);
  174. neededSection = TRUE;
  175. if (!Section) {
  176. LOG ((LOG_ERROR, "Read Inf Into Table: Could not add section %s", p));
  177. __leave;
  178. }
  179. }
  180. else {
  181. //
  182. // We must not care about this section. Make sure we don't add any lines.
  183. //
  184. neededSection = FALSE;
  185. }
  186. }
  187. //
  188. // Otherwise it must be a valid line
  189. //
  190. else {
  191. if (!Section) {
  192. DEBUGMSG ((DBG_WARNING, "InfParse_ReadInfIntoTable: Ignoring unrecognized line %s", p));
  193. continue;
  194. }
  195. if (!neededSection) {
  196. continue;
  197. }
  198. //
  199. // Split key and line: Skip key that is surrounded by quotes, then
  200. // find the first
  201. //
  202. LineFlags = 0;
  203. q = p;
  204. Key = NULL;
  205. Data = Text;
  206. while (q[0] == L'\"') {
  207. q = wcschr (q + 1, L'\"');
  208. if (!q) {
  209. q = p;
  210. break;
  211. } else {
  212. q++;
  213. }
  214. }
  215. i = (DWORD)wcscspn (q, L"\"=");
  216. if (q[i] == L'=') {
  217. q += i;
  218. Data = SkipSpaceW (q + 1);
  219. *q = 0;
  220. q = (PWSTR) SkipSpaceRW (Text, q);
  221. if (q && *q) {
  222. q++;
  223. *q = 0;
  224. }
  225. Key = p;
  226. if (Key[0] == L'\"') {
  227. LineFlags |= LINEFLAG_KEY_QUOTED;
  228. Key++;
  229. p = GetEndOfStringW (Key);
  230. p = (PWSTR) SkipSpaceRW (Key, p);
  231. if (p && *p) {
  232. if (p[0] != L'\"') {
  233. p++;
  234. }
  235. *p = 0;
  236. }
  237. }
  238. }
  239. if (!AddInfLineToTableW (InfFile, Section, Key, Data, LineFlags)) {
  240. LOG ((LOG_ERROR, "Read Inf Into Table: Can't add line %s to table", Text));
  241. __leave;
  242. }
  243. }
  244. }
  245. if (Pos != GetFileSize (InfFile->SourceInfFile, NULL)) {
  246. LOG ((LOG_ERROR, "Read Inf Into Table: Could not read entire INF"));
  247. __leave;
  248. }
  249. Result = TRUE;
  250. }
  251. __finally {
  252. MemFree (g_hHeap, 0, g_Buf1);
  253. MemFree (g_hHeap, 0, g_Buf2);
  254. FreeGrowBuffer (&LineBuf);
  255. if (ht) {
  256. HtFree (ht);
  257. }
  258. }
  259. return Result;
  260. }
  261. VOID
  262. CloseInfFile (
  263. HINF InfFile
  264. )
  265. {
  266. PINFFILE inf = (PINFFILE) InfFile;
  267. PoolMemEmptyPool (inf->InfPool);
  268. PoolMemDestroyPool (inf->InfPool);
  269. MemFree (g_hHeap, 0, inf);
  270. }
  271. HINF
  272. OpenInfFileExA (
  273. IN PCSTR InfFilePath,
  274. IN PSTR SectionList,
  275. IN BOOL KeepComments
  276. )
  277. {
  278. PINFFILE InfFile;
  279. BOOL b = TRUE;
  280. PWSTR wSectionList = NULL;
  281. if (SectionList) {
  282. wSectionList = (PWSTR) ConvertAtoW (SectionList);
  283. }
  284. InfFile = MemAlloc (g_hHeap, HEAP_ZERO_MEMORY, sizeof (INFFILE));
  285. InfFile->SourceInfFile = CreateFileA (
  286. InfFilePath,
  287. GENERIC_READ,
  288. FILE_SHARE_READ,
  289. NULL,
  290. OPEN_EXISTING,
  291. FILE_ATTRIBUTE_NORMAL,
  292. NULL
  293. );
  294. if (InfFile->SourceInfFile == INVALID_HANDLE_VALUE) {
  295. b = FALSE;
  296. } else {
  297. InfFile->InfPool = PoolMemInitNamedPool ("INF File");
  298. b = pReadInfIntoTable (InfFile, wSectionList, KeepComments);
  299. }
  300. if (wSectionList) {
  301. FreeConvertedStr (wSectionList);
  302. }
  303. if (!b) {
  304. if (InfFile->InfPool) {
  305. PoolMemDestroyPool (InfFile->InfPool);
  306. }
  307. MemFree (g_hHeap, 0, InfFile);
  308. return INVALID_HANDLE_VALUE;
  309. }
  310. CloseHandle (InfFile->SourceInfFile);
  311. InfFile->SourceInfFile = INVALID_HANDLE_VALUE;
  312. return (HINF) InfFile;
  313. }
  314. HINF
  315. OpenInfFileExW (
  316. IN PCWSTR InfFilePath,
  317. IN PWSTR SectionList,
  318. IN BOOL KeepComments
  319. )
  320. {
  321. PINFFILE InfFile;
  322. BOOL b = TRUE;
  323. InfFile = MemAlloc (g_hHeap, HEAP_ZERO_MEMORY, sizeof (INFFILE));
  324. InfFile->SourceInfFile = CreateFileW (
  325. InfFilePath,
  326. GENERIC_READ,
  327. FILE_SHARE_READ,
  328. NULL,
  329. OPEN_EXISTING,
  330. FILE_ATTRIBUTE_NORMAL,
  331. NULL
  332. );
  333. if (InfFile->SourceInfFile == INVALID_HANDLE_VALUE) {
  334. b = FALSE;
  335. } else {
  336. InfFile->InfPool = PoolMemInitNamedPool ("INF File");
  337. b = pReadInfIntoTable (InfFile, SectionList, KeepComments);
  338. }
  339. if (!b) {
  340. if (InfFile->InfPool) {
  341. PoolMemDestroyPool (InfFile->InfPool);
  342. }
  343. MemFree (g_hHeap, 0, InfFile);
  344. return INVALID_HANDLE_VALUE;
  345. }
  346. CloseHandle (InfFile->SourceInfFile);
  347. InfFile->SourceInfFile = INVALID_HANDLE_VALUE;
  348. return (HINF) InfFile;
  349. }
  350. BOOL
  351. pWriteFileStringBufferedA (
  352. IN PGROWBUFFER Buffer,
  353. IN HANDLE File,
  354. IN PCSTR String
  355. )
  356. {
  357. UINT stringBytes;
  358. PBYTE byteBuf;
  359. DWORD bytesWritten;
  360. if (!String) {
  361. if (Buffer->End) {
  362. if (!WriteFile (File, Buffer->Buf, Buffer->End, &bytesWritten, NULL)) {
  363. return FALSE;
  364. }
  365. if (bytesWritten != Buffer->End) {
  366. return FALSE;
  367. }
  368. Buffer->End = 0;
  369. }
  370. return TRUE;
  371. }
  372. stringBytes = ByteCountA (String);
  373. if (!stringBytes) {
  374. return TRUE;
  375. }
  376. if (stringBytes + Buffer->End > Buffer->Size) {
  377. //
  378. // Flush buffer by calling ourselves with a NULL String
  379. //
  380. if (!pWriteFileStringBufferedA (Buffer, File, NULL)) {
  381. return FALSE;
  382. }
  383. //
  384. // If string is huge, just write it directly
  385. //
  386. if (stringBytes > Buffer->Size) {
  387. if (!WriteFile (File, String, stringBytes, &bytesWritten, NULL)) {
  388. return FALSE;
  389. }
  390. if (bytesWritten != stringBytes) {
  391. return FALSE;
  392. }
  393. return TRUE;
  394. }
  395. }
  396. //
  397. // Put string in buffer
  398. //
  399. byteBuf = GrowBuffer (Buffer, stringBytes);
  400. CopyMemory (byteBuf, String, stringBytes);
  401. return TRUE;
  402. }
  403. BOOL
  404. pWriteFileStringBufferedW (
  405. IN PGROWBUFFER Buffer,
  406. IN HANDLE File,
  407. IN PCWSTR String
  408. )
  409. {
  410. UINT stringBytes;
  411. PBYTE byteBuf;
  412. DWORD bytesWritten;
  413. if (!String) {
  414. if (Buffer->End) {
  415. if (!WriteFile (File, Buffer->Buf, Buffer->End, &bytesWritten, NULL)) {
  416. return FALSE;
  417. }
  418. if (bytesWritten != Buffer->End) {
  419. return FALSE;
  420. }
  421. Buffer->End = 0;
  422. }
  423. return TRUE;
  424. }
  425. stringBytes = ByteCountW (String);
  426. if (stringBytes + Buffer->End > Buffer->Size) {
  427. //
  428. // Flush buffer by calling ourselves with a NULL String
  429. //
  430. if (!pWriteFileStringBufferedW (Buffer, File, NULL)) {
  431. return FALSE;
  432. }
  433. //
  434. // If string is huge, just write it directly
  435. //
  436. if (stringBytes > Buffer->Size) {
  437. if (!WriteFile (File, String, stringBytes, &bytesWritten, NULL)) {
  438. return FALSE;
  439. }
  440. if (bytesWritten != stringBytes) {
  441. return FALSE;
  442. }
  443. return TRUE;
  444. }
  445. }
  446. //
  447. // Put string in buffer
  448. //
  449. byteBuf = GrowBuffer (Buffer, stringBytes);
  450. CopyMemory (byteBuf, String, stringBytes);
  451. return TRUE;
  452. }
  453. BOOL
  454. pSaveInfToFile (
  455. IN PINFFILE InfFile
  456. )
  457. /*++
  458. Routine Description:
  459. pSaveInfToFile writes the INF represented by the given memory image to disk.
  460. This is done by enumerating the INF data structures in the INF.
  461. Arguments:
  462. InfFile - Specifies the table to process
  463. Return Value:
  464. TRUE if successful, FALSE if not.
  465. --*/
  466. {
  467. PINFSECTION Section;
  468. PINFLINE Line;
  469. BYTE UnicodeHeader[] = { 0xff, 0xfe };
  470. DWORD DontCare;
  471. BOOL b = FALSE;
  472. GROWBUFFER outputBuf = GROWBUF_INIT;
  473. GROWBUFFER conversionBuf = GROWBUF_INIT;
  474. UINT maxBytes;
  475. MYASSERT (InfFile->SourceInfFile == INVALID_HANDLE_VALUE);
  476. MYASSERT (InfFile->DestInfFile != INVALID_HANDLE_VALUE);
  477. //
  478. // Write the INF as we have it in memory
  479. //
  480. __try {
  481. //
  482. // Write the unicode indicator. We rely on the fact that this is first--
  483. // the output buffer is not in use yet.
  484. //
  485. if (InfFile->InfIsUnicode) {
  486. if (!WriteFile (InfFile->DestInfFile, UnicodeHeader, sizeof (UnicodeHeader), &DontCare, NULL)) {
  487. __leave;
  488. }
  489. }
  490. //
  491. // Initialize the output buffer. It will never grow larger than what
  492. // we specify here.
  493. //
  494. GrowBuffer (&outputBuf, 16384);
  495. outputBuf.End = 0;
  496. //
  497. // Loop through all the sections
  498. //
  499. Section = InfFile->FirstInfSection;
  500. while (Section) {
  501. //
  502. // If a section name exists, write it in brackets. Section names
  503. // can be empty when comments appear at the top of the file.
  504. //
  505. if (Section->Name[0]) {
  506. if (InfFile->InfIsUnicode) {
  507. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L"[") ||
  508. !pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, Section->Name) ||
  509. !pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L"]\r\n")
  510. ) {
  511. __leave;
  512. }
  513. } else {
  514. maxBytes = SizeOfStringW (Section->Name);
  515. conversionBuf.End = 0;
  516. GrowBuffer (&conversionBuf, maxBytes);
  517. DirectUnicodeToDbcsN ((PSTR) conversionBuf.Buf, Section->Name, maxBytes);
  518. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, "[") ||
  519. !pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, (PCSTR) conversionBuf.Buf) ||
  520. !pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, "]\r\n")
  521. ) {
  522. __leave;
  523. }
  524. }
  525. }
  526. //
  527. // Write all the lines within the section
  528. //
  529. Line = Section->FirstLine;
  530. while (Line) {
  531. //
  532. // Write the key if it is present. Quote it if necessary.
  533. //
  534. if (Line->Key) {
  535. if (Line->LineFlags & LINEFLAG_KEY_QUOTED) {
  536. if (InfFile->InfIsUnicode) {
  537. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L"\"")) {
  538. __leave;
  539. }
  540. } else {
  541. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, "\"")) {
  542. __leave;
  543. }
  544. }
  545. }
  546. if (InfFile->InfIsUnicode) {
  547. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, Line->Key)) {
  548. __leave;
  549. }
  550. } else {
  551. maxBytes = SizeOfStringW (Line->Key);
  552. conversionBuf.End = 0;
  553. GrowBuffer (&conversionBuf, maxBytes);
  554. DirectUnicodeToDbcsN ((PSTR) conversionBuf.Buf, Line->Key, maxBytes);
  555. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, (PCSTR) conversionBuf.Buf)) {
  556. __leave;
  557. }
  558. }
  559. if (Line->LineFlags & LINEFLAG_KEY_QUOTED) {
  560. if (InfFile->InfIsUnicode) {
  561. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L"\"")) {
  562. __leave;
  563. }
  564. } else {
  565. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, "\"")) {
  566. __leave;
  567. }
  568. }
  569. }
  570. //
  571. // Note that when we write equals, we might add some
  572. // space. Since space is trimmed by the INF parser, we say
  573. // this is acceptable because it improves readability.
  574. //
  575. if (InfFile->InfIsUnicode) {
  576. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L" = ")) {
  577. __leave;
  578. }
  579. } else {
  580. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, " = ")) {
  581. __leave;
  582. }
  583. }
  584. }
  585. //
  586. // Write the rest of the line
  587. //
  588. if (InfFile->InfIsUnicode) {
  589. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, Line->Data) ||
  590. !pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, L"\r\n")
  591. ) {
  592. __leave;
  593. }
  594. } else {
  595. maxBytes = SizeOfStringW (Line->Data);
  596. conversionBuf.End = 0;
  597. GrowBuffer (&conversionBuf, maxBytes);
  598. DirectUnicodeToDbcsN ((PSTR) conversionBuf.Buf, Line->Data, maxBytes);
  599. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, (PCSTR) conversionBuf.Buf) ||
  600. !pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, "\r\n")
  601. ) {
  602. __leave;
  603. }
  604. }
  605. Line = Line->Next;
  606. }
  607. Section = Section->Next;
  608. }
  609. //
  610. // Flush output buffer
  611. //
  612. if (InfFile->InfIsUnicode) {
  613. if (!pWriteFileStringBufferedW (&outputBuf, InfFile->DestInfFile, NULL)) {
  614. __leave;
  615. }
  616. } else {
  617. if (!pWriteFileStringBufferedA (&outputBuf, InfFile->DestInfFile, NULL)) {
  618. __leave;
  619. }
  620. }
  621. b = TRUE;
  622. }
  623. __finally {
  624. FreeGrowBuffer (&outputBuf);
  625. FreeGrowBuffer (&conversionBuf);
  626. DEBUGMSG_IF((!b, DBG_ERROR, "Write Inf To Disk: Cannot write INF"));
  627. }
  628. return b;
  629. }
  630. BOOL
  631. SaveInfFileA (
  632. IN HINF Inf,
  633. IN PCSTR SaveToFileSpec
  634. )
  635. {
  636. PINFFILE InfFile = (PINFFILE) Inf;
  637. BOOL b;
  638. if (Inf == INVALID_HANDLE_VALUE) {
  639. return FALSE;
  640. }
  641. InfFile->DestInfFile = CreateFileA (
  642. SaveToFileSpec,
  643. GENERIC_WRITE,
  644. 0,
  645. NULL,
  646. CREATE_ALWAYS,
  647. FILE_ATTRIBUTE_NORMAL,
  648. NULL
  649. );
  650. if (InfFile->DestInfFile == INVALID_HANDLE_VALUE) {
  651. return FALSE;
  652. }
  653. b = pSaveInfToFile (InfFile);
  654. CloseHandle (InfFile->DestInfFile);
  655. InfFile->DestInfFile = INVALID_HANDLE_VALUE;
  656. if (!b) {
  657. DeleteFileA (SaveToFileSpec);
  658. }
  659. return b;
  660. }
  661. BOOL
  662. SaveInfFileW (
  663. IN HINF Inf,
  664. IN PCWSTR SaveToFileSpec
  665. )
  666. {
  667. PINFFILE InfFile = (PINFFILE) Inf;
  668. BOOL b;
  669. if (Inf == INVALID_HANDLE_VALUE) {
  670. return FALSE;
  671. }
  672. InfFile->DestInfFile = CreateFileW (
  673. SaveToFileSpec,
  674. GENERIC_WRITE,
  675. 0,
  676. NULL,
  677. CREATE_ALWAYS,
  678. FILE_ATTRIBUTE_NORMAL,
  679. NULL
  680. );
  681. if (InfFile->DestInfFile == INVALID_HANDLE_VALUE) {
  682. return FALSE;
  683. }
  684. b = pSaveInfToFile (InfFile);
  685. CloseHandle (InfFile->DestInfFile);
  686. InfFile->DestInfFile = INVALID_HANDLE_VALUE;
  687. if (!b) {
  688. DeleteFileW (SaveToFileSpec);
  689. }
  690. return b;
  691. }
  692. PINFSECTION
  693. AddInfSectionToTableA (
  694. IN HINF Inf,
  695. IN PCSTR SectionName
  696. )
  697. {
  698. PINFSECTION SectionPtr;
  699. PCWSTR UnicodeSectionName;
  700. UnicodeSectionName = ConvertAtoW (SectionName);
  701. SectionPtr = AddInfSectionToTableW (Inf, UnicodeSectionName);
  702. FreeConvertedStr (SectionName);
  703. return SectionPtr;
  704. }
  705. PINFSECTION
  706. AddInfSectionToTableW (
  707. IN HINF Inf,
  708. IN PCWSTR SectionName
  709. )
  710. /*++
  711. Routine Description:
  712. Creates a new section in our linked list structure if necessary.
  713. The return structure can be used to add lines to the section.
  714. Arguments:
  715. Inf - Specifies the INF to add the section to
  716. SectionName - Specifies the name of the new section
  717. Return Value:
  718. A pointer to the new INF section struct, or NULL if an
  719. error occurred.
  720. --*/
  721. {
  722. PINFSECTION NewSection;
  723. PINFFILE InfFile = (PINFFILE) Inf;
  724. //
  725. // Return early if this section already exists
  726. //
  727. NewSection = FindInfSectionInTableW (InfFile, SectionName);
  728. if (NewSection) {
  729. return NewSection;
  730. }
  731. //
  732. // Allocate a section struct
  733. //
  734. NewSection = (PINFSECTION) PoolMemGetAlignedMemory (
  735. InfFile->InfPool,
  736. sizeof (INFSECTION)
  737. );
  738. if (!NewSection) {
  739. return NULL;
  740. }
  741. //
  742. // Fill in members of the struct and link
  743. //
  744. ZeroMemory (NewSection, sizeof (INFSECTION));
  745. NewSection->Name = PoolMemDuplicateStringW (
  746. InfFile->InfPool,
  747. SectionName
  748. );
  749. if (!NewSection->Name) {
  750. return NULL;
  751. }
  752. NewSection->Prev = InfFile->LastInfSection;
  753. if (NewSection->Prev) {
  754. NewSection->Prev->Next = NewSection;
  755. } else {
  756. InfFile->FirstInfSection = NewSection;
  757. }
  758. //
  759. // Add a blank line to LastInfSection to make things tidy
  760. //
  761. if (InfFile->LastInfSection) {
  762. AddInfLineToTableW (Inf, InfFile->LastInfSection, NULL, L"", 0);
  763. }
  764. //
  765. // Finalize linkage
  766. //
  767. InfFile->LastInfSection = NewSection;
  768. return NewSection;
  769. }
  770. PINFLINE
  771. AddInfLineToTableA (
  772. IN HINF Inf,
  773. IN PINFSECTION SectionPtr,
  774. IN PCSTR Key, OPTIONAL
  775. IN PCSTR Data,
  776. IN DWORD LineFlags
  777. )
  778. {
  779. PCWSTR UnicodeKey;
  780. PCWSTR UnicodeData;
  781. PINFLINE Line;
  782. if (Key) {
  783. UnicodeKey = ConvertAtoW (Key);
  784. } else {
  785. UnicodeKey = NULL;
  786. }
  787. UnicodeData = ConvertAtoW (Data);
  788. Line = AddInfLineToTableW (Inf, SectionPtr, UnicodeKey, UnicodeData, LineFlags);
  789. if (Key) {
  790. FreeConvertedStr (UnicodeKey);
  791. }
  792. FreeConvertedStr (UnicodeData);
  793. return Line;
  794. }
  795. PINFLINE
  796. AddInfLineToTableW (
  797. IN HINF Inf,
  798. IN PINFSECTION SectionPtr,
  799. IN PCWSTR Key, OPTIONAL
  800. IN PCWSTR Data,
  801. IN DWORD LineFlags
  802. )
  803. /*++
  804. Routine Description:
  805. Adds a line to the specified section. The caller specifies the
  806. full formatted data, and an optional key. The caller does NOT
  807. supply the equals sign between the key and data.
  808. Arguments:
  809. InfFile - Specifies the table to add the INF line to
  810. SectionName - Specifies the name of the section to add the line to
  811. Key - If specified, supplies the left-hand side of the equals line
  812. Data - Specifies the text for the line, or the right-hand side of
  813. the key = value expression.
  814. LineFlags - Specifies the flags for the INF line (see LINEFLAG_*)
  815. Return Value:
  816. TRUE if the line was added to the structure, or FALSE if not.
  817. --*/
  818. {
  819. PINFLINE NewLine;
  820. PINFFILE InfFile = (PINFFILE) Inf;
  821. //
  822. // Allocate line struct
  823. //
  824. NewLine = (PINFLINE) PoolMemGetAlignedMemory (
  825. InfFile->InfPool,
  826. sizeof (INFLINE)
  827. );
  828. if (!NewLine) {
  829. return NULL;
  830. }
  831. //
  832. // Fill in members of the struct and link. We insert the line at the end
  833. // of the section but before all blank space.
  834. //
  835. ZeroMemory (NewLine, sizeof (INFLINE));
  836. if (Key) {
  837. NewLine->Key = PoolMemDuplicateStringW (
  838. InfFile->InfPool,
  839. Key
  840. );
  841. if (!NewLine->Key) {
  842. return NULL;
  843. }
  844. }
  845. NewLine->Data = PoolMemDuplicateStringW (
  846. InfFile->InfPool,
  847. Data
  848. );
  849. if (!NewLine->Data) {
  850. return NULL;
  851. }
  852. NewLine->Next = NULL;
  853. NewLine->Prev = SectionPtr->LastLine;
  854. NewLine->Section = SectionPtr;
  855. NewLine->LineFlags = LineFlags;
  856. while (NewLine->Prev) {
  857. if (NewLine->Prev->Key || *NewLine->Prev->Data) {
  858. break;
  859. }
  860. NewLine->Next = NewLine->Prev;
  861. NewLine->Prev = NewLine->Prev->Prev;
  862. }
  863. if (NewLine->Prev) {
  864. NewLine->Prev->Next = NewLine;
  865. } else {
  866. SectionPtr->FirstLine = NewLine;
  867. }
  868. if (NewLine->Next) {
  869. NewLine->Next->Prev = NewLine;
  870. } else {
  871. SectionPtr->LastLine = NewLine;
  872. }
  873. SectionPtr->LineCount++;
  874. return NewLine;
  875. }
  876. PINFSECTION
  877. FindInfSectionInTableA (
  878. IN HINF Inf,
  879. IN PCSTR SectionName
  880. )
  881. {
  882. PINFSECTION InfSectionPtr;
  883. PCWSTR UnicodeSectionName;
  884. UnicodeSectionName = ConvertAtoW (SectionName);
  885. InfSectionPtr = FindInfSectionInTableW (Inf, UnicodeSectionName);
  886. FreeConvertedStr (UnicodeSectionName);
  887. return InfSectionPtr;
  888. }
  889. PINFSECTION
  890. FindInfSectionInTableW (
  891. IN HINF Inf,
  892. IN PCWSTR SectionName
  893. )
  894. /*++
  895. Routine Description:
  896. Scans the INF for a specific section. This routine scans
  897. the INF structures sequentially and does a case-insensitive
  898. comparison.
  899. Arguments:
  900. Inf - Specifies the INF to search
  901. SectionName - Specifies the name of the section to find
  902. Return Value:
  903. A pointer to the matching INF section struct, or NULL if
  904. the section was not found.
  905. --*/
  906. {
  907. PINFSECTION Section;
  908. PINFFILE InfFile = (PINFFILE) Inf;
  909. Section = InfFile->FirstInfSection;
  910. while (Section) {
  911. if (StringIMatchW (Section->Name, SectionName)) {
  912. return Section;
  913. }
  914. Section = Section->Next;
  915. }
  916. return NULL;
  917. }
  918. PINFSECTION
  919. GetFirstInfSectionInTable (
  920. IN HINF Inf
  921. )
  922. {
  923. PINFFILE InfFile = (PINFFILE) Inf;
  924. if (InfFile) {
  925. return InfFile->FirstInfSection;
  926. }
  927. return NULL;
  928. }
  929. PINFSECTION
  930. GetNextInfSectionInTable (
  931. IN PINFSECTION Section
  932. )
  933. {
  934. if (Section) {
  935. return Section->Next;
  936. }
  937. return NULL;
  938. }
  939. PINFLINE
  940. FindLineInInfSectionA (
  941. IN HINF Inf,
  942. IN PINFSECTION Section,
  943. IN PCSTR Key
  944. )
  945. {
  946. PCWSTR UnicodeKey;
  947. PINFLINE LinePtr;
  948. UnicodeKey = ConvertAtoW (Key);
  949. LinePtr = FindLineInInfSectionW (Inf, Section, UnicodeKey);
  950. FreeConvertedStr (UnicodeKey);
  951. return LinePtr;
  952. }
  953. PINFLINE
  954. FindLineInInfSectionW (
  955. IN HINF Inf,
  956. IN PINFSECTION Section,
  957. IN PCWSTR Key
  958. )
  959. /*++
  960. Routine Description:
  961. Scans the specified INF section for a specific key. This routine
  962. scans the INF line structures sequentially and does a case-insensitive
  963. comparison.
  964. Arguments:
  965. Inf - Specifies the INF to search
  966. Section - Specifies the section to search
  967. Key - Specifies the key to find
  968. Return Value:
  969. A pointer to the matching INF line struct, or NULL if
  970. the section was not found.
  971. --*/
  972. {
  973. PINFLINE Line;
  974. Line = Section->FirstLine;
  975. while (Line) {
  976. if (Line->Key && StringIMatchW (Line->Key, Key)) {
  977. return Line;
  978. }
  979. Line = Line->Next;
  980. }
  981. return NULL;
  982. }
  983. PINFLINE
  984. GetFirstLineInSectionStruct (
  985. IN PINFSECTION Section
  986. )
  987. /*++
  988. Routine Description:
  989. GetFirstLineInSectionStruct returns the first INFLINE pointer for the
  990. section, or NULL if no lines exist. Call GetNextLineInSection to
  991. continue enumeration.
  992. This routine does not return lines consisting only of comments.
  993. Arguments:
  994. Section - Specifies the section structure to enumerate lines frmo
  995. Return Value:
  996. A pointer to the first INFLINE struct, or NULL if no lines exist.
  997. --*/
  998. {
  999. if (!Section->FirstLine) {
  1000. return NULL;
  1001. }
  1002. if (Section->FirstLine->LineFlags & LINEFLAG_ALL_COMMENTS) {
  1003. return GetNextLineInSection (Section->FirstLine);
  1004. }
  1005. return Section->FirstLine;
  1006. }
  1007. PINFLINE
  1008. GetNextLineInSection (
  1009. IN PINFLINE PrevLine
  1010. )
  1011. /*++
  1012. Routine Description:
  1013. GetNextLineInSection returns the next INFLINE pointer for the
  1014. section, based on the previous line, or NULL if no lines exist.
  1015. This routine does not return lines with comments.
  1016. Arguments:
  1017. PrevLine - Specifies previous line (returned from
  1018. GetFirstLineInSectionStruct or GetFirstLineInSectionStr).
  1019. Return Value:
  1020. This routine does not return lines consisting only of comments.
  1021. --*/
  1022. {
  1023. while (PrevLine) {
  1024. PrevLine = PrevLine->Next;
  1025. if (!PrevLine || !(PrevLine->LineFlags & LINEFLAG_ALL_COMMENTS)) {
  1026. break;
  1027. }
  1028. }
  1029. return PrevLine;
  1030. }
  1031. PINFLINE
  1032. GetFirstLineInSectionStrA (
  1033. IN HINF Inf,
  1034. IN PCSTR Section
  1035. )
  1036. /*++
  1037. Routine Description:
  1038. GetFirstLineInSectionStruct returns the first INFLINE pointer for the
  1039. section, or NULL if no lines exist. Call GetNextLineInSection to
  1040. continue enumeration.
  1041. Arguments:
  1042. Inf - Specifies the INF that has the section
  1043. Section - Specifies the name of the section in the INF
  1044. Return Value:
  1045. A pointer to the first INFLINE struct, or NULL if no lines exist.
  1046. --*/
  1047. {
  1048. PCWSTR UnicodeSection;
  1049. PINFLINE LinePtr;
  1050. UnicodeSection = ConvertAtoW (Section);
  1051. LinePtr = GetFirstLineInSectionStrW (Inf, UnicodeSection);
  1052. FreeConvertedStr (UnicodeSection);
  1053. return LinePtr;
  1054. }
  1055. PINFLINE
  1056. GetFirstLineInSectionStrW (
  1057. IN HINF Inf,
  1058. IN PCWSTR Section
  1059. )
  1060. /*++
  1061. Routine Description:
  1062. GetFirstLineInSectionStruct returns the first INFLINE pointer for the
  1063. section, or NULL if no lines exist. Call GetNextLineInSection to
  1064. continue enumeration.
  1065. Arguments:
  1066. Inf - Specifies the INF that has the section
  1067. Section - Specifies the name of the section in the INF
  1068. Return Value:
  1069. A pointer to the first INFLINE struct, or NULL if no lines exist.
  1070. --*/
  1071. {
  1072. PINFSECTION SectionPtr;
  1073. PINFFILE Table = (PINFFILE) Inf;
  1074. SectionPtr = FindInfSectionInTableW (Table, Section);
  1075. if (!SectionPtr) {
  1076. return NULL;
  1077. }
  1078. return GetFirstLineInSectionStruct (SectionPtr);
  1079. }
  1080. INT
  1081. pGetInfFileByte (
  1082. IN HANDLE File,
  1083. IN DWORD Pos
  1084. )
  1085. /*++
  1086. Routine Description:
  1087. Returns the byte at the specified position, or -1 if the file could
  1088. not be read at that position.
  1089. Two buffers are used to allow fast relative access. Memory-mapped
  1090. files were NOT used because problems were introduced when the
  1091. swap file started filling up during GUI mode.
  1092. Arguments:
  1093. File - Specifies the file to read
  1094. Pos - Specifies the 32-bit file offset to read (zero-based, in bytes)
  1095. Return Value:
  1096. The byte at the specified position, or -1 if an error was encountered.
  1097. (Errors are usually caused by reading past the end of the file.)
  1098. --*/
  1099. {
  1100. DWORD Read;
  1101. PBYTE BufSwap;
  1102. //
  1103. // If we read the buffer previously, then return data in our buffer
  1104. //
  1105. if (Pos >= g_Buf1Start && Pos < g_Buf1End) {
  1106. return g_Buf1[Pos - g_Buf1Start];
  1107. }
  1108. if (Pos >= g_Buf2Start && Pos < g_Buf2End) {
  1109. return g_Buf2[Pos - g_Buf2Start];
  1110. }
  1111. //
  1112. // Buffer not available; move buffer 2 to buffer 1, then read buffer 2
  1113. //
  1114. g_Buf1Start = g_Buf2Start;
  1115. g_Buf1End = g_Buf2End;
  1116. BufSwap = g_Buf1;
  1117. g_Buf1 = g_Buf2;
  1118. g_Buf2 = BufSwap;
  1119. g_Buf2Start = Pos - (Pos % 256);
  1120. SetFilePointer (File, (LONG)g_Buf2Start, NULL, FILE_BEGIN);
  1121. if (!ReadFile (File, g_Buf2, INF_BUFFER_SIZE, &Read, NULL)) {
  1122. return -1;
  1123. }
  1124. g_Buf2End = g_Buf2Start + Read;
  1125. if (Pos >= g_Buf2Start && Pos < g_Buf2End) {
  1126. return g_Buf2[Pos - g_Buf2Start];
  1127. }
  1128. return -1;
  1129. }
  1130. WCHAR
  1131. pGetInfFileWchar (
  1132. IN HANDLE File,
  1133. IN DWORD Pos,
  1134. OUT PBOOL Error
  1135. )
  1136. /*++
  1137. Routine Description:
  1138. Returns the WCHAR at the specified position, or 0 if the file could
  1139. not be read at that position.
  1140. Two buffers are used to allow fast relative access. Memory-mapped
  1141. files were NOT used because problems were introduced when the
  1142. swap file started filling up during GUI mode.
  1143. Arguments:
  1144. File - Specifies the file to read
  1145. Pos - Specifies the 32-bit file offset to read (zero-based, in bytes)
  1146. Error - Receives TRUE if an error was encountered, or FALSE if an
  1147. error was not encountered.
  1148. Return Value:
  1149. The WCHAR at the specified position, or 0 if an error was encountered.
  1150. (Errors are usually caused by reading past the end of the file.)
  1151. If an error was encountered, the Error variable is also set to TRUE.
  1152. --*/
  1153. {
  1154. INT c;
  1155. WCHAR ch;
  1156. c = pGetInfFileByte (File, Pos);
  1157. if (c == -1 || c == 26) {
  1158. *Error = TRUE;
  1159. return (WORD) c;
  1160. }
  1161. ch = (WORD) c;
  1162. c = pGetInfFileByte (File, Pos + 1);
  1163. if (c == -1 || c == 26) {
  1164. *Error = TRUE;
  1165. return 0;
  1166. }
  1167. // pGetInfFileByte return a byte value or -1.
  1168. // Since we checked for -1 the next cast is valid.
  1169. ch += (WORD)(c * 256);
  1170. *Error = FALSE;
  1171. return ch;
  1172. }
  1173. PCSTR
  1174. pGetInfLineA (
  1175. IN HANDLE File,
  1176. IN DWORD StartPos,
  1177. OUT PDWORD EndPosPtr, OPTIONAL
  1178. IN OUT PGROWBUFFER LineBuf
  1179. )
  1180. /*++
  1181. Routine Description:
  1182. Returns a DBCS string supplying the line. This string can be
  1183. any length and is nul-terminated. It does not include the \r or
  1184. \n characters.
  1185. If supplied, the EndPosPtr is updated to point to the start of
  1186. the next line.
  1187. Arguments:
  1188. File - Specifies the file to read
  1189. StartPos - Specifies the 32-bit file offset to read (zero-based, in bytes)
  1190. EndPosPtr - If specified, receives the 32-bit file offset of the next
  1191. line, or equal to the file size for the last line.
  1192. LineBuf - Specifies a reused GROWBUFFER that the caller initializes
  1193. and pGetInfLineA uses for line allocation. The caller is
  1194. responsible for cleanup.
  1195. Return Value:
  1196. A pointer to the DBCS string supplying the full line (with the \r, \n or
  1197. \r\n sequence stripped), or NULL if an error occurs.
  1198. --*/
  1199. {
  1200. DWORD EndPos;
  1201. INT c;
  1202. PBYTE Data;
  1203. DWORD Pos;
  1204. DWORD ByteLen = 0;
  1205. EndPos = StartPos;
  1206. for (;;) {
  1207. c = pGetInfFileByte (File, EndPos);
  1208. if (c == -1 || c == 26) {
  1209. break;
  1210. }
  1211. if (IsDBCSLeadByte ((BYTE) c)) {
  1212. EndPos++;
  1213. c = pGetInfFileByte (File, EndPos);
  1214. if (c == -1 || c == 26) {
  1215. break;
  1216. }
  1217. ByteLen++;
  1218. } else {
  1219. if (c == '\r' || c == '\n') {
  1220. EndPos++;
  1221. if (c == '\r') {
  1222. c = pGetInfFileByte (File, EndPos);
  1223. if (c == '\n') {
  1224. EndPos++;
  1225. }
  1226. }
  1227. break;
  1228. }
  1229. }
  1230. EndPos++;
  1231. ByteLen++;
  1232. }
  1233. //
  1234. // NOTE: If you make a change here, make one below in W version
  1235. //
  1236. // Ctrl+Z ends the file
  1237. if (c == 26) {
  1238. EndPos = GetFileSize (File, NULL);
  1239. }
  1240. // Allocate buffer, caller frees
  1241. LineBuf->End = 0;
  1242. Data = GrowBuffer (LineBuf, ByteLen + 2);
  1243. if (!Data) {
  1244. return NULL;
  1245. }
  1246. // We've been successful -- copy end pos to caller's variable
  1247. if (EndPosPtr) {
  1248. *EndPosPtr = EndPos;
  1249. }
  1250. // End of file condition: zero-length, but not a blank line
  1251. if (!ByteLen && c != '\r' && c != '\n') {
  1252. return NULL;
  1253. }
  1254. // Copy line to buffer
  1255. for (Pos = 0 ; Pos < ByteLen ; Pos++) {
  1256. Data[Pos] = (BYTE) pGetInfFileByte (File, StartPos);
  1257. StartPos++;
  1258. }
  1259. Data[Pos] = 0;
  1260. Data[Pos + 1] = 0;
  1261. return (PCSTR) Data;
  1262. }
  1263. PCWSTR
  1264. pGetInfLineW (
  1265. IN HANDLE File,
  1266. IN DWORD StartPos,
  1267. OUT PDWORD EndPosPtr, OPTIONAL
  1268. IN OUT PGROWBUFFER LineBuf
  1269. )
  1270. /*++
  1271. Routine Description:
  1272. Returns a UNICODE string supplying the line. This string can be
  1273. any length and is nul-terminated. It does not include the \r or
  1274. \n characters.
  1275. If supplied, the EndPosPtr is updated to point to the start of
  1276. the next line.
  1277. Arguments:
  1278. File - Specifies the file to read
  1279. StartPos - Specifies the 32-bit file offset to read (zero-based, in bytes)
  1280. EndPosPtr - If specified, receives the 32-bit file offset of the next
  1281. line, or equal to the file size for the last line.
  1282. LineBuf - Specifies a reused GROWBUFFER that the caller initializes
  1283. and pGetInfLineA uses for line allocation. The caller is
  1284. responsible for cleanup.
  1285. Return Value:
  1286. A pointer to the UNICODE string supplying the full line (with the \r, \n or
  1287. \r\n sequence stripped), or NULL if an error occurs.
  1288. --*/
  1289. {
  1290. DWORD EndPos;
  1291. PBYTE Data;
  1292. DWORD Pos;
  1293. DWORD ByteLen = 0;
  1294. WCHAR ch;
  1295. BOOL Error;
  1296. EndPos = StartPos;
  1297. for (;;) {
  1298. ch = pGetInfFileWchar (File, EndPos, &Error);
  1299. if (Error) {
  1300. break;
  1301. }
  1302. if (ch == L'\r' || ch == L'\n') {
  1303. EndPos += 2;
  1304. if (ch == L'\r') {
  1305. ch = pGetInfFileWchar (File, EndPos, &Error);
  1306. if (ch == '\n') {
  1307. EndPos += 2;
  1308. }
  1309. }
  1310. break;
  1311. }
  1312. EndPos += 2;
  1313. ByteLen += 2;
  1314. }
  1315. //
  1316. // NOTE: If you make a change here, make one above in A version
  1317. //
  1318. // Ctrl+Z ends the file
  1319. if (ch == 26) {
  1320. EndPos = GetFileSize (File, NULL);
  1321. }
  1322. // Allocate buffer
  1323. LineBuf->End = 0;
  1324. Data = GrowBuffer (LineBuf, ByteLen + 2);
  1325. if (!Data) {
  1326. return NULL;
  1327. }
  1328. // We've been successful -- copy end pos to caller's variable
  1329. if (EndPosPtr) {
  1330. *EndPosPtr = EndPos;
  1331. }
  1332. // End of file condition: zero-length, but not a blank line
  1333. if (!ByteLen && ch != L'\r' && ch != L'\n') {
  1334. return NULL;
  1335. }
  1336. // Copy to buffer
  1337. for (Pos = 0 ; Pos < ByteLen ; Pos++) {
  1338. Data[Pos] = (BYTE) pGetInfFileByte (File, StartPos);
  1339. StartPos++;
  1340. }
  1341. Data[Pos] = 0;
  1342. Data[Pos + 1] = 0;
  1343. if (EndPosPtr) {
  1344. *EndPosPtr = EndPos;
  1345. }
  1346. return (PCWSTR) Data;
  1347. }
  1348. PCWSTR
  1349. pGetNextInfLine (
  1350. IN HANDLE File,
  1351. IN PGROWBUFFER LineBuf,
  1352. IN OUT PDWORD Pos,
  1353. IN BOOL UnicodeMode
  1354. )
  1355. /*++
  1356. Routine Description:
  1357. Returns a string supplying the line. This string can be any length and
  1358. is nul-terminated. It does not include the \r or \n characters.
  1359. Arguments:
  1360. File - Specifies the file to read
  1361. LineBuf - Specifies a reused GROWBUFFER that the caller initializes
  1362. and pGetInfLineA uses for line allocation. The caller is
  1363. responsible for cleanup.
  1364. Pos - Specifies the byte offset to the start of the line. Receives
  1365. the byte offset to the next line.
  1366. UnicodeMode - Specifies TRUE if the file being read is a UNICODE file,
  1367. or FALSE if the file being read is a DBCS file.
  1368. Return Value:
  1369. A pointer to the string supplying the full line (with the \r, \n or
  1370. \r\n sequence stripped), or NULL if an error occurs.
  1371. --*/
  1372. {
  1373. PCSTR AnsiStr = NULL;
  1374. PCWSTR UnicodeStr = NULL;
  1375. PCWSTR FinalStr;
  1376. BOOL Converted = FALSE;
  1377. //
  1378. // Obtain the text from the file
  1379. //
  1380. if (UnicodeMode) {
  1381. UnicodeStr = pGetInfLineW (File, *Pos, Pos, LineBuf);
  1382. if (!UnicodeStr) {
  1383. return NULL;
  1384. }
  1385. } else {
  1386. AnsiStr = pGetInfLineA (File, *Pos, Pos, LineBuf);
  1387. if (!AnsiStr) {
  1388. return NULL;
  1389. }
  1390. }
  1391. if (AnsiStr) {
  1392. UnicodeStr = ConvertAtoW (AnsiStr);
  1393. if (!UnicodeStr) {
  1394. return NULL;
  1395. }
  1396. Converted = TRUE;
  1397. }
  1398. FinalStr = UnicodeStr;
  1399. //
  1400. // Copy converted string into line buffer
  1401. //
  1402. if (Converted) {
  1403. LineBuf->End = 0;
  1404. Converted = MultiSzAppendW (LineBuf, FinalStr);
  1405. FreeConvertedStr (FinalStr);
  1406. if (!Converted) {
  1407. return NULL;
  1408. }
  1409. }
  1410. return (PCWSTR) LineBuf->Buf;
  1411. }
  1412. BOOL
  1413. DeleteLineInInfSection (
  1414. IN HINF Inf,
  1415. IN PINFLINE InfLine
  1416. )
  1417. /*++
  1418. Routine Description:
  1419. DeleteLineInInfSection removes the specified InfLine from its section,
  1420. cleaning up memory used by the line.
  1421. Arguments:
  1422. Inf - Specifies the INF to modify
  1423. InfLine - Specifies the line to delete
  1424. Return Value:
  1425. TRUE if the line was deleted successfully, or FALSE if an error
  1426. occurred.
  1427. --*/
  1428. {
  1429. PINFFILE InfFile = (PINFFILE) Inf;
  1430. if (InfLine->Prev) {
  1431. InfLine->Prev->Next = InfLine->Next;
  1432. } else {
  1433. InfLine->Section->FirstLine = InfLine->Next;
  1434. }
  1435. if (InfLine->Next) {
  1436. InfLine->Next->Prev = InfLine->Prev;
  1437. } else {
  1438. InfLine->Section->LastLine = InfLine->Prev;
  1439. }
  1440. if (InfLine->Key) {
  1441. PoolMemReleaseMemory (InfFile->InfPool, (PVOID) InfLine->Key);
  1442. }
  1443. if (InfLine->Data) {
  1444. PoolMemReleaseMemory (InfFile->InfPool, (PVOID) InfLine->Data);
  1445. }
  1446. InfLine->Section->LineCount--;
  1447. PoolMemReleaseMemory (InfFile->InfPool, (PVOID) InfLine);
  1448. return TRUE;
  1449. }
  1450. BOOL
  1451. DeleteSectionInInfFile (
  1452. IN HINF Inf,
  1453. IN PINFSECTION Section
  1454. )
  1455. /*++
  1456. Routine Description:
  1457. DeleteSectionInInfFile removes the specified section from the INF
  1458. data structure, removing all lines cleaning up
  1459. memory used by the section.
  1460. Arguments:
  1461. InfFile - Specifies the table owning the INF line
  1462. Section - Specifies the section to delete
  1463. Return Value:
  1464. TRUE if the section was deleted successfully, or FALSE if an error
  1465. occurred.
  1466. --*/
  1467. {
  1468. PINFLINE InfLine;
  1469. PINFLINE DelInfLine;
  1470. PINFFILE InfFile = (PINFFILE) Inf;
  1471. InfLine = Section->FirstLine;
  1472. while (InfLine) {
  1473. DelInfLine = InfLine;
  1474. InfLine = InfLine->Next;
  1475. if (!DeleteLineInInfSection (InfFile, DelInfLine)) {
  1476. return FALSE;
  1477. }
  1478. }
  1479. if (Section->Prev) {
  1480. Section->Prev->Next = Section->Next;
  1481. } else {
  1482. InfFile->FirstInfSection = Section->Next;
  1483. }
  1484. if (Section->Next) {
  1485. Section->Next->Prev = Section->Prev;
  1486. } else {
  1487. InfFile->LastInfSection = Section->Prev;
  1488. }
  1489. PoolMemReleaseMemory (InfFile->InfPool, (PVOID) Section->Name);
  1490. PoolMemReleaseMemory (InfFile->InfPool, (PVOID) Section);
  1491. return TRUE;
  1492. }
  1493. UINT
  1494. GetInfSectionLineCount (
  1495. IN PINFSECTION Section
  1496. )
  1497. /*++
  1498. Routine Description:
  1499. GetInfSectionLineCount returns the number of lines in the specified
  1500. INF section.
  1501. Arguments:
  1502. Section - Specifies the section to query
  1503. Return Value:
  1504. The number of lines, or zero if the section has no lines.
  1505. --*/
  1506. {
  1507. return Section->LineCount;
  1508. }