1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 //
5 // ==--==
6 
7 // WARNING:
8 //
9 // This is just an IObjectReference proxy for the former MLang Encodings (V1.1)
10 // We keep the old name now even for the Whidbey V2.0 IObjectReference because it also
11 // works with the Everett V1.1 version.
12 namespace System.Text
13 {
14     using System;
15     using System.Runtime.Serialization;
16     using System.Security.Permissions;
17     using System.Diagnostics.Contracts;
18 
19     /*=================================MLangCodePageEncoding==================================
20     ** This class is here only to deserialize the MLang classes from Everett (V1.1) into
21     ** Appropriate Whidbey (V2.0) objects.  We also serialize the Whidbey classes
22     ** using this proxy since we pretty much need one anyway and that solves Whidbey
23     ** to Everett compatibility as well.
24     ==============================================================================*/
25 
26     [Serializable]
27     internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
28     {
29         // Temp stuff
30         [NonSerialized]
31         private int m_codePage;
32         [NonSerialized]
33         private bool m_isReadOnly;
34         [NonSerialized]
35         private bool m_deserializedFromEverett = false;
36 
37         [NonSerialized]
38         private EncoderFallback encoderFallback = null;
39         [NonSerialized]
40         private DecoderFallback decoderFallback = null;
41 
42         // Might need this when GetRealObjecting
43         [NonSerialized]
44         private Encoding realEncoding = null;
45 
46         // Constructor called by serialization.
MLangCodePageEncoding(SerializationInfo info, StreamingContext context)47         internal MLangCodePageEncoding(SerializationInfo info, StreamingContext context)
48         {
49             // Any info?
50             if (info==null) throw new ArgumentNullException("info");
51             Contract.EndContractBlock();
52 
53             // All versions have a code page
54             this.m_codePage = (int)info.GetValue("m_codePage", typeof(int));
55 
56             // See if we have a code page
57             try
58             {
59                 //
60                 // Try Whidbey V2.0 Fields
61                 //
62                 this.m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
63 
64                 this.encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
65                 this.decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
66             }
67             catch (SerializationException)
68             {
69                 //
70                 // Didn't have Whidbey things, must be Everett
71                 //
72                 this.m_deserializedFromEverett = true;
73 
74                 // May as well be read only
75                 this.m_isReadOnly = true;
76             }
77         }
78 
79         // Just get it from GetEncoding
80         [System.Security.SecurityCritical]  // auto-generated
GetRealObject(StreamingContext context)81         public Object GetRealObject(StreamingContext context)
82         {
83             // Get our encoding (Note: This has default fallbacks for readonly and everett cases)
84             this.realEncoding = Encoding.GetEncoding(this.m_codePage);
85 
86             // If its read only then it uses default fallbacks, otherwise pick up the new ones
87             // Otherwise we want to leave the new one read only
88             if (!this.m_deserializedFromEverett && !this.m_isReadOnly)
89             {
90                 this.realEncoding = (Encoding)this.realEncoding.Clone();
91                 this.realEncoding.EncoderFallback = this.encoderFallback;
92                 this.realEncoding.DecoderFallback = this.decoderFallback;
93             }
94 
95             return this.realEncoding;
96         }
97 
98 #if FEATURE_SERIALIZATION
99         // ISerializable implementation
100         [System.Security.SecurityCritical]  // auto-generated_required
ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)101         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
102         {
103             // We cannot ever call this.
104             Contract.Assert(false, "Didn't expect to make it to MLangCodePageEncoding ISerializable.GetObjectData");
105             throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
106         }
107 #endif
108 
109         // Same problem with the Encoder, this only happens with Everett Encoders
110         [Serializable]
111         internal sealed class MLangEncoder : ISerializable, IObjectReference
112         {
113             // Might need this when GetRealObjecting
114             [NonSerialized]
115             private Encoding realEncoding = null;
116 
117             // Constructor called by serialization, have to handle deserializing from Everett
MLangEncoder(SerializationInfo info, StreamingContext context)118             internal MLangEncoder(SerializationInfo info, StreamingContext context)
119             {
120                 // Any info?
121                 if (info==null) throw new ArgumentNullException("info");
122                 Contract.EndContractBlock();
123 
124                 this.realEncoding = (Encoding)info.GetValue("m_encoding", typeof(Encoding));
125             }
126 
127             // Just get it from GetEncoder
128             [System.Security.SecurityCritical]  // auto-generated
GetRealObject(StreamingContext context)129             public Object GetRealObject(StreamingContext context)
130             {
131                 return this.realEncoding.GetEncoder();
132             }
133 
134 #if FEATURE_SERIALIZATION
135             // ISerializable implementation, get data for this object
136             [System.Security.SecurityCritical]  // auto-generated_required
ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)137             void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
138             {
139                 // We cannot ever call this.
140                 Contract.Assert(false, "Didn't expect to make it to MLangCodePageEncoding.MLangEncoder.GetObjectData");
141                 throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
142             }
143 #endif
144         }
145 
146 
147         // Same problem with the Decoder, this only happens with Everett Decoders
148         [Serializable]
149         internal sealed class MLangDecoder : ISerializable, IObjectReference
150         {
151             // Might need this when GetRealObjecting
152             [NonSerialized]
153             private Encoding realEncoding = null;
154 
155             // Constructor called by serialization, have to handle deserializing from Everett
MLangDecoder(SerializationInfo info, StreamingContext context)156             internal MLangDecoder(SerializationInfo info, StreamingContext context)
157             {
158                 // Any info?
159                 if (info==null) throw new ArgumentNullException("info");
160                 Contract.EndContractBlock();
161 
162                 this.realEncoding = (Encoding)info.GetValue("m_encoding", typeof(Encoding));
163             }
164 
165             // Just get it from GetDecoder
166             [System.Security.SecurityCritical]  // auto-generated
GetRealObject(StreamingContext context)167             public Object GetRealObject(StreamingContext context)
168             {
169                 return this.realEncoding.GetDecoder();
170             }
171 
172 #if FEATURE_SERIALIZATION
173             // ISerializable implementation, get data for this object
174             [System.Security.SecurityCritical]  // auto-generated_required
ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)175             void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
176             {
177                 // We cannot ever call this.
178                 Contract.Assert(false, "Didn't expect to make it to MLangCodePageEncoding.MLangDecoder.GetObjectData");
179                 throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
180             }
181 #endif
182         }
183     }
184 }
185