jis.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. following astonishing goo courtesy of kogure.
  3. */
  4. /*
  5. * MicroSoft Kanji Encoding (SJIS) Transformation
  6. */
  7. /*
  8. * void
  9. * J2S(unsigned char *_h, unsigned char *_l)
  10. * JIS X 208 to MS kanji transformation.
  11. *
  12. * Calling/Exit State:
  13. * _h and _l should be in their valid range.
  14. * No return value.
  15. */
  16. #define J2S(_h, _l) { \
  17. /* lower: 21-7e >> 40-9d,9e-fb >> 40-7e,(skip 7f),80-fc */ \
  18. if (((_l) += (((_h)-- % 2) ? 0x1f : 0x7d)) > 0x7e) (_l)++; \
  19. /* upper: 21-7e >> 81-af >> 81-9f,(skip a0-df),e0-ef */ \
  20. if (((_h) = ((_h) / 2 + 0x71)) > 0x9f) (_h) += 0x40; \
  21. }
  22. /*
  23. * void
  24. * S2J(unsigned char *_h, unsigned char *_l)
  25. * MS kanji to JIS X 208 transformation.
  26. *
  27. * Calling/Exit State:
  28. * _h and _l should be in valid range.
  29. * No return value.
  30. */
  31. #define S2J(_h, _l) { \
  32. /* lower: 40-7e,80-fc >> 21-5f,61-dd >> 21-7e,7f-dc */ \
  33. if (((_l) -= 0x1f) > 0x60) (_l)--; \
  34. /* upper: 81-9f,e0-ef >> 00-1e,5f-6e >> 00-2e >> 21-7d */ \
  35. if (((_h) -= 0x81) > 0x5e) (_h) -= 0x40; (_h) *= 2, (_h) += 0x21; \
  36. /* upper: ,21-7d >> ,22-7e ; lower: ,7f-dc >> ,21-7e */ \
  37. if ((_l) > 0x7e) (_h)++, (_l) -= 0x5e; \
  38. }
  39. /*
  40. * int
  41. * ISJKANA(const unsigned char *_b)
  42. * Tests given byte is in the range of JIS X 0201 katakana.
  43. *
  44. * Calling/Exit State:
  45. * Returns 1 if it is, or 0 otherwise.
  46. */
  47. #define ISJKANA(_b) (0xa0 <= (_b) && (_b) < 0xe0)
  48. /*
  49. * int
  50. * CANS2JH(const unsigned char *_h)
  51. * Tests given byte is in the range of valid first byte of MS
  52. * kanji code; either acts as a subroutine of CANS2J() macro
  53. * or can be used to parse MS kanji encoded strings.
  54. *
  55. * Calling/Exit State:
  56. * Returns 1 if it is, or 0 otherwise.
  57. */
  58. #define CANS2JH(_h) ((0x81 <= (_h) && (_h) < 0xf0) && !ISJKANA(_h))
  59. /*
  60. * int
  61. * CANS2JL(const unsigned char *_l)
  62. * Tests given byte is in the range of valid second byte of MS
  63. * kanji code; acts as a subroutine of CANS2J() macro.
  64. *
  65. * Calling/Exit State:
  66. * Returns 1 if it is, or 0 otherwise.
  67. */
  68. #define CANS2JL(_l) (0x40 <= (_l) && (_l) < 0xfd && (_l) != 0x7f)
  69. /*
  70. * int
  71. * CANS2J(const unsigned char *_h, const unsinged char *_l)
  72. * Tests given bytes form a MS kanji code point which can be
  73. * transformed to a valid JIS X 208 code point.
  74. *
  75. * Calling/Exit State:
  76. * Returns 1 if they are, or 0 otherwise.
  77. */
  78. #define CANS2J(_h, _l) (CANS2JH(_h) && CANS2JL(_l))
  79. /*
  80. * int
  81. * CANJ2SB(const unsigned char *_b)
  82. * Tests given bytes is in the range of valid 94 graphic
  83. * character set; acts as a subroutine of CANJ2S() macro.
  84. *
  85. * Calling/Exit State:
  86. * Returns 1 if it is, or 0 otherwise.
  87. */
  88. #define CANJ2SB(_b) (0x21 <= (_b) && (_b) < 0x7f)
  89. /*
  90. * int
  91. * CANJ2S(const unsigned char *_h, const unsigned char *_l)
  92. * Tests given bytes form valid JIS X 208 code points
  93. * (which can be transformed to MS kanji).
  94. *
  95. * Calling/Exit State:
  96. * Returns 1 if they are, or 0 otherwise.
  97. */
  98. #define CANJ2S(_h, _l) (CANJ2SB(_h) && CANJ2SB(_l))