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.

413 lines
13 KiB

  1. //
  2. // File Name: jis2sjis.c
  3. // Owner: Tetsuhide Akaishi
  4. // Revision: 1.00 02/21/'93 Tetsuhide Akaishi
  5. //
  6. #include "win32.h"
  7. #include "fechrcnv.h"
  8. #ifdef DBCS_DIVIDE
  9. extern DBCS_STATUS dStatus0;
  10. extern BOOL blkanji0; // Kanji In Mode
  11. extern DBCS_STATUS dStatus;
  12. extern BOOL blkanji; // Kanji In Mode
  13. extern BOOL blkana; // Kana Mode
  14. #endif
  15. VOID JISChar_to_ShiftJISChar ( UCHAR *pJIS, UCHAR *pSJIS )
  16. // The JISChar_to_ShiftJISChar function convert one character string
  17. // as JIS code to a Shift JIS code string.
  18. //
  19. // UCHAR *pJIS Points to the character string to be converted.
  20. //
  21. // UCHAR *pSJIS Points to a buffer that receives the convert string
  22. // from JIS Code to Shift JIS.
  23. //
  24. // Return Value
  25. // None.
  26. //
  27. {
  28. *pSJIS = ((*pJIS - 0x21) >> 1) +0x81;
  29. if ( *pSJIS > 0x9f ) {
  30. (*pSJIS) += 0x40;
  31. }
  32. *(pSJIS+1) = (*(pJIS+1)) + (*pJIS) & 1 ? 0x1f : 0x7d;
  33. if ( *(pSJIS+1) >= 0x7f ) {
  34. (*(pSJIS+1)) ++;
  35. }
  36. }
  37. int JIS_to_ShiftJIS ( UCHAR *pJIS, int JIS_len, UCHAR *pSJIS, int SJIS_len )
  38. // The JIS_to_ShiftJIS function convert a character string as JIS code
  39. // to a Shift JIS code string.
  40. //
  41. // UCHAR *pJIS Points to the character string to be converted.
  42. //
  43. // int JIS_len Specifies the size in bytes of the string pointed
  44. // to by the pJIS parameter. If this value is -1,
  45. // the string is assumed to be NULL terminated and the
  46. // length is calculated automatically.
  47. //
  48. // UCHAR *pSJIS Points to a buffer that receives the convert string
  49. // from EUC Code to Shift JIS.
  50. //
  51. // int SJIS_len Specifies the size, in Shift JIS characters of the
  52. // buffer pointed to by the pSJIS parameter.
  53. // If the value is zero,
  54. // the function returns the number of Shift JIS characters
  55. // required for the buffer, and makes no use of the pSJIS
  56. // buffer.
  57. //
  58. // Return Value
  59. // If the function succeeds, and SJIS_len is nonzero, the return value is the
  60. // number of Shift JIS characters written to the buffer pointed to by pSJIS.
  61. //
  62. // If the function succeeds, and SJIS_len is zero, the return value is the
  63. // required size, in Shift JIS characters, for a buffer that can receive the
  64. // converted string.
  65. //
  66. // If the function fails, the return value is -1. The error mean pSJIS buffer
  67. // is small for setting converted strings.
  68. //
  69. {
  70. int re; // Convert Lenght
  71. int i; // Loop Counter
  72. #ifndef DBCS_DIVIDE
  73. BOOL blkanji = FALSE; // Kanji In Mode
  74. BOOL blkana = FALSE; // Kana Mode
  75. #endif
  76. if ( JIS_len == -1 ) {
  77. // If length is not set, last character of the strings is NULL.
  78. JIS_len = strlen ( pJIS ) + 1;
  79. }
  80. i = 0;
  81. re = 0;
  82. if ( SJIS_len == 0 ) {
  83. // Only retrun the required size
  84. #ifdef DBCS_DIVIDE
  85. if ( dStatus0.nCodeSet == CODE_JPN_JIS ) {
  86. UCHAR cJIS = dStatus0.cSavedByte;
  87. if ( dStatus0.fESC ){
  88. if ( cJIS ){
  89. if ( cJIS == KANJI_IN_1ST_CHAR &&
  90. ( *pJIS == KANJI_IN_2ND_CHAR1 ||
  91. *pJIS == KANJI_IN_2ND_CHAR2 )){
  92. blkanji0 = TRUE;
  93. pJIS++;
  94. i++;
  95. } else if ( cJIS == KANJI_OUT_1ST_CHAR &&
  96. ( *pJIS == KANJI_OUT_2ND_CHAR1 ||
  97. *pJIS == KANJI_OUT_2ND_CHAR2 )){
  98. blkanji0 = FALSE;
  99. pJIS++;
  100. i++;
  101. } else
  102. re += 2;
  103. } else {
  104. if ( *pJIS == KANJI_IN_1ST_CHAR &&
  105. ( *(pJIS+1) == KANJI_IN_2ND_CHAR1 ||
  106. *(pJIS+1) == KANJI_IN_2ND_CHAR2 )){
  107. blkanji0 = TRUE;
  108. pJIS += 2;
  109. i += 2;
  110. } else if ( *pJIS == KANJI_OUT_1ST_CHAR &&
  111. ( *(pJIS+1) == KANJI_OUT_2ND_CHAR1 ||
  112. *(pJIS+1) == KANJI_OUT_2ND_CHAR2 )){
  113. blkanji0 = FALSE;
  114. pJIS += 2;
  115. i += 2;
  116. } else
  117. re++;
  118. }
  119. } else if ( cJIS ){ // Divide DBCS in KANJI mode
  120. pJIS++;
  121. i++;
  122. re += 2;
  123. }
  124. dStatus0.nCodeSet = CODE_UNKNOWN;
  125. dStatus0.cSavedByte = '\0';
  126. dStatus0.fESC = FALSE;
  127. }
  128. #endif
  129. while ( i < JIS_len ) {
  130. if ( *pJIS == SO ) { // Kana Mode In?
  131. blkana = TRUE;
  132. pJIS++;
  133. i++;
  134. continue;
  135. }
  136. if ( *pJIS == SI ) { // Kana Mode Out ?
  137. blkana = FALSE;
  138. pJIS++;
  139. i++;
  140. continue;
  141. }
  142. if ( blkana == TRUE ) {
  143. pJIS++;
  144. i++;
  145. re++;
  146. continue;
  147. }
  148. if ( *pJIS == ESC ) {
  149. #ifdef DBCS_DIVIDE
  150. if ( i == JIS_len - 1 || i == JIS_len - 2 ){
  151. dStatus0.nCodeSet = CODE_JPN_JIS;
  152. dStatus0.fESC = TRUE;
  153. if( i == JIS_len - 2 )
  154. dStatus0.cSavedByte = *(pJIS+1);
  155. break;
  156. }
  157. #endif
  158. if ( *(pJIS+1) == KANJI_IN_1ST_CHAR &&
  159. ( *(pJIS+2) == KANJI_IN_2ND_CHAR1 ||
  160. *(pJIS+2) == KANJI_IN_2ND_CHAR2 )) {
  161. #ifdef DBCS_DIVIDE
  162. blkanji0 = TRUE;
  163. #else
  164. blkanji = TRUE;
  165. #endif
  166. pJIS+=3;
  167. i+=3;
  168. continue;
  169. }
  170. if ( *(pJIS+1) == KANJI_OUT_1ST_CHAR &&
  171. ( *(pJIS+2) == KANJI_OUT_2ND_CHAR1 ||
  172. *(pJIS+2) == KANJI_OUT_2ND_CHAR2 )) {
  173. #ifdef DBCS_DIVIDE
  174. blkanji0 = FALSE;
  175. #else
  176. blkanji = FALSE;
  177. #endif
  178. pJIS+=3;
  179. i+=3;
  180. continue;
  181. }
  182. pJIS++;
  183. i++;
  184. re++;
  185. continue;
  186. }
  187. else {
  188. #ifdef DBCS_DIVIDE
  189. if ( blkanji0 == FALSE ) {
  190. #else
  191. if ( blkanji == FALSE ) {
  192. #endif
  193. pJIS++;
  194. i++;
  195. re++;
  196. continue;
  197. }
  198. else {
  199. #ifdef DBCS_DIVIDE
  200. if ( i == JIS_len - 1 ){
  201. dStatus0.nCodeSet = CODE_JPN_JIS;
  202. dStatus0.cSavedByte = *pJIS;
  203. break;
  204. }
  205. #endif
  206. if ( *pJIS == '*' ) {
  207. pJIS+=2;
  208. i+=2;
  209. re ++;
  210. continue;
  211. }
  212. else {
  213. pJIS+=2;
  214. i+=2;
  215. re +=2;
  216. continue;
  217. }
  218. }
  219. }
  220. }
  221. return ( re );
  222. }
  223. #ifdef DBCS_DIVIDE
  224. if ( dStatus.nCodeSet == CODE_JPN_JIS ) {
  225. UCHAR cJIS = dStatus.cSavedByte;
  226. if ( dStatus.fESC ){
  227. if ( cJIS){
  228. if ( cJIS == KANJI_IN_1ST_CHAR &&
  229. ( *pJIS == KANJI_IN_2ND_CHAR1 ||
  230. *pJIS == KANJI_IN_2ND_CHAR2 )){
  231. blkanji = TRUE;
  232. pJIS++;
  233. i++;
  234. } else if ( cJIS == KANJI_OUT_1ST_CHAR &&
  235. ( *pJIS == KANJI_OUT_2ND_CHAR1 ||
  236. *pJIS == KANJI_OUT_2ND_CHAR2 )){
  237. blkanji = FALSE;
  238. pJIS++;
  239. i++;
  240. } else {
  241. *pSJIS = ESC;
  242. *(pSJIS+1) = cJIS;
  243. re += 2;
  244. pSJIS += 2;
  245. }
  246. } else {
  247. if ( *pJIS == KANJI_IN_1ST_CHAR &&
  248. ( *(pJIS+1) == KANJI_IN_2ND_CHAR1 ||
  249. *(pJIS+1) == KANJI_IN_2ND_CHAR2 )){
  250. blkanji = TRUE;
  251. pJIS += 2;
  252. i += 2;
  253. } else if ( *pJIS == KANJI_OUT_1ST_CHAR &&
  254. ( *(pJIS+1) == KANJI_OUT_2ND_CHAR1 ||
  255. *(pJIS+1) == KANJI_OUT_2ND_CHAR2 )){
  256. blkanji = FALSE;
  257. pJIS += 2;
  258. i += 2;
  259. } else {
  260. *pSJIS = ESC;
  261. re++;
  262. pSJIS++;
  263. }
  264. }
  265. } else if ( cJIS ){ // Divide DBCS in KANJI mode
  266. // Start One Character Convert from JIS to Shift JIS
  267. *pSJIS = (cJIS - 0x21 >> 1) +0x81;
  268. if ( *pSJIS > 0x9f ) {
  269. (*pSJIS) += 0x40;
  270. }
  271. *(pSJIS+1) = *pJIS + ( cJIS & 1 ? 0x1f : 0x7d );
  272. if ( *(pSJIS+1) >= 0x7f ) {
  273. (*(pSJIS+1)) ++;
  274. }
  275. pJIS++;
  276. i++;
  277. re += 2;
  278. pSJIS += 2;
  279. }
  280. dStatus.nCodeSet = CODE_UNKNOWN;
  281. dStatus.cSavedByte = '\0';
  282. dStatus.fESC = FALSE;
  283. }
  284. #endif
  285. while ( i < JIS_len ) {
  286. if ( *pJIS == SO ) { // Kana Mode In?
  287. blkana = TRUE;
  288. pJIS++;
  289. i++;
  290. continue;
  291. }
  292. if ( *pJIS == SI ) { // Kana Mode Out ?
  293. blkana = FALSE;
  294. pJIS++;
  295. i++;
  296. continue;
  297. }
  298. if ( blkana == TRUE ) {
  299. if ( re >= SJIS_len ) { // Buffer Over flow?
  300. return ( -1 );
  301. }
  302. *pSJIS = (*pJIS) | 0x80;
  303. pJIS++;
  304. i++;
  305. re++;
  306. pSJIS++;
  307. continue;
  308. }
  309. if ( *pJIS == ESC ) {
  310. #ifdef DBCS_DIVIDE
  311. if ( i == JIS_len - 1 || i == JIS_len - 2 ){
  312. dStatus.nCodeSet = CODE_JPN_JIS;
  313. dStatus.fESC = TRUE;
  314. if( i == JIS_len - 2 )
  315. dStatus.cSavedByte = *(pJIS+1);
  316. break;
  317. }
  318. #endif
  319. if ( *(pJIS+1) == KANJI_IN_1ST_CHAR &&
  320. ( *(pJIS+2) == KANJI_IN_2ND_CHAR1 ||
  321. *(pJIS+2) == KANJI_IN_2ND_CHAR2 )) {
  322. blkanji = TRUE;
  323. pJIS+=3;
  324. i+=3;
  325. continue;
  326. }
  327. if ( *(pJIS+1) == KANJI_OUT_1ST_CHAR &&
  328. ( *(pJIS+2) == KANJI_OUT_2ND_CHAR1 ||
  329. *(pJIS+2) == KANJI_OUT_2ND_CHAR2 )) {
  330. blkanji = FALSE;
  331. pJIS+=3;
  332. i+=3;
  333. continue;
  334. }
  335. if ( re >= SJIS_len ) { // Buffer Over flow?
  336. return ( -1 );
  337. }
  338. *pSJIS = *pJIS;
  339. pJIS++;
  340. i++;
  341. re++;
  342. pSJIS++;
  343. continue;
  344. }
  345. else {
  346. if ( blkanji == FALSE ) {
  347. if ( re >= SJIS_len ) { // Buffer Over flow?
  348. return ( -1 );
  349. }
  350. *pSJIS = *pJIS;
  351. pJIS++;
  352. i++;
  353. re++;
  354. pSJIS++;
  355. continue;
  356. }
  357. else {
  358. #ifdef DBCS_DIVIDE
  359. if ( i == JIS_len - 1 ){
  360. dStatus.nCodeSet = CODE_JPN_JIS;
  361. dStatus.cSavedByte = *pJIS;
  362. break;
  363. }
  364. #endif
  365. if ( *pJIS == '*' ) {
  366. if ( re >= SJIS_len ) { // Buffer Over flow?
  367. return ( -1 );
  368. }
  369. *pSJIS = *(pJIS+1) | 0x80;
  370. pJIS+=2;
  371. i+=2;
  372. re ++;
  373. pSJIS++;
  374. continue;
  375. }
  376. else {
  377. // Kanji Code
  378. if ( re + 1 >= SJIS_len ) { // Buffer Over flow?
  379. return ( -1 );
  380. }
  381. // Start One Character Convert from JIS to Shift JIS
  382. *pSJIS = ((*pJIS - 0x21) >> 1) +0x81;
  383. if ( *pSJIS > 0x9f ) {
  384. (*pSJIS) += 0x40;
  385. }
  386. *(pSJIS+1) = (*(pJIS+1)) + ( (*pJIS) & 1 ? 0x1f : 0x7d );
  387. if ( *(pSJIS+1) >= 0x7f ) {
  388. (*(pSJIS+1)) ++;
  389. }
  390. pJIS+=2;
  391. i+=2;
  392. re +=2;
  393. pSJIS+=2;
  394. continue;
  395. }
  396. }
  397. }
  398. }
  399. return ( re );
  400. }