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.

220 lines
11 KiB

  1. #if !defined(_BCL_NTBASEUNICODESTRINGBUFFER_H_INCLUDED_)
  2. #define _BCL_NTBASEUNICODESTRINGBUFFER_H_INCLUDED_
  3. #pragma once
  4. /*++
  5. Copyright (c) 2000 Microsoft Corporation
  6. Module Name:
  7. bcl_w32baseunicodestringbuffer.h
  8. Abstract:
  9. Author:
  10. Michael Grier (MGrier) 2/6/2002
  11. Revision History:
  12. --*/
  13. #include <windows.h>
  14. #include <bcl_inlinestring.h>
  15. #include <bcl_unicodechartraits.h>
  16. #include <bcl_w32common.h>
  17. #include <bcl_vararg.h>
  18. #include <bcl_ntunicodestringalgorithms.h>
  19. #include <limits.h>
  20. namespace BCL
  21. {
  22. template <typename TBuffer, typename TCallDispositionT, typename TPublicErrorReturnTypeT>
  23. class CNtBaseUnicodeStringBufferTraits : public BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>, public BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>
  24. {
  25. public:
  26. typedef CNtBaseUnicodeStringBufferTraits TThis;
  27. friend BCL::CPureString<TThis>;
  28. friend BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>;
  29. typedef BCL::CPureString<TThis> TPureString;
  30. typedef TCallDispositionT TCallDisposition;
  31. typedef TPublicErrorReturnTypeT TPublicErrorReturnType;
  32. typedef CNtStringComparisonResult TComparisonResult;
  33. typedef BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> TConstantPair;
  34. typedef BCL::CMutablePointerAndCountPair<WCHAR, SIZE_T> TMutablePair;
  35. typedef CNtCaseInsensitivityData TCaseInsensitivityData;
  36. typedef SIZE_T TSizeT;
  37. typedef CNtMBCSToUnicodeDataIn TDecodingDataIn;
  38. typedef CNtMBCSToUnicodeDataOut TDecodingDataOut;
  39. typedef CNtUnicodeToMBCSDataIn TEncodingDataIn;
  40. typedef CNtUnicodeToMBCSDataOut TEncodingDataOut;
  41. typedef CConstantPointerAndCountPair<CHAR, SIZE_T> TConstantNonNativePair;
  42. typedef CMutablePointerAndCountPair<CHAR, SIZE_T> TMutableNonNativePair;
  43. typedef BCL::CNtPWSTRAllocationHelper TPWSTRAllocationHelper;
  44. typedef BCL::CNtPSTRAllocationHelper TPSTRAllocationHelper;
  45. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TMutableString;
  46. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TConstantString;
  47. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TMutableNonNativeString;
  48. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TConstantNonNativeString;
  49. // exposing the things from our private base class
  50. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::CopyIntoBuffer;
  51. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::CopyIntoBuffer;
  52. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::DetermineRequiredCharacters;
  53. using BCL::CNtNullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::DetermineRequiredCharacters;
  54. static inline TMutablePair & __fastcall MutableBufferPair(BCL::CBaseString *p) { return static_cast<TBuffer *>(p)->m_pair; }
  55. static inline const TConstantPair & __fastcall BufferPair(const BCL::CBaseString *p) { return static_cast<const TConstantPair &>(static_cast<const TBuffer *>(p)->m_pair); }
  56. static inline TConstantPair __fastcall GetStringPair(const BCL::CBaseString *p) { return TConstantPair(TBuffer::TTraits::GetBufferPtr(p), TBuffer::TTraits::GetStringCch(p)); }
  57. static inline TMutablePair __fastcall GetOffsetMutableBufferPair(const BCL::CBaseString *p, TSizeT cchOffset) { BCL_ASSERT(cchOffset <= TBuffer::TTraits::GetBufferCch(p)); if (cchOffset > TBuffer::TTraits::GetBufferCch(p)) cchOffset = TBuffer::TTraits::GetBufferCch(p); return TMutablePair(const_cast<TMutableString>(TBuffer::TTraits::GetBufferPtr(p)) + cchOffset, TBuffer::TTraits::GetBufferCch(p) - cchOffset); }
  58. static inline bool __fastcall AnyAccessors(const BCL::CBaseString *p) { return (static_cast<const TBuffer *>(p)->m_cAttachedAccessors != 0); }
  59. static inline bool __fastcall NoAccessors(const BCL::CBaseString *p) { return (static_cast<const TBuffer *>(p)->m_cAttachedAccessors == 0); }
  60. static inline TCallDisposition __fastcall NoAccessorsCheck(const BCL::CBaseString *p) { if (TBuffer::TTraits::AnyAccessors(p)) { return TCallDisposition::InternalError_ObjectLocked(); } return TCallDisposition::Success(); }
  61. static inline PCWSTR __fastcall GetBufferPtr(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_pair.GetPointer(); }
  62. static inline PWSTR __fastcall GetMutableBufferPtr(BCL::CBaseString *p) { return static_cast<TBuffer *>(p)->m_pair.GetPointer(); }
  63. static inline SIZE_T __fastcall GetBufferCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_pair.GetCount(); }
  64. static inline void __fastcall SetBufferPointerAndCount(BCL::CBaseString *p, PWSTR pszBuffer, SIZE_T cchBuffer) { static_cast<TBuffer *>(p)->m_pair.SetPointerAndCount(pszBuffer, cchBuffer); }
  65. static inline SIZE_T __fastcall GetStringCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_cchString; }
  66. static inline void _fastcall SetStringCch(BCL::CBaseString *p, SIZE_T cch)
  67. {
  68. BCL_ASSERT((cch == 0) || (cch < TBuffer::TTraits::GetBufferCch(p)));
  69. static_cast<TBuffer *>(p)->m_cchString = cch;
  70. if (TBuffer::TTraits::GetBufferCch(p) != 0)
  71. TBuffer::TTraits::GetMutableBufferPtr(p)[cch] = L'\0';
  72. }
  73. static inline void __fastcall IntegrityCheck(const BCL::CBaseString *p) { }
  74. static inline PWSTR __fastcall GetInlineBufferPtr(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->GetInlineBufferPtr(); }
  75. static inline SIZE_T __fastcall GetInlineBufferCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->GetInlineBufferCch(); }
  76. static inline TCallDisposition __fastcall ReallocateBuffer(BCL::CBaseString *p, SIZE_T cch)
  77. {
  78. PWSTR psz = NULL;
  79. SIZE_T cb = (cch * sizeof(WCHAR));
  80. TBuffer *pBuffer = static_cast<TBuffer *>(p);
  81. if (cch != (cb / sizeof(WCHAR)))
  82. return TCallDisposition::ArithmeticOverflow();
  83. if (pBuffer->GetBufferPtr() != NULL)
  84. {
  85. ::SetLastError(ERROR_SUCCESS);
  86. psz =
  87. reinterpret_cast<PWSTR>(
  88. ::RtlReAllocateHeap(
  89. RtlProcessHeap(),
  90. 0,
  91. const_cast<PWSTR>(pBuffer->GetBufferPtr()),
  92. cb));
  93. if (psz == NULL)
  94. {
  95. const DWORD dwLastError = ::GetLastError();
  96. // HeapReAlloc doesn't always set last error, so we rely on this
  97. // fact to find that the win32 last error hasn't changed from
  98. // before to infer ERROR_OUTOFMEMORY. -mgrier 2/2/2002
  99. if (dwLastError == ERROR_SUCCESS)
  100. return TCallDisposition::OutOfMemory();
  101. return TCallDisposition::FromWin32Error(dwLastError);
  102. }
  103. }
  104. else
  105. {
  106. psz =
  107. reinterpret_cast<PWSTR>(
  108. ::RtlAllocateHeap(
  109. RtlProcessHeap(),
  110. 0,
  111. cb));
  112. }
  113. if (psz == NULL)
  114. return TCallDisposition::OutOfMemory();
  115. pBuffer->SetBufferPointerAndCount(psz, cch);
  116. return TCallDisposition::Success();
  117. }
  118. static inline void __fastcall DeallocateBuffer(PCWSTR psz) { if (psz != NULL) ::RtlFreeHeap(RtlProcessHeap(), 0, reinterpret_cast<PVOID>(const_cast<PWSTR>(psz))); }
  119. static inline void __fastcall DeallocateDynamicBuffer(BCL::CBaseString *p) { static_cast<TBuffer *>(p)->DeallocateDynamicBuffer(); }
  120. }; // class CNtBaseUnicodeStringBufferTraits
  121. class CNtBaseUnicodeStringBufferAddIn
  122. {
  123. protected:
  124. enum { LengthQuantaPerChar = 1; }; // One unit of length is one character
  125. inline CNtBaseUnicodeStringBufferAddIn(PWSTR pszInitialBuffer, SIZE_T cchInitialBuffer) : m_pair(pszInitialBuffer, cchInitialBuffer), m_cchString(0), m_cAttachedAccessors(0) { }
  126. inline static BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> PairFromPCWSTR(PCWSTR psz) { return BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T>(psz, (psz == NULL) ? 0 : wcslen(psz)); }
  127. inline static BCL::CConstantPointerAndCountPair<CHAR, SIZE_T> PairFromPCSTR(PCSTR psz) { return BCL::CConstantPointerAndCountPair<CHAR, SIZE_T>(psz, (psz == NULL) ? 0 : strlen(psz)); }
  128. inline static CNtANSIToUnicodeDataIn ANSIDecodingDataIn() { return CNtANSIToUnicodeDataIn(); }
  129. inline static CNtOEMToUnicodeDataIn OEMDecodingDataIn() { return CNtOEMToUnicodeDataIn(); }
  130. inline static CNtUnicodeToANSIDataIn ANSIEncodingDataIn() { return CNtUnicodeToANSIDataIn t; }
  131. inline static CNtUnicodeToOEMDataIn OEMEncodingDataIn() { return CNtUnicodeToOEMDataIn t; }
  132. inline BCL::CPointerAndCountPair<WCHAR, SIZE_T> MutablePair() { return m_pair; }
  133. inline BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> ConstantPair() const { return m_pair; }
  134. inline BCL::CPointerAndCountPair<WCHAR, SIZE_T> MutableStringPair() { return return BCL::CPointerAndCountRefPair<WCHAR, SIZE_T>(m_pair.GetPointer(), m_cchString); }
  135. inline BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> ConstantStringPair() const { return m_pair; }
  136. private:
  137. BCL::CMutablePointerAndCountPair<WCHAR, SIZE_T> m_pair;
  138. SIZE_T m_cchString;
  139. LONG m_cAttachedAccessors;
  140. };
  141. class CNtBaseUNICODE_STRINGBufferAddIn
  142. {
  143. protected:
  144. enum { LengthQuantaPerChar = 2; }; // Two units of length are one charater
  145. inline CNtBaseUnicodeStringBufferAddIn(PWSTR pszInitialBuffer, USHORT cchInitialBuffer) : m_pair(pszInitialBuffer, cchInitialBuffer), m_cchString(0), m_cAttachedAccessors(0) { }
  146. inline static BCL::CConstantPointerAndCountPair<WCHAR, USHORT> PairFromPCWSTR(PCWSTR psz) { return BCL::CConstantPointerAndCountPair<WCHAR, USHORT>(psz, (psz == NULL) ? 0 : wcslen(psz)); }
  147. inline static BCL::CConstantPointerAndCountPair<CHAR, USHORT> PairFromPCSTR(PCSTR psz) { return BCL::CConstantPointerAndCountPair<CHAR, USHORT>(psz, (psz == NULL) ? 0 : strlen(psz)); }
  148. inline static CNtANSIToUnicodeDataIn ANSIDecodingDataIn() { return CNtANSIToUnicodeDataIn(); }
  149. inline static CNtOEMToUnicodeDataIn OEMDecodingDataIn() { return CNtOEMToUnicodeDataIn(); }
  150. inline static CNtUnicodeToANSIDataIn ANSIEncodingDataIn() { return CNtUnicodeToANSIDataIn t; }
  151. inline static CNtUnicodeToOEMDataIn OEMEncodingDataIn() { return CNtUnicodeToOEMDataIn t; }
  152. inline BCL::CPointerAndCountRefPair<WCHAR, USHORT> MutablePair() { return BCL::CPointerAndCountRefPair<WCHAR, USHORT>(m_us.Buffer, m_us.MaximumLength); }
  153. inline BCL::CConstantPointerAndCountPair<WCHAR, USHORT> ConstantPair() const { return BCL::CConstantPointerAndCountPair<WCHAR, USHORT>(m_us.Buffer, m_us.MaximumLength); }
  154. inline BCL::CPointerAndCountRefPair<WCHAR, USHORT> MutableStringPair() { return BCL::CPointerAndCountRefPair<WCHAR, USHORT>(m_us.Buffer, m_us.Length); }
  155. inline BCL::CConstantPointerAndCountPair<WCHAR, USHORT> ConstantStringPair() const { return BCL::CConstantPointerAndCountPair<WCHAR, USHORT>(m_us.Buffer, m_us.Length); }
  156. private:
  157. UNICODE_STRING m_us;
  158. BCL::CMutablePointerAndCountPair<WCHAR, SIZE_T> m_pair;
  159. SIZE_T m_cchString;
  160. LONG m_cAttachedAccessors;
  161. };
  162. }; // namespace BCL
  163. #endif // !defined(_BCL_NTBASEUNICODESTRINGBUFFER_H_INCLUDED_)