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.

253 lines
7.8 KiB

  1. // Copyright (c) Microsoft Corpration
  2. //
  3. // File Name: sjis2jis.c
  4. // Owner: Tetsuhide Akaishi
  5. // Revision: 1.00 02/21/'93 Tetsuhide Akaishi
  6. //
  7. #include "win32.h"
  8. #include "fechrcnv.h"
  9. void ShiftJISChar_to_JISChar ( UCHAR *pShiftJIS, UCHAR *pJIS )
  10. // The ShiftJISChar_to_JISChar function convert one character string
  11. // as Shift JIS code to a JIS code string.
  12. //
  13. // UCHAR *pShiftJIS Points to the character string to be converted.
  14. //
  15. // UCHAR *pJIS Points to a buffer that receives the convert string
  16. // from Shift JIS Code to JIS.
  17. //
  18. // Return Value
  19. // None
  20. {
  21. USHORT hi_code, low_code;
  22. hi_code = (*pShiftJIS);
  23. low_code = *(pShiftJIS+1);
  24. hi_code -= (hi_code > 0x9f ? 0xb1 : 0x71);
  25. hi_code = hi_code * 2 + 1;
  26. if ( low_code > 0x9e ) {
  27. low_code -= 0x7e;
  28. hi_code ++;
  29. }
  30. else {
  31. if ( low_code > 0x7e ) {
  32. low_code --;
  33. }
  34. low_code -= 0x1f;
  35. }
  36. *(pJIS) = (UCHAR)hi_code;
  37. *(pJIS+1) = (UCHAR)low_code;
  38. return;
  39. }
  40. int ShiftJIS_to_JIS ( UCHAR *pShiftJIS, int ShiftJIS_len,
  41. UCHAR *pJIS, int JIS_len )
  42. // The ShiftJIS_to_JIS function convert a character string as Shift JIS code
  43. // to a JIS code string.
  44. //
  45. // UCHAR *pShiftJIS Points to the character string to be converted.
  46. //
  47. // int ShiftJIS_len Specifies the size in bytes of the string pointed
  48. // to by the pShiftJIS parameter. If this value is -1,
  49. // the string is assumed to be NULL terminated and the
  50. // length is calculated automatically.
  51. //
  52. // UCHAR *pJIS Points to a buffer that receives the convert string
  53. // from Shift JIS Code to JIS.
  54. //
  55. // int JIS_len Specifies the size, in JIS characters of the buffer
  56. // pointed to by the pJIS parameter. If the value is zero,
  57. // the function returns the number of JIS characters
  58. // required for the buffer, and makes no use of the pJIS
  59. // buffer.
  60. //
  61. // Return Value
  62. // If the function succeeds, and JIS_len is nonzero, the return value is the
  63. // number of JIS characters written to the buffer pointed to by pJIS.
  64. //
  65. // If the function succeeds, and JIS_len is zero, the return value is the
  66. // required size, in JIS characters, for a buffer that can receive the
  67. // converted string.
  68. //
  69. // If the function fails, the return value is -1. The error mean pJIS buffer
  70. // is small for setting converted strings.
  71. //
  72. {
  73. BOOL kanji_in = FALSE; // Kanji Mode
  74. BOOL kana_in = FALSE; // Kana Mode
  75. int re; // Convert Lenght
  76. int i; // Loop Counter
  77. if ( ShiftJIS_len == -1 ) {
  78. // If length is not set, last character of the strings is NULL.
  79. ShiftJIS_len = strlen ( pShiftJIS ) + 1;
  80. }
  81. i = 0;
  82. re = 0;
  83. if ( JIS_len == 0 ) {
  84. // Only retrun the required size
  85. while ( i < ShiftJIS_len ) {
  86. if ( SJISISKANJI(*pShiftJIS) ) { // Is this charcter 2 bytes Kanji?
  87. if ( kana_in ) { // Kana Mode?
  88. re ++;
  89. kana_in = FALSE; // Reset Kana Mode;
  90. }
  91. if ( kanji_in == FALSE ) { // Kanji Mode?
  92. re += KANJI_IN_LEN;
  93. kanji_in = TRUE; // Set Kanji Mode
  94. }
  95. i+=2;
  96. re += 2;
  97. pShiftJIS+=2;
  98. }
  99. else if ( SJISISKANA(*pShiftJIS) ) {
  100. if ( kanji_in ) {
  101. re += KANJI_OUT_LEN;
  102. kanji_in = FALSE;
  103. }
  104. if ( kana_in == FALSE ) {
  105. re ++;
  106. kana_in = TRUE;
  107. }
  108. i++;
  109. re++;
  110. pShiftJIS++;
  111. }
  112. else {
  113. if ( kana_in ) {
  114. re ++;
  115. kana_in = FALSE;
  116. }
  117. if ( kanji_in ) {
  118. re += KANJI_OUT_LEN;
  119. kanji_in = FALSE;
  120. }
  121. i++;
  122. re++;
  123. pShiftJIS++;
  124. }
  125. }
  126. if ( kana_in ) {
  127. re ++;
  128. kana_in = FALSE;
  129. }
  130. if ( kanji_in ) {
  131. re += KANJI_OUT_LEN;
  132. kanji_in = FALSE;
  133. }
  134. return ( re );
  135. }
  136. while ( i < ShiftJIS_len ) {
  137. if ( SJISISKANJI(*pShiftJIS) ) { // Is this charcter 2 bytes Kanji?
  138. if ( kana_in ) { // Kana Mode?
  139. if ( re >= JIS_len ) { // Buffer Over?
  140. return ( -1 );
  141. }
  142. (*pJIS++) = SI; // Set Kana Out Charcter
  143. re ++;
  144. kana_in = FALSE; // Reset Kana Mode;
  145. }
  146. if ( kanji_in == FALSE ) { // Kanji Mode?
  147. if ( re + KANJI_IN_LEN > JIS_len ) { // Buffer Over?
  148. return ( -1 );
  149. }
  150. (*pJIS++) = ESC; // Set Kanji In Charcter
  151. (*pJIS++) = KANJI_IN_1ST_CHAR;
  152. (*pJIS++) = KANJI_IN_2ND_CHAR1;
  153. re += KANJI_IN_LEN;
  154. kanji_in = TRUE; // Set Kanji Mode
  155. }
  156. if ( re + 2 > JIS_len ) { // Buffer Over?
  157. return ( -1 );
  158. }
  159. ShiftJISChar_to_JISChar ( pShiftJIS, pJIS );
  160. i+=2;
  161. re += 2;
  162. pShiftJIS+=2;
  163. pJIS += 2;
  164. }
  165. else if ( SJISISKANA(*pShiftJIS) ) {
  166. if ( kanji_in ) {
  167. if ( re + KANJI_OUT_LEN > JIS_len ) { // Buffer Over?
  168. return ( -1 );
  169. }
  170. // Set Kanji Out Charcter
  171. (*pJIS++) = ESC;
  172. (*pJIS++) = KANJI_OUT_1ST_CHAR;
  173. (*pJIS++) = KANJI_OUT_2ND_CHAR1;
  174. re += KANJI_OUT_LEN;
  175. kanji_in = FALSE;
  176. }
  177. if ( kana_in == FALSE ) {
  178. if ( re >= JIS_len ) { // Buffer Over?
  179. return ( -1 );
  180. }
  181. (*pJIS++) = SO; // Set Kana In Charcter
  182. re ++;
  183. kana_in = TRUE;
  184. }
  185. if ( re >= JIS_len ) { // Buffer Over?
  186. return ( -1 );
  187. }
  188. (*pJIS++) = (*pShiftJIS++) & 0x7f;
  189. i++;
  190. re++;
  191. }
  192. else {
  193. if ( kana_in ) {
  194. if ( re >= JIS_len ) { // Buffer Over?
  195. return ( -1 );
  196. }
  197. (*pJIS++) = SI; // Set Kana Out Charcter
  198. re ++;
  199. kana_in = FALSE;
  200. }
  201. if ( kanji_in ) {
  202. if ( re + KANJI_OUT_LEN > JIS_len ) { // Buffer Over?
  203. return ( -1 );
  204. }
  205. // Set Kanji Out Charcter
  206. (*pJIS++) = ESC;
  207. (*pJIS++) = KANJI_OUT_1ST_CHAR;
  208. (*pJIS++) = KANJI_OUT_2ND_CHAR1;
  209. re += KANJI_OUT_LEN;
  210. kanji_in = FALSE;
  211. }
  212. if ( re >= JIS_len ) { // Buffer Over?
  213. return ( -1 );
  214. }
  215. (*pJIS++) = (*pShiftJIS++);
  216. i++;
  217. re++;
  218. }
  219. }
  220. if ( kana_in ) {
  221. if ( re >= JIS_len ) { // Buffer Over?
  222. return ( -1 );
  223. }
  224. (*pJIS++) = SI; // Set Kana Out Charcter
  225. re ++;
  226. kana_in = FALSE;
  227. }
  228. if ( kanji_in ) {
  229. if ( re + KANJI_OUT_LEN > JIS_len ) { // Buffer Over?
  230. return ( -1 );
  231. }
  232. // Set Kanji Out Charcter
  233. (*pJIS++) = ESC;
  234. (*pJIS++) = KANJI_OUT_1ST_CHAR;
  235. (*pJIS++) = KANJI_OUT_2ND_CHAR1;
  236. re += KANJI_OUT_LEN;
  237. kanji_in = FALSE;
  238. }
  239. return ( re );
  240. }