New test.
[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         public sealed class BinaryFormatter : IRemotingFormatter, IFormatter 
40         {
41 #if NET_2_0
42                 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Simple;
43 #else
44                 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Full;
45 #endif
46                 private SerializationBinder binder;
47                 private StreamingContext context;
48                 private ISurrogateSelector surrogate_selector;
49                 private FormatterTypeStyle type_format = FormatterTypeStyle.TypesAlways;
50                 
51 #if NET_1_1
52                 private TypeFilterLevel filter_level = TypeFilterLevel.Full;
53 #endif
54                 
55                 public BinaryFormatter()
56                 {
57                         surrogate_selector=null;
58                         context=new StreamingContext(StreamingContextStates.All);
59                 }
60                 
61                 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
62                 {
63                         surrogate_selector=selector;
64                         this.context=context;
65                 }
66
67                 public FormatterAssemblyStyle AssemblyFormat
68                 {
69                         get {
70                                 return(assembly_format);
71                         }
72                         set {
73                                 assembly_format=value;
74                         }
75                 }
76
77                 public SerializationBinder Binder
78                 {
79                         get {
80                                 return(binder);
81                         }
82                         set {
83                                 binder=value;
84                         }
85                 }
86
87                 public StreamingContext Context 
88                 {
89                         get {
90                                 return(context);
91                         }
92                         set {
93                                 context=value;
94                         }
95                 }
96                 
97                 public ISurrogateSelector SurrogateSelector 
98                 {
99                         get {
100                                 return(surrogate_selector);
101                         }
102                         set {
103                                 surrogate_selector=value;
104                         }
105                 }
106                 
107                 public FormatterTypeStyle TypeFormat 
108                 {
109                         get {
110                                 return(type_format);
111                         }
112                         set {
113                                 type_format=value;
114                         }
115                 }
116
117 #if NET_1_1
118                 [System.Runtime.InteropServices.ComVisible (false)]
119                 public TypeFilterLevel FilterLevel 
120                 {
121                         get { return filter_level; }
122                         set { filter_level = value; }
123                 }
124 #endif
125
126                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
127                 public object Deserialize (Stream serializationStream)
128                 {
129                         return NoCheckDeserialize (serializationStream, null);
130                 }
131
132                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
133                 public object Deserialize (Stream serializationStream, HeaderHandler handler) 
134                 {
135                         return NoCheckDeserialize (serializationStream, handler);
136                 }
137
138                 // shared by Deserialize and UnsafeDeserialize which both involve different security checks
139                 private object NoCheckDeserialize (Stream serializationStream, HeaderHandler handler)
140                 {
141                         if(serializationStream==null) 
142                         {
143                                 throw new ArgumentNullException("serializationStream");
144                         }
145                         if(serializationStream.CanSeek &&
146                                 serializationStream.Length==0) 
147                         {
148                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
149                         }
150
151                         BinaryReader reader = new BinaryReader (serializationStream);
152
153                         bool hasHeader;
154                         ReadBinaryHeader (reader, out hasHeader);
155
156                         // Messages are read using a special static method, which does not use ObjectReader
157                         // if it is not needed. This saves time and memory.
158
159                         BinaryElement elem = (BinaryElement) reader.PeekChar();
160
161                         if (elem == BinaryElement.MethodCall) {
162                                 return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, this);
163                         }
164                         else if (elem == BinaryElement.MethodResponse) {
165                                 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, this);
166                         }
167                         else {
168                                 ObjectReader serializer = new ObjectReader (this);
169
170                                 object result;
171                                 Header[] headers;
172                                 serializer.ReadObjectGraph (reader, hasHeader, out result, out headers);
173                                 if (handler != null) handler(headers);
174                                 return result;
175                         }
176                 }
177                 
178                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
179                 public object DeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
180                 {
181                         return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallmessage);
182                 }
183
184                 // shared by DeserializeMethodResponse and UnsafeDeserializeMethodResponse which both involve different security checks
185                 private object NoCheckDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
186                 {
187                         if(serializationStream==null) {
188                                 throw new ArgumentNullException("serializationStream");
189                         }
190                         if(serializationStream.CanSeek &&
191                            serializationStream.Length==0) {
192                                 throw new SerializationException("serializationStream supports seeking, but its length is 0");
193                         }
194
195                         BinaryReader reader = new BinaryReader (serializationStream);
196
197                         bool hasHeader;
198                         ReadBinaryHeader (reader, out hasHeader);
199                         return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallmessage, this);
200                 }
201
202                 public void Serialize(Stream serializationStream, object graph)
203                 {
204                         Serialize (serializationStream, graph, null);
205                 }
206
207                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
208                 public void Serialize(Stream serializationStream, object graph, Header[] headers)
209                 {
210                         if(serializationStream==null) {
211                                 throw new ArgumentNullException ("serializationStream");
212                         }
213
214                         BinaryWriter writer = new BinaryWriter (serializationStream);
215                         WriteBinaryHeader (writer, headers!=null);
216
217                         if (graph is IMethodCallMessage) {
218                                 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
219                         }
220                         else if (graph is IMethodReturnMessage)  {
221                                 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
222                         }
223                         else {
224                                 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context, assembly_format, type_format);
225                                 serializer.WriteObjectGraph (writer, graph, headers);
226                         }
227                         writer.Flush();
228                 }
229
230                 // faster version (under CAS) as this requires a LinkDemand versus full Demand (i.e. a stack-walk)
231                 // shouldn't be called unless the code is intended to be executed at full-trust
232                 [ComVisible (false)]
233                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
234                 public object UnsafeDeserialize (Stream serializationStream, HeaderHandler handler) 
235                 {
236                         return NoCheckDeserialize (serializationStream, handler);
237                 }
238                 
239                 // faster version (under CAS) as this requires a LinkDemand versus full Demand (i.e. a stack-walk)
240                 // shouldn't be called unless the code is intended to be executed at full-trust
241                 [ComVisible (false)]
242                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
243                 public object UnsafeDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
244                 {
245                         return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallmessage);
246                 }
247                 
248                 private void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
249                 {
250                         writer.Write ((byte)BinaryElement.Header);
251                         writer.Write ((int)1);
252                         if (hasHeaders) writer.Write ((int)2);
253                         else writer.Write ((int)-1);
254                         writer.Write ((int)1);
255                         writer.Write ((int)0);
256                 }
257
258                 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
259                 {
260                         reader.ReadByte();
261                         reader.ReadInt32();
262                         int val = reader.ReadInt32();
263                         hasHeaders = (val==2);
264                         reader.ReadInt32();
265                         reader.ReadInt32();
266                 }
267         }
268 }