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.

91 lines
3.1 KiB

  1. /*--------------------------------------------------------------------
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. --------------------------------------------------------------------*/
  4. #include "windows.h"
  5. #include "locresman.h"
  6. #define SIZE_OF_TEMP_BUFFER 2048 // something reasonable.
  7. int WINAPI LoadStringCodepage_A(HINSTANCE hInstance, // handle to module containing string resource
  8. UINT uID, // resource identifier
  9. char *lpBuffer, // pointer to buffer for resource
  10. int nBufferMax, // size of buffer
  11. UINT uCodepage // desired codepage
  12. )
  13. {
  14. int iRetVal = 0;
  15. WCHAR wzBuffer[SIZE_OF_TEMP_BUFFER];
  16. WCHAR *pwchBuffer;
  17. // use the buffer-on-stack if possible
  18. if (nBufferMax > SIZE_OF_TEMP_BUFFER)
  19. {
  20. pwchBuffer = (WCHAR *) GlobalAlloc(GPTR, nBufferMax * sizeof(WCHAR));
  21. if (NULL == pwchBuffer)
  22. goto L_Return;
  23. }
  24. else
  25. pwchBuffer = wzBuffer;
  26. iRetVal = LoadStringW(hInstance, uID, pwchBuffer, nBufferMax);
  27. if (0 == iRetVal)
  28. goto L_Return;
  29. iRetVal = WideCharToMultiByte(uCodepage, 0, pwchBuffer, iRetVal, lpBuffer, nBufferMax, NULL, NULL);
  30. lpBuffer[iRetVal] = 0;
  31. L_Return :;
  32. if ((NULL != pwchBuffer) && (pwchBuffer != wzBuffer))
  33. GlobalFree(pwchBuffer);
  34. return (iRetVal);
  35. }
  36. HRESULT WINAPI HrConvertStringCodepageEx(UINT uCodepageSrc, char *pchSrc, int cchSrc,
  37. UINT uUcodepageTgt, char *pchTgt, int cchTgtMax, int *pcchTgt,
  38. void *pbScratchBuffer, int iSizeScratchBuffer,
  39. char *pchDefaultChar, BOOL *pfUsedDefaultChar)
  40. {
  41. HRESULT hr = S_OK;
  42. WCHAR *pbBuffer;
  43. int cch;
  44. pbBuffer = (WCHAR *) pbScratchBuffer;
  45. if ((NULL == pbBuffer) || (iSizeScratchBuffer < (int) (cchSrc * sizeof(WCHAR))))
  46. {
  47. pbBuffer = GlobalAlloc(GPTR, (cchSrc + 1)* sizeof(WCHAR));
  48. if (NULL == pbBuffer)
  49. {
  50. hr = E_OUTOFMEMORY;
  51. goto L_Return;
  52. }
  53. ZeroMemory((PVOID)pbBuffer,(cchSrc + 1)* sizeof(WCHAR));
  54. }
  55. // convert to unicode using the source codepage
  56. cch = MultiByteToWideChar(uCodepageSrc, 0, pchSrc, cchSrc, (WCHAR *)pbBuffer, cchSrc);
  57. if (cch <= 0)
  58. {
  59. hr = E_FAIL;
  60. goto L_Return;
  61. }
  62. *pcchTgt = cch = WideCharToMultiByte(uUcodepageTgt, 0, (WCHAR *) pbBuffer, cch, pchTgt, cchTgtMax, pchDefaultChar, pfUsedDefaultChar);
  63. if (cch <= 0)
  64. hr = E_FAIL;
  65. L_Return :;
  66. if ((NULL != pbBuffer) && (pbBuffer != pbScratchBuffer))
  67. GlobalFree(pbBuffer);
  68. return (hr);
  69. }
  70. HRESULT WINAPI HrConvertStringCodepage(UINT uCodepageSrc, char *pchSrc, int cchSrc,
  71. UINT uUcodepageTgt, char *pchTgt, int cchTgtMax, int *pcchTgt,
  72. void *pbScratchBuffer, int iSizeScratchBuffer)
  73. {
  74. return HrConvertStringCodepageEx(uCodepageSrc, pchSrc, cchSrc, uUcodepageTgt,
  75. pchTgt, cchTgtMax, pcchTgt,
  76. pbScratchBuffer, iSizeScratchBuffer,
  77. NULL, NULL);
  78. }