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.

303 lines
8.5 KiB

  1. //
  2. // File Name: euc2sjis.c
  3. // Owner: Tetsuhide Akaishi
  4. // Revision: 1.00 02/21/'93 Tetsuhide Akaishi
  5. //
  6. #include "pch_c.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 (CONV_CONTEXT *pcontext, 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. // we have to run on the given context to be multi-thread safe.
  105. if(!pcontext) return 0;
  106. if ( EUC_len == -1 ) {
  107. // If length is not set, last character of the strings is NULL.
  108. EUC_len = strlen ( pEUC ) + 1;
  109. }
  110. i = 0;
  111. re = 0;
  112. if ( SJIS_len == 0 ) {
  113. // Only retrun the required size
  114. #ifdef DBCS_DIVIDE
  115. if ( pcontext->dStatus0.nCodeSet == CODE_JPN_EUC ){
  116. pEUC++;
  117. i++;
  118. // Is the charcter Hankaku KATAKANA ?
  119. if ( pcontext->dStatus0.cSavedByte == 0x08e )
  120. re++;
  121. else // The character is Kanji.
  122. re+=2;
  123. pcontext->dStatus0.nCodeSet = CODE_UNKNOWN;
  124. pcontext->dStatus0.cSavedByte = '\0';
  125. }
  126. #endif
  127. while ( i < EUC_len ) {
  128. #ifdef DBCS_DIVIDE
  129. if( ( i == EUC_len - 1 ) &&
  130. ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe || *pEUC == 0x08e ) ) {
  131. pcontext->dStatus0.nCodeSet = CODE_JPN_EUC;
  132. pcontext->dStatus0.cSavedByte = *pEUC;
  133. break;
  134. }
  135. #endif
  136. // Is the character Kanji?
  137. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe ) {
  138. pEUC+=2;
  139. i+=2;
  140. re+=2;
  141. continue;
  142. }
  143. // Is the charcter Hankaku KATAKANA ?
  144. if ( *pEUC == 0x08e ) {
  145. pEUC+=2;
  146. i+=2;
  147. re++;
  148. continue;
  149. }
  150. // Is the charcter ASCII charcter ?
  151. pEUC++;
  152. i++;
  153. re++;
  154. }
  155. return ( re );
  156. }
  157. #ifdef DBCS_DIVIDE
  158. if ( pcontext->dStatus.nCodeSet == CODE_JPN_EUC ){
  159. UCHAR cEUC = pcontext->dStatus.cSavedByte;
  160. if ( cEUC >= 0x0a1 && cEUC <= 0x0de ) {
  161. if ( cEUC % 2 == 1 ) { // odd
  162. *pSJIS = (cEUC - 0x0a1) / 2 + 0x081;
  163. goto ODD_SECOND_BYTE2;
  164. }
  165. else { // even
  166. *pSJIS = (cEUC - 0x0a2) / 2 + 0x081;
  167. goto EVEN_SECOND_BYTE2;
  168. }
  169. }
  170. if ( cEUC >= 0x0df && cEUC <= 0x0fe ) {
  171. if ( cEUC % 2 == 1 ) { // odd
  172. *pSJIS = (cEUC - 0x0df) / 2 + 0x0e0;
  173. goto ODD_SECOND_BYTE2;
  174. }
  175. else { // even
  176. *pSJIS = (cEUC - 0x0e0) / 2 + 0x0e0;
  177. goto EVEN_SECOND_BYTE2;
  178. }
  179. }
  180. // Is the charcter Hankaku KATAKANA ?
  181. if ( cEUC == 0x08e ) {
  182. *pSJIS++ = *pEUC++;
  183. i++;
  184. re++;
  185. goto END;
  186. }
  187. ODD_SECOND_BYTE2:
  188. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0df ) {
  189. *(pSJIS+1) = *pEUC - 0x061;
  190. }
  191. else {
  192. *(pSJIS+1) = *pEUC - 0x060;
  193. }
  194. pEUC++;
  195. i++;
  196. re+=2;
  197. pSJIS+=2;
  198. goto END;
  199. EVEN_SECOND_BYTE2:
  200. *(pSJIS+1) = *pEUC - 2;
  201. pEUC++;
  202. i++;
  203. re+=2;
  204. pSJIS+=2;
  205. END:
  206. pcontext->dStatus.nCodeSet = CODE_UNKNOWN;
  207. pcontext->dStatus.cSavedByte = '\0';
  208. }
  209. #endif
  210. while ( i < EUC_len ) {
  211. #ifdef DBCS_DIVIDE
  212. if( ( i == EUC_len - 1 ) &&
  213. ( *pEUC >= 0x0a1 && *pEUC <= 0x0fe || *pEUC == 0x08e ) ) {
  214. pcontext->dStatus.nCodeSet = CODE_JPN_EUC;
  215. pcontext->dStatus.cSavedByte = *pEUC;
  216. break;
  217. }
  218. #endif
  219. if ( re >= SJIS_len ) { // Buffer Over?
  220. return ( -1 );
  221. }
  222. if ( *pEUC >= 0x0a1 && *pEUC <= 0x0de ) {
  223. if ( (*pEUC)%2 == 1 ) { // odd
  224. *pSJIS = ((*pEUC)-0x0a1)/2+0x081;
  225. goto ODD_SECOND_BYTE;
  226. }
  227. else { // even
  228. *pSJIS = ((*pEUC)-0x0a2)/2+0x081;
  229. goto EVEN_SECOND_BYTE;
  230. }
  231. }
  232. if ( *pEUC >= 0x0df && *pEUC <= 0x0fe ) {
  233. if ( (*pEUC)%2 == 1 ) { // odd
  234. *pSJIS = ((*pEUC)-0x0df)/2+0x0e0;
  235. goto ODD_SECOND_BYTE;
  236. }
  237. else { // even
  238. *pSJIS = ((*pEUC)-0x0e0)/2+0x0e0;
  239. goto EVEN_SECOND_BYTE;
  240. }
  241. }
  242. // Is the charcter Hankaku KATAKANA ?
  243. if ( *pEUC == 0x08e ) {
  244. pEUC++;
  245. *pSJIS++ = *pEUC++;
  246. i+=2;
  247. re++;
  248. continue;
  249. }
  250. // Is the charcter ASCII charcter ?
  251. *pSJIS++ = *pEUC++;
  252. i++;
  253. re++;
  254. continue;
  255. ODD_SECOND_BYTE:
  256. if ( re + 1 >= SJIS_len ) { // Buffer Over?
  257. return ( -1 );
  258. }
  259. if ( *(pEUC+1) >= 0x0a1 && *(pEUC+1) <= 0x0df ) {
  260. *(pSJIS+1) = *(pEUC+1) - 0x061;
  261. }
  262. else {
  263. *(pSJIS+1) = *(pEUC+1) - 0x060;
  264. }
  265. pEUC+=2;
  266. i+=2;
  267. re+=2;
  268. pSJIS+=2;
  269. continue;
  270. EVEN_SECOND_BYTE:
  271. if ( re + 1 >= SJIS_len ) { // Buffer Over?
  272. return ( -1 );
  273. }
  274. *(pSJIS+1) = *(pEUC+1)-2;
  275. pEUC+=2;
  276. i+=2;
  277. re+=2;
  278. pSJIS+=2;
  279. continue;
  280. }
  281. return ( re );
  282. }