Merge branch 'linux1'
[oweals/cde.git] / cde / programs / nsgmls / EUCJPCodingSystem.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: EUCJPCodingSystem.C /main/1 1996/07/29 16:49:22 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
26
27 #include "splib.h"
28
29 #ifdef SP_MULTI_BYTE
30
31 #include "EUCJPCodingSystem.h"
32 #if defined(linux)
33 #include <iostream>
34 #else
35 #include <iostream.h>
36 #endif
37
38 #ifdef SP_NAMESPACE
39 namespace SP_NAMESPACE {
40 #endif
41
42 class EUCJPDecoder : public Decoder {
43 public:
44   EUCJPDecoder() { }
45   size_t decode(Char *, const char *, size_t, const char **);
46 private:
47 };
48
49 class EUCJPEncoder : public Encoder {
50 public:
51   EUCJPEncoder() { }
52   void output(const Char *, size_t, streambuf *);
53 };
54
55 Decoder *EUCJPCodingSystem::makeDecoder() const
56 {
57   return new EUCJPDecoder;
58 }
59
60 Encoder *EUCJPCodingSystem::makeEncoder() const
61 {
62   return new EUCJPEncoder;
63 }
64
65 size_t EUCJPDecoder::decode(Char *to, const char *s,
66                            size_t slen, const char **rest)
67 {
68   Char *start = to;
69   const unsigned char *us = (const unsigned char *)s;
70   while (slen > 0) {
71     if (!(*us & 0x80)) {
72       // G0
73       *to++ = *us++;
74       slen--;
75     }
76     else if (*us == 0x8e) {
77       // G2
78       if (slen < 2)
79         break;
80       slen -= 2;
81       ++us;
82       *to++ = *us++ | 0x80;
83     }
84     else if (*us == 0x8f) {
85       // G3
86       if (slen < 3)
87         break;
88       slen -= 3;
89       ++us;
90       unsigned short n = (*us++ | 0x80) << 8;
91       n |= (*us++ & ~0x80);
92       *to++ = n;
93     }
94     else {
95       // G1
96       if (slen < 2)
97         break;
98       slen -= 2;
99       unsigned short n = *us++ << 8;
100       n |= (*us++ | 0x80);
101       *to++ = n;
102     }
103   }
104   *rest = (const char *)us;
105   return to - start;
106 }
107
108 // FIXME handle errors from streambuf::sputc
109
110 void EUCJPEncoder::output(const Char *s, size_t n, streambuf *sb)
111 {
112   for (; n > 0; s++, n--) {
113     Char c = *s;
114     unsigned short mask = (unsigned short)(c & 0x8080);
115     if (mask == 0)
116       sb->sputc(char(c & 0xff));
117     else if (mask == 0x8080) {
118       sb->sputc(char((c >> 8) & 0xff));
119       sb->sputc(char(c & 0xff));
120     }
121     else if (mask == 0x0080) {
122       sb->sputc(0x8e);
123       sb->sputc(char(c & 0xff));
124     }
125     else {
126       // mask == 0x8000
127       sb->sputc(0x8f);
128       sb->sputc(char((c >> 8) & 0xff));
129       sb->sputc(char(c & 0x7f));
130     }
131   }
132 }
133
134 #ifdef SP_NAMESPACE
135 }
136 #endif
137
138 #else /* not SP_MULTI_BYTE */
139
140 #ifndef __GNUG__
141 static char non_empty_translation_unit; // sigh
142 #endif
143
144 #endif /* not SP_MULTI_BYTE */