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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "stoolkit/sencoder/SB_GB18030.h"
#include "stoolkit/SString.h"
#include "stoolkit/SStringVector.h"
static SS_UCS4 pack (SS_UCS4 pi);
static SS_UCS4 unpack (SS_UCS4 pi);
#define SD_GB_ERROR 0xffffffff
/**
* @author: Gaspar Sinai <gaspar@yudit.org>
* Encoder for Chinesee
* @version: 2002-03-27
*/
SB_GB18030::SB_GB18030() : SBEncoder ("\n,\r\n,\r"), gb_18030 ("gb-18030")
{
ok = gb_18030.isOK();
}
SB_GB18030::~SB_GB18030 ()
{
}
/**
* return false if this generic encoder does not exist.
*/
bool
SB_GB18030::isOK() const
{
return ok;
}
/**
* This is encoding a unicode string into a bytestring
* @param input is a unicode string.
*/
const SString&
SB_GB18030::encode (const SV_UCS4& input)
{
const SS_UCS4* in = input.array();
sstring.clear();
sstring.ensure(input.size()*2);
SS_UCS4 key;
SS_UCS4 vle;
unsigned int kindex;
for (unsigned int i=0; i<input.size(); i++)
{
if (in[i] < 0x80)
{
sstring.append ((char) in[i]);
continue;
}
/* surrogate should start with high */
if (in[i] >= 0xdc00 && in[i] <= 0xdfff)
{
quoteString (in[i]);
continue;
}
/* surrogates are encoded as UCS4 */
if (in[i] >= 0xd800 && in[i] <= 0xdbff)
{
if (!(i+1<input.size() && in[i+1] >= 0xdc00 && in[i+1] <= 0xdfff))
{
quoteString (in[i]);
continue;
}
vle = ((in[i] & 0x3ff) << 10) + (in[i+1] & 0x3ff) + 0x10000;
vle = unpack (vle + 0x2E248 - 0x10000);
i++;
}
else if (in[i] > 0xffff)
{
if (in[i] > 0x10ffff)
{
quoteString (in[i]);
continue;
}
vle = unpack (in[i] + 0x2E248 - 0x10000);
}
else
{
/* linear approximation */
if (gb_18030.isOK())
{
kindex = gb_18030.getEncodePosition (in[i]);
key = gb_18030.getEncodeKey(kindex);
vle = gb_18030.getEncodeValue(kindex);
if (vle > 0xffff)
{
vle = pack (vle);
if (vle != SD_GB_ERROR)
{
vle += (in[i] -key);
vle = unpack (vle);
}
}
else
{
vle += (in[i] -key);
}
}
else
{
vle = SD_GB_ERROR;
}
}
if (vle == SD_GB_ERROR)
{
quoteString (in[i]);
continue;
}
if (vle > 0xffff)
{
sstring.append ((char) ((unsigned char)((vle>>24) & 0xff)));
sstring.append ((char) ((unsigned char)((vle>>16) & 0xff)));
}
/* 2 byte */
if (vle > 0xff)
{
sstring.append ((char) ((unsigned char)((vle>>8) & 0xff)));
}
sstring.append ((char) ((unsigned char)((vle>>0) & 0xff)));
}
return sstring;
}
/**
* Decode an input string into a unicode string.
* @param input is a string.
* he output can be null, in this case a line is not
* read fully. If input size is zero output will be flushed.
*/
const SV_UCS4&
SB_GB18030::decode (const SString& input)
{
const unsigned char* in = (unsigned char*) input.array();
ucs4string.clear();
ucs4string.ensure(input.size());
SS_UCS4 key;
SS_UCS4 vle;
SS_UCS4 ikey;
unsigned int kindex;
for (unsigned i=0; i<input.size(); i++)
{
if (in[i] < 0x80)
{
ucs4string.append ((SS_UCS4)input[i]);
continue;
}
/* two byte: 0x81-0xfe + 0x40-0x7e, 0x80-0xfe */
if (input.size() > i+1
&& in[i] >= 0x81 && in[i] <= 0xfe
&& ( (in[i+1] >= 0x40 && in[i+1] <= 0x7e)
|| (in[i+1] >= 0x80 && in[i+1] <= 0xfe)
)
&& gb_18030.isOK())
{
ikey = (SS_UCS4) in[i++]; ikey = ikey << 8;
ikey += (SS_UCS4) in[i];
kindex = gb_18030.getDecodePosition (ikey);
key = gb_18030.getDecodeKey(kindex);
vle = gb_18030.getDecodeValue(kindex);
/* should be 2 byte key, and 2 byte value */
if (vle > 0xffff || key > 0xffff || ikey < key)
{
quoteUCS4 (in[i-1]);
quoteUCS4 (in[i]);
continue;
}
ucs4string.append (vle + ikey - key);
continue;
}
if (input.size() > i+3
&& in[i] >= 0x81 && in[i] <= 0xfe
&& in[i+1] >= 0x30 && in[i+1] <= 0x39
&& in[i+2] >= 0x81 && in[i+2] <= 0xfe
&& in[i+3] >= 0x30 && in[i+3] <= 0x39
&& gb_18030.isOK())
{
ikey = (SS_UCS4) in[i++]; ikey = ikey << 8;
ikey += (SS_UCS4) in[i++]; ikey = ikey << 8;
ikey += (SS_UCS4) in[i++]; ikey = ikey << 8;
ikey += (SS_UCS4) in[i];
/* non-bmp */
if (ikey > 0x8431A439)
{
/* out of range */
if (ikey>0xE3329A35 || ikey<0x90308130)
{
quoteUCS4 (in[i-3]);
quoteUCS4 (in[i-2]);
quoteUCS4 (in[i-1]);
quoteUCS4 (in[i]);
continue;
}
vle = 0x10000;
key = 0x90308130;
}
else
{
kindex = gb_18030.getDecodePosition (ikey);
key = gb_18030.getDecodeKey(kindex);
vle = gb_18030.getDecodeValue(kindex);
}
/* should be 4 byte key */
if (key <= 0xffff || ikey < key)
{
quoteUCS4 (in[i-3]);
quoteUCS4 (in[i-2]);
quoteUCS4 (in[i-1]);
quoteUCS4 (in[i]);
continue;
}
ucs4string.append (vle + pack(ikey) - pack(key));
continue;
}
quoteUCS4 (in[i]);
}
return ucs4string;
}
/**
* These methods guess the line delimiters for the input
* The one without arguments is giving the 'first approximation'
* It returns an inclusive list of all possibilities.
*/
const SStringVector&
SB_GB18030::delimiters ()
{
return realDelimiters;
}
/**
* These methods guess the line delimiters for the input
* The one without arguments is giving the 'first approximation'
* It returns an exact list
*/
const SStringVector&
SB_GB18030::delimiters (const SString& sample)
{
return sampleDelimiters;
}
/**
* pack a gb code into linear code. Return SD_GB_ERROR on fail.
*/
static SS_UCS4
pack (SS_UCS4 up)
{
SS_UCS4 value = (SS_UCS4)up;
unsigned int k0 = (value >> 24) & 0xff; // 0x81..0xfe
unsigned int k1 = (value >> 16) & 0xff; // 0x30..0x39
unsigned int k2 = (value >> 8) & 0xff; // 0x81..0xfe
unsigned int k3 = (value >> 0) & 0xff; // 0x30..0x39
if (k0<0x81 || k0> 0xfe) return SD_GB_ERROR;
if (k1<0x30 || k1> 0x39) return SD_GB_ERROR;
if (k2<0x81 || k2> 0xfe) return SD_GB_ERROR;
if (k3<0x30 || k3> 0x39) return SD_GB_ERROR;
unsigned int num = (k0-0x81); num = num * 10;
num += (k1-0x30); num = num * 126;
num += (k2-0x81); num = num * 10;
num += (k3-0x30);
return ((SS_UCS4)num);
}
/**
* unpack linear code into gb code. Return SD_GB_ERROR on fail.
*/
static SS_UCS4
unpack (SS_UCS4 p)
{
SS_UCS4 num = p;
unsigned int k3 = (num % 10)+0x30; num = num / 10;
unsigned int k2 = (num % 126)+0x81; num = num / 126;
unsigned int k1 = (num % 10)+0x30; num = num / 10;
unsigned int k0 = (num % 126)+0x81;
num = (SS_UCS4) ((k0 << 24) + (k1 << 16) + (k2<<8) + k3);
/* 0x10ffff */
if (num > 0xE3329A35) return SD_GB_ERROR;
return num;
}
|