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.

223 lines
4.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CBString.h
  7. //
  8. // Implementation File:
  9. // CBString.cpp
  10. //
  11. // Description:
  12. // BString provides an exception-safe handler for BSTRs; it also wipes
  13. // clean its contents upon leaving scope, making it usable for short-lived
  14. // password storage.
  15. //
  16. // This class is intended to be used instead of CComBSTR since the
  17. // use of ATL is prohibited in our project.
  18. //
  19. // Maintained By:
  20. // John Franco (jfranco) 17-APR-2002
  21. //
  22. //////////////////////////////////////////////////////////////////////////////
  23. #pragma once
  24. class CBString
  25. {
  26. private:
  27. BSTR m_bstr;
  28. static BSTR AllocateBuffer( UINT cchIn );
  29. static BSTR CopyString( PCWSTR pcwszIn );
  30. static BSTR CopyBSTR( BSTR bstrIn );
  31. public:
  32. CBString( PCWSTR pcwszIn = NULL );
  33. CBString( UINT cchIn );
  34. CBString( const CBString & crbstrOtherIn );
  35. ~CBString( void );
  36. CBString & operator=( const CBString & crbstrOtherIn );
  37. BSTR * operator&( void );
  38. OLECHAR & operator[]( size_t idxIn );
  39. const OLECHAR & operator[]( size_t idxIn ) const;
  40. operator BSTR( void );
  41. operator PCWSTR( void ) const;
  42. void Attach( BSTR bstrIn );
  43. void Detach( void );
  44. UINT Length( void ) const;
  45. void Erase( void );
  46. void Swap( CBString & rbstrOtherInout );
  47. BOOL IsEmpty( void ) const;
  48. }; //*** class CBstring
  49. inline
  50. CBString::CBString( PCWSTR pcwszIn )
  51. : m_bstr( CopyString( pcwszIn ) )
  52. {
  53. TraceFunc1( "pcwszIn = '%ws'", pcwszIn == NULL ? L"<NULL>" : pcwszIn );
  54. TraceFuncExit();
  55. } //*** CBString::CBString( pcwszIn )
  56. inline
  57. CBString::CBString( UINT cchIn )
  58. : m_bstr( AllocateBuffer( cchIn ) )
  59. {
  60. TraceFunc1( "cchIn = '%d'", cchIn );
  61. TraceFuncExit();
  62. } //*** CBString::CBString( cchIn )
  63. inline
  64. CBString::CBString( const CBString & crbstrOtherIn )
  65. : m_bstr( CopyBSTR( crbstrOtherIn.m_bstr ) )
  66. {
  67. TraceFunc1( "crbstrOtherIn = '%ws'", crbstrOtherIn );
  68. TraceFuncExit();
  69. } //*** CBString::CBString( crbstrOtherIn )
  70. inline
  71. void
  72. CBString::Erase( void )
  73. {
  74. TraceFunc( "" );
  75. if ( m_bstr != NULL )
  76. {
  77. SecureZeroMemory( m_bstr, SysStringLen( m_bstr ) * sizeof( *m_bstr ) );
  78. TraceSysFreeString( m_bstr );
  79. }
  80. TraceFuncExit();
  81. } //*** CBString::Erase
  82. inline
  83. void
  84. CBString::Swap( CBString & rbstrOtherInout )
  85. {
  86. TraceFunc1( "rbstrOtherInout = '%ws'", rbstrOtherInout );
  87. BSTR bstrStash = m_bstr;
  88. m_bstr = rbstrOtherInout.m_bstr;
  89. rbstrOtherInout.m_bstr = bstrStash;
  90. TraceFuncExit();
  91. } //*** CBString::Swap
  92. inline
  93. CBString::~CBString( void )
  94. {
  95. TraceFunc( "" );
  96. Erase();
  97. TraceFuncExit();
  98. } //*** CBString::~CBString
  99. inline
  100. CBString &
  101. CBString::operator=( const CBString & crbstrOtherIn )
  102. {
  103. TraceFunc1( "crbstrOtherIn = '%ws'", crbstrOtherIn );
  104. if ( this != &crbstrOtherIn )
  105. {
  106. CBString bstrCopy( crbstrOtherIn );
  107. Swap( bstrCopy );
  108. }
  109. RETURN( *this );
  110. } //*** CBString::operator=
  111. inline
  112. BSTR *
  113. CBString::operator&( void )
  114. {
  115. return &m_bstr;
  116. } //*** CBString::operator&
  117. inline
  118. OLECHAR &
  119. CBString::operator[]( size_t idxIn )
  120. {
  121. return m_bstr[ idxIn ];
  122. } //*** CBString::operator[]
  123. inline
  124. const OLECHAR &
  125. CBString::operator[]( size_t idxIn ) const
  126. {
  127. return m_bstr[ idxIn ];
  128. } //*** CBString::operator[] const
  129. inline
  130. CBString::operator BSTR( void )
  131. {
  132. return m_bstr;
  133. } //*** CBString::operator BSTR
  134. inline
  135. CBString::operator PCWSTR( void ) const
  136. {
  137. return m_bstr;
  138. } //*** CBString::operator PCWSTR
  139. inline
  140. void
  141. CBString::Attach( BSTR bstrIn )
  142. {
  143. TraceFunc1( "bstrIn = '%ws'", bstrIn );
  144. if ( m_bstr != bstrIn )
  145. {
  146. Erase();
  147. m_bstr = bstrIn;
  148. }
  149. TraceFuncExit();
  150. } //*** CBString::Attach
  151. inline
  152. void
  153. CBString::Detach( void )
  154. {
  155. TraceFunc( "" );
  156. m_bstr = NULL;
  157. TraceFuncExit();
  158. } //*** CBString::Detach
  159. inline
  160. UINT
  161. CBString::Length( void ) const
  162. {
  163. return SysStringLen( m_bstr );
  164. } //*** CBString::Length
  165. inline
  166. BOOL
  167. CBString::IsEmpty( void ) const
  168. {
  169. return ( m_bstr == NULL );
  170. } //*** CBString::IsEmpty