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.

285 lines
8.6 KiB

  1. // Copyright (c) Microsoft Corpration
  2. //
  3. // File Name: sjis2euc.c
  4. // Owner: Tetsuhide Akaishi
  5. // Revision: 1.00 02/21/'93 Tetsuhide Akaishi
  6. //
  7. #include "win32.h"
  8. #include "fechrcnv.h"
  9. //Shift JIS(SJC) to EUC Conversion algorithm
  10. //Two byte KANJI
  11. // - 1st byte of Shift JIS charcter
  12. // (XX = Hex value of 1st byte of Shift JIS Charcter)
  13. // Range 0x81 - 0x9f
  14. // (2nd byte of Shift JIS is less than or equals to 0x9e)
  15. // (EUC odd)0xa1-0xdd
  16. // EUC 1st byte = (XX-0x81)*2 + 0xa1
  17. // (2nd byte of Shift JIS is greater than or equals to 0x9f)
  18. // (EUC even)0xa2-0xde
  19. // EUC 1st byte = (XX-0x81)*2 + 0xa2
  20. //
  21. // Range 0xe0 - 0xef
  22. // (2nd byte of Shift JIS is less than or equals to 0x9e)
  23. // (EUC odd)0xdf-0xfd
  24. // EUC 1st byte = (XX-0xe0)*2 + 0xdf
  25. // (2nd byte of Shift JIS is greater than or equals to 0x9f)
  26. // (EUC even)0xa2-0xde
  27. // EUC 1st byte = (XX-0xe0)*2 + 0xe0
  28. //
  29. // - 2nd byte of Shift JIS charcter
  30. // (YY = Hex value of 2nd byte of Shift JIS Charcter)
  31. // Range 0x40 - 0x7e (EUC)0xa1 - 0xdf
  32. // EUC 2nd byte = (YY+0x61)
  33. // Range 0x80 - 0x9e (EUC)0xe0 - 0xfe
  34. // EUC 2nd byte = (YY+0x60)
  35. // Range 0x9f - 0xfc (EUC)0xa1 - 0xfe
  36. // EUC 2nd byte = (YY+0x02)
  37. //
  38. // Range 0x0a1 - 0x0df(Hankaku KATAKANA)
  39. // 1st byte of EUC charcter = 0x08e
  40. // 2nd byte if EUC charcter = C6220 Hankaku KATAKANA code
  41. // (same byte value as Shift JIS Hankaku KATAKANA) (0x0a1 - 0x0df)
  42. //@
  43. //
  44. // Syntax:
  45. int ShiftJISChar_to_EUCChar ( UCHAR *pShiftJIS, UCHAR *pEUC )
  46. // The ShiftJISChar_to_EUCChar function convert one Shift JIS character
  47. // to a JIS code string.
  48. //
  49. // UCHAR *pShiftJIS Points to the character string to be converted.
  50. //
  51. // UCHAR *pEUC Points to a buffer that receives the convert string
  52. // from Shift JIS Code to EUC Code.
  53. //
  54. // Return Value
  55. // The number of bytes to copy.
  56. //
  57. {
  58. if ( *pShiftJIS >= 0x081 && *pShiftJIS <= 0x09f ) {
  59. if ( *(pShiftJIS+1) <= 0x09e ) {
  60. *pEUC = ((*pShiftJIS)-0x081)*2+0x0a1;
  61. }
  62. else {
  63. *pEUC = ((*pShiftJIS)-0x081)*2+0x0a2;
  64. }
  65. goto SECOND_BYTE;
  66. }
  67. if ( *pShiftJIS >= 0x0e0 && *pShiftJIS <= 0x0ef ) {
  68. if ( *(pShiftJIS+1) <= 0x09e ) {
  69. *pEUC = ((*pShiftJIS)-0x0e0)*2+0x0df;
  70. }
  71. else {
  72. *pEUC = ((*pShiftJIS)-0x0e0)*2+0x0e0;
  73. }
  74. goto SECOND_BYTE;
  75. }
  76. // Is the charcter Hankaku KATAKANA ?
  77. if ( *pShiftJIS >= 0x0a1 && *pShiftJIS <= 0x0df ) {
  78. *pEUC = 0x08e;
  79. *(pEUC+1) = *pShiftJIS;
  80. return( 2 );
  81. }
  82. // Is the charcter IBM Extended Charcter?
  83. if ( *pShiftJIS >= 0x0fa && *pShiftJIS <= 0x0fc ) {
  84. // There are no IBM Extended Charcte in EUC charset.
  85. *pEUC = ' ';
  86. *(pEUC+1) = ' ';
  87. return( 2 );
  88. }
  89. // Is the charcter ASCII charcter ?
  90. *pEUC = *pShiftJIS;
  91. return ( 1 );
  92. SECOND_BYTE:
  93. if ( *(pShiftJIS+1) >= 0x040 && *(pShiftJIS+1) <= 0x07e ) {
  94. *(pEUC+1) = *(pShiftJIS + 1) + 0x061;
  95. }
  96. else {
  97. if ( *(pShiftJIS+1) >= 0x080 && *(pShiftJIS+1) <= 0x09e ) {
  98. *(pEUC+1) = *(pShiftJIS + 1) + 0x060;
  99. }
  100. else {
  101. *(pEUC+1) = *(pShiftJIS + 1) + 0x002;
  102. }
  103. }
  104. return ( 2 );
  105. }
  106. int ShiftJIS_to_EUC ( UCHAR *pShiftJIS, int ShiftJIS_len,
  107. UCHAR *pEUC, int EUC_len )
  108. // The ShiftJIS_to_JIS function convert a character string as Shift JIS code
  109. // to a EUC code string.
  110. //
  111. // UCHAR *pShiftJIS Points to the character string to be converted.
  112. //
  113. // int ShiftJIS_len Specifies the size in bytes of the string pointed
  114. // to by the pShiftJIS parameter. If this value is -1,
  115. // the string is assumed to be NULL terminated and the
  116. // length is calculated automatically.
  117. //
  118. // UCHAR *pEUC Points to a buffer that receives the convert string
  119. // from Shift JIS Code to EUC Code.
  120. //
  121. // int EUC_len Specifies the size, in EUC characters of the buffer
  122. // pointed to by the pEUC parameter. If the value is zero,
  123. // the function returns the number of EUC characters
  124. // required for the buffer, and makes no use of the pEUC
  125. // buffer.
  126. //
  127. // Return Value
  128. // If the function succeeds, and EUC_len is nonzero, the return value is the
  129. // number of EUC characters written to the buffer pointed to by pEUC.
  130. //
  131. // If the function succeeds, and EUC_len is zero, the return value is the
  132. // required size, in EUC characters, for a buffer that can receive the
  133. // converted string.
  134. //
  135. // If the function fails, the return value is -1. The error mean pEUC buffer
  136. // is small for setting converted strings.
  137. //
  138. {
  139. int re; // Convert Lenght
  140. int i; // Loop Counter
  141. if ( ShiftJIS_len == -1 ) {
  142. // If length is not set, last character of the strings is NULL.
  143. ShiftJIS_len = strlen ( pShiftJIS ) + 1;
  144. }
  145. i = 0;
  146. re = 0;
  147. if ( EUC_len == 0 ) {
  148. // Only retrun the required size
  149. while ( i < ShiftJIS_len ) {
  150. if ( SJISISKANJI(*pShiftJIS) ) {
  151. pShiftJIS+=2;
  152. i+=2;
  153. re+=2;
  154. continue;
  155. }
  156. if ( SJISISKANA(*pShiftJIS) ) {
  157. pShiftJIS++;
  158. i++;
  159. re+=2;
  160. continue;
  161. }
  162. pShiftJIS++;
  163. i++;
  164. re++;
  165. }
  166. return ( re );
  167. }
  168. while ( i < ShiftJIS_len ) {
  169. if ( *pShiftJIS >= 0x081 && *pShiftJIS <= 0x09f ) {
  170. if ( re + 1 >= EUC_len ) { // Buffer Over?
  171. return ( -1 );
  172. }
  173. if ( *(pShiftJIS+1) <= 0x09e ) {
  174. *pEUC = ((*pShiftJIS)-0x081)*2+0x0a1;
  175. }
  176. else {
  177. *pEUC = ((*pShiftJIS)-0x081)*2+0x0a2;
  178. }
  179. pShiftJIS++; // Next Char
  180. pEUC++;
  181. if ( (*pShiftJIS) >= 0x040 && (*pShiftJIS) <= 0x07e ) {
  182. (*pEUC) = (*pShiftJIS) + 0x061;
  183. }
  184. else {
  185. if ( (*pShiftJIS) >= 0x080 && (*pShiftJIS) <= 0x09e ) {
  186. (*pEUC) = (*pShiftJIS) + 0x060;
  187. }
  188. else {
  189. (*pEUC) = (*pShiftJIS) + 0x002;
  190. }
  191. }
  192. re+=2;
  193. i+=2;
  194. pShiftJIS++;
  195. pEUC++;
  196. continue;
  197. }
  198. if ( *pShiftJIS >= 0x0e0 && *pShiftJIS <= 0x0ef ) {
  199. if ( re + 1 >= EUC_len ) { // Buffer Over?
  200. return ( -1 );
  201. }
  202. if ( *(pShiftJIS+1) <= 0x09e ) {
  203. *pEUC = ((*pShiftJIS)-0x0e0)*2+0x0df;
  204. }
  205. else {
  206. *pEUC = ((*pShiftJIS)-0x0e0)*2+0x0e0;
  207. }
  208. pShiftJIS++; // Next Char
  209. pEUC++;
  210. if ( (*pShiftJIS) >= 0x040 && (*pShiftJIS) <= 0x07e ) {
  211. (*pEUC) = (*pShiftJIS) + 0x061;
  212. }
  213. else {
  214. if ( (*pShiftJIS) >= 0x080 && (*pShiftJIS) <= 0x09e ) {
  215. (*pEUC) = (*pShiftJIS) + 0x060;
  216. }
  217. else {
  218. (*pEUC) = (*pShiftJIS) + 0x002;
  219. }
  220. }
  221. re+=2;
  222. i+=2;
  223. pShiftJIS++;
  224. pEUC++;
  225. continue;
  226. }
  227. // Is the charcter Hankaku KATAKANA ?
  228. if ( *pShiftJIS >= 0x0a1 && *pShiftJIS <= 0x0df ) {
  229. if ( re + 1 >= EUC_len ) { // Buffer Over?
  230. return ( -1 );
  231. }
  232. *pEUC = 0x08e;
  233. pEUC++;
  234. (*pEUC) = *pShiftJIS;
  235. re+=2;
  236. i++;
  237. pShiftJIS++;
  238. pEUC++;
  239. continue;
  240. }
  241. // Is the charcter IBM Extended Charcter?
  242. if ( *pShiftJIS >= 0x0fa && *pShiftJIS <= 0x0fc ) {
  243. if ( re + 1 >= EUC_len ) { // Buffer Over?
  244. return ( -1 );
  245. }
  246. // There are no IBM Extended Charcte in EUC charset.
  247. *pEUC = ' ';
  248. pEUC++;
  249. (*pEUC) = ' ';
  250. re+=2;
  251. i+=2;
  252. pShiftJIS+=2;
  253. pEUC++;
  254. continue;
  255. }
  256. // Is the charcter ASCII charcter ?
  257. if ( re >= EUC_len ) { // Buffer Over?
  258. return ( -1 );
  259. }
  260. *pEUC = *pShiftJIS;
  261. re++;
  262. i++;
  263. pShiftJIS++;
  264. pEUC++;
  265. continue;
  266. }
  267. return ( re );
  268. }