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.

139 lines
3.5 KiB

  1. #include <pch.hxx>
  2. #include "mimeole.h" //for CP_UNICODE
  3. #include "mlang.h"
  4. #include "demand.h"
  5. #include <BadStrFunctions.h>
  6. HRESULT HrLPSZCPToBSTR(UINT cp, LPCSTR lpsz, BSTR *pbstr)
  7. {
  8. HRESULT hr = NOERROR;
  9. BSTR bstr=0;
  10. UINT cchSrc = 0;
  11. UINT cchDest = 0;
  12. IMultiLanguage *pMLang = NULL;
  13. IMLangConvertCharset *pMLangConv = NULL;
  14. if (!cp)
  15. cp = GetACP();
  16. if ((cp == CP_UTF8) && (lpsz[0] == 0xEF) && (lpsz[1] == 0xBB) && (lpsz[2] == 0xBF))
  17. lpsz += 3; // Skip BOM
  18. IF_FAILEXIT(hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER, IID_IMultiLanguage, (void**)&pMLang));
  19. IF_FAILEXIT(hr = pMLang->CreateConvertCharset(cp, CP_UNICODE, NULL, &pMLangConv));
  20. // get byte count
  21. cchDest = cchSrc = lstrlen(lpsz) + 1;
  22. // allocate a wide-string with enough character to hold string - use character count
  23. IF_NULLEXIT(bstr=SysAllocStringLen(NULL, cchSrc));
  24. hr = pMLangConv->DoConversionToUnicode((CHAR*)lpsz, &cchSrc, (WCHAR*)bstr, &cchDest);
  25. *pbstr = bstr;
  26. bstr=0; // freed by caller
  27. exit:
  28. if(bstr)
  29. SysFreeString(bstr);
  30. ReleaseObj(pMLangConv);
  31. ReleaseObj(pMLang);
  32. return hr;
  33. }
  34. HRESULT HrLPSZToBSTR(LPCSTR lpsz, BSTR *pbstr)
  35. {
  36. // GetACP so that it works on non-US platform
  37. return HrLPSZCPToBSTR(GetACP(), lpsz, pbstr);
  38. }
  39. HRESULT HrIStreamToBSTR(UINT cp, LPSTREAM pstm, BSTR *pbstr)
  40. {
  41. HRESULT hr;
  42. ULONG cb;
  43. LPSTR lpsz=0;
  44. BOOL fLittleEndian = FALSE;
  45. if (CP_UNICODE == cp || (S_OK == HrIsStreamUnicode(pstm, &fLittleEndian)))
  46. return(HrIStreamWToBSTR(pstm, pbstr));
  47. hr=HrGetStreamSize(pstm, &cb);
  48. if (FAILED(hr))
  49. goto error;
  50. if (cb==0)
  51. {
  52. hr = E_FAIL;
  53. goto error;
  54. }
  55. HrRewindStream(pstm);
  56. if (!MemAlloc((LPVOID *)&lpsz, cb+1))
  57. {
  58. hr = E_OUTOFMEMORY;
  59. goto error;
  60. }
  61. hr = pstm->Read(lpsz, cb, &cb);
  62. if (FAILED(hr))
  63. goto error;
  64. lpsz[cb]=0;
  65. hr = HrLPSZCPToBSTR(cp,lpsz, pbstr);
  66. if (FAILED(hr))
  67. goto error;
  68. error:
  69. MemFree(lpsz);
  70. return hr;
  71. }
  72. HRESULT HrIStreamWToBSTR(LPSTREAM pstm, BSTR *pbstr)
  73. {
  74. ULONG cb,
  75. cbTest,
  76. cch;
  77. BSTR bstr;
  78. HRESULT hr = E_FAIL;
  79. if (pstm == NULL || pbstr == NULL)
  80. return E_INVALIDARG;
  81. *pbstr = 0;
  82. if (SUCCEEDED(HrGetStreamSize(pstm, &cb)) && cb)
  83. {
  84. cch = cb / sizeof(WCHAR);
  85. HrRewindStream(pstm);
  86. bstr=SysAllocStringLen(NULL, cch);
  87. if(bstr)
  88. {
  89. // Raid 60259 - OE: Cannot successfully insert text file in Unicode (UCS-2)
  90. hr = pstm->Read(bstr, sizeof(WCHAR), &cbTest);
  91. if (SUCCEEDED(hr) && (sizeof(WCHAR) == cbTest) && (bstr[0] != 0xfeff))
  92. HrRewindStream(pstm);
  93. hr = pstm->Read(bstr, cb, &cb);
  94. if (SUCCEEDED(hr))
  95. {
  96. Assert (cb/sizeof(WCHAR) <= cch);
  97. bstr[cb/sizeof(WCHAR)]=0;
  98. *pbstr = bstr;
  99. }
  100. }
  101. }
  102. return hr;
  103. }
  104. HRESULT HrBSTRToLPSZ(UINT cp, BSTR bstr, LPSTR *ppszOut)
  105. {
  106. Assert (bstr && ppszOut);
  107. *ppszOut = PszToANSI(cp, bstr);
  108. return *ppszOut ? S_OK : E_OUTOFMEMORY;
  109. }