Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / nsgmls / UnicodeCodingSystem.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: UnicodeCodingSystem.C /main/1 1996/07/29 17:07:33 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 "UnicodeCodingSystem.h"
32 #include "macros.h"
33 #include "Owner.h"
34
35 #include <stddef.h>
36 #include <string.h>
37 #ifdef DECLARE_MEMMOVE
38 extern "C" {
39   void *memmove(void *, const void *, size_t);
40 }
41 #endif
42 #if defined(linux)
43 #include <iostream>
44 #else
45 #include <iostream.h>
46 #endif
47
48 #ifdef SP_NAMESPACE
49 namespace SP_NAMESPACE {
50 #endif
51
52 const unsigned short byteOrderMark = 0xfeff;
53 const unsigned short swappedByteOrderMark = 0xfffe;
54
55 class UnicodeDecoder : public Decoder {
56 public:
57   UnicodeDecoder(const InputCodingSystem *sub);
58   size_t decode(Char *to, const char *from, size_t fromLen,
59                 const char **rest);
60   Boolean convertOffset(unsigned long &offset) const;
61 private:
62   PackedBoolean hadFirstChar_;
63   PackedBoolean hadByteOrderMark_;
64   PackedBoolean swapBytes_;
65   Owner<Decoder> subDecoder_;
66   const InputCodingSystem *subCodingSystem_;
67 };
68
69 class UnicodeEncoder : public Encoder {
70 public:
71   UnicodeEncoder();
72   ~UnicodeEncoder();
73   void output(Char *, size_t, streambuf *);
74   void output(const Char *, size_t, streambuf *);
75   void startFile(streambuf *);
76 private:
77   void allocBuf(size_t);
78   unsigned short *buf_;
79   size_t bufSize_;
80 };
81
82 UnicodeCodingSystem::UnicodeCodingSystem(const InputCodingSystem *sub)
83 : sub_(sub)
84 {
85 }
86
87 Decoder *UnicodeCodingSystem::makeDecoder() const
88 {
89   return new UnicodeDecoder(sub_);
90 }
91
92 Encoder *UnicodeCodingSystem::makeEncoder() const
93 {
94   return new UnicodeEncoder;
95 }
96
97 unsigned UnicodeCodingSystem::fixedBytesPerChar() const
98 {
99   return 2;
100 }
101
102 UnicodeDecoder::UnicodeDecoder(const InputCodingSystem *subCodingSystem)
103 : Decoder(subCodingSystem ? 1 : 2), subCodingSystem_(subCodingSystem),
104   hadByteOrderMark_(0), hadFirstChar_(0), swapBytes_(0)
105 {
106 }
107
108
109 size_t UnicodeDecoder::decode(Char *to, const char *from, size_t fromLen,
110                               const char **rest)
111 {
112   union U {
113     unsigned short word;
114     char bytes[2];
115   };
116     
117   if (subDecoder_)
118     return subDecoder_->decode(to, from, fromLen, rest);
119   if (!hadFirstChar_) {
120     hadFirstChar_ = 1;
121     minBytesPerChar_ = 2;
122     if (fromLen < 2) {
123       *rest = from;
124       return 0;
125     }
126     U u;
127     u.bytes[0] = from[0];
128     u.bytes[1] = from[1];
129     if (u.word == byteOrderMark) {
130       hadByteOrderMark_ = 1;
131       from += 2;
132       fromLen -= 2;
133     }
134     else if (u.word == swappedByteOrderMark) {
135       hadByteOrderMark_ = 1;
136       from += 2;
137       fromLen -= 2;
138       swapBytes_ = 1;
139     }
140     else if (subCodingSystem_) {
141       subDecoder_ = subCodingSystem_->makeDecoder();
142       minBytesPerChar_ = subDecoder_->minBytesPerChar();
143       return subDecoder_->decode(to, from, fromLen, rest);
144     }
145   }
146   fromLen &= ~1;
147   *rest = from + fromLen;
148   if (sizeof(Char) == 2) {
149     if (!swapBytes_) {
150       if (from != (char *)to)
151         memmove(to, from, fromLen);
152       return fromLen/2;
153     }
154   }
155   if (swapBytes_) {
156     for (size_t n = fromLen; n > 0; n -= 2) {
157       U u;
158       u.bytes[1] = *from++;
159       u.bytes[0] = *from++;
160       *to++ = u.word;
161     }
162   }
163   else  {
164     for (size_t n = fromLen; n > 0; n -= 2) {
165       U u;
166       u.bytes[0] = *from++;
167       u.bytes[1] = *from++;
168       *to++ = u.word;
169     }
170   }
171   return fromLen/2;
172 }
173
174 Boolean UnicodeDecoder::convertOffset(unsigned long &n) const
175 {
176   if (subDecoder_)
177     return subDecoder_->convertOffset(n);
178   n *= 2;
179   if (hadByteOrderMark_)
180     n += 1;
181   return true;
182 }
183
184 UnicodeEncoder::UnicodeEncoder()
185 : buf_(0), bufSize_(0)
186 {
187 }
188
189 UnicodeEncoder::~UnicodeEncoder()
190 {
191   delete [] buf_;
192 }
193
194 void UnicodeEncoder::allocBuf(size_t n)
195 {
196   if (bufSize_ < n) {
197     delete [] buf_;
198     buf_ = new unsigned short[bufSize_ = n];
199   }
200 }
201
202 void UnicodeEncoder::startFile(streambuf *sb)
203 {
204   const unsigned short n = byteOrderMark;
205   sb->sputn((char *)&n, 2);
206 }
207
208 // FIXME handle errors from streambuf::sputn
209
210 void UnicodeEncoder::output(Char *s, size_t n, streambuf *sb)
211 {
212   if (sizeof(Char) == 2) {
213     sb->sputn((char *)s, n*2);
214     return;
215   }
216   ASSERT(sizeof(Char) >= 2);
217   unsigned short *p = (unsigned short *)s;
218   for (size_t i = 0; i < n; i++)
219     p[i] = s[i] & 0xffff;
220   sb->sputn((char *)s, n*2);
221 }
222
223 void UnicodeEncoder::output(const Char *s, size_t n, streambuf *sb)
224 {
225   if (sizeof(Char) == 2) {
226     sb->sputn((char *)s, n*2);
227     return;
228   }
229   allocBuf(n);
230   for (size_t i = 0; i < n; i++)
231     buf_[i] = s[i] & 0xffff;
232   sb->sputn((char *)buf_, n*2);
233 }
234
235 #ifdef SP_NAMESPACE
236 }
237 #endif
238
239 #else /* not SP_MULTI_BYTE */
240
241 #ifndef __GNUG__
242 static char non_empty_translation_unit; // sigh
243 #endif
244
245 #endif /* not SP_MULTI_BYTE */