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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.Xml
{
using System.Runtime;
using System.Runtime.Serialization;
using System.Text;
enum ValueHandleConstStringType
{
String = 0,
Number = 1,
Array = 2,
Object = 3,
Boolean = 4,
Null = 5,
}
static class ValueHandleLength
{
public const int Int8 = 1;
public const int Int16 = 2;
public const int Int32 = 4;
public const int Int64 = 8;
public const int UInt64 = 8;
public const int Single = 4;
public const int Double = 8;
public const int Decimal = 16;
public const int DateTime = 8;
public const int TimeSpan = 8;
public const int Guid = 16;
public const int UniqueId = 16;
}
enum ValueHandleType
{
Empty,
True,
False,
Zero,
One,
Int8,
Int16,
Int32,
Int64,
UInt64,
Single,
Double,
Decimal,
DateTime,
TimeSpan,
Guid,
UniqueId,
UTF8,
EscapedUTF8,
Base64,
Dictionary,
List,
Char,
Unicode,
QName,
ConstString
}
class ValueHandle
{
XmlBufferReader bufferReader;
ValueHandleType type;
int offset;
int length;
static Base64Encoding base64Encoding;
static string[] constStrings = {
"string",
"number",
"array",
"object",
"boolean",
"null",
};
public ValueHandle(XmlBufferReader bufferReader)
{
this.bufferReader = bufferReader;
this.type = ValueHandleType.Empty;
}
static Base64Encoding Base64Encoding
{
get
{
if (base64Encoding == null)
base64Encoding = new Base64Encoding();
return base64Encoding;
}
}
public void SetConstantValue(ValueHandleConstStringType constStringType)
{
type = ValueHandleType.ConstString;
offset = (int)constStringType;
}
public void SetValue(ValueHandleType type)
{
this.type = type;
}
public void SetDictionaryValue(int key)
{
SetValue(ValueHandleType.Dictionary, key, 0);
}
public void SetCharValue(int ch)
{
SetValue(ValueHandleType.Char, ch, 0);
}
public void SetQNameValue(int prefix, int key)
{
SetValue(ValueHandleType.QName, key, prefix);
}
public void SetValue(ValueHandleType type, int offset, int length)
{
this.type = type;
this.offset = offset;
this.length = length;
}
public bool IsWhitespace()
{
switch (this.type)
{
case ValueHandleType.UTF8:
return bufferReader.IsWhitespaceUTF8(this.offset, this.length);
case ValueHandleType.Dictionary:
return bufferReader.IsWhitespaceKey(this.offset);
case ValueHandleType.Char:
int ch = GetChar();
if (ch > char.MaxValue)
return false;
return XmlConverter.IsWhitespace((char)ch);
case ValueHandleType.EscapedUTF8:
return bufferReader.IsWhitespaceUTF8(this.offset, this.length);
case ValueHandleType.Unicode:
return bufferReader.IsWhitespaceUnicode(this.offset, this.length);
case ValueHandleType.True:
case ValueHandleType.False:
case ValueHandleType.Zero:
case ValueHandleType.One:
return false;
case ValueHandleType.ConstString:
return constStrings[offset].Length == 0;
default:
return this.length == 0;
}
}
public Type ToType()
{
switch (type)
{
case ValueHandleType.False:
case ValueHandleType.True:
return typeof(bool);
case ValueHandleType.Zero:
case ValueHandleType.One:
case ValueHandleType.Int8:
case ValueHandleType.Int16:
case ValueHandleType.Int32:
return typeof(int);
case ValueHandleType.Int64:
return typeof(long);
case ValueHandleType.UInt64:
return typeof(ulong);
case ValueHandleType.Single:
return typeof(float);
case ValueHandleType.Double:
return typeof(double);
case ValueHandleType.Decimal:
return typeof(decimal);
case ValueHandleType.DateTime:
return typeof(DateTime);
case ValueHandleType.Empty:
case ValueHandleType.UTF8:
case ValueHandleType.Unicode:
case ValueHandleType.EscapedUTF8:
case ValueHandleType.Dictionary:
case ValueHandleType.Char:
case ValueHandleType.QName:
case ValueHandleType.ConstString:
return typeof(string);
case ValueHandleType.Base64:
return typeof(byte[]);
case ValueHandleType.List:
return typeof(object[]);
case ValueHandleType.UniqueId:
return typeof(UniqueId);
case ValueHandleType.Guid:
return typeof(Guid);
case ValueHandleType.TimeSpan:
return typeof(TimeSpan);
default:
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
}
}
public Boolean ToBoolean()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.False)
return false;
if (type == ValueHandleType.True)
return true;
if (type == ValueHandleType.UTF8)
return XmlConverter.ToBoolean(bufferReader.Buffer, offset, length);
if (type == ValueHandleType.Int8)
{
int value = GetInt8();
if (value == 0)
return false;
if (value == 1)
return true;
}
return XmlConverter.ToBoolean(GetString());
}
public int ToInt()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type == ValueHandleType.Int8)
return GetInt8();
if (type == ValueHandleType.Int16)
return GetInt16();
if (type == ValueHandleType.Int32)
return GetInt32();
if (type == ValueHandleType.Int64)
{
long value = GetInt64();
if (value >= int.MinValue && value <= int.MaxValue)
{
return (int)value;
}
}
if (type == ValueHandleType.UInt64)
{
ulong value = GetUInt64();
if (value <= int.MaxValue)
{
return (int)value;
}
}
if (type == ValueHandleType.UTF8)
return XmlConverter.ToInt32(bufferReader.Buffer, offset, length);
return XmlConverter.ToInt32(GetString());
}
public long ToLong()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type == ValueHandleType.Int8)
return GetInt8();
if (type == ValueHandleType.Int16)
return GetInt16();
if (type == ValueHandleType.Int32)
return GetInt32();
if (type == ValueHandleType.Int64)
return GetInt64();
if (type == ValueHandleType.UInt64)
{
ulong value = GetUInt64();
if (value <= long.MaxValue)
{
return (long)value;
}
}
if (type == ValueHandleType.UTF8)
{
return XmlConverter.ToInt64(bufferReader.Buffer, offset, length);
}
return XmlConverter.ToInt64(GetString());
}
public ulong ToULong()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type >= ValueHandleType.Int8 && type <= ValueHandleType.Int64)
{
long value = ToLong();
if (value >= 0)
return (ulong)value;
}
if (type == ValueHandleType.UInt64)
return GetUInt64();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToUInt64(bufferReader.Buffer, offset, length);
return XmlConverter.ToUInt64(GetString());
}
public Single ToSingle()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Single)
return GetSingle();
if (type == ValueHandleType.Double)
{
double value = GetDouble();
if ((value >= Single.MinValue && value <= Single.MaxValue) || double.IsInfinity(value) || double.IsNaN(value))
return (Single)value;
}
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type == ValueHandleType.Int8)
return GetInt8();
if (type == ValueHandleType.Int16)
return GetInt16();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToSingle(bufferReader.Buffer, offset, length);
return XmlConverter.ToSingle(GetString());
}
public Double ToDouble()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Double)
return GetDouble();
if (type == ValueHandleType.Single)
return GetSingle();
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type == ValueHandleType.Int8)
return GetInt8();
if (type == ValueHandleType.Int16)
return GetInt16();
if (type == ValueHandleType.Int32)
return GetInt32();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToDouble(bufferReader.Buffer, offset, length);
return XmlConverter.ToDouble(GetString());
}
public Decimal ToDecimal()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.Decimal)
return GetDecimal();
if (type == ValueHandleType.Zero)
return 0;
if (type == ValueHandleType.One)
return 1;
if (type >= ValueHandleType.Int8 && type <= ValueHandleType.Int64)
return ToLong();
if (type == ValueHandleType.UInt64)
return GetUInt64();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToDecimal(bufferReader.Buffer, offset, length);
return XmlConverter.ToDecimal(GetString());
}
public DateTime ToDateTime()
{
if (type == ValueHandleType.DateTime)
{
return XmlConverter.ToDateTime(GetInt64());
}
if (type == ValueHandleType.UTF8)
{
return XmlConverter.ToDateTime(bufferReader.Buffer, offset, length);
}
return XmlConverter.ToDateTime(GetString());
}
public UniqueId ToUniqueId()
{
if (type == ValueHandleType.UniqueId)
return GetUniqueId();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToUniqueId(bufferReader.Buffer, offset, length);
return XmlConverter.ToUniqueId(GetString());
}
public TimeSpan ToTimeSpan()
{
if (type == ValueHandleType.TimeSpan)
return new TimeSpan(GetInt64());
if (type == ValueHandleType.UTF8)
return XmlConverter.ToTimeSpan(bufferReader.Buffer, offset, length);
return XmlConverter.ToTimeSpan(GetString());
}
public Guid ToGuid()
{
if (type == ValueHandleType.Guid)
return GetGuid();
if (type == ValueHandleType.UTF8)
return XmlConverter.ToGuid(bufferReader.Buffer, offset, length);
return XmlConverter.ToGuid(GetString());
}
public override string ToString()
{
return GetString();
}
public byte[] ToByteArray()
{
if (type == ValueHandleType.Base64)
{
byte[] buffer = new byte[length];
GetBase64(buffer, 0, length);
return buffer;
}
if (type == ValueHandleType.UTF8 && (length % 4) == 0)
{
try
{
int expectedLength = length / 4 * 3;
if (length > 0)
{
if (bufferReader.Buffer[offset + length - 1] == '=')
{
expectedLength--;
if (bufferReader.Buffer[offset + length - 2] == '=')
expectedLength--;
}
}
byte[] buffer = new byte[expectedLength];
int actualLength = Base64Encoding.GetBytes(bufferReader.Buffer, this.offset, this.length, buffer, 0);
if (actualLength != buffer.Length)
{
byte[] newBuffer = new byte[actualLength];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, actualLength);
buffer = newBuffer;
}
return buffer;
}
catch (FormatException)
{
// Something unhappy with the characters, fall back to the hard way
}
}
try
{
return Base64Encoding.GetBytes(XmlConverter.StripWhitespace(GetString()));
}
catch (FormatException exception)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(exception.Message, exception.InnerException));
}
}
public string GetString()
{
ValueHandleType type = this.type;
if (type == ValueHandleType.UTF8)
return GetCharsText();
switch (type)
{
case ValueHandleType.False:
return "false";
case ValueHandleType.True:
return "true";
case ValueHandleType.Zero:
return "0";
case ValueHandleType.One:
return "1";
case ValueHandleType.Int8:
case ValueHandleType.Int16:
case ValueHandleType.Int32:
return XmlConverter.ToString(ToInt());
case ValueHandleType.Int64:
return XmlConverter.ToString(GetInt64());
case ValueHandleType.UInt64:
return XmlConverter.ToString(GetUInt64());
case ValueHandleType.Single:
return XmlConverter.ToString(GetSingle());
case ValueHandleType.Double:
return XmlConverter.ToString(GetDouble());
case ValueHandleType.Decimal:
return XmlConverter.ToString(GetDecimal());
case ValueHandleType.DateTime:
return XmlConverter.ToString(ToDateTime());
case ValueHandleType.Empty:
return string.Empty;
case ValueHandleType.UTF8:
return GetCharsText();
case ValueHandleType.Unicode:
return GetUnicodeCharsText();
case ValueHandleType.EscapedUTF8:
return GetEscapedCharsText();
case ValueHandleType.Char:
return GetCharText();
case ValueHandleType.Dictionary:
return GetDictionaryString().Value;
case ValueHandleType.Base64:
return Base64Encoding.GetString(ToByteArray());
case ValueHandleType.List:
return XmlConverter.ToString(ToList());
case ValueHandleType.UniqueId:
return XmlConverter.ToString(ToUniqueId());
case ValueHandleType.Guid:
return XmlConverter.ToString(ToGuid());
case ValueHandleType.TimeSpan:
return XmlConverter.ToString(ToTimeSpan());
case ValueHandleType.QName:
return GetQNameDictionaryText();
case ValueHandleType.ConstString:
return constStrings[offset];
default:
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
}
}
// ASSUMPTION (Microsoft): all chars in str will be ASCII
public bool Equals2(string str, bool checkLower)
{
if (this.type != ValueHandleType.UTF8)
return GetString() == str;
if (this.length != str.Length)
return false;
byte[] buffer = bufferReader.Buffer;
for (int i = 0; i < this.length; ++i)
{
Fx.Assert(str[i] < 128, "");
byte ch = buffer[i + this.offset];
if (ch == str[i])
continue;
if (checkLower && char.ToLowerInvariant((char)ch) == str[i])
continue;
return false;
}
return true;
}
public void Sign(XmlSigningNodeWriter writer)
{
switch (type)
{
case ValueHandleType.Int8:
case ValueHandleType.Int16:
case ValueHandleType.Int32:
writer.WriteInt32Text(ToInt());
break;
case ValueHandleType.Int64:
writer.WriteInt64Text(GetInt64());
break;
case ValueHandleType.UInt64:
writer.WriteUInt64Text(GetUInt64());
break;
case ValueHandleType.Single:
writer.WriteFloatText(GetSingle());
break;
case ValueHandleType.Double:
writer.WriteDoubleText(GetDouble());
break;
case ValueHandleType.Decimal:
writer.WriteDecimalText(GetDecimal());
break;
case ValueHandleType.DateTime:
writer.WriteDateTimeText(ToDateTime());
break;
case ValueHandleType.Empty:
break;
case ValueHandleType.UTF8:
writer.WriteEscapedText(bufferReader.Buffer, offset, length);
break;
case ValueHandleType.Base64:
writer.WriteBase64Text(bufferReader.Buffer, 0, bufferReader.Buffer, offset, length);
break;
case ValueHandleType.UniqueId:
writer.WriteUniqueIdText(ToUniqueId());
break;
case ValueHandleType.Guid:
writer.WriteGuidText(ToGuid());
break;
case ValueHandleType.TimeSpan:
writer.WriteTimeSpanText(ToTimeSpan());
break;
default:
writer.WriteEscapedText(GetString());
break;
}
}
public object[] ToList()
{
return bufferReader.GetList(offset, length);
}
public object ToObject()
{
switch (type)
{
case ValueHandleType.False:
case ValueHandleType.True:
return ToBoolean();
case ValueHandleType.Zero:
case ValueHandleType.One:
case ValueHandleType.Int8:
case ValueHandleType.Int16:
case ValueHandleType.Int32:
return ToInt();
case ValueHandleType.Int64:
return ToLong();
case ValueHandleType.UInt64:
return GetUInt64();
case ValueHandleType.Single:
return ToSingle();
case ValueHandleType.Double:
return ToDouble();
case ValueHandleType.Decimal:
return ToDecimal();
case ValueHandleType.DateTime:
return ToDateTime();
case ValueHandleType.Empty:
case ValueHandleType.UTF8:
case ValueHandleType.Unicode:
case ValueHandleType.EscapedUTF8:
case ValueHandleType.Dictionary:
case ValueHandleType.Char:
case ValueHandleType.ConstString:
return ToString();
case ValueHandleType.Base64:
return ToByteArray();
case ValueHandleType.List:
return ToList();
case ValueHandleType.UniqueId:
return ToUniqueId();
case ValueHandleType.Guid:
return ToGuid();
case ValueHandleType.TimeSpan:
return ToTimeSpan();
default:
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
}
}
public bool TryReadBase64(byte[] buffer, int offset, int count, out int actual)
{
if (type == ValueHandleType.Base64)
{
actual = Math.Min(this.length, count);
GetBase64(buffer, offset, actual);
this.offset += actual;
this.length -= actual;
return true;
}
if (type == ValueHandleType.UTF8 && count >= 3 && (this.length % 4) == 0)
{
try
{
int charCount = Math.Min(count / 3 * 4, this.length);
actual = Base64Encoding.GetBytes(bufferReader.Buffer, this.offset, charCount, buffer, offset);
this.offset += charCount;
this.length -= charCount;
return true;
}
catch (FormatException)
{
// Something unhappy with the characters, fall back to the hard way
}
}
actual = 0;
return false;
}
public bool TryReadChars(char[] chars, int offset, int count, out int actual)
{
Fx.Assert(offset + count <= chars.Length, string.Format("offset '{0}' + count '{1}' MUST BE <= chars.Length '{2}'", offset, count, chars.Length));
if (type == ValueHandleType.Unicode)
return TryReadUnicodeChars(chars, offset, count, out actual);
if (type != ValueHandleType.UTF8)
{
actual = 0;
return false;
}
int charOffset = offset;
int charCount = count;
byte[] bytes = bufferReader.Buffer;
int byteOffset = this.offset;
int byteCount = this.length;
bool insufficientSpaceInCharsArray = false;
while (true)
{
while (charCount > 0 && byteCount > 0)
{
// fast path for codepoints U+0000 - U+007F
byte b = bytes[byteOffset];
if (b >= 0x80)
break;
chars[charOffset] = (char)b;
byteOffset++;
byteCount--;
charOffset++;
charCount--;
}
if (charCount == 0 || byteCount == 0 || insufficientSpaceInCharsArray)
break;
int actualByteCount;
int actualCharCount;
UTF8Encoding encoding = new UTF8Encoding(false, true);
try
{
// If we're asking for more than are possibly available, or more than are truly available then we can return the entire thing
if (charCount >= encoding.GetMaxCharCount(byteCount) || charCount >= encoding.GetCharCount(bytes, byteOffset, byteCount))
{
actualCharCount = encoding.GetChars(bytes, byteOffset, byteCount, chars, charOffset);
actualByteCount = byteCount;
}
else
{
Decoder decoder = encoding.GetDecoder();
// Since x bytes can never generate more than x characters this is a safe estimate as to what will fit
actualByteCount = Math.Min(charCount, byteCount);
// We use a decoder so we don't error if we fall across a character boundary
actualCharCount = decoder.GetChars(bytes, byteOffset, actualByteCount, chars, charOffset);
// We might've gotten zero characters though if < 4 bytes were requested because
// codepoints from U+0000 - U+FFFF can be up to 3 bytes in UTF-8, and represented as ONE char
// codepoints from U+10000 - U+10FFFF (last Unicode codepoint representable in UTF-8) are represented by up to 4 bytes in UTF-8
// and represented as TWO chars (high+low surrogate)
// (e.g. 1 char requested, 1 char in the buffer represented in 3 bytes)
while (actualCharCount == 0)
{
// Note the by the time we arrive here, if actualByteCount == 3, the next decoder.GetChars() call will read the 4th byte
// if we don't bail out since the while loop will advance actualByteCount only after reading the byte.
if (actualByteCount >= 3 && charCount < 2)
{
// If we reach here, it means that we're:
// - trying to decode more than 3 bytes and,
// - there is only one char left of charCount where we're stuffing decoded characters.
// In this case, we need to back off since decoding > 3 bytes in UTF-8 means that we will get 2 16-bit chars
// (a high surrogate and a low surrogate) - the Decoder will attempt to provide both at once
// and an ArgumentException will be thrown complaining that there's not enough space in the output char array.
// actualByteCount = 0 when the while loop is broken out of; decoder goes out of scope so its state no longer matters
insufficientSpaceInCharsArray = true;
break;
}
else
{
Fx.Assert(byteOffset + actualByteCount < bytes.Length,
string.Format("byteOffset {0} + actualByteCount {1} MUST BE < bytes.Length {2}", byteOffset, actualByteCount, bytes.Length));
// Request a few more bytes to get at least one character
actualCharCount = decoder.GetChars(bytes, byteOffset + actualByteCount, 1, chars, charOffset);
actualByteCount++;
}
}
// Now that we actually retrieved some characters, figure out how many bytes it actually was
actualByteCount = encoding.GetByteCount(chars, charOffset, actualCharCount);
}
}
catch (FormatException exception)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateEncodingException(bytes, byteOffset, byteCount, exception));
}
// Advance
byteOffset += actualByteCount;
byteCount -= actualByteCount;
charOffset += actualCharCount;
charCount -= actualCharCount;
}
this.offset = byteOffset;
this.length = byteCount;
actual = (count - charCount);
return true;
}
bool TryReadUnicodeChars(char[] chars, int offset, int count, out int actual)
{
int charCount = Math.Min(count, this.length / sizeof(char));
for (int i = 0; i < charCount; i++)
{
chars[offset + i] = (char)bufferReader.GetInt16(this.offset + i * sizeof(char));
}
this.offset += charCount * sizeof(char);
this.length -= charCount * sizeof(char);
actual = charCount;
return true;
}
public bool TryGetDictionaryString(out XmlDictionaryString value)
{
if (type == ValueHandleType.Dictionary)
{
value = GetDictionaryString();
return true;
}
else
{
value = null;
return false;
}
}
public bool TryGetByteArrayLength(out int length)
{
if (type == ValueHandleType.Base64)
{
length = this.length;
return true;
}
length = 0;
return false;
}
string GetCharsText()
{
Fx.Assert(type == ValueHandleType.UTF8, "");
if (length == 1 && bufferReader.GetByte(offset) == '1')
return "1";
return bufferReader.GetString(offset, length);
}
string GetUnicodeCharsText()
{
Fx.Assert(type == ValueHandleType.Unicode, "");
return bufferReader.GetUnicodeString(offset, length);
}
string GetEscapedCharsText()
{
Fx.Assert(type == ValueHandleType.EscapedUTF8, "");
return bufferReader.GetEscapedString(offset, length);
}
string GetCharText()
{
int ch = GetChar();
if (ch > char.MaxValue)
{
SurrogateChar surrogate = new SurrogateChar(ch);
char[] chars = new char[2];
chars[0] = surrogate.HighChar;
chars[1] = surrogate.LowChar;
return new string(chars, 0, 2);
}
else
{
return ((char)ch).ToString();
}
}
int GetChar()
{
Fx.Assert(type == ValueHandleType.Char, "");
return offset;
}
int GetInt8()
{
Fx.Assert(type == ValueHandleType.Int8, "");
return bufferReader.GetInt8(offset);
}
int GetInt16()
{
Fx.Assert(type == ValueHandleType.Int16, "");
return bufferReader.GetInt16(offset);
}
int GetInt32()
{
Fx.Assert(type == ValueHandleType.Int32, "");
return bufferReader.GetInt32(offset);
}
long GetInt64()
{
Fx.Assert(type == ValueHandleType.Int64 || type == ValueHandleType.TimeSpan || type == ValueHandleType.DateTime, "");
return bufferReader.GetInt64(offset);
}
ulong GetUInt64()
{
Fx.Assert(type == ValueHandleType.UInt64, "");
return bufferReader.GetUInt64(offset);
}
float GetSingle()
{
Fx.Assert(type == ValueHandleType.Single, "");
return bufferReader.GetSingle(offset);
}
double GetDouble()
{
Fx.Assert(type == ValueHandleType.Double, "");
return bufferReader.GetDouble(offset);
}
decimal GetDecimal()
{
Fx.Assert(type == ValueHandleType.Decimal, "");
return bufferReader.GetDecimal(offset);
}
UniqueId GetUniqueId()
{
Fx.Assert(type == ValueHandleType.UniqueId, "");
return bufferReader.GetUniqueId(offset);
}
Guid GetGuid()
{
Fx.Assert(type == ValueHandleType.Guid, "");
return bufferReader.GetGuid(offset);
}
void GetBase64(byte[] buffer, int offset, int count)
{
Fx.Assert(type == ValueHandleType.Base64, "");
bufferReader.GetBase64(this.offset, buffer, offset, count);
}
XmlDictionaryString GetDictionaryString()
{
Fx.Assert(type == ValueHandleType.Dictionary, "");
return bufferReader.GetDictionaryString(offset);
}
string GetQNameDictionaryText()
{
Fx.Assert(type == ValueHandleType.QName, "");
return string.Concat(PrefixHandle.GetString(PrefixHandle.GetAlphaPrefix(length)), ":", bufferReader.GetDictionaryString(offset));
}
}
}
|