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.

191 lines
9.5 KiB

  1. #if !defined(_BCL_W32BASEUNICODESTRINGBUFFER_H_INCLUDED_)
  2. #define _BCL_W32BASEUNICODESTRINGBUFFER_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_w32unicodestringalgorithms.h>
  19. #include <limits.h>
  20. namespace BCL
  21. {
  22. template <typename TBuffer, typename TCallDispositionT, typename TPublicErrorReturnTypeT>
  23. class CWin32BaseUnicodeStringBufferTraits : public BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>, public BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>
  24. {
  25. public:
  26. typedef CWin32BaseUnicodeStringBufferTraits 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 CWin32StringComparisonResult TComparisonResult;
  33. typedef BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> TConstantPair;
  34. typedef BCL::CMutablePointerAndCountPair<WCHAR, SIZE_T> TMutablePair;
  35. typedef CWin32CaseInsensitivityData TCaseInsensitivityData;
  36. typedef SIZE_T TSizeT;
  37. typedef CWin32MBCSToUnicodeDataIn TDecodingDataIn;
  38. typedef CWin32MBCSToUnicodeDataOut TDecodingDataOut;
  39. typedef CWin32UnicodeToMBCSDataIn TEncodingDataIn;
  40. typedef CWin32UnicodeToMBCSDataOut TEncodingDataOut;
  41. typedef CConstantPointerAndCountPair<CHAR, SIZE_T> TConstantNonNativePair;
  42. typedef CMutablePointerAndCountPair<CHAR, SIZE_T> TMutableNonNativePair;
  43. typedef BCL::CWin32PWSTRAllocationHelper TPWSTRAllocationHelper;
  44. typedef BCL::CWin32PSTRAllocationHelper TPSTRAllocationHelper;
  45. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TMutableString;
  46. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TConstantString;
  47. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TMutableNonNativeString;
  48. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::TConstantNonNativeString;
  49. // exposing the things from our private base class
  50. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::CopyIntoBuffer;
  51. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::CopyIntoBuffer;
  52. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::DetermineRequiredCharacters;
  53. using BCL::CWin32NullTerminatedUnicodeStringAlgorithms<TBuffer, TCallDispositionT>::DetermineRequiredCharacters;
  54. #if 0
  55. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::CopyIntoBuffer;
  56. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::NullCharacter;
  57. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::IsNullCharacter;
  58. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::StringCchLegal;
  59. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::TrimStringLengthToLegalCharacters;
  60. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::AddWithOverflowCheck;
  61. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::SubtractWithUnderflowCheck;
  62. using BCL::CUnicodeCharTraits<TBuffer, TCallDispositionT>::EqualStrings;
  63. #endif // 0
  64. static inline TMutablePair & __fastcall MutableBufferPair(BCL::CBaseString *p) { return static_cast<TBuffer *>(p)->m_pair; }
  65. static inline const TConstantPair & __fastcall BufferPair(const BCL::CBaseString *p) { return static_cast<const TConstantPair &>(static_cast<const TBuffer *>(p)->m_pair); }
  66. static inline TConstantPair __fastcall GetStringPair(const BCL::CBaseString *p) { return TConstantPair(TBuffer::TTraits::GetBufferPtr(p), TBuffer::TTraits::GetStringCch(p)); }
  67. 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); }
  68. static inline bool __fastcall AnyAccessors(const BCL::CBaseString *p) { return (static_cast<const TBuffer *>(p)->m_cAttachedAccessors != 0); }
  69. static inline bool __fastcall NoAccessors(const BCL::CBaseString *p) { return (static_cast<const TBuffer *>(p)->m_cAttachedAccessors == 0); }
  70. static inline TCallDisposition __fastcall NoAccessorsCheck(const BCL::CBaseString *p) { if (TBuffer::TTraits::AnyAccessors(p)) { return TCallDisposition::InternalError_ObjectLocked(); } return TCallDisposition::Success(); }
  71. static inline PCWSTR __fastcall GetBufferPtr(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_pair.GetPointer(); }
  72. static inline PWSTR __fastcall GetMutableBufferPtr(BCL::CBaseString *p) { return static_cast<TBuffer *>(p)->m_pair.GetPointer(); }
  73. static inline SIZE_T __fastcall GetBufferCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_pair.GetCount(); }
  74. static inline void __fastcall SetBufferPointerAndCount(BCL::CBaseString *p, PWSTR pszBuffer, SIZE_T cchBuffer) { static_cast<TBuffer *>(p)->m_pair.SetPointerAndCount(pszBuffer, cchBuffer); }
  75. static inline SIZE_T __fastcall GetStringCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->m_cchString; }
  76. static inline void _fastcall SetStringCch(BCL::CBaseString *p, SIZE_T cch)
  77. {
  78. BCL_ASSERT((cch == 0) || (cch < TBuffer::TTraits::GetBufferCch(p)));
  79. static_cast<TBuffer *>(p)->m_cchString = cch;
  80. if (TBuffer::TTraits::GetBufferCch(p) != 0)
  81. TBuffer::TTraits::GetMutableBufferPtr(p)[cch] = L'\0';
  82. }
  83. static inline void __fastcall IntegrityCheck(const BCL::CBaseString *p) { }
  84. static inline PWSTR __fastcall GetInlineBufferPtr(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->GetInlineBufferPtr(); }
  85. static inline SIZE_T __fastcall GetInlineBufferCch(const BCL::CBaseString *p) { return static_cast<const TBuffer *>(p)->GetInlineBufferCch(); }
  86. static inline TCallDisposition __fastcall ReallocateBuffer(BCL::CBaseString *p, SIZE_T cch)
  87. {
  88. PWSTR psz = NULL;
  89. SIZE_T cb = (cch * sizeof(WCHAR));
  90. TBuffer *pBuffer = static_cast<TBuffer *>(p);
  91. if (cch != (cb / sizeof(WCHAR)))
  92. return TCallDisposition::ArithmeticOverflow();
  93. if (pBuffer->GetBufferPtr() != NULL)
  94. {
  95. ::SetLastError(ERROR_SUCCESS);
  96. psz =
  97. reinterpret_cast<PWSTR>(
  98. ::HeapReAlloc(
  99. ::GetProcessHeap(),
  100. 0,
  101. const_cast<PWSTR>(pBuffer->GetBufferPtr()),
  102. cb));
  103. if (psz == NULL)
  104. {
  105. const DWORD dwLastError = ::GetLastError();
  106. // HeapReAlloc doesn't always set last error, so we rely on this
  107. // fact to find that the win32 last error hasn't changed from
  108. // before to infer ERROR_OUTOFMEMORY. -mgrier 2/2/2002
  109. if (dwLastError == ERROR_SUCCESS)
  110. return TCallDisposition::OutOfMemory();
  111. return TCallDisposition::FromWin32Error(dwLastError);
  112. }
  113. }
  114. else
  115. {
  116. psz =
  117. reinterpret_cast<PWSTR>(
  118. ::HeapAlloc(
  119. ::GetProcessHeap(),
  120. 0,
  121. cb));
  122. }
  123. if (psz == NULL)
  124. return TCallDisposition::OutOfMemory();
  125. pBuffer->SetBufferPointerAndCount(psz, cch);
  126. return TCallDisposition::Success();
  127. }
  128. static inline void __fastcall DeallocateBuffer(PCWSTR psz) { if (psz != NULL) ::HeapFree(::GetProcessHeap(), 0, reinterpret_cast<PVOID>(const_cast<PWSTR>(psz))); }
  129. static inline void __fastcall DeallocateDynamicBuffer(BCL::CBaseString *p) { static_cast<TBuffer *>(p)->DeallocateDynamicBuffer(); }
  130. }; // class CWin32BaseUnicodeStringBufferTraits
  131. class CWin32BaseUnicodeStringBufferAddIn
  132. {
  133. protected:
  134. inline CWin32BaseUnicodeStringBufferAddIn(PWSTR pszInitialBuffer, SIZE_T cchInitialBuffer) : m_pair(pszInitialBuffer, cchInitialBuffer), m_cchString(0), m_cAttachedAccessors(0) { }
  135. inline static BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T> PairFromPCWSTR(PCWSTR psz) { return BCL::CConstantPointerAndCountPair<WCHAR, SIZE_T>(psz, (psz == NULL) ? 0 : wcslen(psz)); }
  136. inline static BCL::CConstantPointerAndCountPair<CHAR, SIZE_T> PairFromPCSTR(PCSTR psz) { return BCL::CConstantPointerAndCountPair<CHAR, SIZE_T>(psz, (psz == NULL) ? 0 : strlen(psz)); }
  137. inline static CWin32MBCSToUnicodeDataIn ACPDecodingDataIn() { CWin32MBCSToUnicodeDataIn t; t.m_CodePage = CP_ACP; t.m_dwFlags = MB_PRECOMPOSED | MB_ERR_INVALID_CHARS; return t; }
  138. inline static CWin32UnicodeToMBCSDataIn ACPEncodingDataIn() { CWin32UnicodeToMBCSDataIn t; t.m_CodePage = CP_ACP; t.m_dwFlags = WC_NO_BEST_FIT_CHARS; return t; }
  139. BCL::CMutablePointerAndCountPair<WCHAR, SIZE_T> m_pair;
  140. SIZE_T m_cchString;
  141. LONG m_cAttachedAccessors;
  142. };
  143. }; // namespace BCL
  144. #endif // !defined(_BCL_W32BASEUNICODESTRINGBUFFER_H_INCLUDED_)