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.

205 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. Utmb2u.c
  5. Abstract:
  6. Module that contains code to convert a multibyte file
  7. to unicode.
  8. Author:
  9. Ted Miller (tedm) 17-June-1993
  10. Revision History:
  11. --*/
  12. #include "unitext.h"
  13. VOID
  14. MultibyteTextFileToUnicode(
  15. IN LPWSTR SourceFileName,
  16. IN LPWSTR TargetFileName,
  17. IN HANDLE SourceFileHandle,
  18. IN HANDLE TargetFileHandle,
  19. IN DWORD SourceFileSize,
  20. IN UINT SourceCodePage
  21. )
  22. /*++
  23. Routine Description:
  24. Convert an open multibyte text file to a unicode text file,
  25. interpreting the data in the multibyte text file as a stream
  26. of characters in a given codepage.
  27. Arguments:
  28. SourceFileName - name of source (multibyte) text file.
  29. TargetFileName - name of target (unicode) text file.
  30. SourceFileHandle - win32 handle to the open source file.
  31. The file pointer should be fully rewound.
  32. TargetFileHandle - win32 handle to the open target file.
  33. The file pointer should be fully rewound.
  34. SourceFileSize - size in bytes of the source file.
  35. SourceCodePage - codepage for the source file.
  36. Return Value:
  37. None. Does not return if error.
  38. --*/
  39. {
  40. HANDLE SourceMapping,TargetMapping;
  41. LPSTR SourceView;
  42. LPWSTR TargetView;
  43. int CharsConverted;
  44. DWORD MaxTargetSize;
  45. DWORD EndOfFile;
  46. DWORD err;
  47. //
  48. // Tell the user what we're doing.
  49. //
  50. MsgPrintfW(MSG_CONV_MB_TO_UNICODE,SourceFileName,TargetFileName,SourceCodePage);
  51. //
  52. // Create a file mapping object that maps the entire source file.
  53. //
  54. SourceMapping = CreateFileMapping(
  55. SourceFileHandle,
  56. NULL,
  57. PAGE_READONLY,
  58. 0,
  59. SourceFileSize,
  60. NULL
  61. );
  62. if(SourceMapping == NULL) {
  63. ErrorAbort(MSG_CANT_MAP_FILE,SourceFileName,GetLastError());
  64. }
  65. //
  66. // Calculate the maximum target file size. This is twice the
  67. // source file size, plus one wchar for the byte order mark.
  68. // The file could be smaller if there are double-byte characters
  69. // in the source file.
  70. //
  71. MaxTargetSize = (SourceFileSize+1)*sizeof(WCHAR);
  72. //
  73. // Create a file mapping object that maps the maximum size of
  74. // the target file.
  75. //
  76. TargetMapping = CreateFileMapping(
  77. TargetFileHandle,
  78. NULL,
  79. PAGE_READWRITE,
  80. 0,
  81. MaxTargetSize,
  82. NULL
  83. );
  84. if(TargetMapping == NULL) {
  85. CloseHandle(SourceMapping);
  86. ErrorAbort(MSG_CANT_MAP_FILE,TargetFileName,GetLastError());
  87. }
  88. //
  89. // Map views of the two files.
  90. //
  91. SourceView = MapViewOfFile(
  92. SourceMapping,
  93. FILE_MAP_READ,
  94. 0,0,
  95. SourceFileSize
  96. );
  97. if(SourceView == NULL) {
  98. CloseHandle(SourceMapping);
  99. CloseHandle(TargetMapping);
  100. ErrorAbort(MSG_CANT_MAP_FILE,SourceFileName,GetLastError());
  101. }
  102. TargetView = MapViewOfFile(
  103. TargetMapping,
  104. FILE_MAP_WRITE,
  105. 0,0,
  106. MaxTargetSize
  107. );
  108. if(TargetView == NULL) {
  109. UnmapViewOfFile(SourceView);
  110. CloseHandle(SourceMapping);
  111. CloseHandle(TargetMapping);
  112. ErrorAbort(MSG_CANT_MAP_FILE,TargetFileName,GetLastError());
  113. }
  114. //
  115. // Write the byte-order mark into the target file.
  116. //
  117. *TargetView++ = BYTE_ORDER_MARK;
  118. //
  119. // Do the conversion in one fell swoop.
  120. //
  121. CharsConverted = MultiByteToWideChar(
  122. SourceCodePage,
  123. MB_PRECOMPOSED,
  124. SourceView,
  125. SourceFileSize,
  126. TargetView,
  127. MaxTargetSize
  128. );
  129. if(!CharsConverted) {
  130. err = GetLastError();
  131. }
  132. //
  133. // Do some cleanup.
  134. //
  135. UnmapViewOfFile(SourceView);
  136. UnmapViewOfFile(TargetView);
  137. CloseHandle(SourceMapping);
  138. CloseHandle(TargetMapping);
  139. //
  140. // Check for error in conversion.
  141. //
  142. if(!CharsConverted) {
  143. ErrorAbort(MSG_CONVERT_FAILED,err);
  144. }
  145. //
  146. // We know how many characters there are in the target file now,
  147. // so set the target file size accordingly.
  148. //
  149. EndOfFile = (CharsConverted+1)*sizeof(WCHAR);
  150. if(SetFilePointer(TargetFileHandle,EndOfFile,NULL,FILE_BEGIN) != EndOfFile) {
  151. ErrorAbort(MSG_SEEK_ERROR,TargetFileName,GetLastError());
  152. }
  153. if(!SetEndOfFile(TargetFileHandle)) {
  154. ErrorAbort(MSG_ERROR_SET_EOF,TargetFileName,GetLastError());
  155. }
  156. MsgPrintfW(MSG_CONVERT_OK);
  157. }