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.

136 lines
3.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: custring.cxx
  7. //
  8. // Contents: This has the code for the UNICODE_STRING class.
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 26-Jan-93 SudK Created
  15. //
  16. //--------------------------------------------------------------------------
  17. #include <headers.hxx>
  18. #pragma hdrstop
  19. // C Runtime Headers
  20. #include <malloc.h>
  21. #include <memory.h>
  22. #include <string.h>
  23. #include <wchar.h>
  24. #include <stdlib.h>
  25. // NT Headers
  26. #include <nt.h>
  27. #include <ntrtl.h>
  28. #include <windef.h>
  29. //
  30. // String manipulation routines
  31. //
  32. //+-------------------------------------------------------------------------
  33. //
  34. // Function: SRtlDuplicateString
  35. //
  36. // Synopsis: Makes a copy of the string referenced by pSrc
  37. //
  38. // Effects: Memory is allocated from the process heap.
  39. // An exact copy is made, except that:
  40. // Buffer is zero'ed
  41. // Length bytes are copied
  42. //
  43. // Arguments: [pDest] -- The destination unicode string.
  44. // [pSrc] -- The source unicode string.
  45. //
  46. // Returns: Nothing
  47. //
  48. // Notes: The string is terminated with a NULL but this need not be
  49. // a part of the Length of the UNICODE_STRING.
  50. // This func will throw an exception if a memory failure occurs.
  51. //
  52. //--------------------------------------------------------------------------
  53. void
  54. SRtlDuplicateString(PUNICODE_STRING pDest,
  55. PUNICODE_STRING pSrc)
  56. {
  57. pDest->Buffer = new WCHAR[pSrc->MaximumLength/sizeof(WCHAR) + 1];
  58. if (pDest->Buffer != NULL) {
  59. memset(pDest->Buffer, 0, pSrc->MaximumLength);
  60. memmove(pDest->Buffer, pSrc->Buffer, pSrc->Length);
  61. pDest->Buffer[pSrc->Length/sizeof(WCHAR)] = UNICODE_NULL;
  62. pDest->MaximumLength = pSrc->MaximumLength;
  63. pDest->Length = pSrc->Length;
  64. } else {
  65. pDest->MaximumLength = 0;
  66. pDest->Length = 0;
  67. }
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Function: SRtlNewString
  72. //
  73. // Synopsis: Creates a COPY of the wide character string pointer pSrc
  74. // and sets up a UNICODE_STRING appropriately
  75. //
  76. //
  77. // Effects: Memory is allocated off of the process heap
  78. // The string remains null terminated.
  79. //
  80. //
  81. // Arguments: [pDest] -- Destination UNICODE_STRING.
  82. // [pSrc] -- The WCHAR string that needs to be copied.
  83. //
  84. // Returns: Nothing
  85. //
  86. // Note: This func will throw an exception if a memory failure occurs.
  87. // The String is terminated with a NULL but this need not be a
  88. // part of the Length of the UNICODE_STRING. If the input string
  89. // is NULL then this function will create a NULL UNICODE_STRING.
  90. //
  91. //--------------------------------------------------------------------------
  92. void
  93. SRtlNewString( PUNICODE_STRING pDest,
  94. PWCHAR pSrc)
  95. {
  96. UNICODE_STRING Source;
  97. if (pSrc!=NULL) {
  98. Source.Length = wcslen((WCHAR *) pSrc) * sizeof(WCHAR);
  99. Source.MaximumLength = Source.Length + sizeof(WCHAR);
  100. Source.Buffer = pSrc;
  101. SRtlDuplicateString(pDest, &Source);
  102. }
  103. else {
  104. pDest->Length = pDest->MaximumLength = 0;
  105. pDest->Buffer = NULL;
  106. }
  107. }
  108. //+-------------------------------------------------------------------------
  109. //
  110. // Function: SRtlFreeString
  111. //
  112. // Synopsis: Takes a UNICODE_STRING and frees the memory associated with it.
  113. // This method also sets the Buffer Pointer to NULL and zeroes
  114. // out both the Length fields in the UNICODE_STRING.
  115. //
  116. // Arguments: [pString] -- The UNICODE string that needs to be deallocated.
  117. //
  118. // Returns: Nothing
  119. //
  120. //--------------------------------------------------------------------------
  121. void
  122. SRtlFreeString( PUNICODE_STRING pString)
  123. {
  124. delete [] pString->Buffer;
  125. pString->Length = pString->MaximumLength = 0;
  126. pString->Buffer = NULL;
  127. }