1
// Copyright (c) 1994 James Clark
2
// See the file COPYING for copying permission.
4
// This uses a big endian byte order irrespective of host byte order.
5
// Nothing special is done with FEFF/FFFE.
11
#include "Fixed2CodingSystem.h"
15
namespace SP_NAMESPACE {
18
class Fixed2Decoder : public Decoder {
21
size_t decode(Char *to, const char *from, size_t fromLen,
23
Boolean convertOffset(unsigned long &offset) const;
26
class Fixed2Encoder : public Encoder {
30
void output(Char *, size_t, OutputByteStream *);
31
void output(const Char *, size_t, OutputByteStream *);
33
void allocBuf(size_t);
38
Decoder *Fixed2CodingSystem::makeDecoder() const
40
return new Fixed2Decoder;
43
Encoder *Fixed2CodingSystem::makeEncoder() const
45
return new Fixed2Encoder;
48
unsigned Fixed2CodingSystem::fixedBytesPerChar() const
53
Fixed2Decoder::Fixed2Decoder()
58
size_t Fixed2Decoder::decode(Char *to, const char *from, size_t fromLen,
62
if (sizeof(Char) == 2 && from == (char *)to) {
63
*rest = from + (fromLen & ~1);
68
*rest = from + fromLen;
69
for (size_t n = fromLen; n > 0; n -= 2) {
70
*to++ = ((unsigned char)from[0] << 8) + (unsigned char)from[1];
76
Boolean Fixed2Decoder::convertOffset(unsigned long &n) const
82
Fixed2Encoder::Fixed2Encoder()
83
: buf_(0), bufSize_(0)
87
Fixed2Encoder::~Fixed2Encoder()
92
void Fixed2Encoder::allocBuf(size_t n)
96
buf_ = new char[bufSize_ = n];
100
void Fixed2Encoder::output(Char *s, size_t n, OutputByteStream *sb)
103
if (sizeof(Char) == 2) {
104
sb->sputn((char *)s, n*2);
108
ASSERT(sizeof(Char) >= 2);
110
for (size_t i = 0; i < n; i++) {
112
*p++ = (c >> 8) & 0xff;
115
sb->sputn((char *)s, n*2);
118
void Fixed2Encoder::output(const Char *s, size_t n, OutputByteStream *sb)
121
if (sizeof(Char) == 2) {
122
sb->sputn((char *)s, n*2);
127
for (size_t i = 0; i < n; i++) {
128
buf_[i*2] = (s[i] >> 8) & 0xff;
129
buf_[i*2 + 1] = s[i] & 0xff;
131
sb->sputn(buf_, n*2);
138
#else /* not SP_MULTI_BYTE */
141
static char non_empty_translation_unit; // sigh
144
#endif /* not SP_MULTI_BYTE */