merge 99508:99630
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / BinaryFormatter.cs
1 // BinaryFormatter.cs
2 //
3 // Author:
4 //      Dick Porter (dick@ximian.com)
5 //  Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // (C) 2002 Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.IO;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Remoting.Messaging;
35 using System.Security.Permissions;
36
37 namespace System.Runtime.Serialization.Formatters.Binary {
38
39 #if NET_2_0
40         [ComVisible (true)]
41 #endif
42         public sealed class BinaryFormatter : IRemotingFormatter, IFormatter 
43         {
44 #if NET_2_0
45                 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Simple;
46 #else
47                 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Full;
48 #endif
49                 private SerializationBinder binder;
50                 private StreamingContext context;
51                 private ISurrogateSelector surrogate_selector;
52                 private FormatterTypeStyle type_format = FormatterTypeStyle.TypesAlways;
53                 
54 #if NET_1_1
55                 private TypeFilterLevel filter_level = TypeFilterLevel.Full;
56 #endif
57                 
58                 public BinaryFormatter()
59                 {
60                         surrogate_selector=null;
61                         context=new StreamingContext(StreamingContextStates.All);
62                 }
63                 
64                 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
65                 {
66                         surrogate_selector=selector;
67                         this.context=context;
68                 }
69
70                 public FormatterAssemblyStyle AssemblyFormat
71                 {
72                         get {
73                                 return(assembly_format);
74                         }
75                         set {
76                                 assembly_format=value;
77                         }
78                 }
79
80                 public SerializationBinder Binder
81                 {
82                         get {
83                                 return(binder);
84                         }
85                         set {
86                                 binder=value;
87                         }
88                 }
89
90                 public StreamingContext Context 
91                 {
92                         get {
93                                 return(context);
94                         }
95                         set {
96                                 context=value;
97                         }
98                 }
99                 
100                 public ISurrogateSelector SurrogateSelector 
101                 {
102                         get {
103                                 return(surrogate_selector);
104                         }
105                         set {
106                                 surrogate_selector=value;
107                         }
108                 }
109                 
110                 public FormatterTypeStyle TypeFormat 
111                 {
112                         get {
113                                 return(type_format);
114                         }
115                         set {
116                                 type_format=value;
117                         }
118                 }
119
120 #if NET_1_1
121 #if !NET_2_0
122                 [System.Runtime.InteropServices.ComVisible (false)]
123 #endif
124                 public TypeFilterLevel FilterLevel 
125                 {
126                         get { return filter_level; }
127                         set { filter_level = value; }
128                 }
129 #endif
130
131                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
132                 public object Deserialize (Stream serializationStream)
133                 {
134                         return NoCheckDeserialize (serializationStream, null);
135                 }
136
137                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
138                 public object Deserialize (Stream serializationStream, HeaderHandler handler) 
139                 {
140                         return NoCheckDeserialize (serializationStream, handler);
141                 }
142
143                 // shared by Deserialize and UnsafeDeserialize which both involve different security checks
144                 private object NoCheckDeserialize (Stream serializationStream, HeaderHandler handler)
145                 {
146                         if(serializationStream==null) 
147                         {
148                                 throw new ArgumentNullException("serializationStream");
149                         }
150                         if(serializationStream.CanSeek &&
151                                 serializationStream.Length==0) 
152                         {
153                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
154                         }
155
156                         BinaryReader reader = new BinaryReader (serializationStream);
157
158                         bool hasHeader;
159                         ReadBinaryHeader (reader, out hasHeader);
160
161                         // Messages are read using a special static method, which does not use ObjectReader
162                         // if it is not needed. This saves time and memory.
163
164                         BinaryElement elem = (BinaryElement) reader.PeekChar();
165
166                         if (elem == BinaryElement.MethodCall) {
167                                 return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, this);
168                         }
169                         else if (elem == BinaryElement.MethodResponse) {
170                                 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, this);
171                         }
172                         else {
173                                 ObjectReader serializer = new ObjectReader (this);
174
175                                 object result;
176                                 Header[] headers;
177                                 serializer.ReadObjectGraph (reader, hasHeader, out result, out headers);
178                                 if (handler != null) handler(headers);
179                                 return result;
180                         }
181                 }
182                 
183                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
184                 public object DeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
185                 {
186                         return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallMessage);
187                 }
188
189                 // shared by DeserializeMethodResponse and UnsafeDeserializeMethodResponse which both involve different security checks
190                 private object NoCheckDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
191                 {
192                         if(serializationStream==null) {
193                                 throw new ArgumentNullException("serializationStream");
194                         }
195                         if(serializationStream.CanSeek &&
196                            serializationStream.Length==0) {
197                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
198                         }
199
200                         BinaryReader reader = new BinaryReader (serializationStream);
201
202                         bool hasHeader;
203                         ReadBinaryHeader (reader, out hasHeader);
204                         return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallMessage, this);
205                 }
206
207                 public void Serialize(Stream serializationStream, object graph)
208                 {
209                         Serialize (serializationStream, graph, null);
210                 }
211
212                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
213                 public void Serialize(Stream serializationStream, object graph, Header[] headers)
214                 {
215                         if(serializationStream==null) {
216                                 throw new ArgumentNullException ("serializationStream");
217                         }
218
219                         BinaryWriter writer = new BinaryWriter (serializationStream);
220                         WriteBinaryHeader (writer, headers!=null);
221
222                         if (graph is IMethodCallMessage) {
223                                 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
224                         }
225                         else if (graph is IMethodReturnMessage)  {
226                                 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
227                         }
228                         else {
229                                 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context, assembly_format, type_format);
230                                 serializer.WriteObjectGraph (writer, graph, headers);
231                         }
232                         writer.Flush();
233                 }
234
235                 // faster version (under CAS) as this requires a LinkDemand versus full Demand (i.e. a stack-walk)
236                 // shouldn't be called unless the code is intended to be executed at full-trust
237                 [ComVisible (false)]
238                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
239                 public object UnsafeDeserialize (Stream serializationStream, HeaderHandler handler) 
240                 {
241                         return NoCheckDeserialize (serializationStream, handler);
242                 }
243                 
244                 // faster version (under CAS) as this requires a LinkDemand versus full Demand (i.e. a stack-walk)
245                 // shouldn't be called unless the code is intended to be executed at full-trust
246                 [ComVisible (false)]
247                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
248                 public object UnsafeDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
249                 {
250                         return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallmessage);
251                 }
252                 
253                 private void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
254                 {
255                         writer.Write ((byte)BinaryElement.Header);
256                         writer.Write ((int)1);
257                         if (hasHeaders) writer.Write ((int)2);
258                         else writer.Write ((int)-1);
259                         writer.Write ((int)1);
260                         writer.Write ((int)0);
261                 }
262
263                 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
264                 {
265                         reader.ReadByte();
266                         reader.ReadInt32();
267                         int val = reader.ReadInt32();
268                         hasHeaders = (val==2);
269                         reader.ReadInt32();
270                         reader.ReadInt32();
271                 }
272         }
273 }