2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: SJISCodingSystem.C /main/1 1996/07/29 17:03:20 cde-hp $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
31 #include "SJISCodingSystem.h"
33 #if defined(__linux__) || defined(CSRG_BASED) || defined(sun)
40 namespace SP_NAMESPACE {
43 class SJISDecoder : public Decoder {
46 size_t decode(Char *, const char *, size_t, const char **);
50 class SJISEncoder : public Encoder {
53 void output(const Char *, size_t, streambuf *);
56 Decoder *SJISCodingSystem::makeDecoder() const
58 return new SJISDecoder;
61 Encoder *SJISCodingSystem::makeEncoder() const
63 return new SJISEncoder;
67 SJISDecoder::SJISDecoder()
71 size_t SJISDecoder::decode(Char *to, const char *s,
72 size_t slen, const char **rest)
76 unsigned char c = *(unsigned char *)s;
82 else if (129 <= c && c <= 159) {
87 unsigned char c2 = *(unsigned char *)s++;
88 unsigned short n = ((c - 112) << 9) | c2;
89 if (64 <= c2 && c2 <= 127)
100 else if (224 <= c && c <= 239) {
105 unsigned char c2 = *(unsigned char *)s++;
106 unsigned short n = ((c - 176) << 9) | c2;
107 if (64 <= c2 && c2 <= 127)
118 else if (161 <= c && c <= 223) {
133 SJISEncoder::SJISEncoder()
137 // FIXME handle errors from streambuf::sputc
139 void SJISEncoder::output(const Char *s, size_t n, streambuf *sb)
141 for (; n > 0; s++, n--) {
143 unsigned short mask = (unsigned short)(c & 0x8080);
145 sb->sputc(char(c & 0xff));
146 else if (mask == 0x8080) {
147 unsigned char c1 = (c >> 8) & 0x7f;
148 unsigned char c2 = c & 0x7f;
153 out1 = ((c1 + 1) >> 1) + 112;
155 out1 = ((c1 + 1) >> 1) + 176;
171 if (33 <= c2 && c2 <= 126)
181 handleUnencodable(c, sb);
184 handleUnencodable(c, sb);
186 else if (mask == 0x0080) {
187 if (161 <= c && c <= 223)
188 sb->sputc(char(c & 0xff));
190 handleUnencodable(c, sb);
193 handleUnencodable(c, sb);
201 #else /* not SP_MULTI_BYTE */
204 static char non_empty_translation_unit; // sigh
207 #endif /* not SP_MULTI_BYTE */