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.

221 lines
8.5 KiB

  1. /***
  2. *a_str.c - A version of GetStringType.
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Use either GetStringTypeA or GetStringTypeW depending on which is
  8. * unstubbed.
  9. *
  10. *Revision History:
  11. * 09-14-93 CFW Module created.
  12. * 09-17-93 CFW Use unsigned chars.
  13. * 09-23-93 CFW Correct NLS API params and comments about same.
  14. * 10-07-93 CFW Optimize WideCharToMultiByte, use NULL default char.
  15. * 10-22-93 CFW Remove bad verification test from "A" version.
  16. * 10-22-93 CFW Test for invalid MB chars using global preset flag.
  17. * 11-09-93 CFW Allow user to pass in code page.
  18. * 11-18-93 CFW Test for entry point function stubs.
  19. * 02-23-94 CFW Use W flavor whenever possible.
  20. * 03-31-94 CFW Include awint.h.
  21. * 04-18-94 CFW Use lcid value if passed in.
  22. * 04-18-94 CFW Use calloc and don't test the NULL.
  23. * 10-24-94 CFW Must verify GetStringType return.
  24. * 12-21-94 CFW Remove invalid MB chars NT 3.1 hack.
  25. * 12-27-94 CFW Call direct, all OS's have stubs.
  26. * 01-10-95 CFW Debug CRT allocs.
  27. * 02-15-97 RDK For narrow string type, try W version first so
  28. * Windows NT can process nonANSI codepage correctly.
  29. * 03-16-97 RDK Added error flag to __crtGetStringTypeA.
  30. * 05-12-97 GJF Renamed and moved __crtGetStringTypeW into a separate
  31. * file. Revised to use _alloca instead of malloc. Also,
  32. * removed some silly code and reformatted.
  33. * 08-18-98 GJF Use _malloc_crt if _alloca fails.
  34. * 12-10-99 GB Added support for recovery from stack overflow around
  35. * _alloca().
  36. * 05-17-00 GB Use ERROR_CALL_NOT_IMPLEMENTED for existance of W API
  37. * 08-23-00 GB Fixed bug with non Ansi CP on Win9x.
  38. *
  39. *******************************************************************************/
  40. #include <cruntime.h>
  41. #include <internal.h>
  42. #include <stdlib.h>
  43. #include <setlocal.h>
  44. #include <locale.h>
  45. #include <awint.h>
  46. #include <dbgint.h>
  47. #include <malloc.h>
  48. #include <awint.h>
  49. #define USE_W 1
  50. #define USE_A 2
  51. /***
  52. *int __cdecl __crtGetStringTypeA - Get type information about an ANSI string.
  53. *
  54. *Purpose:
  55. * Internal support function. Assumes info in ANSI string format. Tries
  56. * to use NLS API call GetStringTypeA if available and uses GetStringTypeW
  57. * if it must. If neither are available it fails and returns FALSE.
  58. *
  59. *Entry:
  60. * DWORD dwInfoType - see NT\Chicago docs
  61. * LPCSTR lpSrcStr - char (byte) string for which character types
  62. * are requested
  63. * int cchSrc - char (byte) count of lpSrcStr (including NULL
  64. * if any)
  65. * LPWORD lpCharType - word array to receive character type information
  66. * (must be twice the size of lpSrcStr)
  67. * int code_page - for MB/WC conversion. If 0, use __lc_codepage
  68. * int lcid - for A call, specify LCID, If 0, use
  69. * __lc_handle[LC_CTYPE].
  70. * BOOL bError - TRUE if MB_ERR_INVALID_CHARS set on call to
  71. * MultiByteToWideChar when GetStringTypeW used.
  72. *
  73. *Exit:
  74. * Success: TRUE
  75. * Failure: FALSE
  76. *
  77. *Exceptions:
  78. *
  79. *******************************************************************************/
  80. BOOL __cdecl __crtGetStringTypeA(
  81. DWORD dwInfoType,
  82. LPCSTR lpSrcStr,
  83. int cchSrc,
  84. LPWORD lpCharType,
  85. int code_page,
  86. int lcid,
  87. BOOL bError
  88. )
  89. {
  90. static int f_use = 0;
  91. /*
  92. * Look for unstubbed 'preferred' flavor. Otherwise use available
  93. * flavor. Must actually call the function to ensure it's not a stub.
  94. * (Always try wide version first so WinNT can process codepage correctly.)
  95. */
  96. if (0 == f_use)
  97. {
  98. unsigned short dummy;
  99. if (0 != GetStringTypeW(CT_CTYPE1, L"\0", 1, &dummy))
  100. f_use = USE_W;
  101. else if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
  102. f_use = USE_A;
  103. }
  104. /* Use "A" version */
  105. if (USE_A == f_use || f_use == 0)
  106. {
  107. char *cbuffer = NULL;
  108. int ret;
  109. int AnsiCP;
  110. if (0 == lcid)
  111. lcid = __lc_handle[LC_CTYPE];
  112. if (0 == code_page)
  113. code_page = __lc_codepage;
  114. if ( -1 == (AnsiCP = __ansicp(lcid)))
  115. return FALSE;
  116. /* If current code-page is not ansi code page, convert it to ansi code page
  117. * as GetStringTypeA uses ansi code page to find the strig type.
  118. */
  119. if ( AnsiCP != code_page)
  120. {
  121. cbuffer = __convertcp(code_page, AnsiCP, lpSrcStr, &cchSrc, NULL, 0);
  122. if (cbuffer == NULL)
  123. return FALSE;
  124. lpSrcStr = cbuffer;
  125. }
  126. ret = GetStringTypeA(lcid, dwInfoType, lpSrcStr, cchSrc, lpCharType);
  127. if ( cbuffer != NULL)
  128. _free_crt(cbuffer);
  129. return ret;
  130. }
  131. /* Use "W" version */
  132. if (USE_W == f_use)
  133. {
  134. int retval1;
  135. int buff_size;
  136. wchar_t *wbuffer;
  137. BOOL retval2 = FALSE;
  138. int malloc_flag = 0;
  139. /*
  140. * Convert string and return the requested information. Note that
  141. * we are converting to a wide character string so there is not a
  142. * one-to-one correspondence between number of multibyte chars in the
  143. * input string and the number of wide chars in the buffer. However,
  144. * there had *better be* a one-to-one correspondence between the
  145. * number of multibyte characters and the number of WORDs in the
  146. * return buffer.
  147. */
  148. /*
  149. * Use __lc_codepage for conversion if code_page not specified
  150. */
  151. if (0 == code_page)
  152. code_page = __lc_codepage;
  153. /* find out how big a buffer we need */
  154. if ( 0 == (buff_size = MultiByteToWideChar( code_page,
  155. bError ?
  156. MB_PRECOMPOSED |
  157. MB_ERR_INVALID_CHARS
  158. : MB_PRECOMPOSED,
  159. lpSrcStr,
  160. cchSrc,
  161. NULL,
  162. 0 )) )
  163. return FALSE;
  164. /* allocate enough space for wide chars */
  165. __try {
  166. wbuffer = (wchar_t *)_alloca( sizeof(wchar_t) * buff_size );
  167. (void)memset( wbuffer, 0, sizeof(wchar_t) * buff_size );
  168. }
  169. __except( EXCEPTION_EXECUTE_HANDLER ) {
  170. _resetstkoflw();
  171. wbuffer = NULL;
  172. }
  173. if ( wbuffer == NULL ) {
  174. if ( (wbuffer = (wchar_t *)_calloc_crt(sizeof(wchar_t), buff_size))
  175. == NULL )
  176. return FALSE;
  177. malloc_flag++;
  178. }
  179. /* do the conversion */
  180. if ( 0 != (retval1 = MultiByteToWideChar( code_page,
  181. MB_PRECOMPOSED,
  182. lpSrcStr,
  183. cchSrc,
  184. wbuffer,
  185. buff_size )) )
  186. /* obtain result */
  187. retval2 = GetStringTypeW( dwInfoType,
  188. wbuffer,
  189. retval1,
  190. lpCharType );
  191. if ( malloc_flag )
  192. _free_crt(wbuffer);
  193. return retval2;
  194. }
  195. else /* f_use is neither USE_A nor USE_W */
  196. return FALSE;
  197. }