1 // Copyright (c) 1994 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident "%Z%%M% %I% %E% SMI"
4
5 #include "splib.h"
6
7 #ifdef SP_MULTI_BYTE
8
9 #include "EUCJPCodingSystem.h"
10
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14
15 class EUCJPDecoder : public Decoder {
16 public:
EUCJPDecoder()17 EUCJPDecoder() { }
18 size_t decode(Char *, const char *, size_t, const char **);
19 private:
20 };
21
22 class EUCJPEncoder : public Encoder {
23 public:
EUCJPEncoder()24 EUCJPEncoder() { }
25 void output(const Char *, size_t, OutputByteStream *);
output(Char * tmp_char,size_t tmp_size_t,OutputByteStream * tmp_obs)26 void output(Char *tmp_char, size_t tmp_size_t, OutputByteStream *tmp_obs) {
27 output((const Char *)tmp_char, (size_t) tmp_size_t, (OutputByteStream *)tmp_obs);
28 }
29
30 };
31
makeDecoder() const32 Decoder *EUCJPCodingSystem::makeDecoder() const
33 {
34 return new EUCJPDecoder;
35 }
36
makeEncoder() const37 Encoder *EUCJPCodingSystem::makeEncoder() const
38 {
39 return new EUCJPEncoder;
40 }
41
decode(Char * to,const char * s,size_t slen,const char ** rest)42 size_t EUCJPDecoder::decode(Char *to, const char *s,
43 size_t slen, const char **rest)
44 {
45 Char *start = to;
46 const unsigned char *us = (const unsigned char *)s;
47 while (slen > 0) {
48 if (!(*us & 0x80)) {
49 // G0
50 *to++ = *us++;
51 slen--;
52 }
53 else if (*us == 0x8e) {
54 // G2
55 if (slen < 2)
56 break;
57 slen -= 2;
58 ++us;
59 *to++ = *us++ | 0x80;
60 }
61 else if (*us == 0x8f) {
62 // G3
63 if (slen < 3)
64 break;
65 slen -= 3;
66 ++us;
67 unsigned short n = (*us++ | 0x80) << 8;
68 n |= (*us++ & ~0x80);
69 *to++ = n;
70 }
71 else {
72 // G1
73 if (slen < 2)
74 break;
75 slen -= 2;
76 unsigned short n = *us++ << 8;
77 n |= (*us++ | 0x80);
78 *to++ = n;
79 }
80 }
81 *rest = (const char *)us;
82 return to - start;
83 }
84
85
output(const Char * s,size_t n,OutputByteStream * sb)86 void EUCJPEncoder::output(const Char *s, size_t n, OutputByteStream *sb)
87 {
88 for (; n > 0; s++, n--) {
89 Char c = *s;
90 unsigned short mask = (unsigned short)(c & 0x8080);
91 if (mask == 0)
92 sb->sputc((unsigned char)(c & 0xff));
93 else if (mask == 0x8080) {
94 sb->sputc((unsigned char)((c >> 8) & 0xff));
95 sb->sputc((unsigned char)(c & 0xff));
96 }
97 else if (mask == 0x0080) {
98 sb->sputc((unsigned char)0x8e);
99 sb->sputc((unsigned char)(c & 0xff));
100 }
101 else {
102 // mask == 0x8000
103 sb->sputc((unsigned char)0x8f);
104 sb->sputc((unsigned char)((c >> 8) & 0xff));
105 sb->sputc((unsigned char)(c & 0x7f));
106 }
107 }
108 }
109
110 #ifdef SP_NAMESPACE
111 }
112 #endif
113
114 #else /* not SP_MULTI_BYTE */
115
116 #ifndef __GNUG__
117 static char non_empty_translation_unit; // sigh
118 #endif
119
120 #endif /* not SP_MULTI_BYTE */
121