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