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.

1419 lines
35 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. strmap.c
  5. Abstract:
  6. Strmap (formally pathmap) is a fast hueristic-based program that
  7. searches strings and attempts to replace substrings when there
  8. are matching substrings in the mapping database.
  9. Author:
  10. Marc R. Whitten (marcw) 20-Mar-1997
  11. Revision History:
  12. Jim Schmidt (jimschm) 05-Jun-2000 Added multi table capability
  13. Jim Schmidt (jimschm) 08-May-2000 Improved replacement routines and
  14. added consistent filtering and
  15. extra data option
  16. Jim Schmidt (jimschm) 18-Aug-1998 Redesigned to fix two bugs, made
  17. A & W versions
  18. --*/
  19. //
  20. // Includes
  21. //
  22. #include "pch.h"
  23. //
  24. // Strings
  25. //
  26. // None
  27. //
  28. // Constants
  29. //
  30. #define CHARNODE_SINGLE_BYTE 0x0000
  31. #define CHARNODE_DOUBLE_BYTE 0x0001
  32. #define CHARNODE_REQUIRE_WACK_OR_NUL 0x0002
  33. //
  34. // Macros
  35. //
  36. // None
  37. //
  38. // Types
  39. //
  40. // None
  41. //
  42. // Globals
  43. //
  44. // None
  45. //
  46. // Macro expansion list
  47. //
  48. // None
  49. //
  50. // Private function prototypes
  51. //
  52. // None
  53. //
  54. // Macro expansion definition
  55. //
  56. // None
  57. //
  58. // Code
  59. //
  60. PMAPSTRUCT
  61. CreateStringMappingEx (
  62. IN BOOL UsesFilters,
  63. IN BOOL UsesExtraData
  64. )
  65. /*++
  66. Routine Description:
  67. CreateStringMapping allocates a string mapping data structure and
  68. initializes it. Callers can enable filter callbacks, extra data support, or
  69. both. The mapping structure contains either CHARNODE elements, or
  70. CHARNODEEX elements, depending on the UsesFilters or UsesExtraData flag.
  71. Arguments:
  72. UsesFilters - Specifies TRUE to enable filter callbacks. If enabled,
  73. those who add string pairs must specify the filter callback
  74. (each search/replace pair has its own callback)
  75. UsesExtraData - Specifies TRUE to associate extra data with the string
  76. mapping pair.
  77. Return Value:
  78. A handle to the string mapping structure, or NULL if a structure could not
  79. be created.
  80. --*/
  81. {
  82. POOLHANDLE Pool;
  83. PMAPSTRUCT Map;
  84. Pool = PoolMemInitNamedPool ("String Mapping");
  85. MYASSERT (Pool);
  86. Map = (PMAPSTRUCT) PoolMemGetAlignedMemory (Pool, sizeof (MAPSTRUCT));
  87. MYASSERT (Map);
  88. ZeroMemory (Map, sizeof (MAPSTRUCT));
  89. Map->Pool = Pool;
  90. Map->UsesExNode = UsesFilters|UsesExtraData;
  91. Map->UsesFilter = UsesFilters;
  92. Map->UsesExtraData = UsesExtraData;
  93. return Map;
  94. }
  95. VOID
  96. DestroyStringMapping (
  97. IN PMAPSTRUCT Map
  98. )
  99. {
  100. if (Map) {
  101. PoolMemEmptyPool (Map->Pool);
  102. PoolMemDestroyPool (Map->Pool);
  103. // Map is no longer valid
  104. }
  105. }
  106. PCHARNODE
  107. pFindCharNode (
  108. IN PMAPSTRUCT Map,
  109. IN PCHARNODE PrevNode, OPTIONAL
  110. IN WORD Char
  111. )
  112. {
  113. PCHARNODE Node;
  114. if (!PrevNode) {
  115. Node = Map->FirstLevelRoot;
  116. } else {
  117. Node = PrevNode->NextLevel;
  118. }
  119. while (Node) {
  120. if (Node->Char == Char) {
  121. return Node;
  122. }
  123. Node = Node->NextPeer;
  124. }
  125. return NULL;
  126. }
  127. PCHARNODE
  128. pAddCharNode (
  129. IN PMAPSTRUCT Map,
  130. IN PCHARNODE PrevNode, OPTIONAL
  131. IN WORD Char,
  132. IN WORD Flags
  133. )
  134. {
  135. PCHARNODE Node;
  136. PCHARNODEEX exNode;
  137. if (Map->UsesExNode) {
  138. exNode = PoolMemGetAlignedMemory (Map->Pool, sizeof (CHARNODEEX));
  139. Node = (PCHARNODE) exNode;
  140. MYASSERT (Node);
  141. ZeroMemory (exNode, sizeof (CHARNODEEX));
  142. } else {
  143. Node = PoolMemGetAlignedMemory (Map->Pool, sizeof (CHARNODE));
  144. MYASSERT (Node);
  145. ZeroMemory (Node, sizeof (CHARNODE));
  146. }
  147. Node->Char = Char;
  148. Node->Flags = Flags;
  149. if (PrevNode) {
  150. Node->NextPeer = PrevNode->NextLevel;
  151. PrevNode->NextLevel = Node;
  152. } else {
  153. Node->NextPeer = Map->FirstLevelRoot;
  154. Map->FirstLevelRoot = Node;
  155. }
  156. return Node;
  157. }
  158. VOID
  159. AddStringMappingPairExA (
  160. IN OUT PMAPSTRUCT Map,
  161. IN PCSTR Old,
  162. IN PCSTR New,
  163. IN REG_REPLACE_FILTER Filter, OPTIONAL
  164. IN ULONG_PTR ExtraData, OPTIONAL
  165. IN DWORD Flags
  166. )
  167. /*++
  168. Routine Description:
  169. AddStringMappingPairEx adds a search and replace string pair to the linked
  170. list data structures. If the same search string is already in the
  171. structures, then the replace string and optional extra data is updated.
  172. Arguments:
  173. Map - Specifies the string mapping
  174. Old - Specifies the search string
  175. New - Specifies the replace string
  176. Filter - Specifies the callback filter. This is only supported if the
  177. map was created with filter support enabled.
  178. ExtraData - Specifies arbitrary data to assign to the search/replace pair.
  179. This is only valid if the map was created with extra data
  180. enabled.
  181. Flags - Specifies optional flag STRMAP_REQUIRE_WACK_OR_NUL
  182. Return Value:
  183. None.
  184. --*/
  185. {
  186. PSTR OldCopy;
  187. PSTR NewCopy;
  188. PCSTR p;
  189. WORD w;
  190. PCHARNODE Prev;
  191. PCHARNODE Node;
  192. PCHARNODEEX exNode;
  193. WORD nodeFlags = 0;
  194. if (Flags & STRMAP_REQUIRE_WACK_OR_NUL) {
  195. nodeFlags = CHARNODE_REQUIRE_WACK_OR_NUL;
  196. }
  197. MYASSERT (Map);
  198. MYASSERT (Old);
  199. MYASSERT (New);
  200. MYASSERT (*Old);
  201. //
  202. // Duplicate strings
  203. //
  204. OldCopy = PoolMemDuplicateStringA (Map->Pool, Old);
  205. NewCopy = PoolMemDuplicateStringA (Map->Pool, New);
  206. //
  207. // Make OldCopy all lowercase
  208. //
  209. CharLowerA (OldCopy);
  210. //
  211. // Add the letters that are not in the mapping
  212. //
  213. for (Prev = NULL, p = OldCopy ; *p ; p = _mbsinc (p)) {
  214. w = (WORD) _mbsnextc (p);
  215. Node = pFindCharNode (Map, Prev, w);
  216. if (!Node) {
  217. break;
  218. }
  219. Prev = Node;
  220. }
  221. for ( ; *p ; p = _mbsinc (p)) {
  222. w = (WORD) _mbsnextc (p);
  223. nodeFlags |= (WORD) (IsLeadByte (*p) ? CHARNODE_DOUBLE_BYTE : CHARNODE_SINGLE_BYTE);
  224. Prev = pAddCharNode (Map, Prev, w, nodeFlags);
  225. }
  226. if (Prev) {
  227. StringCopyA (OldCopy, Old);
  228. Prev->OriginalStr = (PVOID) OldCopy;
  229. Prev->ReplacementStr = (PVOID) NewCopy;
  230. Prev->ReplacementBytes = ByteCountA (NewCopy);
  231. exNode = (PCHARNODEEX) Prev;
  232. if (Map->UsesExtraData) {
  233. exNode->ExtraData = ExtraData;
  234. }
  235. if (Map->UsesFilter) {
  236. exNode->Filter = Filter;
  237. }
  238. }
  239. }
  240. VOID
  241. AddStringMappingPairExW (
  242. IN OUT PMAPSTRUCT Map,
  243. IN PCWSTR Old,
  244. IN PCWSTR New,
  245. IN REG_REPLACE_FILTER Filter, OPTIONAL
  246. IN ULONG_PTR ExtraData, OPTIONAL
  247. IN DWORD Flags
  248. )
  249. /*++
  250. Routine Description:
  251. AddStringMappingPairEx adds a search and replace string pair to the linked
  252. list data structures. If the same search string is already in the
  253. structures, then the replace string and optional extra data is updated.
  254. Arguments:
  255. Map - Specifies the string mapping
  256. Old - Specifies the search string
  257. New - Specifies the replace string
  258. Filter - Specifies the callback filter. This is only supported if the
  259. map was created with filter support enabled.
  260. ExtraData - Specifies arbitrary data to assign to the search/replace pair.
  261. This is only valid if the map was created with extra data
  262. enabled.
  263. Flags - Specifies optional flag STRMAP_REQUIRE_WACK_OR_NUL
  264. Return Value:
  265. None.
  266. --*/
  267. {
  268. PWSTR OldCopy;
  269. PWSTR NewCopy;
  270. PCWSTR p;
  271. WORD w;
  272. PCHARNODE Prev;
  273. PCHARNODE Node;
  274. PCHARNODEEX exNode;
  275. WORD nodeFlags = 0;
  276. if (Flags & STRMAP_REQUIRE_WACK_OR_NUL) {
  277. nodeFlags = CHARNODE_REQUIRE_WACK_OR_NUL;
  278. }
  279. MYASSERT (Map);
  280. MYASSERT (Old);
  281. MYASSERT (New);
  282. MYASSERT (*Old);
  283. //
  284. // Duplicate strings
  285. //
  286. OldCopy = PoolMemDuplicateStringW (Map->Pool, Old);
  287. NewCopy = PoolMemDuplicateStringW (Map->Pool, New);
  288. //
  289. // Make OldCopy all lowercase
  290. //
  291. CharLowerW (OldCopy);
  292. //
  293. // Add the letters that are not in the mapping
  294. //
  295. Prev = NULL;
  296. p = OldCopy;
  297. while (w = *p) { // intentional assignment optimization
  298. Node = pFindCharNode (Map, Prev, w);
  299. if (!Node) {
  300. break;
  301. }
  302. Prev = Node;
  303. p++;
  304. }
  305. while (w = *p) { // intentional assignment optimization
  306. Prev = pAddCharNode (Map, Prev, w, nodeFlags);
  307. p++;
  308. }
  309. if (Prev) {
  310. StringCopyW (OldCopy, Old);
  311. Prev->OriginalStr = OldCopy;
  312. Prev->ReplacementStr = (PVOID) NewCopy;
  313. Prev->ReplacementBytes = ByteCountW (NewCopy);
  314. exNode = (PCHARNODEEX) Prev;
  315. if (Map->UsesExtraData) {
  316. exNode->ExtraData = ExtraData;
  317. }
  318. if (Map->UsesFilter) {
  319. exNode->Filter = Filter;
  320. }
  321. }
  322. }
  323. PCSTR
  324. pFindReplacementStringInOneMapA (
  325. IN PMAPSTRUCT Map,
  326. IN PCSTR Source,
  327. IN INT MaxSourceBytes,
  328. OUT PINT SourceBytesPtr,
  329. OUT PINT ReplacementBytesPtr,
  330. IN PREG_REPLACE_DATA Data,
  331. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  332. IN BOOL RequireWackOrNul
  333. )
  334. {
  335. PCHARNODE BestMatch;
  336. PCHARNODE Node;
  337. WORD Char;
  338. PCSTR OrgSource;
  339. PCSTR newString;
  340. INT newStringSizeInBytes;
  341. PCHARNODEEX exNode;
  342. BOOL replacementFound;
  343. *SourceBytesPtr = 0;
  344. Node = NULL;
  345. BestMatch = NULL;
  346. OrgSource = Source;
  347. while (*Source) {
  348. Char = (WORD) _mbsnextc (Source);
  349. Node = pFindCharNode (Map, Node, Char);
  350. if (Node) {
  351. //
  352. // Advance string pointer
  353. //
  354. if (Node->Flags & CHARNODE_DOUBLE_BYTE) {
  355. Source += 2;
  356. } else {
  357. Source++;
  358. }
  359. if (((PBYTE) Source - (PBYTE) OrgSource) > MaxSourceBytes) {
  360. break;
  361. }
  362. //
  363. // If replacement string is available, keep it
  364. // until a longer match comes along
  365. //
  366. replacementFound = (Node->ReplacementStr != NULL);
  367. if ((RequireWackOrNul || (Node->Flags & CHARNODE_REQUIRE_WACK_OR_NUL)) && replacementFound) {
  368. if (*Source && _mbsnextc (Source) != '\\') {
  369. replacementFound = FALSE;
  370. }
  371. }
  372. if (replacementFound) {
  373. newString = (PCSTR) Node->ReplacementStr;
  374. newStringSizeInBytes = Node->ReplacementBytes;
  375. if (Map->UsesFilter) {
  376. //
  377. // Call rename filter to allow denial of match
  378. //
  379. exNode = (PCHARNODEEX) Node;
  380. if (exNode->Filter) {
  381. Data->Ansi.BeginningOfMatch = OrgSource;
  382. Data->Ansi.OldSubString = (PCSTR) Node->OriginalStr;
  383. Data->Ansi.NewSubString = newString;
  384. Data->Ansi.NewSubStringSizeInBytes = newStringSizeInBytes;
  385. if (!exNode->Filter (Data)) {
  386. replacementFound = FALSE;
  387. } else {
  388. newString = Data->Ansi.NewSubString;
  389. newStringSizeInBytes = Data->Ansi.NewSubStringSizeInBytes;
  390. }
  391. }
  392. }
  393. if (replacementFound) {
  394. BestMatch = Node;
  395. *SourceBytesPtr = (HALF_PTR) ((PBYTE) Source - (PBYTE) OrgSource);
  396. }
  397. }
  398. } else {
  399. //
  400. // No Node ends the search
  401. //
  402. break;
  403. }
  404. }
  405. if (BestMatch) {
  406. //
  407. // Return replacement data to caller
  408. //
  409. if (ExtraDataValue) {
  410. if (Map->UsesExtraData) {
  411. exNode = (PCHARNODEEX) BestMatch;
  412. *ExtraDataValue = exNode->ExtraData;
  413. } else {
  414. *ExtraDataValue = 0;
  415. }
  416. }
  417. *ReplacementBytesPtr = newStringSizeInBytes;
  418. return newString;
  419. }
  420. return NULL;
  421. }
  422. PCSTR
  423. pFindReplacementStringA (
  424. IN PMAPSTRUCT *MapArray,
  425. IN UINT MapArrayCount,
  426. IN PCSTR Source,
  427. IN INT MaxSourceBytes,
  428. OUT PINT SourceBytesPtr,
  429. OUT PINT ReplacementBytesPtr,
  430. IN PREG_REPLACE_DATA Data,
  431. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  432. IN BOOL RequireWackOrNul
  433. )
  434. {
  435. UINT u;
  436. PCSTR result;
  437. for (u = 0 ; u < MapArrayCount ; u++) {
  438. if (!MapArray[u]) {
  439. continue;
  440. }
  441. result = pFindReplacementStringInOneMapA (
  442. MapArray[u],
  443. Source,
  444. MaxSourceBytes,
  445. SourceBytesPtr,
  446. ReplacementBytesPtr,
  447. Data,
  448. ExtraDataValue,
  449. RequireWackOrNul
  450. );
  451. if (result) {
  452. return result;
  453. }
  454. }
  455. return NULL;
  456. }
  457. PCWSTR
  458. pFindReplacementStringInOneMapW (
  459. IN PMAPSTRUCT Map,
  460. IN PCWSTR Source,
  461. IN INT MaxSourceBytes,
  462. OUT PINT SourceBytesPtr,
  463. OUT PINT ReplacementBytesPtr,
  464. IN PREG_REPLACE_DATA Data,
  465. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  466. IN BOOL RequireWackOrNul
  467. )
  468. {
  469. PCHARNODE BestMatch;
  470. PCHARNODE Node;
  471. PCWSTR OrgSource;
  472. PCWSTR newString;
  473. INT newStringSizeInBytes;
  474. BOOL replacementFound;
  475. PCHARNODEEX exNode;
  476. *SourceBytesPtr = 0;
  477. Node = NULL;
  478. BestMatch = NULL;
  479. OrgSource = Source;
  480. while (*Source) {
  481. Node = pFindCharNode (Map, Node, *Source);
  482. if (Node) {
  483. //
  484. // Advance string pointer
  485. //
  486. Source++;
  487. if (((PBYTE) Source - (PBYTE) OrgSource) > MaxSourceBytes) {
  488. break;
  489. }
  490. //
  491. // If replacement string is available, keep it
  492. // until a longer match comes along
  493. //
  494. replacementFound = (Node->ReplacementStr != NULL);
  495. if ((RequireWackOrNul || (Node->Flags & CHARNODE_REQUIRE_WACK_OR_NUL)) && replacementFound) {
  496. if (*Source && *Source != L'\\') {
  497. replacementFound = FALSE;
  498. }
  499. }
  500. if (replacementFound) {
  501. newString = (PCWSTR) Node->ReplacementStr;
  502. newStringSizeInBytes = Node->ReplacementBytes;
  503. if (Map->UsesFilter) {
  504. //
  505. // Call rename filter to allow denial of match
  506. //
  507. exNode = (PCHARNODEEX) Node;
  508. if (exNode->Filter) {
  509. Data->Unicode.BeginningOfMatch = OrgSource;
  510. Data->Unicode.OldSubString = (PCWSTR) Node->OriginalStr;
  511. Data->Unicode.NewSubString = newString;
  512. Data->Unicode.NewSubStringSizeInBytes = newStringSizeInBytes;
  513. if (!exNode->Filter (Data)) {
  514. replacementFound = FALSE;
  515. } else {
  516. newString = Data->Unicode.NewSubString;
  517. newStringSizeInBytes = Data->Unicode.NewSubStringSizeInBytes;
  518. }
  519. }
  520. }
  521. if (replacementFound) {
  522. BestMatch = Node;
  523. *SourceBytesPtr = (HALF_PTR) ((PBYTE) Source - (PBYTE) OrgSource);
  524. }
  525. }
  526. } else {
  527. //
  528. // No Node ends the search
  529. //
  530. break;
  531. }
  532. }
  533. if (BestMatch) {
  534. //
  535. // Return replacement data to caller
  536. //
  537. if (ExtraDataValue) {
  538. if (Map->UsesExtraData) {
  539. exNode = (PCHARNODEEX) BestMatch;
  540. *ExtraDataValue = exNode->ExtraData;
  541. } else {
  542. *ExtraDataValue = 0;
  543. }
  544. }
  545. *ReplacementBytesPtr = newStringSizeInBytes;
  546. return newString;
  547. }
  548. return NULL;
  549. }
  550. PCWSTR
  551. pFindReplacementStringW (
  552. IN PMAPSTRUCT *MapArray,
  553. IN UINT MapArrayCount,
  554. IN PCWSTR Source,
  555. IN INT MaxSourceBytes,
  556. OUT PINT SourceBytesPtr,
  557. OUT PINT ReplacementBytesPtr,
  558. IN PREG_REPLACE_DATA Data,
  559. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  560. IN BOOL RequireWackOrNul
  561. )
  562. {
  563. UINT u;
  564. PCWSTR result;
  565. for (u = 0 ; u < MapArrayCount ; u++) {
  566. if (!MapArray[u]) {
  567. continue;
  568. }
  569. result = pFindReplacementStringInOneMapW (
  570. MapArray[u],
  571. Source,
  572. MaxSourceBytes,
  573. SourceBytesPtr,
  574. ReplacementBytesPtr,
  575. Data,
  576. ExtraDataValue,
  577. RequireWackOrNul
  578. );
  579. if (result) {
  580. return result;
  581. }
  582. }
  583. return NULL;
  584. }
  585. BOOL
  586. MappingMultiTableSearchAndReplaceExA (
  587. IN PMAPSTRUCT *MapArray,
  588. IN UINT MapArrayCount,
  589. IN PCSTR SrcBuffer,
  590. OUT PSTR Buffer, // can be the same as SrcBuffer
  591. IN INT InboundBytes, OPTIONAL
  592. OUT PINT OutboundBytesPtr, OPTIONAL
  593. IN INT MaxSizeInBytes,
  594. IN DWORD Flags,
  595. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  596. OUT PCSTR *EndOfString OPTIONAL
  597. )
  598. /*++
  599. Routine Description:
  600. MappingSearchAndReplaceEx performs a search/replace operation based on the
  601. specified string mapping. The replace can be in-place or to another buffer.
  602. Arguments:
  603. MapArray - Specifies an array of string mapping tables that holds
  604. zero or more search/replace pairs
  605. MapArrayCount - Specifies the number of mapping tables in MapArray
  606. SrcBuffer - Specifies the source string that might contain one or
  607. more search strings
  608. Buffer - Specifies the outbound buffer. This arg can be the same
  609. as SrcBuffer.
  610. InboundBytes - Specifies the number of bytes in SrcBuffer to process,
  611. or 0 to process a nul-terminated string in SrcBuffer.
  612. If InboundBytes is specified, it must point to the nul
  613. terminator of SrcBuffer.
  614. OutbountBytesPtr - Receives the number of bytes that Buffer contains,
  615. excluding the nul terminator.
  616. MaxSizeInBytes - Specifies the size of Buffer, in bytes.
  617. Flags - Specifies flags that control the search/replace:
  618. STRMAP_COMPLETE_MATCH_ONLY
  619. STRMAP_FIRST_CHAR_MUST_MATCH
  620. STRMAP_RETURN_AFTER_FIRST_REPLACE
  621. STRMAP_REQUIRE_WACK_OR_NUL
  622. ExtraDataValue - Receives the extra data associated with the first search/
  623. replace pair.
  624. EndOfString - Receives a pointer to the end of the replace string, or
  625. the nul pointer when the entire string is processed. The
  626. pointer is within the string contained in Buffer.
  627. --*/
  628. {
  629. UINT sizeOfTempBuf;
  630. INT inboundSize;
  631. PCSTR lowerCaseSrc;
  632. PCSTR orgSrc;
  633. PCSTR lowerSrcPos;
  634. PCSTR orgSrcPos;
  635. INT orgSrcBytesLeft;
  636. PSTR destPos;
  637. PCSTR lowerSrcEnd;
  638. INT searchStringBytes;
  639. INT replaceStringBytes;
  640. INT destBytesLeft;
  641. REG_REPLACE_DATA filterData;
  642. PCSTR replaceString;
  643. BOOL result = FALSE;
  644. INT i;
  645. PCSTR endPtr;
  646. //
  647. // Empty string case
  648. //
  649. if (*SrcBuffer == 0 || MaxSizeInBytes <= sizeof (CHAR)) {
  650. if (MaxSizeInBytes >= sizeof (CHAR)) {
  651. *Buffer = 0;
  652. }
  653. if (OutboundBytesPtr) {
  654. *OutboundBytesPtr = 0;
  655. }
  656. return FALSE;
  657. }
  658. //
  659. // If caller did not specify inbound size, compute it now
  660. //
  661. if (!InboundBytes) {
  662. InboundBytes = ByteCountA (SrcBuffer);
  663. } else {
  664. i = 0;
  665. while (i < InboundBytes) {
  666. if (IsLeadByte (SrcBuffer[i])) {
  667. MYASSERT (SrcBuffer[i + 1]);
  668. i += 2;
  669. } else {
  670. i++;
  671. }
  672. }
  673. if (i > InboundBytes) {
  674. InboundBytes--;
  675. }
  676. }
  677. inboundSize = InboundBytes + sizeof (CHAR);
  678. //
  679. // Allocate a buffer big enough for the lower-cased input string,
  680. // plus (optionally) a copy of the entire destination buffer. Then
  681. // copy the data to the buffer.
  682. //
  683. sizeOfTempBuf = inboundSize;
  684. if (SrcBuffer == Buffer) {
  685. sizeOfTempBuf += MaxSizeInBytes;
  686. }
  687. lowerCaseSrc = AllocTextA (sizeOfTempBuf);
  688. if (!lowerCaseSrc) {
  689. return FALSE;
  690. }
  691. CopyMemory ((PSTR) lowerCaseSrc, SrcBuffer, InboundBytes);
  692. *((PSTR) ((PBYTE) lowerCaseSrc + InboundBytes)) = 0;
  693. CharLowerBuffA ((PSTR) lowerCaseSrc, InboundBytes / sizeof (CHAR));
  694. if (SrcBuffer == Buffer && !(Flags & STRMAP_COMPLETE_MATCH_ONLY)) {
  695. orgSrc = (PCSTR) ((PBYTE) lowerCaseSrc + inboundSize);
  696. //
  697. // If we are processing entire inbound string, then just copy the
  698. // whole string. Otherwise, copy the entire destination buffer, so we
  699. // don't lose data beyond the partial inbound string.
  700. //
  701. if (*((PCSTR) ((PBYTE) SrcBuffer + InboundBytes))) {
  702. CopyMemory ((PSTR) orgSrc, SrcBuffer, MaxSizeInBytes);
  703. } else {
  704. CopyMemory ((PSTR) orgSrc, SrcBuffer, inboundSize);
  705. }
  706. } else {
  707. orgSrc = SrcBuffer;
  708. }
  709. //
  710. // Walk the lower cased string, looking for strings to replace
  711. //
  712. orgSrcPos = orgSrc;
  713. lowerSrcPos = lowerCaseSrc;
  714. lowerSrcEnd = (PCSTR) ((PBYTE) lowerSrcPos + InboundBytes);
  715. destPos = Buffer;
  716. destBytesLeft = MaxSizeInBytes - sizeof (CHAR);
  717. filterData.UnicodeData = FALSE;
  718. filterData.Ansi.OriginalString = orgSrc;
  719. filterData.Ansi.CurrentString = Buffer;
  720. endPtr = NULL;
  721. while (lowerSrcPos < lowerSrcEnd) {
  722. replaceString = pFindReplacementStringA (
  723. MapArray,
  724. MapArrayCount,
  725. lowerSrcPos,
  726. (HALF_PTR) ((PBYTE) lowerSrcEnd - (PBYTE) lowerSrcPos),
  727. &searchStringBytes,
  728. &replaceStringBytes,
  729. &filterData,
  730. ExtraDataValue,
  731. (Flags & STRMAP_REQUIRE_WACK_OR_NUL) != 0
  732. );
  733. if (replaceString) {
  734. //
  735. // Implement complete match flag
  736. //
  737. if (Flags & STRMAP_COMPLETE_MATCH_ONLY) {
  738. if (InboundBytes != searchStringBytes) {
  739. break;
  740. }
  741. }
  742. result = TRUE;
  743. //
  744. // Verify replacement string isn't growing string too much. If it
  745. // is, truncate the replacement string.
  746. //
  747. if (destBytesLeft < replaceStringBytes) {
  748. //
  749. // Respect logical dbcs characters
  750. //
  751. replaceStringBytes = 0;
  752. i = 0;
  753. while (i < destBytesLeft) {
  754. MYASSERT (replaceString[i]);
  755. if (IsLeadByte (replaceString[i])) {
  756. MYASSERT (replaceString[i + 1]);
  757. i += 2;
  758. } else {
  759. i++;
  760. }
  761. }
  762. if (i > destBytesLeft) {
  763. destBytesLeft--;
  764. }
  765. replaceStringBytes = destBytesLeft;
  766. } else {
  767. destBytesLeft -= replaceStringBytes;
  768. }
  769. //
  770. // Transfer the memory
  771. //
  772. CopyMemory (destPos, replaceString, replaceStringBytes);
  773. destPos = (PSTR) ((PBYTE) destPos + replaceStringBytes);
  774. lowerSrcPos = (PCSTR) ((PBYTE) lowerSrcPos + searchStringBytes);
  775. orgSrcPos = (PCSTR) ((PBYTE) orgSrcPos + searchStringBytes);
  776. //
  777. // Implement single match flag
  778. //
  779. if (Flags & STRMAP_RETURN_AFTER_FIRST_REPLACE) {
  780. endPtr = destPos;
  781. break;
  782. }
  783. } else if (Flags & (STRMAP_FIRST_CHAR_MUST_MATCH|STRMAP_COMPLETE_MATCH_ONLY)) {
  784. //
  785. // This string does not match any search strings
  786. //
  787. break;
  788. } else {
  789. //
  790. // This character does not match, so copy it to the destination and
  791. // try the next string.
  792. //
  793. if (IsLeadByte (*orgSrcPos)) {
  794. //
  795. // Copy double-byte character
  796. //
  797. if (destBytesLeft < sizeof (CHAR) * 2) {
  798. break;
  799. }
  800. MYASSERT (sizeof (CHAR) * 2 == sizeof (WORD));
  801. *((PWORD) destPos)++ = *((PWORD) orgSrcPos)++;
  802. destBytesLeft -= sizeof (WORD);
  803. lowerSrcPos = (PCSTR) ((PBYTE) lowerSrcPos + sizeof (WORD));
  804. } else {
  805. //
  806. // Copy single-byte character
  807. //
  808. if (destBytesLeft < sizeof (CHAR)) {
  809. break;
  810. }
  811. *destPos++ = *orgSrcPos++;
  812. destBytesLeft -= sizeof (CHAR);
  813. lowerSrcPos++;
  814. }
  815. }
  816. }
  817. //
  818. // Copy any remaining part of the original source to the
  819. // destination, unless destPos == Buffer == SrcBuffer
  820. //
  821. if (destPos != SrcBuffer) {
  822. if (*orgSrcPos) {
  823. orgSrcBytesLeft = ByteCountA (orgSrcPos);
  824. orgSrcBytesLeft = min (orgSrcBytesLeft, destBytesLeft);
  825. CopyMemory (destPos, orgSrcPos, orgSrcBytesLeft);
  826. destPos = (PSTR) ((PBYTE) destPos + orgSrcBytesLeft);
  827. }
  828. MYASSERT ((PBYTE) (destPos + 1) <= ((PBYTE) Buffer + MaxSizeInBytes));
  829. *destPos = 0;
  830. if (!endPtr) {
  831. endPtr = destPos;
  832. }
  833. } else {
  834. MYASSERT (SrcBuffer == Buffer);
  835. if (EndOfString || OutboundBytesPtr) {
  836. endPtr = GetEndOfStringA (destPos);
  837. }
  838. }
  839. if (EndOfString) {
  840. MYASSERT (endPtr);
  841. *EndOfString = endPtr;
  842. }
  843. if (OutboundBytesPtr) {
  844. MYASSERT (endPtr);
  845. if (*endPtr) {
  846. endPtr = GetEndOfStringA (endPtr);
  847. }
  848. *OutboundBytesPtr = (HALF_PTR) ((PBYTE) endPtr - (PBYTE) Buffer);
  849. }
  850. FreeTextA (lowerCaseSrc);
  851. return result;
  852. }
  853. BOOL
  854. MappingMultiTableSearchAndReplaceExW (
  855. IN PMAPSTRUCT *MapArray,
  856. IN UINT MapArrayCount,
  857. IN PCWSTR SrcBuffer,
  858. OUT PWSTR Buffer, // can be the same as SrcBuffer
  859. IN INT InboundBytes, OPTIONAL
  860. OUT PINT OutboundBytesPtr, OPTIONAL
  861. IN INT MaxSizeInBytes,
  862. IN DWORD Flags,
  863. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  864. OUT PCWSTR *EndOfString OPTIONAL
  865. )
  866. {
  867. UINT sizeOfTempBuf;
  868. INT inboundSize;
  869. PCWSTR lowerCaseSrc;
  870. PCWSTR orgSrc;
  871. PCWSTR lowerSrcPos;
  872. PCWSTR orgSrcPos;
  873. INT orgSrcBytesLeft;
  874. PWSTR destPos;
  875. PCWSTR lowerSrcEnd;
  876. INT searchStringBytes;
  877. INT replaceStringBytes;
  878. INT destBytesLeft;
  879. REG_REPLACE_DATA filterData;
  880. PCWSTR replaceString;
  881. BOOL result = FALSE;
  882. PCWSTR endPtr;
  883. //
  884. // Empty string case
  885. //
  886. if (*SrcBuffer == 0 || MaxSizeInBytes <= sizeof (CHAR)) {
  887. if (MaxSizeInBytes >= sizeof (CHAR)) {
  888. *Buffer = 0;
  889. }
  890. if (OutboundBytesPtr) {
  891. *OutboundBytesPtr = 0;
  892. }
  893. return FALSE;
  894. }
  895. //
  896. // If caller did not specify inbound size, compute it now
  897. //
  898. if (!InboundBytes) {
  899. InboundBytes = ByteCountW (SrcBuffer);
  900. } else {
  901. InboundBytes = (InboundBytes / sizeof (WCHAR)) * sizeof (WCHAR);
  902. }
  903. inboundSize = InboundBytes + sizeof (WCHAR);
  904. //
  905. // Allocate a buffer big enough for the lower-cased input string,
  906. // plus (optionally) a copy of the entire destination buffer. Then
  907. // copy the data to the buffer.
  908. //
  909. sizeOfTempBuf = inboundSize;
  910. if (SrcBuffer == Buffer) {
  911. sizeOfTempBuf += MaxSizeInBytes;
  912. }
  913. lowerCaseSrc = AllocTextW (sizeOfTempBuf);
  914. if (!lowerCaseSrc) {
  915. return FALSE;
  916. }
  917. CopyMemory ((PWSTR) lowerCaseSrc, SrcBuffer, InboundBytes);
  918. *((PWSTR) ((PBYTE) lowerCaseSrc + InboundBytes)) = 0;
  919. CharLowerBuffW ((PWSTR) lowerCaseSrc, InboundBytes / sizeof (WCHAR));
  920. if (SrcBuffer == Buffer && !(Flags & STRMAP_COMPLETE_MATCH_ONLY)) {
  921. orgSrc = (PCWSTR) ((PBYTE) lowerCaseSrc + inboundSize);
  922. //
  923. // If we are processing entire inbound string, then just copy the
  924. // whole string. Otherwise, copy the entire destination buffer, so we
  925. // don't lose data beyond the partial inbound string.
  926. //
  927. if (*((PCWSTR) ((PBYTE) SrcBuffer + InboundBytes))) {
  928. CopyMemory ((PWSTR) orgSrc, SrcBuffer, MaxSizeInBytes);
  929. } else {
  930. CopyMemory ((PWSTR) orgSrc, SrcBuffer, inboundSize);
  931. }
  932. } else {
  933. orgSrc = SrcBuffer;
  934. }
  935. //
  936. // Walk the lower cased string, looking for strings to replace
  937. //
  938. orgSrcPos = orgSrc;
  939. lowerSrcPos = lowerCaseSrc;
  940. lowerSrcEnd = (PCWSTR) ((PBYTE) lowerSrcPos + InboundBytes);
  941. destPos = Buffer;
  942. destBytesLeft = MaxSizeInBytes - sizeof (WCHAR);
  943. filterData.UnicodeData = TRUE;
  944. filterData.Unicode.OriginalString = orgSrc;
  945. filterData.Unicode.CurrentString = Buffer;
  946. endPtr = NULL;
  947. while (lowerSrcPos < lowerSrcEnd) {
  948. replaceString = pFindReplacementStringW (
  949. MapArray,
  950. MapArrayCount,
  951. lowerSrcPos,
  952. (HALF_PTR) ((PBYTE) lowerSrcEnd - (PBYTE) lowerSrcPos),
  953. &searchStringBytes,
  954. &replaceStringBytes,
  955. &filterData,
  956. ExtraDataValue,
  957. (Flags & STRMAP_REQUIRE_WACK_OR_NUL) != 0
  958. );
  959. if (replaceString) {
  960. //
  961. // Implement complete match flag
  962. //
  963. if (Flags & STRMAP_COMPLETE_MATCH_ONLY) {
  964. if (InboundBytes != searchStringBytes) {
  965. break;
  966. }
  967. }
  968. result = TRUE;
  969. //
  970. // Verify replacement string isn't growing string too much. If it
  971. // is, truncate the replacement string.
  972. //
  973. if (destBytesLeft < replaceStringBytes) {
  974. replaceStringBytes = destBytesLeft;
  975. } else {
  976. destBytesLeft -= replaceStringBytes;
  977. }
  978. //
  979. // Transfer the memory
  980. //
  981. CopyMemory (destPos, replaceString, replaceStringBytes);
  982. destPos = (PWSTR) ((PBYTE) destPos + replaceStringBytes);
  983. lowerSrcPos = (PCWSTR) ((PBYTE) lowerSrcPos + searchStringBytes);
  984. orgSrcPos = (PCWSTR) ((PBYTE) orgSrcPos + searchStringBytes);
  985. //
  986. // Implement single match flag
  987. //
  988. if (Flags & STRMAP_RETURN_AFTER_FIRST_REPLACE) {
  989. endPtr = destPos;
  990. break;
  991. }
  992. } else if (Flags & (STRMAP_FIRST_CHAR_MUST_MATCH|STRMAP_COMPLETE_MATCH_ONLY)) {
  993. //
  994. // This string does not match any search strings
  995. //
  996. break;
  997. } else {
  998. //
  999. // This character does not match, so copy it to the destination and
  1000. // try the next string.
  1001. //
  1002. if (destBytesLeft < sizeof (WCHAR)) {
  1003. break;
  1004. }
  1005. *destPos++ = *orgSrcPos++;
  1006. destBytesLeft -= sizeof (WCHAR);
  1007. lowerSrcPos++;
  1008. }
  1009. }
  1010. //
  1011. // Copy any remaining part of the original source to the
  1012. // destination, unless destPos == Buffer == SrcBuffer
  1013. //
  1014. if (destPos != SrcBuffer) {
  1015. if (*orgSrcPos) {
  1016. orgSrcBytesLeft = ByteCountW (orgSrcPos);
  1017. orgSrcBytesLeft = min (orgSrcBytesLeft, destBytesLeft);
  1018. CopyMemory (destPos, orgSrcPos, orgSrcBytesLeft);
  1019. destPos = (PWSTR) ((PBYTE) destPos + orgSrcBytesLeft);
  1020. }
  1021. MYASSERT ((PBYTE) (destPos + 1) <= ((PBYTE) Buffer + MaxSizeInBytes));
  1022. *destPos = 0;
  1023. if (!endPtr) {
  1024. endPtr = destPos;
  1025. }
  1026. } else {
  1027. MYASSERT (SrcBuffer == Buffer);
  1028. if (EndOfString || OutboundBytesPtr) {
  1029. endPtr = GetEndOfStringW (destPos);
  1030. }
  1031. }
  1032. if (EndOfString) {
  1033. MYASSERT (endPtr);
  1034. *EndOfString = endPtr;
  1035. }
  1036. if (OutboundBytesPtr) {
  1037. MYASSERT (endPtr);
  1038. if (*endPtr) {
  1039. endPtr = GetEndOfStringW (endPtr);
  1040. }
  1041. *OutboundBytesPtr = (HALF_PTR) ((PBYTE) endPtr - (PBYTE) Buffer);
  1042. }
  1043. FreeTextW (lowerCaseSrc);
  1044. return result;
  1045. }
  1046. BOOL
  1047. MappingSearchAndReplaceExA (
  1048. IN PMAPSTRUCT Map,
  1049. IN PCSTR SrcBuffer,
  1050. OUT PSTR Buffer, // can be the same as SrcBuffer
  1051. IN INT InboundBytes, OPTIONAL
  1052. OUT PINT OutboundBytesPtr, OPTIONAL
  1053. IN INT MaxSizeInBytes,
  1054. IN DWORD Flags,
  1055. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  1056. OUT PCSTR *EndOfString OPTIONAL
  1057. )
  1058. {
  1059. return MappingMultiTableSearchAndReplaceExA (
  1060. &Map,
  1061. 1,
  1062. SrcBuffer,
  1063. Buffer,
  1064. InboundBytes,
  1065. OutboundBytesPtr,
  1066. MaxSizeInBytes,
  1067. Flags,
  1068. ExtraDataValue,
  1069. EndOfString
  1070. );
  1071. }
  1072. BOOL
  1073. MappingSearchAndReplaceExW (
  1074. IN PMAPSTRUCT Map,
  1075. IN PCWSTR SrcBuffer,
  1076. OUT PWSTR Buffer, // can be the same as SrcBuffer
  1077. IN INT InboundBytes, OPTIONAL
  1078. OUT PINT OutboundBytesPtr, OPTIONAL
  1079. IN INT MaxSizeInBytes,
  1080. IN DWORD Flags,
  1081. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  1082. OUT PCWSTR *EndOfString OPTIONAL
  1083. )
  1084. {
  1085. return MappingMultiTableSearchAndReplaceExW (
  1086. &Map,
  1087. 1,
  1088. SrcBuffer,
  1089. Buffer,
  1090. InboundBytes,
  1091. OutboundBytesPtr,
  1092. MaxSizeInBytes,
  1093. Flags,
  1094. ExtraDataValue,
  1095. EndOfString
  1096. );
  1097. }