/Modules/cjkcodecs/_codecs_hk.c

http://unladen-swallow.googlecode.com/ · C · 183 lines · 137 code · 30 blank · 16 comment · 43 complexity · fca97f38784b5c249b51c882ecb0ea58 MD5 · raw file

  1. /*
  2. * _codecs_hk.c: Codecs collection for encodings from Hong Kong
  3. *
  4. * Written by Hye-Shik Chang <perky@FreeBSD.org>
  5. */
  6. #define USING_IMPORTED_MAPS
  7. #include "cjkcodecs.h"
  8. #include "mappings_hk.h"
  9. /*
  10. * BIG5HKSCS codec
  11. */
  12. static const encode_map *big5_encmap = NULL;
  13. static const decode_map *big5_decmap = NULL;
  14. CODEC_INIT(big5hkscs)
  15. {
  16. static int initialized = 0;
  17. if (!initialized && IMPORT_MAP(tw, big5, &big5_encmap, &big5_decmap))
  18. return -1;
  19. initialized = 1;
  20. return 0;
  21. }
  22. /*
  23. * There are four possible pair unicode -> big5hkscs maps as in HKSCS 2004:
  24. * U+00CA U+0304 -> 8862 (U+00CA alone is mapped to 8866)
  25. * U+00CA U+030C -> 8864
  26. * U+00EA U+0304 -> 88a3 (U+00EA alone is mapped to 88a7)
  27. * U+00EA U+030C -> 88a5
  28. * These are handled by not mapping tables but a hand-written code.
  29. */
  30. static const DBCHAR big5hkscs_pairenc_table[4] = {0x8862, 0x8864, 0x88a3, 0x88a5};
  31. ENCODER(big5hkscs)
  32. {
  33. while (inleft > 0) {
  34. ucs4_t c = **inbuf;
  35. DBCHAR code;
  36. Py_ssize_t insize;
  37. if (c < 0x80) {
  38. REQUIRE_OUTBUF(1)
  39. **outbuf = (unsigned char)c;
  40. NEXT(1, 1)
  41. continue;
  42. }
  43. DECODE_SURROGATE(c)
  44. insize = GET_INSIZE(c);
  45. REQUIRE_OUTBUF(2)
  46. if (c < 0x10000) {
  47. TRYMAP_ENC(big5hkscs_bmp, code, c) {
  48. if (code == MULTIC) {
  49. if (inleft >= 2 &&
  50. ((c & 0xffdf) == 0x00ca) &&
  51. (((*inbuf)[1] & 0xfff7) == 0x0304)) {
  52. code = big5hkscs_pairenc_table[
  53. ((c >> 4) |
  54. ((*inbuf)[1] >> 3)) & 3];
  55. insize = 2;
  56. }
  57. else if (inleft < 2 &&
  58. !(flags & MBENC_FLUSH))
  59. return MBERR_TOOFEW;
  60. else {
  61. if (c == 0xca)
  62. code = 0x8866;
  63. else /* c == 0xea */
  64. code = 0x88a7;
  65. }
  66. }
  67. }
  68. else TRYMAP_ENC(big5, code, c);
  69. else return 1;
  70. }
  71. else if (c < 0x20000)
  72. return insize;
  73. else if (c < 0x30000) {
  74. TRYMAP_ENC(big5hkscs_nonbmp, code, c & 0xffff);
  75. else return insize;
  76. }
  77. else
  78. return insize;
  79. OUT1(code >> 8)
  80. OUT2(code & 0xFF)
  81. NEXT(insize, 2)
  82. }
  83. return 0;
  84. }
  85. #define BH2S(c1, c2) (((c1) - 0x87) * (0xfe - 0x40 + 1) + ((c2) - 0x40))
  86. DECODER(big5hkscs)
  87. {
  88. while (inleft > 0) {
  89. unsigned char c = IN1;
  90. ucs4_t decoded;
  91. REQUIRE_OUTBUF(1)
  92. if (c < 0x80) {
  93. OUT1(c)
  94. NEXT(1, 1)
  95. continue;
  96. }
  97. REQUIRE_INBUF(2)
  98. if (0xc6 <= c && c <= 0xc8 && (c >= 0xc7 || IN2 >= 0xa1))
  99. goto hkscsdec;
  100. TRYMAP_DEC(big5, **outbuf, c, IN2) {
  101. NEXT(2, 1)
  102. }
  103. else
  104. hkscsdec: TRYMAP_DEC(big5hkscs, decoded, c, IN2) {
  105. int s = BH2S(c, IN2);
  106. const unsigned char *hintbase;
  107. assert(0x87 <= c && c <= 0xfe);
  108. assert(0x40 <= IN2 && IN2 <= 0xfe);
  109. if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
  110. hintbase = big5hkscs_phint_0;
  111. s -= BH2S(0x87, 0x40);
  112. }
  113. else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
  114. hintbase = big5hkscs_phint_12130;
  115. s -= BH2S(0xc6, 0xa1);
  116. }
  117. else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
  118. hintbase = big5hkscs_phint_21924;
  119. s -= BH2S(0xf9, 0xd6);
  120. }
  121. else
  122. return MBERR_INTERNAL;
  123. if (hintbase[s >> 3] & (1 << (s & 7))) {
  124. WRITEUCS4(decoded | 0x20000)
  125. NEXT_IN(2)
  126. }
  127. else {
  128. OUT1(decoded)
  129. NEXT(2, 1)
  130. }
  131. }
  132. else {
  133. switch ((c << 8) | IN2) {
  134. case 0x8862: WRITE2(0x00ca, 0x0304); break;
  135. case 0x8864: WRITE2(0x00ca, 0x030c); break;
  136. case 0x88a3: WRITE2(0x00ea, 0x0304); break;
  137. case 0x88a5: WRITE2(0x00ea, 0x030c); break;
  138. default: return 2;
  139. }
  140. NEXT(2, 2) /* all decoded codepoints are pairs, above. */
  141. }
  142. }
  143. return 0;
  144. }
  145. BEGIN_MAPPINGS_LIST
  146. MAPPING_DECONLY(big5hkscs)
  147. MAPPING_ENCONLY(big5hkscs_bmp)
  148. MAPPING_ENCONLY(big5hkscs_nonbmp)
  149. END_MAPPINGS_LIST
  150. BEGIN_CODECS_LIST
  151. CODEC_STATELESS_WINIT(big5hkscs)
  152. END_CODECS_LIST
  153. I_AM_A_MODULE_FOR(hk)