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.

140 lines
3.7 KiB

  1. /***
  2. *tojisjms.c: Converts JIS to JMS code, and JMS to JIS code.
  3. *
  4. * Copyright (c) 1988-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Convert JIS code into Microsoft Kanji code, and vice versa.
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 05-28-93 KRS Ikura #27: Validate output is legal JIS.
  12. * 08-20-93 CFW Change short params to int for 32-bit tree.
  13. * 09-24-93 CFW Removed #ifdef _KANJI
  14. * 09-29-93 CFW Return c unchanged if not Kanji code page.
  15. * 10-06-93 GJF Replaced _CRTAPI1 with __cdecl.
  16. * 04-21-98 GJF Implemented multithread support based on threadmbcinfo
  17. * structs
  18. *
  19. *******************************************************************************/
  20. #ifdef _MBCS
  21. #include <cruntime.h>
  22. #include <mbdata.h>
  23. #include <mbstring.h>
  24. #include <mbctype.h>
  25. #include <mtdll.h>
  26. /***
  27. *unsigned int _mbcjistojms(c) - Converts JIS code to Microsoft Kanji Code.
  28. *
  29. *Purpose:
  30. * Convert JIS code to Microsoft Kanji code.
  31. *
  32. *Entry:
  33. * unsigned int c - JIS code to be converted. First byte is the upper
  34. * 8 bits, and second is the lower 8 bits.
  35. *
  36. *Exit:
  37. * Returns related Microsoft Kanji Code. First byte is the upper 8 bits
  38. * and second byte is the lower 8 bits.
  39. *
  40. *Exceptions:
  41. * If c is out of range, _mbcjistojms returns zero.
  42. *
  43. *******************************************************************************/
  44. unsigned int __cdecl _mbcjistojms(
  45. unsigned int c
  46. )
  47. {
  48. unsigned int h, l;
  49. if (__mbcodepage != _KANJI_CP)
  50. return (c);
  51. h = (c >> 8) & 0xff;
  52. l = c & 0xff;
  53. if (h < 0x21 || h > 0x7e || l < 0x21 || l > 0x7e)
  54. return 0;
  55. if (h & 0x01) { /* first byte is odd */
  56. if (l <= 0x5f)
  57. l += 0x1f;
  58. else
  59. l += 0x20;
  60. }
  61. else
  62. l += 0x7e;
  63. h = ((h - 0x21) >> 1) + 0x81;
  64. if (h > 0x9f)
  65. h += 0x40;
  66. return (h << 8) | l;
  67. }
  68. /***
  69. *unsigned int _mbcjmstojis(c) - Converts Microsoft Kanji code into JIS code.
  70. *
  71. *Purpose:
  72. * To convert Microsoft Kanji code into JIS code.
  73. *
  74. *Entry:
  75. * unsigned int c - Microsoft Kanji code to be converted. First byte is
  76. * the upper 8 bits, and the second is the lower 8 bits.
  77. *
  78. *Exit:
  79. * Returns related JIS Code. First byte is the upper 8 bits and the second
  80. * byte is the lower 8 bits. If c is out of range, return zero.
  81. *
  82. *Exceptions:
  83. *
  84. *******************************************************************************/
  85. unsigned int __cdecl _mbcjmstojis(
  86. unsigned int c
  87. )
  88. {
  89. unsigned int h, l;
  90. #ifdef _MT
  91. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  92. if ( ptmbci != __ptmbcinfo )
  93. ptmbci = __updatetmbcinfo();
  94. if ( ptmbci->mbcodepage != _KANJI_CP )
  95. #else
  96. if ( __mbcodepage != _KANJI_CP )
  97. #endif
  98. return (c);
  99. h = (c >> 8) & 0xff;
  100. l = c & 0xff;
  101. /* make sure input is valid shift-JIS */
  102. #ifdef _MT
  103. if ( (!(__ismbblead_mt(ptmbci, h))) || (!(__ismbbtrail_mt(ptmbci, l))) )
  104. #else
  105. if ( (!(_ismbblead(h))) || (!(_ismbbtrail(l))) )
  106. #endif
  107. return 0;
  108. h -= (h >= 0xa0) ? 0xc1 : 0x81;
  109. if(l >= 0x9f) {
  110. c = (h << 9) + 0x2200;
  111. c |= l - 0x7e;
  112. } else {
  113. c = (h << 9) + 0x2100;
  114. c |= l - ((l <= 0x7e) ? 0x1f : 0x20);
  115. }
  116. /* not all shift-JIS maps to JIS, so make sure output is valid */
  117. if ( (c>0x7E7E) || (c<0x2121) || ((c&0xFF)>0x7E) || ((c&0xFF)<0x21) )
  118. return 0;
  119. return c;
  120. }
  121. #endif /* _MBCS */