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.

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