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 librararies 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"
35 namespace SP_NAMESPACE {
38 class SJISDecoder : public Decoder {
41 size_t decode(Char *, const char *, size_t, const char **);
45 class SJISEncoder : public Encoder {
48 void output(const Char *, size_t, streambuf *);
51 Decoder *SJISCodingSystem::makeDecoder() const
53 return new SJISDecoder;
56 Encoder *SJISCodingSystem::makeEncoder() const
58 return new SJISEncoder;
62 SJISDecoder::SJISDecoder()
66 size_t SJISDecoder::decode(Char *to, const char *s,
67 size_t slen, const char **rest)
71 unsigned char c = *(unsigned char *)s;
77 else if (129 <= c && c <= 159) {
82 unsigned char c2 = *(unsigned char *)s++;
83 unsigned short n = ((c - 112) << 9) | c2;
84 if (64 <= c2 && c2 <= 127)
95 else if (224 <= c && c <= 239) {
100 unsigned char c2 = *(unsigned char *)s++;
101 unsigned short n = ((c - 176) << 9) | c2;
102 if (64 <= c2 && c2 <= 127)
113 else if (161 <= c && c <= 223) {
128 SJISEncoder::SJISEncoder()
132 // FIXME handle errors from streambuf::sputc
134 void SJISEncoder::output(const Char *s, size_t n, streambuf *sb)
136 for (; n > 0; s++, n--) {
138 unsigned short mask = (unsigned short)(c & 0x8080);
140 sb->sputc(char(c & 0xff));
141 else if (mask == 0x8080) {
142 unsigned char c1 = (c >> 8) & 0x7f;
143 unsigned char c2 = c & 0x7f;
148 out1 = ((c1 + 1) >> 1) + 112;
150 out1 = ((c1 + 1) >> 1) + 176;
166 if (33 <= c2 && c2 <= 126)
176 handleUnencodable(c, sb);
179 handleUnencodable(c, sb);
181 else if (mask == 0x0080) {
182 if (161 <= c && c <= 223)
183 sb->sputc(char(c & 0xff));
185 handleUnencodable(c, sb);
188 handleUnencodable(c, sb);
196 #else /* not SP_MULTI_BYTE */
199 static char non_empty_translation_unit; // sigh
202 #endif /* not SP_MULTI_BYTE */