Beispiel #1
0
        private Type DeserializeType(SerializerBinaryReader reader)
        {
            byte num = reader.ReadByte();

            if (num == 0x2b)
            {
                int num2 = reader.ReadEncodedInt32();
                return((Type)this._typeList[num2]);
            }
            string name = reader.ReadString();
            Type   type = null;

            try
            {
                if (num == 0x2a)
                {
                    type = HttpContext.SystemWebAssembly.GetType(name, true);
                }
                else
                {
                    type = Type.GetType(name, true);
                }
            }
            catch (Exception exception)
            {
                if (this._throwOnErrorDeserializing)
                {
                    throw;
                }
                WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_Type", new object[] { name }), this, 0xbc3, 0, exception);
            }
            this.AddDeserializationTypeReference(type);
            return(type);
        }
Beispiel #2
0
        private IndexedString DeserializeIndexedString(SerializerBinaryReader reader, byte token)
        {
            if (token == 0x1f)
            {
                int index = reader.ReadByte();
                return(new IndexedString(this._stringList[index]));
            }
            string s = reader.ReadString();

            this.AddDeserializationStringReference(s);
            return(new IndexedString(s));
        }
Beispiel #3
0
        public object Deserialize(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            Exception innerException = null;

            this.InitializeDeserializer();
            SerializerBinaryReader reader = new SerializerBinaryReader(inputStream);

            try
            {
                if ((reader.ReadByte() == 0xff) && (reader.ReadByte() == 1))
                {
                    return(this.DeserializeValue(reader));
                }
            }
            catch (Exception exception2)
            {
                innerException = exception2;
            }
            throw new ArgumentException(System.Web.SR.GetString("InvalidSerializedData"), innerException);
        }
        /// <devdoc>
        /// Deserializes a single value from the underlying stream.
        /// Essentially a token is read, followed by as much data needed to recreate
        /// the single value.
        /// </devdoc>
        private object DeserializeValue(SerializerBinaryReader reader) {
            byte token = reader.ReadByte();

            // NOTE: Preserve the order here with the order of the logic in
            //       the SerializeValue method.

            switch (token) {
                case Token_Null:
                    return null;
                case Token_EmptyString:
                    return String.Empty;
                case Token_String:
                    return reader.ReadString();
                case Token_ZeroInt32:
                    return 0;
                case Token_Int32:
                    return reader.ReadEncodedInt32();
                case Token_Pair:
                    return new Pair(DeserializeValue(reader),
                                    DeserializeValue(reader));
                case Token_Triplet:
                    return new Triplet(DeserializeValue(reader),
                                       DeserializeValue(reader),
                                       DeserializeValue(reader));
                case Token_IndexedString:
                case Token_IndexedStringAdd:
                    return DeserializeIndexedString(reader, token);
                case Token_ArrayList:
                    {
                        int count = reader.ReadEncodedInt32();
                        ArrayList list = new ArrayList(count);
                        for (int i = 0; i < count; i++) {
                            list.Add(DeserializeValue(reader));
                        }

                        return list;
                    }
                case Token_True:
                    return true;
                case Token_False:
                    return false;
                case Token_Byte:
                    return reader.ReadByte();
                case Token_Char:
                    return reader.ReadChar();
                case Token_DateTime:
                    return DateTime.FromBinary(reader.ReadInt64());
                case Token_Double:
                    return reader.ReadDouble();
                case Token_Int16:
                    return reader.ReadInt16();
                case Token_Single:
                    return reader.ReadSingle();
                case Token_Hashtable:
                case Token_HybridDictionary:
                    {
                        int count = reader.ReadEncodedInt32();

                        IDictionary table;
                        if (token == Token_Hashtable) {
                            table = new Hashtable(count);
                        }
                        else {
                            table = new HybridDictionary(count);
                        }
                        for (int i = 0; i < count; i++) {
                            table.Add(DeserializeValue(reader),
                                      DeserializeValue(reader));
                        }

                        return table;
                    }
                case Token_Type:
                    return DeserializeType(reader);
                case Token_StringArray:
                    {
                        int count = reader.ReadEncodedInt32();

                        string[] array = new string[count];
                        for (int i = 0; i < count; i++) {
                            array[i] = reader.ReadString();
                        }

                        return array;
                    }
                case Token_Array:
                    {
                        Type elementType = DeserializeType(reader);
                        int count = reader.ReadEncodedInt32();

                        Array list = Array.CreateInstance(elementType, count);
                        for (int i = 0; i < count; i++) {
                            list.SetValue(DeserializeValue(reader), i);
                        }

                        return list;
                    }
                case Token_IntEnum:
                    {
                        Type enumType = DeserializeType(reader);
                        int enumValue = reader.ReadEncodedInt32();

                        return Enum.ToObject(enumType, enumValue);
                    }
                case Token_Color:
                    return Color.FromArgb(reader.ReadInt32());
                case Token_EmptyColor:
                    return Color.Empty;
                case Token_KnownColor:
                    return Color.FromKnownColor((KnownColor)reader.ReadEncodedInt32());
                case Token_Unit:
                    return new Unit(reader.ReadDouble(), (UnitType)reader.ReadInt32());
                case Token_EmptyUnit:
                    return Unit.Empty;
                case Token_EventValidationStore:
                    return EventValidationStore.DeserializeFrom(reader.BaseStream);
                case Token_SparseArray:
                    {
                        Type elementType = DeserializeType(reader);
                        int count = reader.ReadEncodedInt32();
                        int itemCount = reader.ReadEncodedInt32();

                        // Guard against bad data
                        if (itemCount > count) {
                            throw new InvalidOperationException(SR.GetString(SR.InvalidSerializedData));
                        }

                        Array list = Array.CreateInstance(elementType, count);
                        for (int i = 0; i < itemCount; ++i) {
                            // Data is encoded as <index, Item>
                            int nextPos = reader.ReadEncodedInt32();

                            // Guard against bad data (nextPos way too big, or nextPos not increasing)
                            if (nextPos >= count || nextPos < 0) {
                                throw new InvalidOperationException(SR.GetString(SR.InvalidSerializedData));
                            }
                            list.SetValue(DeserializeValue(reader), nextPos);
                        }

                        return list;
                    }
                case Token_StringFormatted:
                    {
                        object result = null;

                        Type valueType = DeserializeType(reader);
                        string formattedValue = reader.ReadString();

                        if (valueType != null) {
                            TypeConverter converter = TypeDescriptor.GetConverter(valueType);
                            // TypeDescriptor.GetConverter() will never return null.  The ref docs
                            // for this method are incorrect.
                            try {
                                result = converter.ConvertFromInvariantString(formattedValue);
                            }
                            catch (Exception exception) {
                                if (_throwOnErrorDeserializing) {
                                    throw;
                                }
                                else {
                                    WebBaseEvent.RaiseSystemEvent(
                                        SR.GetString(SR.Webevent_msg_OSF_Deserialization_String, valueType.AssemblyQualifiedName),
                                        this, 
                                        WebEventCodes.WebErrorObjectStateFormatterDeserializationError, 
                                        WebEventCodes.UndefinedEventDetailCode, 
                                        exception);
                                }
                            }
                        }

                        return result;
                    }
                case Token_BinarySerialized:
                    {
                        int length = reader.ReadEncodedInt32();

                        byte[] buffer = new byte[length];
                        if (length != 0) {
                            reader.Read(buffer, 0, length);
                        }

                        object result = null;
                        MemoryStream ms = GetMemoryStream();
                        try {
                            ms.Write(buffer, 0, length);
                            ms.Position = 0;
                            IFormatter formatter = new BinaryFormatter();

                            result = formatter.Deserialize(ms);
                        }
                        catch (Exception exception) {
                            if (_throwOnErrorDeserializing) {
                                throw;
                            }
                            else {
                                WebBaseEvent.RaiseSystemEvent(
                                    SR.GetString(SR.Webevent_msg_OSF_Deserialization_Binary), 
                                    this, 
                                    WebEventCodes.WebErrorObjectStateFormatterDeserializationError, 
                                    WebEventCodes.UndefinedEventDetailCode, 
                                    exception);
                            }
                        }
                        finally {
                            ReleaseMemoryStream(ms);
                        }
                        return result;
                    }
                default:
                    throw new InvalidOperationException(SR.GetString(SR.InvalidSerializedData));
            }
        }
        /// <devdoc>
        /// Deserializes a Type. A Type can either be its name (the first occurrence),
        /// or a reference to it by index into the type table.  If we cannot load the type,
        /// we throw an exception if _throwOnErrorDeserializing is true, and we return null if
        /// _throwOnErrorDeserializing is false.
        /// </devdoc>
        private Type DeserializeType(SerializerBinaryReader reader) {
            byte token = reader.ReadByte();
            Debug.Assert((token == Token_TypeRef) ||
                         (token == Token_TypeRefAdd) ||
                         (token == Token_TypeRefAddLocal));

            if (token == Token_TypeRef) {
                // reference by index into type table
                int typeID = reader.ReadEncodedInt32();
                return (Type)_typeList[typeID];
            }
            else {
                // first occurrence of this type. Read in the type, resolve it, and
                // add it to the type table
                string typeName = reader.ReadString();

                Type resolvedType = null;
                try {
                    if (token == Token_TypeRefAddLocal) {
                        resolvedType = HttpContext.SystemWebAssembly.GetType(typeName, true);
                    }
                    else {
                        resolvedType = Type.GetType(typeName, true);
                    }
                }
                catch (Exception exception) {
                    if (_throwOnErrorDeserializing) {
                        throw;
                    }
                    else {
                        // Log error message
                        WebBaseEvent.RaiseSystemEvent(
                            SR.GetString(SR.Webevent_msg_OSF_Deserialization_Type, typeName),
                            this, 
                            WebEventCodes.WebErrorObjectStateFormatterDeserializationError, 
                            WebEventCodes.UndefinedEventDetailCode, 
                            exception);
                    }
                }

                AddDeserializationTypeReference(resolvedType);
                return resolvedType;
            }
        }
        /// <devdoc>
        /// Deserializes an IndexedString. An IndexedString can either be the string itself (the
        /// first occurrence), or a reference to it by index into the string table.
        /// </devdoc>
        private IndexedString DeserializeIndexedString(SerializerBinaryReader reader, byte token) {
            Debug.Assert((token == Token_IndexedStringAdd) || (token == Token_IndexedString));

            if (token == Token_IndexedString) {
                // reference to string in the current string table
                int tableIndex = (int)reader.ReadByte();

                Debug.Assert(_stringList[tableIndex] != null);
                return new IndexedString(_stringList[tableIndex]);
            }
            else {
                // first occurrence of this indexed string. Read in the string, and add
                // a reference to it, so future references can be resolved.
                string s = reader.ReadString();

                AddDeserializationStringReference(s);
                return new IndexedString(s);
            }
        }
        /// <devdoc>
        /// Deserializes an object graph from its binary serialized form
        /// contained in the specified stream.
        /// </devdoc>
        public object Deserialize(Stream inputStream) {
            if (inputStream == null) {
                throw new ArgumentNullException("inputStream");
            }

            Exception deserializationException = null;

            InitializeDeserializer();

            SerializerBinaryReader reader = new SerializerBinaryReader(inputStream);
            try {
                byte formatMarker = reader.ReadByte();

                if (formatMarker == Marker_Format) {
                    byte versionMarker = reader.ReadByte();

                    Debug.Assert(versionMarker == Marker_Version_1);
                    if (versionMarker == Marker_Version_1) {
                        return DeserializeValue(reader);
                    }
                }
            }
            catch (Exception e) {
                deserializationException = e;
            }

            // throw an exception if there was an exception during deserialization
            // or if deserialization was skipped because of invalid format or
            // version data in the stream

            throw new ArgumentException(SR.GetString(SR.InvalidSerializedData), deserializationException);
        }
Beispiel #8
0
        private object DeserializeValue(SerializerBinaryReader reader)
        {
            int         num4;
            IDictionary dictionary;
            byte        token = reader.ReadByte();

            switch (token)
            {
            case 1:
                return(reader.ReadInt16());

            case 2:
                return(reader.ReadEncodedInt32());

            case 3:
                return(reader.ReadByte());

            case 4:
                return(reader.ReadChar());

            case 5:
                return(reader.ReadString());

            case 6:
                return(DateTime.FromBinary(reader.ReadInt64()));

            case 7:
                return(reader.ReadDouble());

            case 8:
                return(reader.ReadSingle());

            case 9:
                return(Color.FromArgb(reader.ReadInt32()));

            case 10:
                return(Color.FromKnownColor((KnownColor)reader.ReadEncodedInt32()));

            case 11:
            {
                Type enumType = this.DeserializeType(reader);
                int  num10    = reader.ReadEncodedInt32();
                return(Enum.ToObject(enumType, num10));
            }

            case 12:
                return(Color.Empty);

            case 15:
                return(new Pair(this.DeserializeValue(reader), this.DeserializeValue(reader)));

            case 0x10:
                return(new Triplet(this.DeserializeValue(reader), this.DeserializeValue(reader), this.DeserializeValue(reader)));

            case 20:
            {
                Type  elementType = this.DeserializeType(reader);
                int   length      = reader.ReadEncodedInt32();
                Array array       = Array.CreateInstance(elementType, length);
                for (int j = 0; j < length; j++)
                {
                    array.SetValue(this.DeserializeValue(reader), j);
                }
                return(array);
            }

            case 0x15:
            {
                int      num6     = reader.ReadEncodedInt32();
                string[] strArray = new string[num6];
                for (int k = 0; k < num6; k++)
                {
                    strArray[k] = reader.ReadString();
                }
                return(strArray);
            }

            case 0x16:
            {
                int       capacity = reader.ReadEncodedInt32();
                ArrayList list     = new ArrayList(capacity);
                for (int m = 0; m < capacity; m++)
                {
                    list.Add(this.DeserializeValue(reader));
                }
                return(list);
            }

            case 0x17:
            case 0x18:
                num4 = reader.ReadEncodedInt32();
                if (token != 0x17)
                {
                    dictionary = new HybridDictionary(num4);
                    break;
                }
                dictionary = new Hashtable(num4);
                break;

            case 0x19:
                return(this.DeserializeType(reader));

            case 0x1b:
                return(new Unit(reader.ReadDouble(), (UnitType)reader.ReadInt32()));

            case 0x1c:
                return(Unit.Empty);

            case 30:
            case 0x1f:
                return(this.DeserializeIndexedString(reader, token));

            case 40:
            {
                object obj2 = null;
                Type   type = this.DeserializeType(reader);
                string text = reader.ReadString();
                if (type != null)
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(type);
                    try
                    {
                        obj2 = converter.ConvertFromInvariantString(text);
                    }
                    catch (Exception exception)
                    {
                        if (this._throwOnErrorDeserializing)
                        {
                            throw;
                        }
                        WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_String", new object[] { type.AssemblyQualifiedName }), this, 0xbc3, 0, exception);
                    }
                }
                return(obj2);
            }

            case 50:
            {
                int    count  = reader.ReadEncodedInt32();
                byte[] buffer = new byte[count];
                if (count != 0)
                {
                    reader.Read(buffer, 0, count);
                }
                object       obj3         = null;
                MemoryStream memoryStream = GetMemoryStream();
                try
                {
                    memoryStream.Write(buffer, 0, count);
                    memoryStream.Position = 0L;
                    IFormatter formatter = new BinaryFormatter();
                    obj3 = formatter.Deserialize(memoryStream);
                }
                catch (Exception exception2)
                {
                    if (this._throwOnErrorDeserializing)
                    {
                        throw;
                    }
                    WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_Binary"), this, 0xbc3, 0, exception2);
                }
                finally
                {
                    ReleaseMemoryStream(memoryStream);
                }
                return(obj3);
            }

            case 60:
            {
                Type type3 = this.DeserializeType(reader);
                int  num11 = reader.ReadEncodedInt32();
                int  num12 = reader.ReadEncodedInt32();
                if (num12 > num11)
                {
                    throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
                }
                Array array2 = Array.CreateInstance(type3, num11);
                for (int n = 0; n < num12; n++)
                {
                    int index = reader.ReadEncodedInt32();
                    if ((index >= num11) || (index < 0))
                    {
                        throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
                    }
                    array2.SetValue(this.DeserializeValue(reader), index);
                }
                return(array2);
            }

            case 100:
                return(null);

            case 0x65:
                return(string.Empty);

            case 0x66:
                return(0);

            case 0x67:
                return(true);

            case 0x68:
                return(false);

            default:
                throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
            }
            for (int i = 0; i < num4; i++)
            {
                dictionary.Add(this.DeserializeValue(reader), this.DeserializeValue(reader));
            }
            return(dictionary);
        }
        private object DeserializeValue(SerializerBinaryReader reader)
        {
            int num4;
            IDictionary dictionary;
            byte token = reader.ReadByte();
            switch (token)
            {
                case 1:
                    return reader.ReadInt16();

                case 2:
                    return reader.ReadEncodedInt32();

                case 3:
                    return reader.ReadByte();

                case 4:
                    return reader.ReadChar();

                case 5:
                    return reader.ReadString();

                case 6:
                    return DateTime.FromBinary(reader.ReadInt64());

                case 7:
                    return reader.ReadDouble();

                case 8:
                    return reader.ReadSingle();

                case 9:
                    return Color.FromArgb(reader.ReadInt32());

                case 10:
                    return Color.FromKnownColor((KnownColor) reader.ReadEncodedInt32());

                case 11:
                {
                    Type enumType = this.DeserializeType(reader);
                    int num10 = reader.ReadEncodedInt32();
                    return Enum.ToObject(enumType, num10);
                }
                case 12:
                    return Color.Empty;

                case 15:
                    return new Pair(this.DeserializeValue(reader), this.DeserializeValue(reader));

                case 0x10:
                    return new Triplet(this.DeserializeValue(reader), this.DeserializeValue(reader), this.DeserializeValue(reader));

                case 20:
                {
                    Type elementType = this.DeserializeType(reader);
                    int length = reader.ReadEncodedInt32();
                    Array array = Array.CreateInstance(elementType, length);
                    for (int j = 0; j < length; j++)
                    {
                        array.SetValue(this.DeserializeValue(reader), j);
                    }
                    return array;
                }
                case 0x15:
                {
                    int num6 = reader.ReadEncodedInt32();
                    string[] strArray = new string[num6];
                    for (int k = 0; k < num6; k++)
                    {
                        strArray[k] = reader.ReadString();
                    }
                    return strArray;
                }
                case 0x16:
                {
                    int capacity = reader.ReadEncodedInt32();
                    ArrayList list = new ArrayList(capacity);
                    for (int m = 0; m < capacity; m++)
                    {
                        list.Add(this.DeserializeValue(reader));
                    }
                    return list;
                }
                case 0x17:
                case 0x18:
                    num4 = reader.ReadEncodedInt32();
                    if (token != 0x17)
                    {
                        dictionary = new HybridDictionary(num4);
                        break;
                    }
                    dictionary = new Hashtable(num4);
                    break;

                case 0x19:
                    return this.DeserializeType(reader);

                case 0x1b:
                    return new Unit(reader.ReadDouble(), (UnitType) reader.ReadInt32());

                case 0x1c:
                    return Unit.Empty;

                case 30:
                case 0x1f:
                    return this.DeserializeIndexedString(reader, token);

                case 40:
                {
                    object obj2 = null;
                    Type type = this.DeserializeType(reader);
                    string text = reader.ReadString();
                    if (type != null)
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(type);
                        try
                        {
                            obj2 = converter.ConvertFromInvariantString(text);
                        }
                        catch (Exception exception)
                        {
                            if (this._throwOnErrorDeserializing)
                            {
                                throw;
                            }
                            WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_String", new object[] { type.AssemblyQualifiedName }), this, 0xbc3, 0, exception);
                        }
                    }
                    return obj2;
                }
                case 50:
                {
                    int count = reader.ReadEncodedInt32();
                    byte[] buffer = new byte[count];
                    if (count != 0)
                    {
                        reader.Read(buffer, 0, count);
                    }
                    object obj3 = null;
                    MemoryStream memoryStream = GetMemoryStream();
                    try
                    {
                        memoryStream.Write(buffer, 0, count);
                        memoryStream.Position = 0L;
                        IFormatter formatter = new BinaryFormatter();
                        obj3 = formatter.Deserialize(memoryStream);
                    }
                    catch (Exception exception2)
                    {
                        if (this._throwOnErrorDeserializing)
                        {
                            throw;
                        }
                        WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_Binary"), this, 0xbc3, 0, exception2);
                    }
                    finally
                    {
                        ReleaseMemoryStream(memoryStream);
                    }
                    return obj3;
                }
                case 60:
                {
                    Type type3 = this.DeserializeType(reader);
                    int num11 = reader.ReadEncodedInt32();
                    int num12 = reader.ReadEncodedInt32();
                    if (num12 > num11)
                    {
                        throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
                    }
                    Array array2 = Array.CreateInstance(type3, num11);
                    for (int n = 0; n < num12; n++)
                    {
                        int index = reader.ReadEncodedInt32();
                        if ((index >= num11) || (index < 0))
                        {
                            throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
                        }
                        array2.SetValue(this.DeserializeValue(reader), index);
                    }
                    return array2;
                }
                case 100:
                    return null;

                case 0x65:
                    return string.Empty;

                case 0x66:
                    return 0;

                case 0x67:
                    return true;

                case 0x68:
                    return false;

                default:
                    throw new InvalidOperationException(System.Web.SR.GetString("InvalidSerializedData"));
            }
            for (int i = 0; i < num4; i++)
            {
                dictionary.Add(this.DeserializeValue(reader), this.DeserializeValue(reader));
            }
            return dictionary;
        }
 private Type DeserializeType(SerializerBinaryReader reader)
 {
     byte num = reader.ReadByte();
     if (num == 0x2b)
     {
         int num2 = reader.ReadEncodedInt32();
         return (Type) this._typeList[num2];
     }
     string name = reader.ReadString();
     Type type = null;
     try
     {
         if (num == 0x2a)
         {
             type = HttpContext.SystemWebAssembly.GetType(name, true);
         }
         else
         {
             type = Type.GetType(name, true);
         }
     }
     catch (Exception exception)
     {
         if (this._throwOnErrorDeserializing)
         {
             throw;
         }
         WebBaseEvent.RaiseSystemEvent(System.Web.SR.GetString("Webevent_msg_OSF_Deserialization_Type", new object[] { name }), this, 0xbc3, 0, exception);
     }
     this.AddDeserializationTypeReference(type);
     return type;
 }
 private IndexedString DeserializeIndexedString(SerializerBinaryReader reader, byte token)
 {
     if (token == 0x1f)
     {
         int index = reader.ReadByte();
         return new IndexedString(this._stringList[index]);
     }
     string s = reader.ReadString();
     this.AddDeserializationStringReference(s);
     return new IndexedString(s);
 }
 public object Deserialize(Stream inputStream)
 {
     if (inputStream == null)
     {
         throw new ArgumentNullException("inputStream");
     }
     Exception innerException = null;
     this.InitializeDeserializer();
     SerializerBinaryReader reader = new SerializerBinaryReader(inputStream);
     try
     {
         if ((reader.ReadByte() == 0xff) && (reader.ReadByte() == 1))
         {
             return this.DeserializeValue(reader);
         }
     }
     catch (Exception exception2)
     {
         innerException = exception2;
     }
     throw new ArgumentException(System.Web.SR.GetString("InvalidSerializedData"), innerException);
 }