Add GNU LGPL headers to all .c .C and .h files
[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 #include <iostream.h>
33
34 #ifdef SP_NAMESPACE
35 namespace SP_NAMESPACE {
36 #endif
37
38 class EUCJPDecoder : public Decoder {
39 public:
40   EUCJPDecoder() { }
41   size_t decode(Char *, const char *, size_t, const char **);
42 private:
43 };
44
45 class EUCJPEncoder : public Encoder {
46 public:
47   EUCJPEncoder() { }
48   void output(const Char *, size_t, streambuf *);
49 };
50
51 Decoder *EUCJPCodingSystem::makeDecoder() const
52 {
53   return new EUCJPDecoder;
54 }
55
56 Encoder *EUCJPCodingSystem::makeEncoder() const
57 {
58   return new EUCJPEncoder;
59 }
60
61 size_t EUCJPDecoder::decode(Char *to, const char *s,
62                            size_t slen, const char **rest)
63 {
64   Char *start = to;
65   const unsigned char *us = (const unsigned char *)s;
66   while (slen > 0) {
67     if (!(*us & 0x80)) {
68       // G0
69       *to++ = *us++;
70       slen--;
71     }
72     else if (*us == 0x8e) {
73       // G2
74       if (slen < 2)
75         break;
76       slen -= 2;
77       ++us;
78       *to++ = *us++ | 0x80;
79     }
80     else if (*us == 0x8f) {
81       // G3
82       if (slen < 3)
83         break;
84       slen -= 3;
85       ++us;
86       unsigned short n = (*us++ | 0x80) << 8;
87       n |= (*us++ & ~0x80);
88       *to++ = n;
89     }
90     else {
91       // G1
92       if (slen < 2)
93         break;
94       slen -= 2;
95       unsigned short n = *us++ << 8;
96       n |= (*us++ | 0x80);
97       *to++ = n;
98     }
99   }
100   *rest = (const char *)us;
101   return to - start;
102 }
103
104 // FIXME handle errors from streambuf::sputc
105
106 void EUCJPEncoder::output(const Char *s, size_t n, streambuf *sb)
107 {
108   for (; n > 0; s++, n--) {
109     Char c = *s;
110     unsigned short mask = (unsigned short)(c & 0x8080);
111     if (mask == 0)
112       sb->sputc(char(c & 0xff));
113     else if (mask == 0x8080) {
114       sb->sputc(char((c >> 8) & 0xff));
115       sb->sputc(char(c & 0xff));
116     }
117     else if (mask == 0x0080) {
118       sb->sputc(0x8e);
119       sb->sputc(char(c & 0xff));
120     }
121     else {
122       // mask == 0x8000
123       sb->sputc(0x8f);
124       sb->sputc(char((c >> 8) & 0xff));
125       sb->sputc(char(c & 0x7f));
126     }
127   }
128 }
129
130 #ifdef SP_NAMESPACE
131 }
132 #endif
133
134 #else /* not SP_MULTI_BYTE */
135
136 #ifndef __GNUG__
137 static char non_empty_translation_unit; // sigh
138 #endif
139
140 #endif /* not SP_MULTI_BYTE */