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.

333 lines
9.6 KiB

  1. /**************************** Module Header ********************************\
  2. * Module Name: oemxlate.c
  3. *
  4. * Copyright (c) 1985 - 1999, Microsoft Corporation
  5. *
  6. * ANSI/UNICODE(U+00--) to/from OEM translation routines for CP 437
  7. *
  8. * The goal of this module is to translate strings from ANSI/U+00-- to Oem
  9. * character set or the opposite. If there is no equivalent character
  10. * we use the followings rules:
  11. *
  12. * 1) we put a similar character (e.g. character without accent)
  13. * 2) In OemToChar, graphics vertical, horizontal, and junction characters
  14. * are usually translated to '|', '-', and '+' characters, as appropriate,
  15. * unless the ANSI set is expanded to include such graphics.
  16. * 3) Otherwise we put underscore "_".
  17. *
  18. * History:
  19. * IanJa 4/10/91 from Win3.1 \\pucus\win31ro!drivers\keyboard\xlat*.*
  20. \***************************************************************************/
  21. #include "precomp.h"
  22. #pragma hdrstop
  23. /***************************************************************************\
  24. * CharToOemA
  25. *
  26. * CharToOemA(pSrc, pDst) - Translates the ANSI string at pSrc into
  27. * the OEM string at pDst. pSrc == pDst is legal.
  28. * Always returns TRUE
  29. *
  30. \***************************************************************************/
  31. FUNCLOG2(LOG_GENERAL, BOOL, WINAPI, CharToOemA, LPCSTR, pSrc, LPSTR, pDst)
  32. BOOL WINAPI CharToOemA(
  33. LPCSTR pSrc,
  34. LPSTR pDst)
  35. {
  36. UserAssert(gpsi);
  37. if (pSrc == NULL || pDst == NULL) {
  38. return FALSE;
  39. }
  40. do {
  41. *pDst++ = gpsi->acAnsiToOem[(UCHAR)*pSrc];
  42. } while (*pSrc++);
  43. return TRUE;
  44. }
  45. /***************************************************************************\
  46. * CharToOemBuffA
  47. *
  48. * CharToOemBuffA(pSrc, pDst, nLength) - Translates nLength characters from
  49. * the ANSI string at pSrc into OEM characters in the buffer at pDst.
  50. * pSrc == pDst is legal.
  51. *
  52. * History:
  53. \***************************************************************************/
  54. FUNCLOG3(LOG_GENERAL, BOOL, WINAPI, CharToOemBuffA, LPCSTR, pSrc, LPSTR, pDst, DWORD, nLength)
  55. BOOL WINAPI CharToOemBuffA(
  56. LPCSTR pSrc,
  57. LPSTR pDst,
  58. DWORD nLength)
  59. {
  60. UserAssert(gpsi);
  61. if (pSrc == NULL || pDst == NULL) {
  62. return FALSE;
  63. }
  64. while (nLength--) {
  65. *pDst++ = gpsi->acAnsiToOem[(UCHAR)*pSrc++];
  66. }
  67. return TRUE;
  68. }
  69. /***************************************************************************\
  70. * OemToCharA
  71. *
  72. * OemToCharA(pSrc, pDst) - Translates the OEM string at pSrc into
  73. * the ANSI string at pDst. pSrc == pDst is legal.
  74. *
  75. * Always returns TRUE
  76. *
  77. * History:
  78. \***************************************************************************/
  79. FUNCLOG2(LOG_GENERAL, BOOL, WINAPI, OemToCharA, LPCSTR, pSrc, LPSTR, pDst)
  80. BOOL WINAPI OemToCharA(
  81. LPCSTR pSrc,
  82. LPSTR pDst)
  83. {
  84. UserAssert(gpsi);
  85. if (pSrc == NULL || pDst == NULL) {
  86. return FALSE;
  87. }
  88. do {
  89. *pDst++ = gpsi->acOemToAnsi[(UCHAR)*pSrc];
  90. } while (*pSrc++);
  91. return TRUE;
  92. }
  93. /***************************************************************************\
  94. * OemToCharBuffA
  95. *
  96. * OemToCharBuffA(pSrc, pDst, nLength) - Translates nLength OEM characters from
  97. * the buffer at pSrc into ANSI characters in the buffer at pDst.
  98. * pSrc == pDst is legal.
  99. *
  100. * Always returns TRUE
  101. *
  102. * History:
  103. \***************************************************************************/
  104. FUNCLOG3(LOG_GENERAL, BOOL, WINAPI, OemToCharBuffA, LPCSTR, pSrc, LPSTR, pDst, DWORD, nLength)
  105. BOOL WINAPI OemToCharBuffA(
  106. LPCSTR pSrc,
  107. LPSTR pDst,
  108. DWORD nLength)
  109. {
  110. UserAssert(gpsi);
  111. if (pSrc == NULL || pDst == NULL) {
  112. return FALSE;
  113. }
  114. while (nLength--) {
  115. *pDst++ = gpsi->acOemToAnsi[(UCHAR)*pSrc++];
  116. }
  117. return TRUE;
  118. }
  119. /***************************************************************************\
  120. * CharToOemW
  121. *
  122. * CharToOemW(pSrc, pDst) - Translates the Unicode string at pSrc into
  123. * the OEM string at pDst. pSrc == pDst is legal.
  124. *
  125. * History:
  126. \***************************************************************************/
  127. FUNCLOG2(LOG_GENERAL, BOOL, WINAPI, CharToOemW, LPCWSTR, pSrc, LPSTR, pDst)
  128. BOOL WINAPI CharToOemW(
  129. LPCWSTR pSrc,
  130. LPSTR pDst)
  131. {
  132. int cch;
  133. if (pSrc == NULL || pDst == NULL) {
  134. return FALSE;
  135. } else if (pSrc == (LPCWSTR)pDst) {
  136. /*
  137. * WideCharToMultiByte() requires pSrc != pDst: fail this call.
  138. * LATER: Is this really true?
  139. */
  140. return FALSE;
  141. }
  142. cch = wcslen(pSrc) + 1;
  143. WideCharToMultiByte(
  144. CP_OEMCP, // Unicode -> OEM
  145. 0, // gives best visual match
  146. (LPWSTR)pSrc, cch, // source & length
  147. pDst, // dest
  148. cch * 2, // max poss.length (DBCS may * 2)
  149. "_", // default char
  150. NULL); // (don't care whether defaulted)
  151. return TRUE;
  152. }
  153. /***************************************************************************\
  154. * CharToOemBuffW
  155. *
  156. * CharToOemBuffW(pSrc, pDst, nLength) - Translates nLength characters from
  157. * the Unicode string at pSrc into OEM characters in the buffer at pDst.
  158. * pSrc == pDst is legal.
  159. *
  160. * History:
  161. \***************************************************************************/
  162. FUNCLOG3(LOG_GENERAL, BOOL, WINAPI, CharToOemBuffW, LPCWSTR, pSrc, LPSTR, pDst, DWORD, nLength)
  163. BOOL WINAPI CharToOemBuffW(
  164. LPCWSTR pSrc,
  165. LPSTR pDst,
  166. DWORD nLength)
  167. {
  168. if (pSrc == NULL || pDst == NULL) {
  169. return FALSE;
  170. } else if (pSrc == (LPCWSTR)pDst) {
  171. /*
  172. * WideCharToMultiByte() requires pSrc != pDst: fail this call.
  173. * LATER: Is this really true?
  174. */
  175. return FALSE;
  176. }
  177. WideCharToMultiByte(
  178. CP_OEMCP, // Unicode -> OEM
  179. 0, // gives best visual match
  180. (LPWSTR)pSrc, (int)nLength, // source & length
  181. pDst, // dest
  182. (int)nLength * 2, // max poss. length (DBCS may * 2)
  183. "_", // default char
  184. NULL); // (don't care whether defaulted)
  185. return TRUE;
  186. }
  187. /***************************************************************************\
  188. * OemToCharW
  189. *
  190. * OemToCharW(pSrc, pDst) - Translates the OEM string at pSrc into
  191. * the Unicode string at pDst. pSrc == pDst is not legal.
  192. *
  193. * History:
  194. \***************************************************************************/
  195. FUNCLOG2(LOG_GENERAL, BOOL, WINAPI, OemToCharW, LPCSTR, pSrc, LPWSTR, pDst)
  196. BOOL WINAPI OemToCharW(
  197. LPCSTR pSrc,
  198. LPWSTR pDst)
  199. {
  200. int cch;
  201. if (pSrc == NULL || pDst == NULL) {
  202. return FALSE;
  203. } else if (pSrc == (LPCSTR)pDst) {
  204. /*
  205. * MultiByteToWideChar() requires pSrc != pDst: fail this call.
  206. * LATER: Is this really true?
  207. */
  208. return FALSE;
  209. }
  210. cch = strlen(pSrc) + 1;
  211. MultiByteToWideChar(
  212. CP_OEMCP, // Unicode -> OEM
  213. MB_PRECOMPOSED | MB_USEGLYPHCHARS, // visual map to precomposed
  214. (LPSTR)pSrc, cch, // source & length
  215. pDst, // destination
  216. cch); // max poss. precomposed length
  217. return TRUE;
  218. }
  219. /***************************************************************************\
  220. * OemToCharBuffW
  221. *
  222. * OemToCharBuffW(pSrc, pDst, nLength) - Translates nLength OEM characters from
  223. * the buffer at pSrc into Unicode characters in the buffer at pDst.
  224. * pSrc == pDst is not legal.
  225. *
  226. * History:
  227. \***************************************************************************/
  228. FUNCLOG3(LOG_GENERAL, BOOL, WINAPI, OemToCharBuffW, LPCSTR, pSrc, LPWSTR, pDst, DWORD, nLength)
  229. BOOL WINAPI OemToCharBuffW(
  230. LPCSTR pSrc,
  231. LPWSTR pDst,
  232. DWORD nLength)
  233. {
  234. if (pSrc == NULL || pDst == NULL) {
  235. return FALSE;
  236. } else if (pSrc == (LPCSTR)pDst) {
  237. /*
  238. * MultiByteToWideChar() requires pSrc != pDst: fail this call.
  239. * LATER: Is this really true?
  240. */
  241. return FALSE;
  242. }
  243. if (MultiByteToWideChar(
  244. CP_OEMCP, // Unicode -> OEM
  245. MB_PRECOMPOSED | MB_USEGLYPHCHARS, // visual map to precomposed
  246. (LPSTR)pSrc, nLength, // source & length
  247. pDst, // destination
  248. nLength)) { // max poss. precomposed length
  249. return TRUE;
  250. }
  251. return FALSE;
  252. }
  253. /***************************************************************************\
  254. * OemKeyScan (API)
  255. *
  256. * Converts an OEM character into a scancode plus shift state, returning
  257. * scancode in low byte, shift state in high byte.
  258. *
  259. * Returns -1 on error.
  260. *
  261. \***************************************************************************/
  262. FUNCLOG1(LOG_GENERAL, DWORD, WINAPI, OemKeyScan, WORD, wOemChar)
  263. DWORD WINAPI OemKeyScan(
  264. WORD wOemChar)
  265. {
  266. WCHAR wchOem;
  267. SHORT sVk;
  268. UINT dwRet;
  269. #ifdef FE_SB // OemKeyScan()
  270. /*
  271. * Return 0xFFFFFFFF for DBCS LeadByte character.
  272. */
  273. if (IsDBCSLeadByte(LOBYTE(wOemChar))) {
  274. return 0xFFFFFFFF;
  275. }
  276. #endif // FE_SB
  277. if (!OemToCharBuffW((LPCSTR)&wOemChar, &wchOem, 1)) {
  278. return 0xFFFFFFFF;
  279. }
  280. sVk = VkKeyScanW(wchOem);
  281. if ((dwRet = MapVirtualKeyW(LOBYTE(sVk), 0)) == 0) {
  282. return 0xFFFFFFFF;
  283. }
  284. return dwRet | ((sVk & 0xFF00) << 8);
  285. }