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.

1413 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. PMHANDLE Pool;
  83. PMAPSTRUCT Map;
  84. Pool = PmCreateNamedPool ("String Mapping");
  85. MYASSERT (Pool);
  86. Map = (PMAPSTRUCT) PmGetAlignedMemory (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. PmEmptyPool (Map->Pool);
  102. PmDestroyPool (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 = PmGetAlignedMemory (Map->Pool, sizeof (CHARNODEEX));
  139. Node = (PCHARNODE) exNode;
  140. MYASSERT (Node);
  141. ZeroMemory (exNode, sizeof (CHARNODEEX));
  142. } else {
  143. Node = PmGetAlignedMemory (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 = PmDuplicateStringA (Map->Pool, Old);
  205. NewCopy = PmDuplicateStringA (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 = PmDuplicateStringW (Map->Pool, Old);
  287. NewCopy = PmDuplicateStringW (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 = NULL;
  340. INT newStringSizeInBytes = 0;
  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 = NULL;
  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. CopyMemory ((PSTR) lowerCaseSrc, SrcBuffer, InboundBytes);
  689. *((PSTR) ((PBYTE) lowerCaseSrc + InboundBytes)) = 0;
  690. CharLowerBuffA ((PSTR) lowerCaseSrc, InboundBytes / sizeof (CHAR));
  691. if (SrcBuffer == Buffer && !(Flags & STRMAP_COMPLETE_MATCH_ONLY)) {
  692. orgSrc = (PCSTR) ((PBYTE) lowerCaseSrc + inboundSize);
  693. //
  694. // If we are processing entire inbound string, then just copy the
  695. // whole string. Otherwise, copy the entire destination buffer, so we
  696. // don't lose data beyond the partial inbound string.
  697. //
  698. if (*((PCSTR) ((PBYTE) SrcBuffer + InboundBytes))) {
  699. CopyMemory ((PSTR) orgSrc, SrcBuffer, MaxSizeInBytes);
  700. } else {
  701. CopyMemory ((PSTR) orgSrc, SrcBuffer, inboundSize);
  702. }
  703. } else {
  704. orgSrc = SrcBuffer;
  705. }
  706. //
  707. // Walk the lower cased string, looking for strings to replace
  708. //
  709. orgSrcPos = orgSrc;
  710. lowerSrcPos = lowerCaseSrc;
  711. lowerSrcEnd = (PCSTR) ((PBYTE) lowerSrcPos + InboundBytes);
  712. destPos = Buffer;
  713. destBytesLeft = MaxSizeInBytes - sizeof (CHAR);
  714. filterData.UnicodeData = FALSE;
  715. filterData.Ansi.OriginalString = orgSrc;
  716. filterData.Ansi.CurrentString = Buffer;
  717. endPtr = NULL;
  718. while (lowerSrcPos < lowerSrcEnd) {
  719. replaceString = pFindReplacementStringA (
  720. MapArray,
  721. MapArrayCount,
  722. lowerSrcPos,
  723. (HALF_PTR) ((PBYTE) lowerSrcEnd - (PBYTE) lowerSrcPos),
  724. &searchStringBytes,
  725. &replaceStringBytes,
  726. &filterData,
  727. ExtraDataValue,
  728. (Flags & STRMAP_REQUIRE_WACK_OR_NUL) != 0
  729. );
  730. if (replaceString) {
  731. //
  732. // Implement complete match flag
  733. //
  734. if (Flags & STRMAP_COMPLETE_MATCH_ONLY) {
  735. if (InboundBytes != searchStringBytes) {
  736. break;
  737. }
  738. }
  739. result = TRUE;
  740. //
  741. // Verify replacement string isn't growing string too much. If it
  742. // is, truncate the replacement string.
  743. //
  744. if (destBytesLeft < replaceStringBytes) {
  745. //
  746. // Respect logical dbcs characters
  747. //
  748. replaceStringBytes = 0;
  749. i = 0;
  750. while (i < destBytesLeft) {
  751. MYASSERT (replaceString[i]);
  752. if (IsLeadByte (&replaceString[i])) {
  753. MYASSERT (replaceString[i + 1]);
  754. i += 2;
  755. } else {
  756. i++;
  757. }
  758. }
  759. if (i > destBytesLeft) {
  760. destBytesLeft--;
  761. }
  762. replaceStringBytes = destBytesLeft;
  763. } else {
  764. destBytesLeft -= replaceStringBytes;
  765. }
  766. //
  767. // Transfer the memory
  768. //
  769. CopyMemory (destPos, replaceString, replaceStringBytes);
  770. destPos = (PSTR) ((PBYTE) destPos + replaceStringBytes);
  771. lowerSrcPos = (PCSTR) ((PBYTE) lowerSrcPos + searchStringBytes);
  772. orgSrcPos = (PCSTR) ((PBYTE) orgSrcPos + searchStringBytes);
  773. //
  774. // Implement single match flag
  775. //
  776. if (Flags & STRMAP_RETURN_AFTER_FIRST_REPLACE) {
  777. endPtr = destPos;
  778. break;
  779. }
  780. } else if (Flags & (STRMAP_FIRST_CHAR_MUST_MATCH|STRMAP_COMPLETE_MATCH_ONLY)) {
  781. //
  782. // This string does not match any search strings
  783. //
  784. break;
  785. } else {
  786. //
  787. // This character does not match, so copy it to the destination and
  788. // try the next string.
  789. //
  790. if (IsLeadByte (orgSrcPos)) {
  791. //
  792. // Copy double-byte character
  793. //
  794. if (destBytesLeft < sizeof (CHAR) * 2) {
  795. break;
  796. }
  797. MYASSERT (sizeof (CHAR) * 2 == sizeof (WORD));
  798. *((PWORD) destPos)++ = *((PWORD) orgSrcPos)++;
  799. destBytesLeft -= sizeof (WORD);
  800. lowerSrcPos = (PCSTR) ((PBYTE) lowerSrcPos + sizeof (WORD));
  801. } else {
  802. //
  803. // Copy single-byte character
  804. //
  805. if (destBytesLeft < sizeof (CHAR)) {
  806. break;
  807. }
  808. *destPos++ = *orgSrcPos++;
  809. destBytesLeft -= sizeof (CHAR);
  810. lowerSrcPos++;
  811. }
  812. }
  813. }
  814. //
  815. // Copy any remaining part of the original source to the
  816. // destination, unless destPos == Buffer == SrcBuffer
  817. //
  818. if (destPos != SrcBuffer) {
  819. if (*orgSrcPos) {
  820. orgSrcBytesLeft = ByteCountA (orgSrcPos);
  821. orgSrcBytesLeft = min (orgSrcBytesLeft, destBytesLeft);
  822. CopyMemory (destPos, orgSrcPos, orgSrcBytesLeft);
  823. destPos = (PSTR) ((PBYTE) destPos + orgSrcBytesLeft);
  824. }
  825. MYASSERT ((PBYTE) (destPos + 1) <= ((PBYTE) Buffer + MaxSizeInBytes));
  826. *destPos = 0;
  827. if (!endPtr) {
  828. endPtr = destPos;
  829. }
  830. } else {
  831. MYASSERT (SrcBuffer == Buffer);
  832. if (EndOfString || OutboundBytesPtr) {
  833. endPtr = GetEndOfStringA (destPos);
  834. }
  835. }
  836. if (EndOfString) {
  837. MYASSERT (endPtr);
  838. *EndOfString = endPtr;
  839. }
  840. if (OutboundBytesPtr) {
  841. MYASSERT (endPtr);
  842. if (*endPtr) {
  843. endPtr = GetEndOfStringA (endPtr);
  844. }
  845. *OutboundBytesPtr = (HALF_PTR) ((PBYTE) endPtr - (PBYTE) Buffer);
  846. }
  847. FreeTextA (lowerCaseSrc);
  848. return result;
  849. }
  850. BOOL
  851. MappingMultiTableSearchAndReplaceExW (
  852. IN PMAPSTRUCT *MapArray,
  853. IN UINT MapArrayCount,
  854. IN PCWSTR SrcBuffer,
  855. OUT PWSTR Buffer, // can be the same as SrcBuffer
  856. IN INT InboundBytes, OPTIONAL
  857. OUT PINT OutboundBytesPtr, OPTIONAL
  858. IN INT MaxSizeInBytes,
  859. IN DWORD Flags,
  860. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  861. OUT PCWSTR *EndOfString OPTIONAL
  862. )
  863. {
  864. UINT sizeOfTempBuf;
  865. INT inboundSize;
  866. PCWSTR lowerCaseSrc;
  867. PCWSTR orgSrc;
  868. PCWSTR lowerSrcPos;
  869. PCWSTR orgSrcPos;
  870. INT orgSrcBytesLeft;
  871. PWSTR destPos;
  872. PCWSTR lowerSrcEnd;
  873. INT searchStringBytes;
  874. INT replaceStringBytes;
  875. INT destBytesLeft;
  876. REG_REPLACE_DATA filterData;
  877. PCWSTR replaceString;
  878. BOOL result = FALSE;
  879. PCWSTR endPtr;
  880. //
  881. // Empty string case
  882. //
  883. if (*SrcBuffer == 0 || MaxSizeInBytes <= sizeof (CHAR)) {
  884. if (MaxSizeInBytes >= sizeof (CHAR)) {
  885. *Buffer = 0;
  886. }
  887. if (OutboundBytesPtr) {
  888. *OutboundBytesPtr = 0;
  889. }
  890. return FALSE;
  891. }
  892. //
  893. // If caller did not specify inbound size, compute it now
  894. //
  895. if (!InboundBytes) {
  896. InboundBytes = ByteCountW (SrcBuffer);
  897. } else {
  898. InboundBytes = (InboundBytes / sizeof (WCHAR)) * sizeof (WCHAR);
  899. }
  900. inboundSize = InboundBytes + sizeof (WCHAR);
  901. //
  902. // Allocate a buffer big enough for the lower-cased input string,
  903. // plus (optionally) a copy of the entire destination buffer. Then
  904. // copy the data to the buffer.
  905. //
  906. sizeOfTempBuf = inboundSize;
  907. if (SrcBuffer == Buffer) {
  908. sizeOfTempBuf += MaxSizeInBytes;
  909. }
  910. lowerCaseSrc = AllocTextW (sizeOfTempBuf);
  911. CopyMemory ((PWSTR) lowerCaseSrc, SrcBuffer, InboundBytes);
  912. *((PWSTR) ((PBYTE) lowerCaseSrc + InboundBytes)) = 0;
  913. CharLowerBuffW ((PWSTR) lowerCaseSrc, InboundBytes / sizeof (WCHAR));
  914. if (SrcBuffer == Buffer && !(Flags & STRMAP_COMPLETE_MATCH_ONLY)) {
  915. orgSrc = (PCWSTR) ((PBYTE) lowerCaseSrc + inboundSize);
  916. //
  917. // If we are processing entire inbound string, then just copy the
  918. // whole string. Otherwise, copy the entire destination buffer, so we
  919. // don't lose data beyond the partial inbound string.
  920. //
  921. if (*((PCWSTR) ((PBYTE) SrcBuffer + InboundBytes))) {
  922. CopyMemory ((PWSTR) orgSrc, SrcBuffer, MaxSizeInBytes);
  923. } else {
  924. CopyMemory ((PWSTR) orgSrc, SrcBuffer, inboundSize);
  925. }
  926. } else {
  927. orgSrc = SrcBuffer;
  928. }
  929. //
  930. // Walk the lower cased string, looking for strings to replace
  931. //
  932. orgSrcPos = orgSrc;
  933. lowerSrcPos = lowerCaseSrc;
  934. lowerSrcEnd = (PCWSTR) ((PBYTE) lowerSrcPos + InboundBytes);
  935. destPos = Buffer;
  936. destBytesLeft = MaxSizeInBytes - sizeof (WCHAR);
  937. filterData.UnicodeData = TRUE;
  938. filterData.Unicode.OriginalString = orgSrc;
  939. filterData.Unicode.CurrentString = Buffer;
  940. endPtr = NULL;
  941. while (lowerSrcPos < lowerSrcEnd) {
  942. replaceString = pFindReplacementStringW (
  943. MapArray,
  944. MapArrayCount,
  945. lowerSrcPos,
  946. (HALF_PTR) ((PBYTE) lowerSrcEnd - (PBYTE) lowerSrcPos),
  947. &searchStringBytes,
  948. &replaceStringBytes,
  949. &filterData,
  950. ExtraDataValue,
  951. (Flags & STRMAP_REQUIRE_WACK_OR_NUL) != 0
  952. );
  953. if (replaceString) {
  954. //
  955. // Implement complete match flag
  956. //
  957. if (Flags & STRMAP_COMPLETE_MATCH_ONLY) {
  958. if (InboundBytes != searchStringBytes) {
  959. break;
  960. }
  961. }
  962. result = TRUE;
  963. //
  964. // Verify replacement string isn't growing string too much. If it
  965. // is, truncate the replacement string.
  966. //
  967. if (destBytesLeft < replaceStringBytes) {
  968. replaceStringBytes = destBytesLeft;
  969. } else {
  970. destBytesLeft -= replaceStringBytes;
  971. }
  972. //
  973. // Transfer the memory
  974. //
  975. CopyMemory (destPos, replaceString, replaceStringBytes);
  976. destPos = (PWSTR) ((PBYTE) destPos + replaceStringBytes);
  977. lowerSrcPos = (PCWSTR) ((PBYTE) lowerSrcPos + searchStringBytes);
  978. orgSrcPos = (PCWSTR) ((PBYTE) orgSrcPos + searchStringBytes);
  979. //
  980. // Implement single match flag
  981. //
  982. if (Flags & STRMAP_RETURN_AFTER_FIRST_REPLACE) {
  983. endPtr = destPos;
  984. break;
  985. }
  986. } else if (Flags & (STRMAP_FIRST_CHAR_MUST_MATCH|STRMAP_COMPLETE_MATCH_ONLY)) {
  987. //
  988. // This string does not match any search strings
  989. //
  990. break;
  991. } else {
  992. //
  993. // This character does not match, so copy it to the destination and
  994. // try the next string.
  995. //
  996. if (destBytesLeft < sizeof (WCHAR)) {
  997. break;
  998. }
  999. *destPos++ = *orgSrcPos++;
  1000. destBytesLeft -= sizeof (WCHAR);
  1001. lowerSrcPos++;
  1002. }
  1003. }
  1004. //
  1005. // Copy any remaining part of the original source to the
  1006. // destination, unless destPos == Buffer == SrcBuffer
  1007. //
  1008. if (destPos != SrcBuffer) {
  1009. if (*orgSrcPos) {
  1010. orgSrcBytesLeft = ByteCountW (orgSrcPos);
  1011. orgSrcBytesLeft = min (orgSrcBytesLeft, destBytesLeft);
  1012. CopyMemory (destPos, orgSrcPos, orgSrcBytesLeft);
  1013. destPos = (PWSTR) ((PBYTE) destPos + orgSrcBytesLeft);
  1014. }
  1015. MYASSERT ((PBYTE) (destPos + 1) <= ((PBYTE) Buffer + MaxSizeInBytes));
  1016. *destPos = 0;
  1017. if (!endPtr) {
  1018. endPtr = destPos;
  1019. }
  1020. } else {
  1021. MYASSERT (SrcBuffer == Buffer);
  1022. if (EndOfString || OutboundBytesPtr) {
  1023. endPtr = GetEndOfStringW (destPos);
  1024. }
  1025. }
  1026. if (EndOfString) {
  1027. MYASSERT (endPtr);
  1028. *EndOfString = endPtr;
  1029. }
  1030. if (OutboundBytesPtr) {
  1031. MYASSERT (endPtr);
  1032. if (*endPtr) {
  1033. endPtr = GetEndOfStringW (endPtr);
  1034. }
  1035. *OutboundBytesPtr = (HALF_PTR) ((PBYTE) endPtr - (PBYTE) Buffer);
  1036. }
  1037. FreeTextW (lowerCaseSrc);
  1038. return result;
  1039. }
  1040. BOOL
  1041. MappingSearchAndReplaceExA (
  1042. IN PMAPSTRUCT Map,
  1043. IN PCSTR SrcBuffer,
  1044. OUT PSTR Buffer, // can be the same as SrcBuffer
  1045. IN INT InboundBytes, OPTIONAL
  1046. OUT PINT OutboundBytesPtr, OPTIONAL
  1047. IN INT MaxSizeInBytes,
  1048. IN DWORD Flags,
  1049. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  1050. OUT PCSTR *EndOfString OPTIONAL
  1051. )
  1052. {
  1053. return MappingMultiTableSearchAndReplaceExA (
  1054. &Map,
  1055. 1,
  1056. SrcBuffer,
  1057. Buffer,
  1058. InboundBytes,
  1059. OutboundBytesPtr,
  1060. MaxSizeInBytes,
  1061. Flags,
  1062. ExtraDataValue,
  1063. EndOfString
  1064. );
  1065. }
  1066. BOOL
  1067. MappingSearchAndReplaceExW (
  1068. IN PMAPSTRUCT Map,
  1069. IN PCWSTR SrcBuffer,
  1070. OUT PWSTR Buffer, // can be the same as SrcBuffer
  1071. IN INT InboundBytes, OPTIONAL
  1072. OUT PINT OutboundBytesPtr, OPTIONAL
  1073. IN INT MaxSizeInBytes,
  1074. IN DWORD Flags,
  1075. OUT ULONG_PTR *ExtraDataValue, OPTIONAL
  1076. OUT PCWSTR *EndOfString OPTIONAL
  1077. )
  1078. {
  1079. return MappingMultiTableSearchAndReplaceExW (
  1080. &Map,
  1081. 1,
  1082. SrcBuffer,
  1083. Buffer,
  1084. InboundBytes,
  1085. OutboundBytesPtr,
  1086. MaxSizeInBytes,
  1087. Flags,
  1088. ExtraDataValue,
  1089. EndOfString
  1090. );
  1091. }