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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
//------------------------------------------------------------------------------
// <copyright file="XmlEncoding.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System.Text;
using System.Diagnostics;
namespace System.Xml {
internal class UTF16Decoder : System.Text.Decoder {
private bool bigEndian;
private int lastByte;
private const int CharSize = 2;
public UTF16Decoder( bool bigEndian ) {
this.lastByte = -1;
this.bigEndian = bigEndian;
}
public override int GetCharCount( byte[] bytes, int index, int count ) {
return GetCharCount( bytes, index, count, false );
}
public override int GetCharCount( byte[] bytes, int index, int count, bool flush ) {
int byteCount = count + ( ( lastByte >= 0 ) ? 1 : 0 );
if ( flush && ( byteCount % CharSize != 0 ) ) {
throw new ArgumentException( Res.GetString( Res.Enc_InvalidByteInEncoding, new object[1] { -1 } ), (string)null );
}
return byteCount / CharSize;
}
public override int GetChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
int charCount = GetCharCount( bytes, byteIndex, byteCount );
if ( lastByte >= 0 ) {
if ( byteCount == 0 ) {
return charCount;
}
int nextByte = bytes[byteIndex++];
byteCount--;
chars[charIndex++] = bigEndian
? (char)( lastByte << 8 | nextByte )
: (char)( nextByte << 8 | lastByte );
lastByte = -1;
}
if ( ( byteCount & 1 ) != 0 ) {
lastByte = bytes[byteIndex + --byteCount];
}
// use the fast BlockCopy if possible
if ( bigEndian == BitConverter.IsLittleEndian ) {
int byteEnd = byteIndex + byteCount;
if ( bigEndian ) {
while ( byteIndex < byteEnd ) {
int hi = bytes[byteIndex++];
int lo = bytes[byteIndex++];
chars[charIndex++] = (char)( hi << 8 | lo );
}
}
else {
while ( byteIndex < byteEnd ) {
int lo = bytes[byteIndex++];
int hi = bytes[byteIndex++];
chars[charIndex++] = (char)( hi << 8 | lo );
}
}
}
else {
Buffer.BlockCopy( bytes, byteIndex, chars, charIndex * CharSize, byteCount );
}
return charCount;
}
public override void Convert( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed ) {
charsUsed = 0;
bytesUsed = 0;
if ( lastByte >= 0 ) {
if ( byteCount == 0 ) {
completed = true;
return;
}
int nextByte = bytes[byteIndex++];
byteCount--;
bytesUsed++;
chars[charIndex++] = bigEndian
? (char)( lastByte << 8 | nextByte )
: (char)( nextByte << 8 | lastByte );
charCount--;
charsUsed++;
lastByte = -1;
}
if ( charCount * CharSize < byteCount ) {
byteCount = charCount * CharSize;
completed = false;
}
else {
completed = true;
}
if ( bigEndian == BitConverter.IsLittleEndian ) {
int i = byteIndex;
int byteEnd = i + ( byteCount & ~0x1 );
if ( bigEndian ) {
while ( i < byteEnd ) {
int hi = bytes[i++];
int lo = bytes[i++];
chars[charIndex++] = (char)( hi << 8 | lo );
}
}
else {
while ( i < byteEnd ) {
int lo = bytes[i++];
int hi = bytes[i++];
chars[charIndex++] = (char)( hi << 8 | lo );
}
}
}
else {
Buffer.BlockCopy( bytes, byteIndex, chars, charIndex * CharSize, (int)(byteCount & ~0x1) );
}
charsUsed += byteCount / CharSize;
bytesUsed += byteCount;
if ( ( byteCount & 1 ) != 0 ) {
lastByte = bytes[byteIndex + byteCount - 1];
}
}
}
internal class SafeAsciiDecoder : Decoder {
public SafeAsciiDecoder() {
}
public override int GetCharCount( byte[] bytes, int index, int count ) {
return count;
}
public override int GetChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
int i = byteIndex;
int j = charIndex;
while ( i < byteIndex + byteCount ) {
chars[j++] = (char)bytes[i++];
}
return byteCount;
}
public override void Convert( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed ) {
if ( charCount < byteCount ) {
byteCount = charCount;
completed = false;
}
else {
completed = true;
}
int i = byteIndex;
int j = charIndex;
int byteEndIndex = byteIndex + byteCount;
while ( i < byteEndIndex ) {
chars[j++] = (char)bytes[i++];
}
charsUsed = byteCount;
bytesUsed = byteCount;
}
}
#if !SILVERLIGHT
internal class Ucs4Encoding : Encoding {
internal Ucs4Decoder ucs4Decoder;
public override string WebName {
get {
return this.EncodingName;
}
}
public override Decoder GetDecoder() {
return ucs4Decoder;
}
public override int GetByteCount( char[] chars, int index, int count ) {
return checked( count * 4 );
}
public override int GetByteCount( char[] chars ) {
return chars.Length * 4;
}
public override byte[] GetBytes( string s ) {
return null; //ucs4Decoder.GetByteCount(chars, index, count);
}
public override int GetBytes( char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex ) {
return 0;
}
public override int GetMaxByteCount( int charCount ) {
return 0;
}
public override int GetCharCount( byte[] bytes, int index, int count ) {
return ucs4Decoder.GetCharCount( bytes, index, count );
}
public override int GetChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
return ucs4Decoder.GetChars( bytes, byteIndex, byteCount, chars, charIndex );
}
public override int GetMaxCharCount( int byteCount ) {
return ( byteCount + 3 ) / 4;
}
public override int CodePage {
get {
return 0;
}
}
public override int GetCharCount( byte[] bytes ) {
return bytes.Length / 4;
}
public override Encoder GetEncoder() {
return null;
}
internal static Encoding UCS4_Littleendian {
get {
return new Ucs4Encoding4321();
}
}
internal static Encoding UCS4_Bigendian {
get {
return new Ucs4Encoding1234();
}
}
internal static Encoding UCS4_2143 {
get {
return new Ucs4Encoding2143();
}
}
internal static Encoding UCS4_3412 {
get {
return new Ucs4Encoding3412();
}
}
}
internal class Ucs4Encoding1234 : Ucs4Encoding {
public Ucs4Encoding1234() {
ucs4Decoder = new Ucs4Decoder1234();
}
public override string EncodingName {
get {
return "ucs-4 (Bigendian)";
}
}
public override byte[] GetPreamble() {
return new byte[4] { 0x00, 0x00, 0xfe, 0xff };
}
}
internal class Ucs4Encoding4321 : Ucs4Encoding {
public Ucs4Encoding4321() {
ucs4Decoder = new Ucs4Decoder4321();
}
public override string EncodingName {
get {
return "ucs-4";
}
}
public override byte[] GetPreamble() {
return new byte[4] { 0xff, 0xfe, 0x00, 0x00 };
}
}
internal class Ucs4Encoding2143 : Ucs4Encoding {
public Ucs4Encoding2143() {
ucs4Decoder = new Ucs4Decoder2143();
}
public override string EncodingName {
get {
return "ucs-4 (order 2143)";
}
}
public override byte[] GetPreamble() {
return new byte[4] { 0x00, 0x00, 0xff, 0xfe };
}
}
internal class Ucs4Encoding3412 : Ucs4Encoding {
public Ucs4Encoding3412() {
ucs4Decoder = new Ucs4Decoder3412();
}
public override string EncodingName {
get {
return "ucs-4 (order 3412)";
}
}
public override byte[] GetPreamble() {
return new byte[4] { 0xfe, 0xff, 0x00, 0x00 };
}
}
internal abstract class Ucs4Decoder : Decoder {
internal byte [] lastBytes = new byte[4];
internal int lastBytesCount = 0;
public override int GetCharCount( byte[] bytes, int index, int count ) {
return ( count + lastBytesCount ) / 4;
}
internal abstract int GetFullChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex );
public override int GetChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
// finish a character from the bytes that were cached last time
int i = lastBytesCount;
if ( lastBytesCount > 0 ) {
// copy remaining bytes into the cache
for ( ; lastBytesCount < 4 && byteCount > 0; lastBytesCount++ ) {
lastBytes[lastBytesCount] = bytes[byteIndex];
byteIndex++;
byteCount--;
}
// still not enough bytes -> return
if ( lastBytesCount < 4 ) {
return 0;
}
// decode 1 character from the byte cache
i = GetFullChars( lastBytes, 0 , 4, chars, charIndex );
Debug.Assert( i == 1 );
charIndex += i;
lastBytesCount = 0;
}
else {
i = 0;
}
// decode block of byte quadruplets
i = GetFullChars( bytes, byteIndex, byteCount, chars, charIndex ) + i;
// cache remaining bytes that does not make up a character
int bytesLeft = ( byteCount & 0x3 );
if ( bytesLeft >= 0 ) {
for( int j = 0; j < bytesLeft; j++ ) {
lastBytes[j] = bytes[byteIndex + byteCount - bytesLeft + j];
}
lastBytesCount = bytesLeft;
}
return i;
}
public override void Convert( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed ) {
bytesUsed = 0;
charsUsed = 0;
// finish a character from the bytes that were cached last time
int i = 0;
int lbc = lastBytesCount;
if ( lbc > 0 ) {
// copy remaining bytes into the cache
for ( ; lbc < 4 && byteCount > 0; lbc++ ) {
lastBytes[lbc] = bytes[byteIndex];
byteIndex++;
byteCount--;
bytesUsed++;
}
// still not enough bytes -> return
if ( lbc < 4 ) {
lastBytesCount = lbc;
completed = true;
return;
}
// decode 1 character from the byte cache
i = GetFullChars( lastBytes, 0 , 4, chars, charIndex );
Debug.Assert( i == 1 );
charIndex += i;
charCount -= i;
charsUsed = i;
lastBytesCount = 0;
// if that's all that was requested -> return
if ( charCount == 0 ) {
completed = ( byteCount == 0 );
return;
}
}
else {
i = 0;
}
// modify the byte count for GetFullChars depending on how many characters were requested
if ( charCount * 4 < byteCount ) {
byteCount = charCount * 4;
completed = false;
}
else {
completed = true;
}
bytesUsed += byteCount;
// decode block of byte quadruplets
charsUsed = GetFullChars( bytes, byteIndex, byteCount, chars, charIndex ) + i;
// cache remaining bytes that does not make up a character
int bytesLeft = ( byteCount & 0x3 );
if ( bytesLeft >= 0 ) {
for( int j = 0; j < bytesLeft; j++ ) {
lastBytes[j] = bytes[byteIndex + byteCount - bytesLeft + j];
}
lastBytesCount = bytesLeft;
}
}
internal void Ucs4ToUTF16(uint code, char[] chars, int charIndex) {
chars[charIndex] = (char)(XmlCharType.SurHighStart + (char)((code >> 16) - 1) + (char)((code >> 10) & 0x3F));
chars[charIndex + 1] = (char)(XmlCharType.SurLowStart + (char)(code & 0x3FF));
}
}
internal class Ucs4Decoder4321 : Ucs4Decoder {
internal override int GetFullChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
uint code;
int i, j;
byteCount += byteIndex;
for ( i = byteIndex, j = charIndex; i + 3 < byteCount; ) {
code = (uint)( ( bytes[i+3] << 24 ) | ( bytes[i+2] << 16 ) | ( bytes[i+1] << 8 ) | bytes[i] );
if ( code > 0x10FFFF ) {
throw new ArgumentException( Res.GetString( Res.Enc_InvalidByteInEncoding, new object[1] { i } ), (string)null );
}
else if ( code > 0xFFFF ) {
Ucs4ToUTF16(code, chars, j);
j++;
}
else {
if ( XmlCharType.IsSurrogate( (int)code ) ) {
throw new XmlException( Res.Xml_InvalidCharInThisEncoding, string.Empty );
}
else {
chars[j] = (char)code;
}
}
j++;
i += 4;
}
return j - charIndex;
}
};
internal class Ucs4Decoder1234 : Ucs4Decoder {
internal override int GetFullChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
uint code;
int i,j;
byteCount += byteIndex;
for ( i = byteIndex, j = charIndex; i+3 < byteCount; ) {
code = (uint)( ( bytes[i] << 24 ) | ( bytes[i+1] << 16 ) | ( bytes[i+2] << 8 ) | bytes[i+3] );
if ( code > 0x10FFFF ) {
throw new ArgumentException( Res.GetString( Res.Enc_InvalidByteInEncoding, new object[1] { i } ), (string)null );
}
else if ( code > 0xFFFF ) {
Ucs4ToUTF16(code, chars, j);
j++;
}
else {
if ( XmlCharType.IsSurrogate( (int)code ) ) {
throw new XmlException( Res.Xml_InvalidCharInThisEncoding, string.Empty );
}
else {
chars[j] = (char)code;
}
}
j++;
i += 4;
}
return j - charIndex;
}
}
internal class Ucs4Decoder2143 : Ucs4Decoder {
internal override int GetFullChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
uint code;
int i,j;
byteCount += byteIndex;
for ( i = byteIndex, j = charIndex; i+3 < byteCount; ) {
code = (uint)( ( bytes[i+1] << 24 ) | ( bytes[i] << 16 ) | ( bytes[i+3] << 8 ) | bytes[i+2] );
if ( code > 0x10FFFF ) {
throw new ArgumentException( Res.GetString( Res.Enc_InvalidByteInEncoding, new object[1] { i } ), (string)null );
}
else if ( code > 0xFFFF ) {
Ucs4ToUTF16(code, chars, j);
j++;
}
else {
if ( XmlCharType.IsSurrogate( (int)code ) ) {
throw new XmlException( Res.Xml_InvalidCharInThisEncoding, string.Empty );
}
else {
chars[j] = (char)code;
}
}
j++;
i += 4;
}
return j - charIndex;
}
}
internal class Ucs4Decoder3412 : Ucs4Decoder {
internal override int GetFullChars( byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex ) {
uint code;
int i,j;
byteCount += byteIndex;
for ( i = byteIndex, j = charIndex; i+3 < byteCount; ) {
code = (uint)( ( bytes[i+2] << 24 ) | ( bytes[i+3] << 16 ) | ( bytes[i] << 8 ) | bytes[i+1] );
if ( code > 0x10FFFF ) {
throw new ArgumentException( Res.GetString( Res.Enc_InvalidByteInEncoding, new object[1] { i } ), (string)null );
}
else if ( code > 0xFFFF ) {
Ucs4ToUTF16(code, chars, j);
j++;
}
else {
if ( XmlCharType.IsSurrogate( (int)code ) ) {
throw new XmlException( Res.Xml_InvalidCharInThisEncoding, string.Empty );
}
else {
chars[j] = (char)code;
}
}
j++;
i += 4;
}
return j - charIndex;
}
}
#endif
}
|