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 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
|
//
// System.Web.UI.ObjectStateFormatter
//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
// Gonzalo Paniagua (gonzalo@ximian.com)
//
// (C) 2003 Ben Maurer
// (c) Copyright 2004-2010 Novell, Inc. (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//#define TRACE
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.Util;
using System.Diagnostics;
using System.Web.Configuration;
namespace System.Web.UI
{
public sealed class ObjectStateFormatter : IFormatter, IStateFormatter
{
const ushort SERIALIZED_STREAM_MAGIC = 0x01FF;
Page page;
MachineKeySection section;
public ObjectStateFormatter ()
{
}
internal ObjectStateFormatter (Page page)
{
this.page = page;
}
bool EnableMac {
get {
return (page == null) ? (section != null) : page.EnableViewStateMac;
}
}
bool NeedViewStateEncryption {
get {
return (page == null) ? false : page.NeedViewStateEncryption;
}
}
internal MachineKeySection Section {
get {
if (section == null)
section = (MachineKeySection) WebConfigurationManager.GetWebApplicationSection ("system.web/machineKey");
return section;
}
set {
section = value;
}
}
// There's no need to implement encryption support in this overload. Encryption is
// performed only when ObjectStateFormatter is created in the Page context, and that
// can happen only internally to System.Web. Since System.Web doesn't use this
// overload, the encryption code in here would be effectively dead.
public object Deserialize (Stream inputStream)
{
if (inputStream == null)
throw new ArgumentNullException ("inputStream");
BinaryReader reader = new BinaryReader (inputStream);
short magic = reader.ReadInt16 ();
if (magic != SERIALIZED_STREAM_MAGIC)
throw new ArgumentException ("The serialized data is invalid");
return DeserializeObject (reader);
}
public object Deserialize (string inputString)
{
if (inputString == null)
throw new ArgumentNullException ("inputString");
if (inputString.Length == 0)
throw new ArgumentNullException ("inputString");
byte [] data = Convert.FromBase64String (inputString);
if (data == null || (data.Length) == 0)
throw new ArgumentNullException ("inputString");
if (NeedViewStateEncryption) {
if (EnableMac) {
data = MachineKeySectionUtils.VerifyDecrypt (Section, data);
} else {
data = MachineKeySectionUtils.Decrypt (Section, data);
}
} else if (EnableMac) {
data = MachineKeySectionUtils.Verify (Section, data);
}
if (data == null)
throw new HttpException ("Unable to validate data.");
using (MemoryStream ms = new MemoryStream (data)) {
return Deserialize (ms);
}
}
public string Serialize (object stateGraph)
{
if (stateGraph == null)
return String.Empty;
byte[] data = null;
using (MemoryStream ms = new MemoryStream ()) {
Serialize (ms, stateGraph);
data = ms.GetBuffer ();
}
if (NeedViewStateEncryption) {
if (EnableMac) {
data = MachineKeySectionUtils.EncryptSign (Section, data);
} else {
data = MachineKeySectionUtils.Encrypt (Section, data);
}
} else if (EnableMac) {
data = MachineKeySectionUtils.Sign (Section, data);
}
return Convert.ToBase64String (data, 0, data.Length);
}
// There's no need to implement encryption support in this overload. Encryption is
// performed only when ObjectStateFormatter is created in the Page context, and that
// can happen only internally to System.Web. Since System.Web doesn't use this
// overload, the encryption code in here would be effectively dead.
public void Serialize (Stream outputStream, object stateGraph)
{
if (outputStream == null)
throw new ArgumentNullException ("outputStream");
if (stateGraph == null)
throw new ArgumentNullException ("stateGraph");
BinaryWriter writer = new BinaryWriter (outputStream);
writer.Write (SERIALIZED_STREAM_MAGIC);
SerializeValue (writer, stateGraph);
}
void SerializeValue (BinaryWriter w, object o)
{
ObjectFormatter.WriteObject (w, o, new WriterContext ());
}
object DeserializeObject (BinaryReader r)
{
return ObjectFormatter.ReadObject (r, new ReaderContext ());
}
#region IFormatter
object IFormatter.Deserialize (Stream serializationStream)
{
return Deserialize (serializationStream);
}
void IFormatter.Serialize (Stream serializationStream, object stateGraph)
{
Serialize (serializationStream, stateGraph);
}
SerializationBinder IFormatter.Binder {
get { return null; }
set { }
}
StreamingContext IFormatter.Context {
get { return new StreamingContext (StreamingContextStates.All); }
set { }
}
ISurrogateSelector IFormatter.SurrogateSelector {
get { return null; }
set { }
}
#endregion
#region Object Readers/Writers
sealed class WriterContext
{
Hashtable cache;
short nextKey = 0;
short key = 0;
public short Key {
get { return key; }
}
public bool RegisterCache (object o)
{
if (nextKey == short.MaxValue)
return false;
if (cache == null) {
cache = new Hashtable ();
cache.Add (o, key = nextKey++);
return false;
}
object posKey = cache [o];
if (posKey == null) {
cache.Add (o, key = nextKey++);
return false;
}
key = (short) posKey;
return true;
}
}
sealed class ReaderContext
{
ArrayList cache;
public void CacheItem (object o)
{
if (cache == null)
cache = new ArrayList ();
cache.Add (o);
}
public object GetCache (short key)
{
return cache [key];
}
}
abstract class ObjectFormatter
{
static readonly Hashtable writeMap = new Hashtable ();
static ObjectFormatter [] readMap = new ObjectFormatter [256];
static BinaryObjectFormatter binaryObjectFormatter;
static TypeFormatter typeFormatter;
static EnumFormatter enumFormatter;
static SingleRankArrayFormatter singleRankArrayFormatter;
static TypeConverterFormatter typeConverterFormatter;
static ObjectFormatter ()
{
new StringFormatter ().Register ();
new Int64Formatter ().Register ();
new Int32Formatter ().Register ();
new Int16Formatter ().Register ();
new ByteFormatter ().Register ();
new BooleanFormatter ().Register ();
new CharFormatter ().Register ();
new DateTimeFormatter ().Register ();
new PairFormatter ().Register ();
new TripletFormatter ().Register ();
new ArrayListFormatter ().Register ();
new HashtableFormatter ().Register ();
new ObjectArrayFormatter ().Register ();
new UnitFormatter ().Register ();
new FontUnitFormatter ().Register ();
new IndexedStringFormatter ().Register ();
new ColorFormatter ().Register ();
enumFormatter = new EnumFormatter ();
enumFormatter.Register ();
typeFormatter = new TypeFormatter ();
typeFormatter.Register ();
singleRankArrayFormatter = new SingleRankArrayFormatter ();
singleRankArrayFormatter.Register ();
typeConverterFormatter = new TypeConverterFormatter ();
typeConverterFormatter.Register ();
binaryObjectFormatter = new BinaryObjectFormatter ();
binaryObjectFormatter.Register ();
}
// 0 == null
static byte nextId = 1;
public ObjectFormatter ()
{
PrimaryId = nextId ++;
if (NumberOfIds == 1)
return;
SecondaryId = nextId ++;
if (NumberOfIds == 2)
return;
TertiaryId = nextId ++;
if (NumberOfIds == 3)
return;
throw new Exception ();
}
protected readonly byte PrimaryId, SecondaryId = 255, TertiaryId = 255;
protected abstract void Write (BinaryWriter w, object o, WriterContext ctx);
protected abstract object Read (byte token, BinaryReader r, ReaderContext ctx);
protected abstract Type Type { get; }
protected virtual int NumberOfIds { get { return 1; } }
public virtual void Register ()
{
writeMap [Type] = this;
readMap [PrimaryId] = this;
if (SecondaryId != 255) {
readMap [SecondaryId] = this;
if (TertiaryId != 255)
readMap [TertiaryId] = this;
}
}
public static void WriteObject (BinaryWriter w, object o, WriterContext ctx)
{
#if TRACE
if (o != null) {
Trace.WriteLine (String.Format ("Writing {0} (type: {1})", o, o.GetType ()));
Trace.Indent ();
} else {
Trace.WriteLine ("Writing null");
}
long pos = w.BaseStream.Position;
#endif
if (o == null) {
w.Write ((byte) 0);
return;
}
Type t = o.GetType ();
#if TRACE
Trace.WriteLine (String.Format ("Looking up formatter for type {0}", t));
#endif
ObjectFormatter fmt = writeMap [t] as ObjectFormatter;
#if TRACE
Trace.WriteLine (String.Format ("Formatter from writeMap: '{0}'", fmt));
#endif
if (fmt == null) {
// Handle abstract types here
if (o is Type)
fmt = typeFormatter;
else if (t.IsEnum)
fmt = enumFormatter;
else if (t.IsArray && ((Array) o).Rank == 1)
fmt = singleRankArrayFormatter;
else {
TypeConverter converter;
converter = TypeDescriptor.GetConverter (o);
#if TRACE
Trace.WriteLine (String.Format ("Type converter: '{0}' (to string: {1}; from {2}: {3})",
converter,
converter != null ? converter.CanConvertTo (typeof (string)) : false,
t,
converter != null ? converter.CanConvertFrom (t) : false));
#endif
// Do not use the converter if it's an instance of
// TypeConverter itself - it reports it is able to
// convert to string, but it's only a conversion
// consisting of a call to ToString() with no
// reverse conversion supported. This leads to
// problems when deserializing the object.
if (converter == null || converter.GetType () == typeof (TypeConverter) ||
!converter.CanConvertTo (typeof (string)) || !converter.CanConvertFrom (typeof (string)))
fmt = binaryObjectFormatter;
else {
typeConverterFormatter.Converter = converter;
fmt = typeConverterFormatter;
}
}
}
#if TRACE
Trace.WriteLine (String.Format ("Writing with formatter '{0}'", fmt.GetType ()));
#endif
fmt.Write (w, o, ctx);
#if TRACE
Trace.Unindent ();
Trace.WriteLine (String.Format ("Wrote {0} (type: {1}) {2} bytes", o, o.GetType (), w.BaseStream.Position - pos));
#endif
}
public static object ReadObject (BinaryReader r, ReaderContext ctx)
{
byte sig = r.ReadByte ();
if (sig == 0)
return null;
return readMap [sig].Read (sig, r, ctx);
}
protected void Write7BitEncodedInt (BinaryWriter w, int value)
{
do {
int high = (value >> 7) & 0x01ffffff;
byte b = (byte)(value & 0x7f);
if (high != 0)
b = (byte)(b | 0x80);
w.Write(b);
value = high;
} while(value != 0);
}
protected int Read7BitEncodedInt (BinaryReader r)
{
int ret = 0;
int shift = 0;
byte b;
do {
b = r.ReadByte();
ret = ret | ((b & 0x7f) << shift);
shift += 7;
} while ((b & 0x80) == 0x80);
return ret;
}
}
#region Primitive Formatters
class StringFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
if (ctx.RegisterCache (o)) {
w.Write (SecondaryId);
w.Write (ctx.Key);
} else {
w.Write (PrimaryId);
w.Write ((string)o);
}
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
if (token == PrimaryId) {
string s = r.ReadString ();
ctx.CacheItem (s);
return s;
} else {
return ctx.GetCache (r.ReadInt16 ());
}
}
protected override Type Type {
get { return typeof (string); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class IndexedStringFormatter : StringFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
IndexedString s = o as IndexedString;
if (s == null)
throw new InvalidOperationException ("object is not of the IndexedString type");
base.Write (w, s.Value, ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
string s = base.Read (token, r, ctx) as string;
if (String.IsNullOrEmpty (s))
throw new InvalidOperationException ("string must not be null or empty.");
return new IndexedString (s);
}
protected override Type Type {
get { return typeof (IndexedString); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class Int64Formatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
w.Write ((long)o);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return r.ReadInt64 ();
}
protected override Type Type {
get { return typeof (long); }
}
}
class Int32Formatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
int i = (int) o;
if ((int)(byte) i == i) {
w.Write (SecondaryId);
w.Write ((byte) i);
} else {
w.Write (PrimaryId);
w.Write (i);
}
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
if (token == PrimaryId)
return r.ReadInt32 ();
else
return (int) r.ReadByte ();
}
protected override Type Type {
get { return typeof (int); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class Int16Formatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
w.Write ((short)o);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return r.ReadInt16 ();
}
protected override Type Type {
get { return typeof (short); }
}
}
class ByteFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
w.Write ((byte)o);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return r.ReadByte ();
}
protected override Type Type {
get { return typeof (byte); }
}
}
class BooleanFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
if ((bool)o == true)
w.Write (PrimaryId);
else
w.Write (SecondaryId);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return token == PrimaryId;
}
protected override Type Type {
get { return typeof (bool); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class CharFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
w.Write ((char) o);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return r.ReadChar ();
}
protected override Type Type {
get { return typeof (char); }
}
}
class DateTimeFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
w.Write (((DateTime) o).Ticks);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return new DateTime (r.ReadInt64 ());
}
protected override Type Type {
get { return typeof (DateTime); }
}
}
class PairFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
Pair p = (Pair) o;
w.Write (PrimaryId);
WriteObject (w, p.First, ctx);
WriteObject (w, p.Second, ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
Pair p = new Pair ();
p.First = ReadObject (r, ctx);
p.Second = ReadObject (r, ctx);
return p;
}
protected override Type Type {
get { return typeof (Pair); }
}
}
class TripletFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
Triplet t = (Triplet) o;
w.Write (PrimaryId);
WriteObject (w, t.First, ctx);
WriteObject (w, t.Second, ctx);
WriteObject (w, t.Third, ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
Triplet t = new Triplet ();
t.First = ReadObject (r, ctx);
t.Second = ReadObject (r, ctx);
t.Third = ReadObject (r, ctx);
return t;
}
protected override Type Type {
get { return typeof (Triplet); }
}
}
class ArrayListFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
ArrayList l = (ArrayList) o;
w.Write (PrimaryId);
Write7BitEncodedInt (w, l.Count);
for (int i = 0; i < l.Count; i++)
WriteObject (w, l [i], ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
int len = Read7BitEncodedInt (r);
ArrayList l = new ArrayList (len);
for (int i = 0; i < len; i++)
l.Add (ReadObject (r, ctx));
return l;
}
protected override Type Type {
get { return typeof (ArrayList); }
}
}
class HashtableFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
Hashtable ht = (Hashtable) o;
w.Write (PrimaryId);
Write7BitEncodedInt (w, ht.Count);
foreach (DictionaryEntry de in ht) {
WriteObject (w, de.Key, ctx);
WriteObject (w, de.Value, ctx);
}
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
int len = Read7BitEncodedInt (r);
Hashtable ht = new Hashtable (len);
for (int i = 0; i < len; i++) {
object key = ReadObject (r, ctx);
object val = ReadObject (r, ctx);
ht.Add (key, val);
}
return ht;
}
protected override Type Type {
get { return typeof (Hashtable); }
}
}
class ObjectArrayFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
object [] val = (object []) o;
w.Write (PrimaryId);
Write7BitEncodedInt (w, val.Length);
for (int i = 0; i < val.Length; i++)
WriteObject (w, val [i], ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
int len = Read7BitEncodedInt (r);
object [] ret = new object [len];
for (int i = 0; i < len; i++)
ret [i] = ReadObject (r, ctx);
return ret;
}
protected override Type Type {
get { return typeof (object []); }
}
}
#endregion
#region System.Web Optimizations
class ColorFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
Color c = (Color) o;
if (c.IsEmpty || c.IsKnownColor) {
w.Write (SecondaryId);
if (c.IsEmpty)
w.Write (-1); //isempty marker
else
w.Write ((int) c.ToKnownColor ());
} else {
w.Write (PrimaryId);
w.Write (c.ToArgb ());
}
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
int value = r.ReadInt32 ();
if (token == PrimaryId)
return Color.FromArgb (value);
else {
if (value == -1) //isempty marker
return Color.Empty;
return Color.FromKnownColor ((KnownColor)value);
}
}
protected override Type Type {
get { return typeof (Color); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
#endregion
#region Special Formatters
class EnumFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
object value = Convert.ChangeType (o, ((Enum) o).GetTypeCode ());
w.Write (PrimaryId);
WriteObject (w, o.GetType (), ctx);
WriteObject (w, value, ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
Type t = (Type) ReadObject (r, ctx);
object value = ReadObject (r, ctx);
return Enum.ToObject (t, value);
}
protected override Type Type {
get { return typeof (Enum); }
}
}
class TypeFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
if (ctx.RegisterCache (o)) {
w.Write (SecondaryId);
w.Write (ctx.Key);
} else {
w.Write (PrimaryId);
w.Write (((Type) o).FullName);
// We should cache the name of the assembly
w.Write (((Type) o).Assembly.FullName);
}
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
if (token == PrimaryId) {
string type = r.ReadString ();
string assembly = r.ReadString ();
Type t = Assembly.Load (assembly).GetType (type);
ctx.CacheItem (t);
return t;
} else {
return ctx.GetCache (r.ReadInt16 ());
}
}
protected override Type Type {
get { return typeof (Type); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class SingleRankArrayFormatter : ObjectFormatter
{
readonly BinaryFormatter _binaryFormatter = new BinaryFormatter ();
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
Array val = (Array) o;
if (val.GetType ().GetElementType ().IsPrimitive) {
w.Write (SecondaryId);
_binaryFormatter.Serialize (w.BaseStream, o);
return;
}
w.Write (PrimaryId);
WriteObject (w, val.GetType ().GetElementType (), ctx);
Write7BitEncodedInt (w, val.Length);
for (int i = 0; i < val.Length; i++)
WriteObject (w, val.GetValue (i), ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
if (token == SecondaryId)
return _binaryFormatter.Deserialize (r.BaseStream);
Type t = (Type) ReadObject (r, ctx);
int len = Read7BitEncodedInt (r);
Array val = Array.CreateInstance (t, len);
for (int i = 0; i < len; i++)
val.SetValue (ReadObject (r, ctx), i);
return val;
}
protected override Type Type {
get { return typeof (Array); }
}
protected override int NumberOfIds {
get { return 2; }
}
}
class FontUnitFormatter : StringFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
base.Write (w, o.ToString (), ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return FontUnit.Parse ((string) base.Read (token, r, ctx));
}
protected override Type Type {
get { return typeof (FontUnit); }
}
}
class UnitFormatter : StringFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
base.Write (w, o.ToString (), ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
return Unit.Parse ((string) base.Read (token, r, ctx));
}
protected override Type Type {
get { return typeof (Unit); }
}
}
class TypeConverterFormatter : StringFormatter
{
TypeConverter converter;
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
ObjectFormatter.WriteObject (w, o.GetType (), ctx);
string v = (string) converter.ConvertTo (null, Helpers.InvariantCulture,
o, typeof (string));
base.Write (w, v, ctx);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
Type t = (Type) ObjectFormatter.ReadObject (r, ctx);
converter = TypeDescriptor.GetConverter (t);
token = r.ReadByte ();
string v = (string) base.Read (token, r, ctx);
return converter.ConvertFrom (null, Helpers.InvariantCulture, v);
}
protected override Type Type {
get { return typeof (TypeConverter); }
}
public TypeConverter Converter {
set { converter = value; }
}
}
class BinaryObjectFormatter : ObjectFormatter
{
protected override void Write (BinaryWriter w, object o, WriterContext ctx)
{
w.Write (PrimaryId);
MemoryStream ms = new MemoryStream (128);
new BinaryFormatter ().Serialize (ms, o);
byte [] buf = ms.GetBuffer ();
Write7BitEncodedInt (w, buf.Length);
w.Write (buf, 0, buf.Length);
}
protected override object Read (byte token, BinaryReader r, ReaderContext ctx)
{
int len = Read7BitEncodedInt (r);
byte [] buf = r.ReadBytes (len);
if (buf.Length != len)
throw new Exception ();
return new BinaryFormatter ().Deserialize (new MemoryStream (buf));
}
protected override Type Type {
get { return typeof (object); }
}
}
#endregion
#endregion
}
}
|