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.

77 lines
1.8 KiB

  1. #include "ctlspriv.h"
  2. #include <limits.h>
  3. /*
  4. * Creates a buffer for a unicode string, and then copies the ANSI text
  5. * into it (converting it to unicode in the process)
  6. *
  7. * The returned pointer should be freed with LocalFree after use.
  8. */
  9. LPWSTR ProduceWFromA( UINT uiCodePage, LPCSTR psz )
  10. {
  11. LPWSTR pszW;
  12. int cch;
  13. if (psz == NULL || psz == LPSTR_TEXTCALLBACKA)
  14. return (LPWSTR)psz;
  15. // The old code would call lstrlen and lstrcpy which would fault internal to the
  16. // api, this should do about the same...
  17. if (IsBadReadPtr(psz,1))
  18. return NULL; // For now lets try not setting a string...
  19. cch = MultiByteToWideChar(uiCodePage, 0, psz, -1, NULL, 0);
  20. if (cch == 0)
  21. cch = 1;
  22. pszW = LocalAlloc( LMEM_FIXED, cch * sizeof(WCHAR) );
  23. if (pszW != NULL )
  24. {
  25. if (MultiByteToWideChar( uiCodePage, MB_PRECOMPOSED, psz, -1, pszW, cch ) == FALSE)
  26. {
  27. LocalFree(pszW);
  28. pszW = NULL;
  29. }
  30. }
  31. return pszW;
  32. }
  33. /*
  34. * Creates a buffer for a unicode string, and then copies the ANSI text
  35. * into it (converting it to unicode in the process)
  36. *
  37. * The returned pointer should be freed with LocalFree after use.
  38. */
  39. LPSTR ProduceAFromW( UINT uiCodePage, LPCWSTR psz )
  40. {
  41. LPSTR pszA;
  42. int cch;
  43. if (psz == NULL || psz == LPSTR_TEXTCALLBACKW)
  44. return (LPSTR)psz;
  45. cch = WideCharToMultiByte(uiCodePage, 0, psz, -1, NULL, 0, NULL, NULL);
  46. if (cch == 0)
  47. cch = 1;
  48. pszA = LocalAlloc( LMEM_FIXED, cch * sizeof(char) );
  49. if (pszA != NULL )
  50. {
  51. if (WideCharToMultiByte(uiCodePage, 0, psz, -1, pszA, cch, NULL, NULL) == FALSE)
  52. {
  53. LocalFree(pszA);
  54. pszA = NULL;
  55. }
  56. }
  57. return pszA;
  58. }