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.

831 lines
19 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. strings.c
  5. Abstract:
  6. This module defines functions for manipulating counted strings (STRING).
  7. A counted string is a data structure containing three fields. The Buffer
  8. field is a pointer to the string itself. The MaximumLength field contains
  9. the maximum number of bytes that can be stored in the memory pointed to
  10. by the Buffer field. The Length field contains the current length, in
  11. bytes, of the string pointed to by the Buffer field. Users of counted
  12. strings should not make any assumptions about the existence of a null
  13. byte at the end of the string, unless the null byte is explicitly
  14. included in the Length of the string.
  15. Author:
  16. Steve Wood (stevewo) 31-Mar-1989
  17. Revision History:
  18. 22-Sep-1993 JulieB Fixed TO_UPPER macro for chars above 0x7f.
  19. --*/
  20. #include "string.h"
  21. #include "nt.h"
  22. #include "ntrtlp.h"
  23. #if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
  24. //#pragma alloc_text(NONPAGE,RtlInitString)
  25. //#pragma alloc_text(NONPAGE,RtlInitAnsiString)
  26. //#pragma alloc_text(NONPAGE,RtlInitUnicodeString)
  27. #pragma alloc_text(PAGE,RtlUpperChar)
  28. #pragma alloc_text(PAGE,RtlCompareString)
  29. #pragma alloc_text(PAGE,RtlPrefixString)
  30. #pragma alloc_text(PAGE,RtlCreateUnicodeStringFromAsciiz)
  31. #pragma alloc_text(PAGE,RtlUpperString)
  32. #pragma alloc_text(PAGE,RtlAppendAsciizToString)
  33. #pragma alloc_text(PAGE,RtlAppendStringToString)
  34. #endif
  35. //
  36. // Global data used for translations.
  37. //
  38. extern PUSHORT NlsAnsiToUnicodeData; // Ansi CP to Unicode translation table
  39. extern PCH NlsUnicodeToAnsiData; // Unicode to Ansi CP translation table
  40. extern const PUSHORT NlsLeadByteInfo; // Lead byte info for ACP
  41. extern PUSHORT NlsUnicodeToMbAnsiData; // Unicode to Multibyte Ansi CP translation table
  42. extern BOOLEAN NlsMbCodePageTag; // TRUE -> Multibyte ACP, FALSE -> Singlebyte ACP
  43. #if !defined(_M_IX86)
  44. VOID
  45. RtlInitString(
  46. OUT PSTRING DestinationString,
  47. IN PCSZ SourceString OPTIONAL
  48. )
  49. /*++
  50. Routine Description:
  51. The RtlInitString function initializes an NT counted string.
  52. The DestinationString is initialized to point to the SourceString
  53. and the Length and MaximumLength fields of DestinationString are
  54. initialized to the length of the SourceString, which is zero if
  55. SourceString is not specified.
  56. Arguments:
  57. DestinationString - Pointer to the counted string to initialize
  58. SourceString - Optional pointer to a null terminated string that
  59. the counted string is to point to.
  60. Return Value:
  61. None.
  62. --*/
  63. {
  64. ULONG Length;
  65. DestinationString->Buffer = (PCHAR)SourceString;
  66. if (ARGUMENT_PRESENT( SourceString )) {
  67. Length = strlen(SourceString);
  68. DestinationString->Length = (USHORT)Length;
  69. DestinationString->MaximumLength = (USHORT)(Length+1);
  70. }
  71. else {
  72. DestinationString->Length = 0;
  73. DestinationString->MaximumLength = 0;
  74. }
  75. }
  76. VOID
  77. RtlInitAnsiString(
  78. OUT PANSI_STRING DestinationString,
  79. IN PCSZ SourceString OPTIONAL
  80. )
  81. /*++
  82. Routine Description:
  83. The RtlInitAnsiString function initializes an NT counted string.
  84. The DestinationString is initialized to point to the SourceString
  85. and the Length and MaximumLength fields of DestinationString are
  86. initialized to the length of the SourceString, which is zero if
  87. SourceString is not specified.
  88. Arguments:
  89. DestinationString - Pointer to the counted string to initialize
  90. SourceString - Optional pointer to a null terminated string that
  91. the counted string is to point to.
  92. Return Value:
  93. None.
  94. --*/
  95. {
  96. ULONG Length;
  97. DestinationString->Buffer = (PCHAR)SourceString;
  98. if (ARGUMENT_PRESENT( SourceString )) {
  99. Length = strlen(SourceString);
  100. DestinationString->Length = (USHORT)Length;
  101. DestinationString->MaximumLength = (USHORT)(Length+1);
  102. }
  103. else {
  104. DestinationString->Length = 0;
  105. DestinationString->MaximumLength = 0;
  106. }
  107. }
  108. VOID
  109. RtlInitUnicodeString(
  110. OUT PUNICODE_STRING DestinationString,
  111. IN PCWSTR SourceString OPTIONAL
  112. )
  113. /*++
  114. Routine Description:
  115. The RtlInitUnicodeString function initializes an NT counted
  116. unicode string. The DestinationString is initialized to point to
  117. the SourceString and the Length and MaximumLength fields of
  118. DestinationString are initialized to the length of the SourceString,
  119. which is zero if SourceString is not specified.
  120. Arguments:
  121. DestinationString - Pointer to the counted string to initialize
  122. SourceString - Optional pointer to a null terminated unicode string that
  123. the counted string is to point to.
  124. Return Value:
  125. None.
  126. --*/
  127. {
  128. ULONG Length;
  129. DestinationString->Buffer = (PWSTR)SourceString;
  130. if (ARGUMENT_PRESENT( SourceString )) {
  131. Length = wcslen( SourceString ) * sizeof( WCHAR );
  132. DestinationString->Length = (USHORT)Length;
  133. DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
  134. }
  135. else {
  136. DestinationString->MaximumLength = 0;
  137. DestinationString->Length = 0;
  138. }
  139. }
  140. #endif // !defined(_M_IX86)
  141. NTSTATUS
  142. RtlInitUnicodeStringEx(
  143. OUT PUNICODE_STRING DestinationString,
  144. IN PCWSTR SourceString OPTIONAL
  145. )
  146. {
  147. if (SourceString != NULL) {
  148. SIZE_T Length = wcslen(SourceString);
  149. // We are actually limited to 32765 characters since we want to store a meaningful
  150. // MaximumLength also.
  151. if (Length > (UNICODE_STRING_MAX_CHARS - 1)) {
  152. return STATUS_NAME_TOO_LONG;
  153. }
  154. Length *= sizeof(WCHAR);
  155. DestinationString->Length = (USHORT) Length;
  156. DestinationString->MaximumLength = (USHORT) (Length + sizeof(WCHAR));
  157. DestinationString->Buffer = (PWSTR) SourceString;
  158. } else {
  159. DestinationString->Length = 0;
  160. DestinationString->MaximumLength = 0;
  161. DestinationString->Buffer = NULL;
  162. }
  163. return STATUS_SUCCESS;
  164. }
  165. VOID
  166. RtlCopyString(
  167. OUT PSTRING DestinationString,
  168. IN const STRING *SourceString OPTIONAL
  169. )
  170. /*++
  171. Routine Description:
  172. The RtlCopyString function copies the SourceString to the
  173. DestinationString. If SourceString is not specified, then
  174. the Length field of DestinationString is set to zero. The
  175. MaximumLength and Buffer fields of DestinationString are not
  176. modified by this function.
  177. The number of bytes copied from the SourceString is either the
  178. Length of SourceString or the MaximumLength of DestinationString,
  179. whichever is smaller.
  180. Arguments:
  181. DestinationString - Pointer to the destination string.
  182. SourceString - Optional pointer to the source string.
  183. Return Value:
  184. None.
  185. --*/
  186. {
  187. PSZ src, dst;
  188. ULONG n;
  189. if (ARGUMENT_PRESENT( SourceString )) {
  190. dst = DestinationString->Buffer;
  191. src = SourceString->Buffer;
  192. n = SourceString->Length;
  193. if ((USHORT)n > DestinationString->MaximumLength) {
  194. n = DestinationString->MaximumLength;
  195. }
  196. DestinationString->Length = (USHORT)n;
  197. while (n) {
  198. *dst++ = *src++;
  199. n--;
  200. }
  201. }
  202. else {
  203. DestinationString->Length = 0;
  204. }
  205. }
  206. CHAR
  207. RtlUpperChar (
  208. register IN CHAR Character
  209. )
  210. /*++
  211. Routine Description:
  212. This routine returns a character uppercased
  213. .
  214. Arguments:
  215. IN CHAR Character - Supplies the character to upper case
  216. Return Value:
  217. CHAR - Uppercased version of the charac
  218. ter
  219. --*/
  220. {
  221. RTL_PAGED_CODE();
  222. //
  223. // NOTE: This assumes an ANSI string and it does NOT upper case
  224. // DOUBLE BYTE characters properly.
  225. //
  226. //
  227. // Handle a - z separately.
  228. //
  229. if (Character <= 'z') {
  230. if (Character >= 'a') {
  231. return Character ^ 0x20;
  232. }
  233. else {
  234. return Character;
  235. }
  236. }
  237. else {
  238. WCHAR wCh;
  239. /*
  240. * Handle extended characters.
  241. */
  242. if (!NlsMbCodePageTag) {
  243. //
  244. // Single byte code page.
  245. //
  246. wCh = NlsAnsiToUnicodeData[(UCHAR)Character];
  247. wCh = NLS_UPCASE(wCh);
  248. return NlsUnicodeToAnsiData[(USHORT)wCh];
  249. }
  250. else {
  251. //
  252. // Multi byte code page. Do nothing to the character
  253. // if it's a lead byte or if the translation of the
  254. // upper case Unicode character is a DBCS character.
  255. //
  256. if (!NlsLeadByteInfo[Character]) {
  257. wCh = NlsAnsiToUnicodeData[(UCHAR)Character];
  258. wCh = NLS_UPCASE(wCh);
  259. wCh = NlsUnicodeToMbAnsiData[(USHORT)wCh];
  260. if (!HIBYTE(wCh)) {
  261. return LOBYTE(wCh);
  262. }
  263. }
  264. }
  265. }
  266. return Character;
  267. }
  268. LONG
  269. RtlCompareString(
  270. IN const STRING *String1,
  271. IN const STRING *String2,
  272. IN BOOLEAN CaseInSensitive
  273. )
  274. /*++
  275. Routine Description:
  276. The RtlCompareString function compares two counted strings. The return
  277. value indicates if the strings are equal or String1 is less than String2
  278. or String1 is greater than String2.
  279. The CaseInSensitive parameter specifies if case is to be ignored when
  280. doing the comparison.
  281. Arguments:
  282. String1 - Pointer to the first string.
  283. String2 - Pointer to the second string.
  284. CaseInsensitive - TRUE if case should be ignored when doing the
  285. comparison.
  286. Return Value:
  287. Signed value that gives the results of the comparison:
  288. Zero - String1 equals String2
  289. < Zero - String1 less than String2
  290. > Zero - String1 greater than String2
  291. --*/
  292. {
  293. PUCHAR s1, s2, Limit;
  294. LONG n1, n2;
  295. UCHAR c1, c2;
  296. RTL_PAGED_CODE();
  297. s1 = String1->Buffer;
  298. s2 = String2->Buffer;
  299. n1 = String1->Length;
  300. n2 = String2->Length;
  301. Limit = s1 + (n1 <= n2 ? n1 : n2);
  302. if (CaseInSensitive) {
  303. while (s1 < Limit) {
  304. c1 = *s1++;
  305. c2 = *s2++;
  306. if (c1 !=c2) {
  307. c1 = RtlUpperChar(c1);
  308. c2 = RtlUpperChar(c2);
  309. if (c1 != c2) {
  310. return (LONG)c1 - (LONG)c2;
  311. }
  312. }
  313. }
  314. } else {
  315. while (s1 < Limit) {
  316. c1 = *s1++;
  317. c2 = *s2++;
  318. if (c1 != c2) {
  319. return (LONG)c1 - (LONG)c2;
  320. }
  321. }
  322. }
  323. return n1 - n2;
  324. }
  325. BOOLEAN
  326. RtlEqualString(
  327. IN const STRING *String1,
  328. IN const STRING *String2,
  329. IN BOOLEAN CaseInSensitive
  330. )
  331. /*++
  332. Routine Description:
  333. The RtlEqualString function compares two counted strings for equality.
  334. The CaseInSensitive parameter specifies if case is to be ignored when
  335. doing the comparison.
  336. Arguments:
  337. String1 - Pointer to the first string.
  338. String2 - Pointer to the second string.
  339. CaseInsensitive - TRUE if case should be ignored when doing the
  340. comparison.
  341. Return Value:
  342. Boolean value that is TRUE if String1 equals String2 and FALSE otherwise.
  343. --*/
  344. {
  345. PUCHAR s1, s2, Limit;
  346. LONG n1, n2;
  347. UCHAR c1, c2;
  348. n1 = String1->Length;
  349. n2 = String2->Length;
  350. if (n1 == n2) {
  351. s1 = String1->Buffer;
  352. s2 = String2->Buffer;
  353. Limit = s1 + n1;
  354. if (CaseInSensitive) {
  355. while (s1 < Limit) {
  356. c1 = *s1++;
  357. c2 = *s2++;
  358. if (c1 != c2) {
  359. c1 = RtlUpperChar(c1);
  360. c2 = RtlUpperChar(c2);
  361. if (c1 != c2) {
  362. return FALSE;
  363. }
  364. }
  365. }
  366. return TRUE;
  367. } else {
  368. while (s1 < Limit) {
  369. c1 = *s1++;
  370. c2 = *s2++;
  371. if (c1 != c2) {
  372. return FALSE;
  373. }
  374. }
  375. return TRUE;
  376. }
  377. } else {
  378. return FALSE;
  379. }
  380. }
  381. BOOLEAN
  382. RtlPrefixString(
  383. IN PSTRING String1,
  384. IN PSTRING String2,
  385. IN BOOLEAN CaseInSensitive
  386. )
  387. /*++
  388. Routine Description:
  389. The RtlPrefixString function determines if the String1 counted string
  390. parameter is a prefix of the String2 counted string parameter.
  391. The CaseInSensitive parameter specifies if case is to be ignored when
  392. doing the comparison.
  393. Arguments:
  394. String1 - Pointer to the first string.
  395. String2 - Pointer to the second string.
  396. CaseInsensitive - TRUE if case should be ignored when doing the
  397. comparison.
  398. Return Value:
  399. Boolean value that is TRUE if String1 equals a prefix of String2 and
  400. FALSE otherwise.
  401. --*/
  402. {
  403. PSZ s1, s2;
  404. USHORT n;
  405. UCHAR c1, c2;
  406. RTL_PAGED_CODE();
  407. s1 = String1->Buffer;
  408. s2 = String2->Buffer;
  409. n = String1->Length;
  410. if (String2->Length < n) {
  411. return( FALSE );
  412. }
  413. if (CaseInSensitive) {
  414. while (n) {
  415. c1 = *s1++;
  416. c2 = *s2++;
  417. if (c1 != c2 && RtlUpperChar(c1) != RtlUpperChar(c2)) {
  418. return( FALSE );
  419. }
  420. n--;
  421. }
  422. }
  423. else {
  424. while (n) {
  425. if (*s1++ != *s2++) {
  426. return( FALSE );
  427. }
  428. n--;
  429. }
  430. }
  431. return TRUE;
  432. }
  433. BOOLEAN
  434. RtlCreateUnicodeStringFromAsciiz(
  435. OUT PUNICODE_STRING DestinationString,
  436. IN PCSZ SourceString
  437. )
  438. {
  439. ANSI_STRING AnsiString;
  440. NTSTATUS Status;
  441. RTL_PAGED_CODE();
  442. RtlInitAnsiString( &AnsiString, SourceString );
  443. Status = RtlAnsiStringToUnicodeString( DestinationString, &AnsiString, TRUE );
  444. if (NT_SUCCESS( Status )) {
  445. return( TRUE );
  446. }
  447. else {
  448. return( FALSE );
  449. }
  450. }
  451. VOID
  452. RtlUpperString(
  453. IN PSTRING DestinationString,
  454. IN const STRING *SourceString
  455. )
  456. /*++
  457. Routine Description:
  458. The RtlUpperString function copies the SourceString to the
  459. DestinationString, converting it to upper case. The MaximumLength
  460. and Buffer fields of DestinationString are not modified by this
  461. function.
  462. The number of bytes copied from the SourceString is either the
  463. Length of SourceString or the MaximumLength of DestinationString,
  464. whichever is smaller.
  465. Arguments:
  466. DestinationString - Pointer to the destination string.
  467. SourceString - Pointer to the source string.
  468. Return Value:
  469. None.
  470. --*/
  471. {
  472. PSZ src, dst;
  473. ULONG n;
  474. RTL_PAGED_CODE();
  475. dst = DestinationString->Buffer;
  476. src = SourceString->Buffer;
  477. n = SourceString->Length;
  478. if ((USHORT)n > DestinationString->MaximumLength) {
  479. n = DestinationString->MaximumLength;
  480. }
  481. DestinationString->Length = (USHORT)n;
  482. while (n) {
  483. *dst++ = RtlUpperChar(*src++);
  484. n--;
  485. }
  486. }
  487. NTSTATUS
  488. RtlAppendAsciizToString (
  489. IN PSTRING Destination,
  490. IN PCSZ Source OPTIONAL
  491. )
  492. /*++
  493. Routine Description:
  494. This routine appends the supplied ASCIIZ string to an existing PSTRING.
  495. It will copy bytes from the Source PSZ to the destination PSTRING up to
  496. the destinations PSTRING->MaximumLength field.
  497. Arguments:
  498. IN PSTRING Destination, - Supplies a pointer to the destination string
  499. IN PSZ Source - Supplies the string to append to the destination
  500. Return Value:
  501. STATUS_SUCCESS - The source string was successfully appended to the
  502. destination counted string.
  503. STATUS_BUFFER_TOO_SMALL - The destination string length was not big
  504. enough to allow the source string to be appended. The Destination
  505. string length is not updated.
  506. --*/
  507. {
  508. USHORT n;
  509. RTL_PAGED_CODE();
  510. if (ARGUMENT_PRESENT( Source )) {
  511. n = (USHORT)strlen( Source );
  512. if ((n + Destination->Length) > Destination->MaximumLength) {
  513. return( STATUS_BUFFER_TOO_SMALL );
  514. }
  515. RtlMoveMemory( &Destination->Buffer[ Destination->Length ], Source, n );
  516. Destination->Length += n;
  517. }
  518. return( STATUS_SUCCESS );
  519. }
  520. NTSTATUS
  521. RtlAppendStringToString (
  522. IN PSTRING Destination,
  523. IN const STRING *Source
  524. )
  525. /*++
  526. Routine Description:
  527. This routine will concatinate two PSTRINGs together. It will copy
  528. bytes from the source up to the MaximumLength of the destination.
  529. Arguments:
  530. IN PSTRING Destination, - Supplies the destination string
  531. IN PSTRING Source - Supplies the source for the string copy
  532. Return Value:
  533. STATUS_SUCCESS - The source string was successfully appended to the
  534. destination counted string.
  535. STATUS_BUFFER_TOO_SMALL - The destination string length was not big
  536. enough to allow the source string to be appended. The Destination
  537. string length is not updated.
  538. --*/
  539. {
  540. USHORT n = Source->Length;
  541. RTL_PAGED_CODE();
  542. if (n) {
  543. if ((n + Destination->Length) > Destination->MaximumLength) {
  544. return( STATUS_BUFFER_TOO_SMALL );
  545. }
  546. RtlMoveMemory( &Destination->Buffer[ Destination->Length ],
  547. Source->Buffer,
  548. n
  549. );
  550. Destination->Length += n;
  551. }
  552. return( STATUS_SUCCESS );
  553. }
  554. #if !defined(_X86_) && !defined(_AMD64_)
  555. SIZE_T
  556. NTAPI
  557. RtlCompareMemoryUlong(
  558. PVOID Source,
  559. SIZE_T Length,
  560. ULONG Pattern
  561. )
  562. /*++
  563. Routine Description:
  564. This function compares two blocks of memory and returns the number
  565. of bytes that compared equal.
  566. N.B. This routine requires that the source address is aligned on a
  567. longword boundary and that the length is an even multiple of
  568. longwords.
  569. Arguments:
  570. Source - Supplies a pointer to the block of memory to compare against.
  571. Length - Supplies the Length, in bytes, of the memory to be
  572. compared.
  573. Pattern - Supplies a 32-bit pattern to compare against the block of
  574. memory.
  575. Return Value:
  576. The number of bytes that compared equal is returned as the function
  577. value. If all bytes compared equal, then the length of the orginal
  578. block of memory is returned. Returns zero if either the Source
  579. address is not longword aligned or the length is not a multiple of
  580. longwords.
  581. --*/
  582. {
  583. SIZE_T CountLongs;
  584. PULONG p = (PULONG)Source;
  585. PCHAR p1, p2;
  586. if (((ULONG_PTR)p & (sizeof( ULONG )-1)) ||
  587. (Length & (sizeof( ULONG )-1))
  588. ) {
  589. return( 0 );
  590. }
  591. CountLongs = Length / sizeof( ULONG );
  592. while (CountLongs--) {
  593. if (*p++ != Pattern) {
  594. p1 = (PCHAR)(p - 1);
  595. p2 = (PCHAR)&Pattern;
  596. Length = p1 - (PCHAR)Source;
  597. while (*p1++ == *p2++) {
  598. if (p1 > (PCHAR)p) {
  599. break;
  600. }
  601. Length++;
  602. }
  603. }
  604. }
  605. return( Length );
  606. }
  607. #endif // !defined(_X86_) && !defined(_AMD64_)