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.

195 lines
5.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 2000.
  5. //
  6. // File: mbutil.cxx
  7. //
  8. // Contents: MultiByte To Unicode and ViceVersa utility functions and
  9. // classes.
  10. //
  11. // History: 96/Jan/3 DwightKr Created
  12. // Aug 20 1996 Srikants Moved from escurl.hxx to this file
  13. //
  14. //
  15. //----------------------------------------------------------------------------
  16. #include <pch.cxx>
  17. #pragma hdrstop
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Function: MultiByteToXArrayWideChar
  21. //
  22. // Arguments: [pbBuffer] - ASCII buffer to convert to WCHAR
  23. // [cbBuffer] - length of the buffer, not including the null
  24. // [codePage] - codePage for conversion
  25. // [wcsBuffer] - resulting WCHAR buffer
  26. //
  27. // Synopsis: Converts a multibyte string to a wide character string
  28. //
  29. //----------------------------------------------------------------------------
  30. ULONG MultiByteToXArrayWideChar( BYTE const * pbBuffer,
  31. ULONG cbBuffer,
  32. UINT codePage,
  33. XArray<WCHAR> & wcsBuffer )
  34. {
  35. Win4Assert( 0 != pbBuffer );
  36. //
  37. // MultiByteToWideChar expects a length of at least 1 char.
  38. //
  39. int cwcBuffer = cbBuffer + (cbBuffer/2) + 2;
  40. if ( wcsBuffer.Get() == 0 || (ULONG) cwcBuffer > wcsBuffer.Count() )
  41. {
  42. delete [] wcsBuffer.Acquire();
  43. wcsBuffer.Init(cwcBuffer);
  44. }
  45. if ( 0 == cbBuffer )
  46. {
  47. wcsBuffer[0] = 0;
  48. return 0;
  49. }
  50. int cwcConvert;
  51. do
  52. {
  53. cwcConvert = MultiByteToWideChar( codePage,
  54. 0,
  55. (const char *) pbBuffer,
  56. cbBuffer, // Size of input buf
  57. wcsBuffer.Get(), // Ptr to output buf
  58. cwcBuffer - 1 ); // Size of output buf
  59. if ( 0 == cwcConvert )
  60. {
  61. if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  62. {
  63. cwcBuffer += (cwcBuffer/2);
  64. delete wcsBuffer.Acquire();
  65. wcsBuffer.Init(cwcBuffer);
  66. }
  67. else
  68. {
  69. THROW( CException() );
  70. }
  71. }
  72. else
  73. {
  74. wcsBuffer[cwcConvert] = 0; // Null terminate the buffer
  75. }
  76. } while ( 0 == cwcConvert );
  77. if ( 0 == pbBuffer[cbBuffer-1] )
  78. return cwcConvert - 1;
  79. return cwcConvert;
  80. } //MultiByteToXArrayWideChar
  81. //+---------------------------------------------------------------------------
  82. //
  83. // Function: WideCharToXArrayMultiByte
  84. //
  85. // Arguments: [wcsMesage] - WCHAR buffer to convert to ASCII
  86. // [cwcMessage] - length of the buffer, not including the null
  87. // [codePage] - code page for conversion
  88. // [pszBuffer] - resulting CHAR buffer
  89. //
  90. // Synopsis: Converts a wide character string to ASCII
  91. //
  92. //----------------------------------------------------------------------------
  93. DWORD WideCharToXArrayMultiByte( WCHAR const * wcsMessage,
  94. ULONG cwcMessage,
  95. UINT codePage,
  96. XArray<BYTE> & pszMessage )
  97. {
  98. Win4Assert( 0 != cwcMessage );
  99. DWORD cbConvert;
  100. ULONG cbMessage = pszMessage.Count();
  101. do
  102. {
  103. cbConvert = ::WideCharToMultiByte( codePage,
  104. WC_COMPOSITECHECK,
  105. wcsMessage,
  106. cwcMessage,
  107. (CHAR *) pszMessage.Get(),
  108. cbMessage,
  109. NULL,
  110. NULL );
  111. if ( (0 == cbConvert) || (0 == cbMessage) )
  112. {
  113. if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
  114. {
  115. cbMessage += ++cbMessage;
  116. delete pszMessage.Acquire();
  117. pszMessage.Init(cbMessage);
  118. }
  119. else
  120. {
  121. THROW( CException() );
  122. }
  123. }
  124. } while ( 0 == cbConvert );
  125. if ( cwcMessage > 0 && 0 == wcsMessage[cwcMessage-1] )
  126. return cbConvert - 1;
  127. else
  128. return cbConvert;
  129. }
  130. //+---------------------------------------------------------------------------
  131. //
  132. // Function: wcsipattern
  133. //
  134. // Synopsis: A case-insensitive, WCHAR implemtation of strstr.
  135. //
  136. // Arguments: [wcsString] - string to search
  137. // [wcsPattern] - pattern to look for
  138. //
  139. // Returns: pointer to pattern, 0 if no match found.
  140. //
  141. // History: 96/Jan/03 DwightKr created
  142. //
  143. // NOTE: Warning the first character of wcsPattern must be a case
  144. // insensitive letter. This results in a significant performance
  145. // improvement.
  146. //
  147. //----------------------------------------------------------------------------
  148. WCHAR * wcsipattern( WCHAR * wcsString, WCHAR const * wcsPattern )
  149. {
  150. Win4Assert ( (wcsPattern != 0) && (*wcsPattern != 0) );
  151. ULONG cwcPattern = wcslen(wcsPattern);
  152. Win4Assert( *wcsPattern == towupper(*wcsPattern) );
  153. while ( *wcsString != 0 )
  154. {
  155. while ( (*wcsString != 0) &&
  156. (*wcsString != *wcsPattern) )
  157. {
  158. wcsString++;
  159. }
  160. if ( 0 == *wcsString )
  161. {
  162. return 0;
  163. }
  164. if ( _wcsnicmp( wcsString, wcsPattern, cwcPattern) == 0 )
  165. {
  166. return wcsString;
  167. }
  168. wcsString++;
  169. }
  170. return 0;
  171. }