Source code of Windows XP (NT5)
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.

221 lines
5.7 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rcunicod.c
  5. Abstract:
  6. Routines added to rcpp to support 16-bit unicode file parsing.
  7. Note that as of Aug 91, rcpp will not fully transfer the unicode
  8. characters but only the string constants are guaranteed to be passed
  9. cleanly.
  10. Author:
  11. David J. Marsyla (t-davema) 25-Aug-1991
  12. Revision History:
  13. --*/
  14. #include "rc.h"
  15. extern BOOL WINAPI LocalIsTextUnicode(CONST LPVOID Buffer, int Size, LPINT Result);
  16. INT
  17. DetermineFileType (
  18. IN PFILE fpInputFile
  19. )
  20. /*++
  21. Routine Description:
  22. This function is used to determine what type of file is being read.
  23. Note, the file is returned to it's proper position after function.
  24. Arguments:
  25. fpInputFile - File pointer to file we are checking, must be
  26. open with read permissions.
  27. Return Value:
  28. DFT_FILE_IS_UNKNOWN - It was impossible to determine what type of file
  29. we were checking. This usually happens when EOF
  30. is unexpectedly reached.
  31. DFT_FILE_IS_8_BIT - File was determined to be in standard 8-bit
  32. format.
  33. DFT_FILE_IS_16_BIT - File was determined to be a 16 bit unicode file
  34. which can be directly read into a WCHAR array.
  35. DFT_FILE_IS_16_BIT_REV - File was determined to be a 16 bit unicode file
  36. which has it's bytes reversed in order.
  37. --*/
  38. {
  39. LONG lStartFilePos; // Storage for file position.
  40. BYTE buf[DFT_TEST_SIZE+2];
  41. LONG chRead;
  42. INT val = 0xFFFF;
  43. INT fFileType;
  44. //
  45. // Store position so we can get back to it.
  46. //
  47. lStartFilePos = ftell (fpInputFile);
  48. //
  49. // Make sure we start on an even byte to simplify routines.
  50. //
  51. if (lStartFilePos % 2)
  52. fgetc (fpInputFile);
  53. chRead = fread (buf, 1, DFT_TEST_SIZE, fpInputFile);
  54. memset (buf + chRead, 0, sizeof(WCHAR));
  55. if (LocalIsTextUnicode (buf, chRead, &val))
  56. {
  57. if ((val & IS_TEXT_UNICODE_REVERSE_SIGNATURE) == IS_TEXT_UNICODE_REVERSE_SIGNATURE)
  58. fFileType = DFT_FILE_IS_16_BIT_REV;
  59. else
  60. fFileType = DFT_FILE_IS_16_BIT;
  61. }
  62. else
  63. fFileType = DFT_FILE_IS_8_BIT;
  64. //
  65. // Return to starting file position. (usually beginning)
  66. //
  67. fseek (fpInputFile, lStartFilePos, SEEK_SET);
  68. return (fFileType);
  69. }
  70. INT
  71. DetermineSysEndianType (
  72. VOID
  73. )
  74. /*++
  75. Routine Description:
  76. This function is used to determine how the current system stores its
  77. integers in memory.
  78. For those of us who are confused by little endian and big endian formats,
  79. here is a brief recap.
  80. Little Endian: (This is used on Intel 80x86 chips. The MIPS RS4000 chip
  81. is switchable, but will run in little endian format for NT.)
  82. This is where the high order bytes of a short or long are stored higher
  83. in memory. For example the number 0x80402010 is stored as follows.
  84. Address: Value:
  85. 00 10
  86. 01 20
  87. 02 40
  88. 03 80
  89. This looks backwards when memory is dumped in order: 10 20 40 80
  90. Big Endian: (This is not currently used on any NT systems but hey, this
  91. is supposed to be portable!!)
  92. This is where the high order bytes of a short or long are stored lower
  93. in memory. For example the number 0x80402010 is stored as follows.
  94. Address: Value:
  95. 00 80
  96. 01 40
  97. 02 20
  98. 03 10
  99. This looks correct when memory is dumped in order: 80 40 20 10
  100. Arguments:
  101. None.
  102. Return Value:
  103. DSE_SYS_LITTLE_ENDIAN - The system stores integers in little endian
  104. format. (this is 80x86 default).
  105. DSE_SYS_BIG_ENDIAN - The system stores integers in big endian format.
  106. --*/
  107. {
  108. INT nCheckInteger;
  109. CHAR rgchTestBytes [sizeof (INT)];
  110. //
  111. // Clear the test bytes to zero.
  112. //
  113. *((INT *)rgchTestBytes) = 0;
  114. //
  115. // Set first to some value.
  116. //
  117. rgchTestBytes [0] = (CHAR)0xFF;
  118. //
  119. // Map it to an integer.
  120. //
  121. nCheckInteger = *((INT *)rgchTestBytes);
  122. //
  123. // See if value was stored in low order of integer.
  124. // If so then system is little endian.
  125. //
  126. if (nCheckInteger == 0xFF)
  127. return (DSE_SYS_LITTLE_ENDIAN);
  128. else
  129. return (DSE_SYS_LITTLE_ENDIAN);
  130. }
  131. //
  132. // UnicodeCommandLine
  133. //
  134. // Makes a Unicode buffer copy of command line argv arguments
  135. //
  136. WCHAR ** UnicodeCommandLine (int argc, char ** argv)
  137. {
  138. WCHAR ** argv_U;
  139. WCHAR ** pU;
  140. WCHAR * str;
  141. int nbytes;
  142. int i;
  143. // Calculate the size of buffer
  144. for (i = 0, nbytes = 0; i < argc; i++)
  145. nbytes += strlen(argv[i]) + 1;
  146. nbytes *= sizeof(WCHAR);
  147. /* allocate space for argv[] vector and strings */
  148. argv_U = (WCHAR **) MyAlloc((argc + 1) * sizeof(WCHAR *) + nbytes);
  149. if (!argv_U)
  150. return (NULL);
  151. /* store args and argv ptrs in just allocated block */
  152. str = (WCHAR *)(((PBYTE)argv_U) + (argc + 1) * sizeof(WCHAR *));
  153. for (i = 0, pU = argv_U; i < argc; i++)
  154. {
  155. *pU++ = str;
  156. nbytes = strlen(argv[i]) + 1;
  157. MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, argv[i], nbytes, str, nbytes);
  158. str += nbytes;
  159. }
  160. *pU = NULL;
  161. return (argv_U);
  162. }