_codecs_tw.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * _codecs_tw.c: Codecs collection for Taiwan's encodings
  3. *
  4. * Written by Hye-Shik Chang <perky@FreeBSD.org>
  5. */
  6. #include "cjkcodecs.h"
  7. #include "mappings_tw.h"
  8. /*
  9. * BIG5 codec
  10. */
  11. ENCODER(big5)
  12. {
  13. while (*inpos < inlen) {
  14. Py_UCS4 c = INCHAR1;
  15. DBCHAR code;
  16. if (c < 0x80) {
  17. REQUIRE_OUTBUF(1);
  18. **outbuf = (unsigned char)c;
  19. NEXT(1, 1);
  20. continue;
  21. }
  22. if (c > 0xFFFF)
  23. return 1;
  24. REQUIRE_OUTBUF(2);
  25. if (TRYMAP_ENC(big5, code, c))
  26. ;
  27. else
  28. return 1;
  29. OUTBYTE1(code >> 8);
  30. OUTBYTE2(code & 0xFF);
  31. NEXT(1, 2);
  32. }
  33. return 0;
  34. }
  35. DECODER(big5)
  36. {
  37. while (inleft > 0) {
  38. unsigned char c = INBYTE1;
  39. Py_UCS4 decoded;
  40. if (c < 0x80) {
  41. OUTCHAR(c);
  42. NEXT_IN(1);
  43. continue;
  44. }
  45. REQUIRE_INBUF(2);
  46. if (TRYMAP_DEC(big5, decoded, c, INBYTE2)) {
  47. OUTCHAR(decoded);
  48. NEXT_IN(2);
  49. }
  50. else return 1;
  51. }
  52. return 0;
  53. }
  54. /*
  55. * CP950 codec
  56. */
  57. ENCODER(cp950)
  58. {
  59. while (*inpos < inlen) {
  60. Py_UCS4 c = INCHAR1;
  61. DBCHAR code;
  62. if (c < 0x80) {
  63. WRITEBYTE1((unsigned char)c);
  64. NEXT(1, 1);
  65. continue;
  66. }
  67. if (c > 0xFFFF)
  68. return 1;
  69. REQUIRE_OUTBUF(2);
  70. if (TRYMAP_ENC(cp950ext, code, c))
  71. ;
  72. else if (TRYMAP_ENC(big5, code, c))
  73. ;
  74. else
  75. return 1;
  76. OUTBYTE1(code >> 8);
  77. OUTBYTE2(code & 0xFF);
  78. NEXT(1, 2);
  79. }
  80. return 0;
  81. }
  82. DECODER(cp950)
  83. {
  84. while (inleft > 0) {
  85. unsigned char c = INBYTE1;
  86. Py_UCS4 decoded;
  87. if (c < 0x80) {
  88. OUTCHAR(c);
  89. NEXT_IN(1);
  90. continue;
  91. }
  92. REQUIRE_INBUF(2);
  93. if (TRYMAP_DEC(cp950ext, decoded, c, INBYTE2))
  94. OUTCHAR(decoded);
  95. else if (TRYMAP_DEC(big5, decoded, c, INBYTE2))
  96. OUTCHAR(decoded);
  97. else
  98. return 1;
  99. NEXT_IN(2);
  100. }
  101. return 0;
  102. }
  103. BEGIN_MAPPINGS_LIST(2)
  104. MAPPING_ENCDEC(big5)
  105. MAPPING_ENCDEC(cp950ext)
  106. END_MAPPINGS_LIST
  107. BEGIN_CODECS_LIST(2)
  108. CODEC_STATELESS(big5)
  109. CODEC_STATELESS(cp950)
  110. END_CODECS_LIST
  111. I_AM_A_MODULE_FOR(tw)