Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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