| 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 |
namespace I18N.Common |
| 26 |
{ |
| 27 |
|
| 28 |
using System; |
| 29 |
using System.Runtime.InteropServices; |
| 30 |
using System.Text; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
[Serializable] |
| 38 |
public abstract class ByteEncoding : MonoEncoding |
| 39 |
{ |
| 40 |
|
| 41 |
protected char[] toChars; |
| 42 |
protected String encodingName; |
| 43 |
protected String bodyName; |
| 44 |
protected String headerName; |
| 45 |
protected String webName; |
| 46 |
protected bool isBrowserDisplay; |
| 47 |
protected bool isBrowserSave; |
| 48 |
protected bool isMailNewsDisplay; |
| 49 |
protected bool isMailNewsSave; |
| 50 |
protected int windowsCodePage; |
| 51 |
static byte [] isNormalized; |
| 52 |
static byte [] isNormalizedComputed; |
| 53 |
static byte [] normalization_bytes; |
| 54 |
|
| 55 |
|
| 56 |
protected ByteEncoding(int codePage, char[] toChars, |
| 57 |
String encodingName, String bodyName, |
| 58 |
String headerName, String webName, |
| 59 |
bool isBrowserDisplay, bool isBrowserSave, |
| 60 |
bool isMailNewsDisplay, bool isMailNewsSave, |
| 61 |
int windowsCodePage) |
| 62 |
: base(codePage) |
| 63 |
{ |
| 64 |
if (toChars.Length != byte.MaxValue + 1) |
| 65 |
throw new ArgumentException("toChars"); |
| 66 |
|
| 67 |
this.toChars = toChars; |
| 68 |
this.encodingName = encodingName; |
| 69 |
this.bodyName = bodyName; |
| 70 |
this.headerName = headerName; |
| 71 |
this.webName = webName; |
| 72 |
this.isBrowserDisplay = isBrowserDisplay; |
| 73 |
this.isBrowserSave = isBrowserSave; |
| 74 |
this.isMailNewsDisplay = isMailNewsDisplay; |
| 75 |
this.isMailNewsSave = isMailNewsSave; |
| 76 |
this.windowsCodePage = windowsCodePage; |
| 77 |
} |
| 78 |
|
| 79 |
public override bool IsAlwaysNormalized (NormalizationForm form) |
| 80 |
{ |
| 81 |
if (form != NormalizationForm.FormC) |
| 82 |
return false; |
| 83 |
|
| 84 |
if (isNormalized == null) |
| 85 |
isNormalized = new byte [0x10000 / 8]; |
| 86 |
if (isNormalizedComputed == null) |
| 87 |
isNormalizedComputed = new byte [0x10000 / 8]; |
| 88 |
|
| 89 |
if (normalization_bytes == null) { |
| 90 |
normalization_bytes = new byte [0x100]; |
| 91 |
lock (normalization_bytes) { |
| 92 |
for (int i = 0; i < 0x100; i++) |
| 93 |
normalization_bytes [i] = (byte) i; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
byte offset = (byte) (1 << (CodePage % 8)); |
| 98 |
if ((isNormalizedComputed [CodePage / 8] & offset) == 0) { |
| 99 |
Encoding e = Clone () as Encoding; |
| 100 |
e.DecoderFallback = new DecoderReplacementFallback (""); |
| 101 |
string s = e.GetString (normalization_bytes); |
| 102 |
|
| 103 |
if (s != s.Normalize (form)) |
| 104 |
isNormalized [CodePage / 8] |= offset; |
| 105 |
isNormalizedComputed [CodePage / 8] |= offset; |
| 106 |
} |
| 107 |
|
| 108 |
return (isNormalized [CodePage / 8] & offset) == 0; |
| 109 |
} |
| 110 |
|
| 111 |
public override bool IsSingleByte { |
| 112 |
get { return true; } |
| 113 |
} |
| 114 |
|
| 115 |
public override int GetByteCount(String s) |
| 116 |
{ |
| 117 |
if(s == null) |
| 118 |
{ |
| 119 |
throw new ArgumentNullException("s"); |
| 120 |
} |
| 121 |
return s.Length; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
public unsafe override int GetByteCountImpl (char* chars, int count) |
| 126 |
{ |
| 127 |
return count; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected unsafe abstract void ToBytes ( |
| 133 |
char* chars, int charCount, byte* bytes, int byteCount); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected unsafe virtual void ToBytes(char[] chars, int charIndex, int charCount, |
| 150 |
byte[] bytes, int byteIndex) |
| 151 |
{ |
| 152 |
|
| 153 |
|
| 154 |
if (charCount == 0 || bytes.Length == byteIndex) |
| 155 |
return; |
| 156 |
if (charIndex < 0 || charIndex > chars.Length) { |
| 157 |
throw new ArgumentOutOfRangeException |
| 158 |
("charIndex", Strings.GetString("ArgRange_Array")); |
| 159 |
} |
| 160 |
if (byteIndex < 0 || byteIndex > bytes.Length) { |
| 161 |
throw new ArgumentOutOfRangeException |
| 162 |
("byteIndex", Strings.GetString("ArgRange_Array")); |
| 163 |
} |
| 164 |
if (charCount < 0 || charIndex + charCount > chars.Length || byteIndex + charCount > bytes.Length) { |
| 165 |
throw new ArgumentOutOfRangeException |
| 166 |
("charCount", Strings.GetString("ArgRange_Array")); |
| 167 |
} |
| 168 |
fixed (char* cptr = chars) { |
| 169 |
fixed (byte* bptr = bytes) { |
| 170 |
ToBytes (cptr + charIndex, charCount, |
| 171 |
bptr + byteIndex, bytes.Length - byteIndex); |
| 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 |
public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) |
| 197 |
{ |
| 198 |
ToBytes (chars, charCount, bytes, byteCount); |
| 199 |
return charCount; |
| 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 |
public override int GetCharCount(byte[] bytes, int index, int count) |
| 279 |
{ |
| 280 |
if(bytes == null) |
| 281 |
{ |
| 282 |
throw new ArgumentNullException("bytes"); |
| 283 |
} |
| 284 |
if(index < 0 || index > bytes.Length) |
| 285 |
{ |
| 286 |
throw new ArgumentOutOfRangeException |
| 287 |
("index", Strings.GetString("ArgRange_Array")); |
| 288 |
} |
| 289 |
if(count < 0 || count > (bytes.Length - index)) |
| 290 |
{ |
| 291 |
throw new ArgumentOutOfRangeException |
| 292 |
("count", Strings.GetString("ArgRange_Array")); |
| 293 |
} |
| 294 |
return count; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, |
| 299 |
char[] chars, int charIndex) |
| 300 |
{ |
| 301 |
if(bytes == null) |
| 302 |
{ |
| 303 |
throw new ArgumentNullException("bytes"); |
| 304 |
} |
| 305 |
if(chars == null) |
| 306 |
{ |
| 307 |
throw new ArgumentNullException("chars"); |
| 308 |
} |
| 309 |
if(byteIndex < 0 || byteIndex > bytes.Length) |
| 310 |
{ |
| 311 |
throw new ArgumentOutOfRangeException |
| 312 |
("byteIndex", Strings.GetString("ArgRange_Array")); |
| 313 |
} |
| 314 |
if(byteCount < 0 || byteCount > (bytes.Length - byteIndex)) |
| 315 |
{ |
| 316 |
throw new ArgumentOutOfRangeException |
| 317 |
("byteCount", Strings.GetString("ArgRange_Array")); |
| 318 |
} |
| 319 |
if(charIndex < 0 || charIndex > chars.Length) |
| 320 |
{ |
| 321 |
throw new ArgumentOutOfRangeException |
| 322 |
("charIndex", Strings.GetString("ArgRange_Array")); |
| 323 |
} |
| 324 |
if((chars.Length - charIndex) < byteCount) |
| 325 |
{ |
| 326 |
throw new ArgumentException |
| 327 |
(Strings.GetString("Arg_InsufficientSpace")); |
| 328 |
} |
| 329 |
int count = byteCount; |
| 330 |
char[] cvt = toChars; |
| 331 |
while(count-- > 0) |
| 332 |
{ |
| 333 |
chars[charIndex++] = cvt[(int)(bytes[byteIndex++])]; |
| 334 |
} |
| 335 |
return byteCount; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public override int GetMaxByteCount(int charCount) |
| 341 |
{ |
| 342 |
if(charCount < 0) |
| 343 |
{ |
| 344 |
throw new ArgumentOutOfRangeException |
| 345 |
("charCount", |
| 346 |
Strings.GetString("ArgRange_NonNegative")); |
| 347 |
} |
| 348 |
return charCount; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public override int GetMaxCharCount(int byteCount) |
| 354 |
{ |
| 355 |
if(byteCount < 0) |
| 356 |
{ |
| 357 |
throw new ArgumentOutOfRangeException |
| 358 |
("byteCount", |
| 359 |
Strings.GetString("ArgRange_NonNegative")); |
| 360 |
} |
| 361 |
return byteCount; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
public unsafe override String GetString(byte[] bytes, int index, int count) |
| 366 |
{ |
| 367 |
if(bytes == null) |
| 368 |
{ |
| 369 |
throw new ArgumentNullException("bytes"); |
| 370 |
} |
| 371 |
if(index < 0 || index > bytes.Length) |
| 372 |
{ |
| 373 |
throw new ArgumentOutOfRangeException |
| 374 |
("index", Strings.GetString("ArgRange_Array")); |
| 375 |
} |
| 376 |
if(count < 0 || count > (bytes.Length - index)) |
| 377 |
{ |
| 378 |
throw new ArgumentOutOfRangeException |
| 379 |
("count", Strings.GetString("ArgRange_Array")); |
| 380 |
} |
| 381 |
|
| 382 |
if (count == 0) |
| 383 |
return string.Empty; |
| 384 |
|
| 385 |
string s = new string ((char) 0, count); |
| 386 |
|
| 387 |
fixed (byte* bytePtr = bytes) |
| 388 |
fixed (char* charPtr = s) |
| 389 |
fixed (char* cvt = toChars) { |
| 390 |
byte* b = bytePtr + index; |
| 391 |
char* c = charPtr; |
| 392 |
while(count-- != 0) |
| 393 |
*(c++) = cvt[*(b++)]; |
| 394 |
} |
| 395 |
|
| 396 |
return s; |
| 397 |
} |
| 398 |
public override String GetString(byte[] bytes) |
| 399 |
{ |
| 400 |
if(bytes == null) |
| 401 |
{ |
| 402 |
throw new ArgumentNullException("bytes"); |
| 403 |
} |
| 404 |
|
| 405 |
return GetString (bytes, 0, bytes.Length); |
| 406 |
} |
| 407 |
|
| 408 |
#if !ECMA_COMPAT |
| 409 |
|
| 410 |
|
| 411 |
public override String BodyName |
| 412 |
{ |
| 413 |
get |
| 414 |
{ |
| 415 |
return bodyName; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
public override String EncodingName |
| 421 |
{ |
| 422 |
get |
| 423 |
{ |
| 424 |
return encodingName; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
public override String HeaderName |
| 430 |
{ |
| 431 |
get |
| 432 |
{ |
| 433 |
return headerName; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
public override bool IsBrowserDisplay |
| 439 |
{ |
| 440 |
get |
| 441 |
{ |
| 442 |
return isBrowserDisplay; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
public override bool IsBrowserSave |
| 448 |
{ |
| 449 |
get |
| 450 |
{ |
| 451 |
return isBrowserSave; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
public override bool IsMailNewsDisplay |
| 457 |
{ |
| 458 |
get |
| 459 |
{ |
| 460 |
return isMailNewsDisplay; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
public override bool IsMailNewsSave |
| 466 |
{ |
| 467 |
get |
| 468 |
{ |
| 469 |
return isMailNewsSave; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
public override String WebName |
| 475 |
{ |
| 476 |
get |
| 477 |
{ |
| 478 |
return webName; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
public override int WindowsCodePage |
| 484 |
{ |
| 485 |
get |
| 486 |
{ |
| 487 |
return windowsCodePage; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
#endif |
| 492 |
|
| 493 |
}; |
| 494 |
|
| 495 |
}; |