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.

109 lines
3.3 KiB

  1. //
  2. // xstring.cpp
  3. //
  4. // Unicode/ansi conversion.
  5. //
  6. #include "private.h"
  7. #include "xstring.h"
  8. //+------------------------------------------------------------------------
  9. //
  10. // Function: UnicodeToAnsi
  11. //
  12. // Synopsis: Converts unicode to mbcs. If the supplied ansi buffer
  13. // is not large enough to accomodate the converted text a new
  14. // buffer is allocated.
  15. //
  16. // uLenW -> the length in unicode chars of pchW, not including a '\0' if present.
  17. // pchW is not assumed to be null terminated.
  18. // uSizeA -> the size in ansi chars of the pchAIn array.
  19. // Pass in uSizeA == 0 to force memory allocation.
  20. // Use BufferAllocFree to free any allocated memory.
  21. //
  22. //-------------------------------------------------------------------------
  23. char *UnicodeToAnsi(UINT uCodePage, const WCHAR *pchW, UINT uLenW, char *pchAIn, UINT uSizeA)
  24. {
  25. char *pchA;
  26. UINT uLenA;
  27. BOOL fUsedDefault;
  28. Assert(uSizeA == 0 || (uSizeA && pchAIn));
  29. uLenA = WideCharToMultiByte(uCodePage, 0, pchW, uLenW, NULL, 0, NULL, NULL);
  30. pchA = (uLenA >= uSizeA) ? (char *)cicMemAlloc(uLenA + 1) : pchAIn;
  31. if (pchA == NULL)
  32. return NULL;
  33. if ((!WideCharToMultiByte(uCodePage, 0, pchW, uLenW, pchA, uLenA, NULL, &fUsedDefault) && uLenW) ||
  34. fUsedDefault)
  35. {
  36. BufferAllocFree(pchAIn, pchA);
  37. pchA = NULL;
  38. }
  39. else
  40. {
  41. pchA[uLenA] = '\0';
  42. }
  43. return pchA;
  44. }
  45. //+------------------------------------------------------------------------
  46. //
  47. // Function: AnsiToUnicode
  48. //
  49. // Synopsis: Converts mbcs to unicode. If the supplied unicode buffer
  50. // is not large enough to accomodate the converted text a new
  51. // buffer is allocated.
  52. //
  53. // uLenA -> the length in ansi chars of pchA, not including a '\0' if present.
  54. // pchA is not assumed to be null terminated.
  55. // uSizeW -> the size in unicode chars of the pchWIn array.
  56. // Pass in uSizeW == 0 to force memory allocation.
  57. // Use BufferAllocFree to free any allocated memory.
  58. //
  59. // Copied from dimm.dll/util.cpp
  60. //-------------------------------------------------------------------------
  61. WCHAR *AnsiToUnicode(UINT uCodePage, const char *pchA, UINT uLenA, WCHAR *pchWIn, UINT uSizeW)
  62. {
  63. WCHAR *pchW;
  64. UINT uLenW;
  65. Assert(uSizeW == 0 || (uSizeW && pchWIn));
  66. uLenW = MultiByteToWideChar(uCodePage, 0, pchA, uLenA, NULL, 0);
  67. pchW = (uLenW >= uSizeW) ? (WCHAR *)cicMemAlloc((uLenW + 1) * sizeof(WCHAR)) : pchWIn;
  68. if (pchW == NULL)
  69. return NULL;
  70. if (!MultiByteToWideChar(uCodePage, MB_ERR_INVALID_CHARS, pchA, uLenA, pchW, uLenW) && uLenA)
  71. {
  72. BufferAllocFree(pchWIn, pchW);
  73. pchW = NULL;
  74. }
  75. else
  76. {
  77. pchW[uLenW] = '\0';
  78. }
  79. return pchW;
  80. }
  81. //+------------------------------------------------------------------------
  82. //
  83. // Function: BufferAllocFree
  84. //
  85. // Synopsis: Frees any memory allocated in a previous call to UnicodeToAnsi.
  86. //
  87. //-------------------------------------------------------------------------
  88. void BufferAllocFree(void *pBuffer, void *pAllocMem)
  89. {
  90. if (pAllocMem && pAllocMem != pBuffer)
  91. {
  92. cicMemFree(pAllocMem);
  93. }
  94. }