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.

250 lines
5.6 KiB

  1. //--------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1999, Microsoft Corporation
  4. //
  5. // File: dfsstrings.hxx
  6. //
  7. //--------------------------------------------------------------------------
  8. #ifndef __DFS_STRINGS__
  9. #define __DFS_STRINGS__
  10. #include "dfsmisc.h"
  11. class DfsString
  12. {
  13. private:
  14. UNICODE_STRING _StringData;
  15. BOOLEAN _Allocated;
  16. DFSSTATUS
  17. AllocateString(size_t CharacterCount)
  18. {
  19. DFSSTATUS Status = ERROR_SUCCESS;
  20. if (CharacterCount >= MAXUSHORT)
  21. {
  22. return ERROR_INVALID_PARAMETER;
  23. }
  24. if (_StringData.MaximumLength >= (CharacterCount * sizeof(WCHAR)))
  25. {
  26. _StringData.Length = 0;
  27. return ERROR_SUCCESS;
  28. }
  29. if (_StringData.Buffer != NULL)
  30. {
  31. delete [] _StringData.Buffer;
  32. RtlInitUnicodeString(&_StringData, NULL);
  33. }
  34. _StringData.Buffer = new WCHAR[CharacterCount];
  35. if (_StringData.Buffer == NULL)
  36. {
  37. Status = ERROR_NOT_ENOUGH_MEMORY;
  38. }
  39. else
  40. {
  41. _Allocated = TRUE;
  42. _StringData.MaximumLength = CharacterCount * sizeof(WCHAR);
  43. }
  44. return Status;
  45. }
  46. DFSSTATUS
  47. CopyCountedString( IN PUNICODE_STRING pUnicodeString)
  48. {
  49. ULONG TotalCount = pUnicodeString->Length / sizeof(WCHAR);
  50. ULONG StringLength = TotalCount;
  51. DFSSTATUS Status;
  52. if ((TotalCount * sizeof(WCHAR)) >= MAXUSHORT)
  53. {
  54. return ERROR_INVALID_PARAMETER;
  55. }
  56. if( (_StringData.Buffer == pUnicodeString->Buffer) &&
  57. (pUnicodeString->Buffer != NULL))
  58. {
  59. return ERROR_INVALID_PARAMETER;
  60. }
  61. //
  62. // Total Count will include the last null. String length does not.
  63. //
  64. if ((StringLength == 0) ||
  65. (pUnicodeString->Buffer[StringLength - 1] != UNICODE_NULL))
  66. {
  67. TotalCount++;
  68. }
  69. else
  70. {
  71. StringLength--;
  72. }
  73. Status = AllocateString(TotalCount);
  74. if (Status == ERROR_SUCCESS)
  75. {
  76. RtlCopyMemory(_StringData.Buffer,
  77. pUnicodeString->Buffer,
  78. StringLength * sizeof(WCHAR));
  79. _StringData.Length = (USHORT)(StringLength * sizeof(WCHAR));
  80. _StringData.Buffer[StringLength] = UNICODE_NULL;
  81. }
  82. return Status;
  83. }
  84. BOOLEAN
  85. CompareCountedString( IN PUNICODE_STRING pInUnicodeString)
  86. {
  87. if (RtlCompareUnicodeString(&_StringData,
  88. pInUnicodeString,
  89. TRUE) == 0)
  90. {
  91. return TRUE;
  92. }
  93. return FALSE;
  94. }
  95. public:
  96. DfsString()
  97. {
  98. _Allocated = FALSE;
  99. RtlInitUnicodeString(&_StringData, NULL);
  100. }
  101. DfsString(size_t CharacterCount,
  102. DFSSTATUS *pStatus)
  103. {
  104. *pStatus = ERROR_SUCCESS;
  105. _Allocated = FALSE;
  106. RtlInitUnicodeString(&_StringData, NULL);
  107. *pStatus = AllocateString(CharacterCount);
  108. }
  109. DFSSTATUS
  110. CreateString(IN LPWSTR InString)
  111. {
  112. DFSSTATUS Status = ERROR_SUCCESS;
  113. UNICODE_STRING UnicodeString;
  114. Status = DfsRtlInitUnicodeStringEx(&UnicodeString, InString);
  115. if (Status == ERROR_SUCCESS)
  116. {
  117. Status = CopyCountedString( &UnicodeString);
  118. }
  119. return Status;
  120. }
  121. DFSSTATUS
  122. CreateString(IN PUNICODE_STRING pInUnicodeString)
  123. {
  124. DFSSTATUS Status = ERROR_SUCCESS;
  125. Status = CopyCountedString( pInUnicodeString );
  126. return Status;
  127. }
  128. DFSSTATUS
  129. CreateString(IN DfsString *pString)
  130. {
  131. DFSSTATUS Status = ERROR_SUCCESS;
  132. Status = CopyCountedString( pString->GetCountedString());
  133. return Status;
  134. }
  135. BOOLEAN
  136. operator==(IN LPWSTR InString)
  137. {
  138. UNICODE_STRING UnicodeString;
  139. DFSSTATUS Status;
  140. Status = DfsRtlInitUnicodeStringEx(&UnicodeString, InString);
  141. if (Status != ERROR_SUCCESS)
  142. {
  143. return FALSE;
  144. }
  145. return CompareCountedString( &UnicodeString );
  146. }
  147. BOOLEAN
  148. operator==(IN DfsString *pString)
  149. {
  150. return CompareCountedString( pString->GetCountedString());
  151. }
  152. BOOLEAN
  153. operator==(IN PUNICODE_STRING pInUnicodeString)
  154. {
  155. return CompareCountedString( pInUnicodeString );
  156. }
  157. DFSSTATUS
  158. SetStringToPointer(LPWSTR Buffer)
  159. {
  160. DFSSTATUS Status;
  161. if (wcslen(Buffer) * sizeof(WCHAR) > MAXUSHORT)
  162. {
  163. return ERROR_INVALID_PARAMETER;
  164. }
  165. _Allocated = FALSE;
  166. Status = DfsRtlInitUnicodeStringEx(&_StringData, Buffer);
  167. return Status;
  168. }
  169. DFSSTATUS
  170. SetStringToPointer(PUNICODE_STRING pInput)
  171. {
  172. _StringData = *pInput;
  173. return ERROR_SUCCESS;
  174. }
  175. PUNICODE_STRING
  176. GetCountedString()
  177. {
  178. return &_StringData;
  179. }
  180. LPWSTR
  181. GetString()
  182. {
  183. return _StringData.Buffer;
  184. }
  185. ~DfsString()
  186. {
  187. if (_Allocated && (_StringData.Buffer != NULL))
  188. {
  189. delete [] _StringData.Buffer;
  190. }
  191. RtlInitUnicodeString(&_StringData, NULL);
  192. }
  193. };
  194. #endif // __DFS_STRINGS