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.

294 lines
12 KiB

  1. // File Name: unix2pc.c
  2. // Owner: Tetsuhide Akaishi
  3. // Revision: 1.00 02/21/'93 Tetsuhide Akaishi
  4. //
  5. #include "win32.h"
  6. #include "fechrcnv.h"
  7. #ifdef DBCS_DIVIDE
  8. extern DBCS_STATUS dStatus0;
  9. extern DBCS_STATUS dStatus;
  10. #endif
  11. #ifdef IEXPLORE
  12. extern int nCurrentCodeSet;
  13. #endif
  14. int JPNUNIX_to_PC ( int CodeSet,
  15. UCHAR *pUNIXChar, int UNIXChar_len,
  16. UCHAR *pPCChar, int PCChar_len )
  17. // The JPNUNIX_to_PC function convert a character string as Japanese UNIX code
  18. // set string to a PC code set string.
  19. //
  20. //
  21. // int CodeSet Code Set Type.
  22. // There are three Japanese Code set in UNIX world.
  23. // These code sets are JIS, EUC and Shift JIS.
  24. // When CodePage is Japanese, the following Code set
  25. // constants are defined:
  26. //
  27. // Value Meaning
  28. // CODE_UNKNOWN Unknown. If this value is CODE_UNKNOWN,
  29. // Code Type is checked automatically.
  30. //
  31. // CODE_JPN_JIS JIS Code Set. The function convert
  32. // pUNIXChar string as JIS code set string
  33. // to a PC code set string.
  34. // CODE_JPN_EUC EUC Code Set. The function convert
  35. // pUNIXChar string as EUC code set string
  36. // to a PC code set string.
  37. // CODE_JPN_SJIS Shift JIS Code Set.
  38. //
  39. // UCHAR *pUNIXChar Points to the character string to be converted.
  40. //
  41. // int UNIXChar_len Specifies the size in bytes of the string pointed
  42. // to by the pUNIXChar parameter. If this value is -1,
  43. // the string is assumed to be NULL terminated and the
  44. // length is calculated automatically.
  45. //
  46. // UCHAR *pPCChar Points to a buffer that receives the convert string
  47. // from UNIX Code to PC Code.
  48. //
  49. // int PCChar_len Specifies the size, in PC characters of the buffer
  50. // pointed to by the pPCChar parameter. If the value is zero,
  51. // the function returns the number of PC characters
  52. // required for the buffer, and makes no use of the pPCChar
  53. // buffer.
  54. //
  55. // Return Value
  56. // If the function succeeds, and PCChar_len is nonzero, the return value is the
  57. // number of PC characters written to the buffer pointed to by pPCChar.
  58. //
  59. // If the function succeeds, and PCChar_len is zero, the return value is the
  60. // required size, in PC characters, for a buffer that can receive the
  61. // converted string.
  62. //
  63. // If the function fails, the return value is -1. The error mean pPCChar buffer
  64. // is small for setting converted strings.
  65. //
  66. {
  67. int re;
  68. #ifdef DBCS_DIVIDE
  69. int i = 0, nDelta = 0;
  70. if ( PCChar_len == 0 && dStatus0.nCodeSet != CODE_UNKNOWN)
  71. CodeSet = dStatus0.nCodeSet;
  72. else if ( PCChar_len != 0 && dStatus.nCodeSet != CODE_UNKNOWN )
  73. CodeSet = dStatus.nCodeSet;
  74. else
  75. #endif
  76. #ifdef IEXPLORE
  77. if ( nCurrentCodeSet == CODE_UNKNOWN ) {
  78. #endif
  79. if ( CodeSet == CODE_UNKNOWN ) {
  80. if ( ( CodeSet = DetectJPNCode ( pUNIXChar, UNIXChar_len ) )
  81. == CODE_ONLY_SBCS ) {
  82. CodeSet = CODE_JPN_JIS;
  83. }
  84. }
  85. #ifdef IEXPLORE
  86. nCurrentCodeSet = CodeSet;
  87. } else
  88. CodeSet = nCurrentCodeSet;
  89. #endif
  90. switch ( CodeSet ) {
  91. case CODE_JPN_JIS: // Japanese JIS Code
  92. // Convert from JIS to Shift JIS
  93. re = JIS_to_ShiftJIS ( pUNIXChar, UNIXChar_len,
  94. pPCChar, PCChar_len );
  95. break;
  96. case CODE_JPN_EUC: // Japanese EUC Code
  97. // Convert from EUC to Shift JIS
  98. re = EUC_to_ShiftJIS ( pUNIXChar, UNIXChar_len,
  99. pPCChar, PCChar_len );
  100. break;
  101. default:
  102. case CODE_JPN_SJIS: // Japanese Shift JIS Code
  103. // Start Only Copy Process
  104. if ( UNIXChar_len == -1 ) {
  105. UNIXChar_len = strlen ( pUNIXChar ) + 1;
  106. }
  107. if ( PCChar_len != 0 ) {
  108. #ifdef DBCS_DIVIDE
  109. UCHAR *pPCCharEnd = pPCChar + PCChar_len - 1;
  110. if ( dStatus.nCodeSet == CODE_JPN_SJIS && dStatus.cSavedByte){
  111. *pPCChar++ = dStatus.cSavedByte;
  112. *pPCChar = *pUNIXChar;
  113. ++UNIXChar_len;
  114. ++nDelta;
  115. ++i;
  116. dStatus.nCodeSet = CODE_UNKNOWN;
  117. dStatus.cSavedByte = '\0';
  118. }
  119. while(i < UNIXChar_len - nDelta){
  120. if(IsDBCSLeadByte(*(pUNIXChar + i))){
  121. if(i == UNIXChar_len - nDelta - 1){
  122. dStatus.nCodeSet = CODE_JPN_SJIS;
  123. dStatus.cSavedByte = *(pUNIXChar + i);
  124. --UNIXChar_len;
  125. break;
  126. } else if((i == UNIXChar_len - nDelta - 2) &&
  127. (*(pUNIXChar + i + 1) == '\0')){
  128. dStatus.nCodeSet = CODE_JPN_SJIS;
  129. dStatus.cSavedByte = *(pUNIXChar + i);
  130. *(pPCChar + i + 1) = '\0';
  131. --UNIXChar_len;
  132. break;
  133. }
  134. if(pPCChar + i > pPCCharEnd) // check destination buf
  135. break;
  136. *(pPCChar + i++) = *(pUNIXChar + i);
  137. *(pPCChar + i++) = *(pUNIXChar + i);
  138. } else
  139. *(pPCChar + i++) = *(pUNIXChar + i);
  140. }
  141. #else
  142. if ( UNIXChar_len > PCChar_len ) { // Is the buffer small?
  143. return ( -1 );
  144. }
  145. memmove ( pPCChar, pUNIXChar, UNIXChar_len );
  146. #endif
  147. }
  148. #ifdef DBCS_DIVIDE
  149. else { // Only retrun the required size
  150. if ( dStatus0.nCodeSet == CODE_JPN_SJIS ){ // 1st byte was saved
  151. ++UNIXChar_len;
  152. ++nDelta;
  153. ++i;
  154. dStatus0.nCodeSet = CODE_UNKNOWN;
  155. dStatus0.cSavedByte = '\0';
  156. }
  157. while(i < UNIXChar_len - nDelta){
  158. if(IsDBCSLeadByte(*(pUNIXChar + i))){
  159. if(i == UNIXChar_len - nDelta - 1){
  160. dStatus0.nCodeSet = CODE_JPN_SJIS;
  161. dStatus0.cSavedByte = *(pUNIXChar + i);
  162. --UNIXChar_len;
  163. break;
  164. } else if((i == UNIXChar_len - nDelta - 2) &&
  165. (*(pUNIXChar + i + 1) == '\0')){
  166. dStatus0.nCodeSet = CODE_JPN_SJIS;
  167. dStatus0.cSavedByte = *(pUNIXChar + i);
  168. --UNIXChar_len;
  169. break;
  170. }
  171. i+=2;
  172. } else
  173. i++;
  174. }
  175. }
  176. #endif
  177. re = UNIXChar_len;
  178. break;
  179. }
  180. return ( re );
  181. }
  182. int WINAPI UNIX_to_PC ( int CodePage, int CodeSet,
  183. UCHAR *pUNIXChar, int UNIXChar_len,
  184. UCHAR *pPCChar, int PCChar_len )
  185. // The UNIX_to_PC function convert a character string as UNIX code
  186. // set string to a PC code set string.
  187. //
  188. // int CodePage Country Code Page.
  189. // If this value is -1, the function use OS CodePage from
  190. // Operating System automatically.
  191. //
  192. // Value Meaning
  193. // -1 Auto Detect Mode.
  194. // 932 Japan.
  195. // ??? Taiwan.
  196. // ??? Korea.
  197. // ??? PRC(Chaina)?
  198. //
  199. // int CodeSet Code Set Type.
  200. // There are three Japanese Code set in UNIX world.
  201. // These code sets are JIS, EUC and Shift JIS.
  202. // When CodePage is Japanese, the following Code set
  203. // constants are defined:
  204. //
  205. // Value Meaning
  206. // CODE_UNKNOWN Unknown. If this value is CODE_UNKNOWN,
  207. // Code Type is checked automatically.
  208. //
  209. // CODE_JPN_JIS JIS Code Set. The function convert
  210. // pUNIXChar string as JIS code set string
  211. // to a PC code set string.
  212. // CODE_JPN_EUC EUC Code Set. The function convert
  213. // pUNIXChar string as EUC code set string
  214. // to a PC code set string.
  215. // CODE_JPN_SJIS Shift JIS Code Set.
  216. //
  217. // UCHAR *pUNIXChar Points to the character string to be converted.
  218. //
  219. // int UNIXChar_len Specifies the size in bytes of the string pointed
  220. // to by the pUNIXChar parameter. If this value is -1,
  221. // the string is assumed to be NULL terminated and the
  222. // length is calculated automatically.
  223. //
  224. // UCHAR *pPCChar Points to a buffer that receives the convert string
  225. // from UNIX Code to PC Code.
  226. //
  227. // int PCChar_len Specifies the size, in PC characters of the buffer
  228. // pointed to by the pPCChar parameter. If the value is zero,
  229. // the function returns the number of PC characters
  230. // required for the buffer, and makes no use of the pPCChar
  231. // buffer.
  232. //
  233. // Return Value
  234. // If the function succeeds, and PCChar_len is nonzero, the return value is the
  235. // number of PC characters written to the buffer pointed to by pPCChar.
  236. //
  237. // If the function succeeds, and PCChar_len is zero, the return value is the
  238. // required size, in PC characters, for a buffer that can receive the
  239. // converted string.
  240. //
  241. // If the function fails, the return value is -1. The error mean pPCChar buffer
  242. // is small for setting converted strings.
  243. //
  244. //@
  245. {
  246. int re;
  247. if ( CodePage == -1 ) {
  248. CodePage = (int)GetOEMCP();
  249. }
  250. switch ( CodePage ) {
  251. case 932: // Japanese Code Page
  252. re = JPNUNIX_to_PC ( CodeSet, pUNIXChar, UNIXChar_len,
  253. pPCChar, PCChar_len );
  254. break;
  255. // case ???: // Taiwan Code Page
  256. // re = TAIWANUNIX_to_PC (,,,,,,);
  257. // break;
  258. // case ???: // Korea Code Page
  259. // re = KOREAUNIX_to_PC (,,,,,,);
  260. // break;
  261. // case ???: // PRC Code Page
  262. // re = PRCUNIX_to_PC (,,,,,,);
  263. // break;
  264. default:
  265. // Start Only Copy Process
  266. if ( UNIXChar_len == -1 ) {
  267. UNIXChar_len = strlen ( pUNIXChar ) + 1;
  268. }
  269. if ( PCChar_len != 0 ) {
  270. if ( UNIXChar_len > PCChar_len ) { // Is the buffer small?
  271. return ( -1 );
  272. }
  273. memmove ( pPCChar, pUNIXChar, UNIXChar_len );
  274. }
  275. re = UNIXChar_len;
  276. break;
  277. }
  278. return ( re );
  279. }