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.

299 lines
8.0 KiB

  1. //
  2. // File Name: euc2sjis.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 DBCS_STATUS dStatus;
  11. #endif
  12. //@
  13. //
  14. // Syntax:
  15. int EUCChar_to_ShiftJISChar ( UCHAR *pEUC, UCHAR *pShiftJIS )
  16. // The EUCChar_to_ShiftJISChar function convert one character string
  17. // as EUC code to a Shift JIS code string.
  18. //
  19. // UCHAR *pEUC Points to the character string to be converted.
  20. //
  21. // UCHAR *pShiftJIS Points to a buffer that receives the convert string
  22. // from EUC Code to Shift JIS.
  23. //
  24. // Return Value
  25. // The return value is the
  26. // number of Shift JIS characters written to the buffer pointed to by pSJIS.
  27. //
  28. {
  29. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0de ) {
  30. if ( (*pEUC)%2 == 1 ) { // odd
  31. *pShiftJIS = ((*pEUC)-0x0a1)/2+0x081;
  32. goto ODD_SECOND_BYTE;
  33. }
  34. else { // even
  35. *pShiftJIS = ((*pEUC)-0x0a2)/2+0x081;
  36. goto EVEN_SECOND_BYTE;
  37. }
  38. }
  39. if ( *pEUC >= 0x0df && *pEUC <= 0x0fe ) {
  40. if ( (*pEUC)%2 == 1 ) { // odd
  41. *pShiftJIS = ((*pEUC)-0x0df)/2+0x0e0;
  42. goto ODD_SECOND_BYTE;
  43. }
  44. else { // even
  45. *pShiftJIS = ((*pEUC)-0x0e0)/2+0x0e0;
  46. goto EVEN_SECOND_BYTE;
  47. }
  48. }
  49. // Is the charcter Hankaku KATAKANA ?
  50. if ( *pEUC == 0x08e ) {
  51. *pShiftJIS = *(pEUC+1);
  52. return( 1 );
  53. }
  54. // Is the charcter ASCII charcter ?
  55. *pShiftJIS = *pEUC;
  56. return ( 1 );
  57. ODD_SECOND_BYTE:
  58. if ( *(pEUC+1) >= 0x0a1 && *(pEUC+1) <= 0x0df ) {
  59. *(pShiftJIS+1) = *(pEUC+1) - 0x061;
  60. }
  61. else {
  62. *(pShiftJIS+1) = *(pEUC+1) - 0x060;
  63. }
  64. return ( 2 );
  65. EVEN_SECOND_BYTE:
  66. *(pShiftJIS+1) = *(pEUC+1)-2;
  67. return ( 2 );
  68. }
  69. int EUC_to_ShiftJIS ( UCHAR *pEUC, int EUC_len, UCHAR *pSJIS, int SJIS_len )
  70. // The EUC_to_ShiftJIS function convert a character string as EUC code
  71. // to a Shift JIS code string.
  72. //
  73. // UCHAR *pEUC Points to the character string to be converted.
  74. //
  75. // int EUC_len Specifies the size in bytes of the string pointed
  76. // to by the pEUC parameter. If this value is -1,
  77. // the string is assumed to be NULL terminated and the
  78. // length is calculated automatically.
  79. //
  80. // UCHAR *pSJIS Points to a buffer that receives the convert string
  81. // from EUC Code to Shift JIS.
  82. //
  83. // int SJIS_len Specifies the size, in Shift JIS characters of the
  84. // buffer pointed to by the pSJIS parameter.
  85. // If the value is zero,
  86. // the function returns the number of Shift JIS characters
  87. // required for the buffer, and makes no use of the pSJIS
  88. // buffer.
  89. //
  90. // Return Value
  91. // If the function succeeds, and SJIS_len is nonzero, the return value is the
  92. // number of Shift JIS characters written to the buffer pointed to by pSJIS.
  93. //
  94. // If the function succeeds, and SJIS_len is zero, the return value is the
  95. // required size, in Shift JIS characters, for a buffer that can receive the
  96. // converted string.
  97. //
  98. // If the function fails, the return value is -1. The error mean pSJIS buffer
  99. // is small for setting converted strings.
  100. //
  101. {
  102. int re; // Convert Lenght
  103. int i; // Loop Counter
  104. if ( EUC_len == -1 ) {
  105. // If length is not set, last character of the strings is NULL.
  106. EUC_len = strlen ( pEUC ) + 1;
  107. }
  108. i = 0;
  109. re = 0;
  110. if ( SJIS_len == 0 ) {
  111. // Only retrun the required size
  112. #ifdef DBCS_DIVIDE
  113. if ( dStatus0.nCodeSet == CODE_JPN_EUC ){
  114. pEUC++;
  115. i++;
  116. // Is the charcter Hankaku KATAKANA ?
  117. if ( dStatus0.cSavedByte == 0x08e )
  118. re++;
  119. else // The character is Kanji.
  120. re+=2;
  121. dStatus0.nCodeSet = CODE_UNKNOWN;
  122. dStatus0.cSavedByte = '\0';
  123. }
  124. #endif
  125. while ( i < EUC_len ) {
  126. #ifdef DBCS_DIVIDE
  127. if( ( i == EUC_len - 1 ) &&
  128. ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe || *pEUC == 0x08e ) ) {
  129. dStatus0.nCodeSet = CODE_JPN_EUC;
  130. dStatus0.cSavedByte = *pEUC;
  131. break;
  132. }
  133. #endif
  134. // Is the character Kanji?
  135. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe ) {
  136. pEUC+=2;
  137. i+=2;
  138. re+=2;
  139. continue;
  140. }
  141. // Is the charcter Hankaku KATAKANA ?
  142. if ( *pEUC == 0x08e ) {
  143. pEUC+=2;
  144. i+=2;
  145. re++;
  146. continue;
  147. }
  148. // Is the charcter ASCII charcter ?
  149. pEUC++;
  150. i++;
  151. re++;
  152. }
  153. return ( re );
  154. }
  155. #ifdef DBCS_DIVIDE
  156. if ( dStatus.nCodeSet == CODE_JPN_EUC ){
  157. UCHAR cEUC = dStatus.cSavedByte;
  158. if ( cEUC >= 0x0a1 && cEUC <= 0x0de ) {
  159. if ( cEUC % 2 == 1 ) { // odd
  160. *pSJIS = (cEUC - 0x0a1) / 2 + 0x081;
  161. goto ODD_SECOND_BYTE2;
  162. }
  163. else { // even
  164. *pSJIS = (cEUC - 0x0a2) / 2 + 0x081;
  165. goto EVEN_SECOND_BYTE2;
  166. }
  167. }
  168. if ( cEUC >= 0x0df && cEUC <= 0x0fe ) {
  169. if ( cEUC % 2 == 1 ) { // odd
  170. *pSJIS = (cEUC - 0x0df) / 2 + 0x0e0;
  171. goto ODD_SECOND_BYTE2;
  172. }
  173. else { // even
  174. *pSJIS = (cEUC - 0x0e0) / 2 + 0x0e0;
  175. goto EVEN_SECOND_BYTE2;
  176. }
  177. }
  178. // Is the charcter Hankaku KATAKANA ?
  179. if ( cEUC == 0x08e ) {
  180. *pSJIS++ = *pEUC++;
  181. i++;
  182. re++;
  183. goto END;
  184. }
  185. ODD_SECOND_BYTE2:
  186. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0df ) {
  187. *(pSJIS+1) = *pEUC - 0x061;
  188. }
  189. else {
  190. *(pSJIS+1) = *pEUC - 0x060;
  191. }
  192. pEUC++;
  193. i++;
  194. re+=2;
  195. pSJIS+=2;
  196. goto END;
  197. EVEN_SECOND_BYTE2:
  198. *(pSJIS+1) = *pEUC - 2;
  199. pEUC++;
  200. i++;
  201. re+=2;
  202. pSJIS+=2;
  203. END:
  204. dStatus.nCodeSet = CODE_UNKNOWN;
  205. dStatus.cSavedByte = '\0';
  206. }
  207. #endif
  208. while ( i < EUC_len ) {
  209. #ifdef DBCS_DIVIDE
  210. if( ( i == EUC_len - 1 ) &&
  211. ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe || *pEUC == 0x08e ) ) {
  212. dStatus.nCodeSet = CODE_JPN_EUC;
  213. dStatus.cSavedByte = *pEUC;
  214. break;
  215. }
  216. #endif
  217. if ( re >= SJIS_len ) { // Buffer Over?
  218. return ( -1 );
  219. }
  220. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0de ) {
  221. if ( (*pEUC)%2 == 1 ) { // odd
  222. *pSJIS = ((*pEUC)-0x0a1)/2+0x081;
  223. goto ODD_SECOND_BYTE;
  224. }
  225. else { // even
  226. *pSJIS = ((*pEUC)-0x0a2)/2+0x081;
  227. goto EVEN_SECOND_BYTE;
  228. }
  229. }
  230. if ( *pEUC >= 0x0df && *pEUC <= 0x0fe ) {
  231. if ( (*pEUC)%2 == 1 ) { // odd
  232. *pSJIS = ((*pEUC)-0x0df)/2+0x0e0;
  233. goto ODD_SECOND_BYTE;
  234. }
  235. else { // even
  236. *pSJIS = ((*pEUC)-0x0e0)/2+0x0e0;
  237. goto EVEN_SECOND_BYTE;
  238. }
  239. }
  240. // Is the charcter Hankaku KATAKANA ?
  241. if ( *pEUC == 0x08e ) {
  242. pEUC++;
  243. *pSJIS++ = *pEUC++;
  244. i+=2;
  245. re++;
  246. continue;
  247. }
  248. // Is the charcter ASCII charcter ?
  249. *pSJIS++ = *pEUC++;
  250. i++;
  251. re++;
  252. continue;
  253. ODD_SECOND_BYTE:
  254. if ( re + 1 >= SJIS_len ) { // Buffer Over?
  255. return ( -1 );
  256. }
  257. if ( *(pEUC+1) >= 0x0a1 && *(pEUC+1) <= 0x0df ) {
  258. *(pSJIS+1) = *(pEUC+1) - 0x061;
  259. }
  260. else {
  261. *(pSJIS+1) = *(pEUC+1) - 0x060;
  262. }
  263. pEUC+=2;
  264. i+=2;
  265. re+=2;
  266. pSJIS+=2;
  267. continue;
  268. EVEN_SECOND_BYTE:
  269. if ( re + 1 >= SJIS_len ) { // Buffer Over?
  270. return ( -1 );
  271. }
  272. *(pSJIS+1) = *(pEUC+1)-2;
  273. pEUC+=2;
  274. i+=2;
  275. re+=2;
  276. pSJIS+=2;
  277. continue;
  278. }
  279. return ( re );
  280. }