1*0Sstevel@tonic-gate // Copyright (c) 1997 James Clark
2*0Sstevel@tonic-gate // See the file COPYING for copying permission.
3*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate #include "splib.h"
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate #ifdef SP_MULTI_BYTE
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #include "Big5CodingSystem.h"
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate #ifdef SP_NAMESPACE
12*0Sstevel@tonic-gate namespace SP_NAMESPACE {
13*0Sstevel@tonic-gate #endif
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate class Big5Decoder : public Decoder {
16*0Sstevel@tonic-gate public:
Big5Decoder()17*0Sstevel@tonic-gate   Big5Decoder() { }
18*0Sstevel@tonic-gate   size_t decode(Char *, const char *, size_t, const char **);
19*0Sstevel@tonic-gate private:
20*0Sstevel@tonic-gate };
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate class Big5Encoder : public Encoder {
23*0Sstevel@tonic-gate public:
Big5Encoder()24*0Sstevel@tonic-gate   Big5Encoder() { }
25*0Sstevel@tonic-gate   void output(const Char *, size_t, OutputByteStream *);
output(Char * tmp_char,size_t tmp_size_t,OutputByteStream * tmp_obs)26*0Sstevel@tonic-gate   void output(Char *tmp_char, size_t tmp_size_t, OutputByteStream *tmp_obs) {
27*0Sstevel@tonic-gate         output((const Char *)tmp_char, (size_t) tmp_size_t, (OutputByteStream *)tmp_obs);
28*0Sstevel@tonic-gate   }
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate };
31*0Sstevel@tonic-gate 
makeDecoder() const32*0Sstevel@tonic-gate Decoder *Big5CodingSystem::makeDecoder() const
33*0Sstevel@tonic-gate {
34*0Sstevel@tonic-gate   return new Big5Decoder;
35*0Sstevel@tonic-gate }
36*0Sstevel@tonic-gate 
makeEncoder() const37*0Sstevel@tonic-gate Encoder *Big5CodingSystem::makeEncoder() const
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate   return new Big5Encoder;
40*0Sstevel@tonic-gate }
41*0Sstevel@tonic-gate 
decode(Char * to,const char * s,size_t slen,const char ** rest)42*0Sstevel@tonic-gate size_t Big5Decoder::decode(Char *to, const char *s,
43*0Sstevel@tonic-gate 			   size_t slen, const char **rest)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate   Char *start = to;
46*0Sstevel@tonic-gate   const unsigned char *us = (const unsigned char *)s;
47*0Sstevel@tonic-gate   while (slen > 0) {
48*0Sstevel@tonic-gate     if (!(*us & 0x80)) {
49*0Sstevel@tonic-gate       *to++ = *us++;
50*0Sstevel@tonic-gate       slen--;
51*0Sstevel@tonic-gate     }
52*0Sstevel@tonic-gate     else {
53*0Sstevel@tonic-gate       if (slen < 2)
54*0Sstevel@tonic-gate 	break;
55*0Sstevel@tonic-gate       slen -= 2;
56*0Sstevel@tonic-gate       unsigned short n = *us++ << 8;
57*0Sstevel@tonic-gate       n |= *us++;
58*0Sstevel@tonic-gate       *to++ = n;
59*0Sstevel@tonic-gate     }
60*0Sstevel@tonic-gate   }
61*0Sstevel@tonic-gate   *rest = (const char *)us;
62*0Sstevel@tonic-gate   return to - start;
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
output(const Char * s,size_t n,OutputByteStream * sb)65*0Sstevel@tonic-gate void Big5Encoder::output(const Char *s, size_t n, OutputByteStream *sb)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate   for (; n > 0; s++, n--) {
68*0Sstevel@tonic-gate     Char c = *s;
69*0Sstevel@tonic-gate     if (c < 0x80)
70*0Sstevel@tonic-gate       sb->sputc((unsigned char)c);
71*0Sstevel@tonic-gate     else if (c & 0x8000) {
72*0Sstevel@tonic-gate       sb->sputc((unsigned char)(c >> 8));
73*0Sstevel@tonic-gate       sb->sputc((unsigned char)(c & 0xff));
74*0Sstevel@tonic-gate     }
75*0Sstevel@tonic-gate     else
76*0Sstevel@tonic-gate       handleUnencodable(c, sb);
77*0Sstevel@tonic-gate   }
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate #ifdef SP_NAMESPACE
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #else /* not SP_MULTI_BYTE */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate #ifndef __GNUG__
87*0Sstevel@tonic-gate static char non_empty_translation_unit;	// sigh
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate #endif /* not SP_MULTI_BYTE */
91