1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.
#include "splib.h"
#ifdef SP_MULTI_BYTE
#include "UTF8CodingSystem.h"
#include "constant.h"
#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif
enum {
// cmaskN is mask for first byte to test for N byte sequence
cmask1 = 0x80,
cmask2 = 0xe0,
cmask3 = 0xf0,
cmask4 = 0xf8,
cmask5 = 0xfc,
cmask6 = 0xfe,
// cvalN is value of masked first byte of N byte sequence
cval1 = 0x00,
cval2 = 0xc0,
cval3 = 0xe0,
cval4 = 0xf0,
cval5 = 0xf8,
cval6 = 0xfc,
// vmaskN is mask to get value from first byte in N byte sequence
vmask2 = 0x1f,
vmask3 = 0xf,
vmask4 = 0x7,
vmask5 = 0x3,
vmask6 = 0x1,
// minN is minimum legal resulting value for N byte sequence
min2 = 0x80,
min3 = 0x800,
min4 = 0x10000,
min5 = 0x200000,
min6 = 0x4000000,
max6 = 0x7fffffff
};
class UTF8Decoder : public Decoder {
public:
UTF8Decoder();
size_t decode(Char *, const char *, size_t, const char **);
Boolean convertOffset(unsigned long &offset) const;
private:
// value for encoding error
enum { invalid = 0xfffd };
Boolean recovering_;
PackedBoolean hadFirstChar_;
PackedBoolean hadByteOrderMark_;
};
class UTF8Encoder : public Encoder {
public:
UTF8Encoder();
void output(const Char *, size_t, OutputByteStream *);
};
Decoder *UTF8CodingSystem::makeDecoder() const
{
return new UTF8Decoder;
}
Encoder *UTF8CodingSystem::makeEncoder() const
{
return new UTF8Encoder;
}
UTF8Decoder::UTF8Decoder()
: recovering_(0), hadFirstChar_(0), hadByteOrderMark_(0)
{
}
size_t UTF8Decoder::decode(Char *to, const char *s,
size_t slen, const char **result)
{
// Check for byte-order mark
if (!hadFirstChar_ && slen >= 3) {
hadFirstChar_ = 1;
if ((unsigned char)s[0] == 0xEF &&
(unsigned char)s[1] == 0xBB &&
(unsigned char)s[2] == 0xBF) {
s += 3;
slen -= 3;
hadByteOrderMark_ = 1;
}
}
Char *start = to;
const unsigned char *us = (const unsigned char *)s;
if (recovering_) {
recovering_ = 0;
goto recover;
}
while (slen > 0) {
unsigned c0;
c0 = us[0];
if ((c0 & cmask1) == cval1) {
*to++ = c0;
us++;
slen--;
}
else if ((c0 & cmask2) == cval2) {
if (slen < 2)
goto done;
unsigned c1 = us[1] ^ 0x80;
if (c1 & 0xc0)
goto error;
unsigned c = ((c0 & vmask2) << 6) | c1;
if (c < min2)
c = invalid;
*to++ = c;
slen -= 2;
us += 2;
}
else if ((c0 & cmask3) == cval3) {
if (slen < 3)
goto done;
unsigned c1 = us[1] ^ 0x80;
unsigned c2 = us[2] ^ 0x80;
if ((c1 | c2) & 0xc0)
goto error;
unsigned c = ((((c0 & vmask3) << 6) | c1) << 6) | c2;
if (c < min3)
c = invalid;
*to++ = c;
slen -= 3;
us += 3;
}
else if ((c0 & cmask4) == cval4) {
if (slen < 4)
goto done;
unsigned c1 = us[1] ^ 0x80;
unsigned c2 = us[2] ^ 0x80;
unsigned c3 = us[3] ^ 0x80;
if ((c1 | c2 | c3) & 0xc0)
goto error;
if (charMax < min5 - 1)
*to++ = invalid;
else {
unsigned long c = ((((c0 & vmask4) << 6) | c1) << 6) | c2;
c = (c << 6) | c3;
if (c < min4)
c = invalid;
*to++ = c;
}
slen -= 4;
us += 4;
}
else if ((c0 & cmask5) == cval5) {
if (slen < 5)
goto done;
unsigned c1 = us[1] ^ 0x80;
unsigned c2 = us[2] ^ 0x80;
unsigned c3 = us[3] ^ 0x80;
unsigned c4 = us[4] ^ 0x80;
if ((c1 | c2 | c3 | c4) & 0xc0)
goto error;
if (charMax < min6 - 1)
*to++ = invalid;
else {
unsigned long c = ((((c0 & vmask5) << 6) | c1) << 6) | c2;
c = (((c << 6) | c3) << 6) | c4;
if (c < min5)
c = invalid;
*to++ = c;
}
slen -= 5;
us += 5;
}
else if ((c0 & cmask6) == cval6) {
if (slen < 6)
goto done;
unsigned c1 = us[1] ^ 0x80;
unsigned c2 = us[2] ^ 0x80;
unsigned c3 = us[3] ^ 0x80;
unsigned c4 = us[4] ^ 0x80;
unsigned c5 = us[5] ^ 0x80;
if ((c1 | c2 | c3 | c4 | c5) & 0xc0)
goto error;
if (charMax < max6)
*to++ = invalid;
else {
unsigned long c = ((((c0 & vmask6) << 6) | c1) << 6) | c2;
c = (((((c << 6) | c3) << 6) | c4) << 6) | c5;
if (c < min6)
c = invalid;
*to++ = c;
}
slen -= 6;
us += 6;
}
else {
error:
us++;
slen--;
*to++ = invalid;
recover:
for (;;) {
if (slen == 0) {
recovering_ = 1;
goto done;
}
if ((*us & 0xc0) != 0x80)
break;
us++;
slen--;
}
}
}
done:
*result = (char *)us;
return to - start;
}
Boolean UTF8Decoder::convertOffset(unsigned long &n) const
{
if (hadByteOrderMark_)
n += 3;
return true;
}
UTF8Encoder::UTF8Encoder()
{
}
void UTF8Encoder::output(const Char *s, size_t n, OutputByteStream *sb)
{
for (; n > 0; s++, n--) {
Char c = *s;
if (c < min2)
sb->sputc((unsigned char)c);
else if (c < min3) {
sb->sputc((c >> 6) | cval2);
sb->sputc((c & 0x3f) | 0x80);
}
else if (c < min4) {
sb->sputc((c >> 12) | cval3);
sb->sputc(((c >> 6) & 0x3f) | 0x80);
sb->sputc((c & 0x3f) | 0x80);
}
else if (c < min5) {
sb->sputc((c >> 18) | cval4);
sb->sputc(((c >> 12) & 0x3f) | 0x80);
sb->sputc(((c >> 6) & 0x3f) | 0x80);
sb->sputc((c & 0x3f) | 0x80);
}
else if (c < min6) {
sb->sputc((c >> 24) | cval5);
sb->sputc(((c >> 18) & 0x3f) | 0x80);
sb->sputc(((c >> 12) & 0x3f) | 0x80);
sb->sputc(((c >> 6) & 0x3f) | 0x80);
sb->sputc((c & 0x3f) | 0x80);
}
else if (c <= max6) {
sb->sputc((c >> 30) | cval6);
sb->sputc(((c >> 24) & 0x3f) | 0x80);
sb->sputc(((c >> 18) & 0x3f) | 0x80);
sb->sputc(((c >> 12) & 0x3f) | 0x80);
sb->sputc(((c >> 6) & 0x3f) | 0x80);
sb->sputc((c & 0x3f) | 0x80);
}
}
}
#ifdef SP_NAMESPACE
}
#endif
#else /* not SP_MULTI_BYTE */
#ifndef __GNUG__
static char non_empty_translation_unit; // sigh
#endif
#endif /* not SP_MULTI_BYTE */
|