4 // Dick Porter (dick@ximian.com)
5 // Lluis Sanchez Gual (lluis@ideary.com)
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Remoting.Messaging;
35 using System.Security.Permissions;
37 namespace System.Runtime.Serialization.Formatters.Binary {
40 public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
42 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Simple;
43 private SerializationBinder binder;
44 private StreamingContext context;
45 private ISurrogateSelector surrogate_selector;
46 private FormatterTypeStyle type_format = FormatterTypeStyle.TypesAlways;
47 private TypeFilterLevel filter_level = TypeFilterLevel.Full;
49 public BinaryFormatter()
51 surrogate_selector=null;
52 context=new StreamingContext(StreamingContextStates.All);
55 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
57 surrogate_selector=selector;
61 public FormatterAssemblyStyle AssemblyFormat
64 return(assembly_format);
67 assembly_format=value;
71 public SerializationBinder Binder
81 public StreamingContext Context
91 public ISurrogateSelector SurrogateSelector
94 return(surrogate_selector);
97 surrogate_selector=value;
101 public FormatterTypeStyle TypeFormat
111 public TypeFilterLevel FilterLevel
113 get { return filter_level; }
114 set { filter_level = value; }
117 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
118 public object Deserialize (Stream serializationStream)
120 return NoCheckDeserialize (serializationStream, null);
123 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
124 public object Deserialize (Stream serializationStream, HeaderHandler handler)
126 return NoCheckDeserialize (serializationStream, handler);
129 // shared by Deserialize and UnsafeDeserialize which both involve different security checks
130 private object NoCheckDeserialize (Stream serializationStream, HeaderHandler handler)
132 if(serializationStream==null)
134 throw new ArgumentNullException("serializationStream");
136 if(serializationStream.CanSeek &&
137 serializationStream.Length==0)
139 throw new SerializationException("serializationStream supports seeking, but its length is 0");
142 BinaryReader reader = new BinaryReader (serializationStream);
145 ReadBinaryHeader (reader, out hasHeader);
147 // Messages are read using a special static method, which does not use ObjectReader
148 // if it is not needed. This saves time and memory.
150 BinaryElement elem = (BinaryElement) reader.Read ();
152 if (elem == BinaryElement.MethodCall) {
153 return MessageFormatter.ReadMethodCall (elem, reader, hasHeader, handler, this);
155 else if (elem == BinaryElement.MethodResponse) {
156 return MessageFormatter.ReadMethodResponse (elem, reader, hasHeader, handler, null, this);
159 ObjectReader serializer = new ObjectReader (this);
163 serializer.ReadObjectGraph (elem, reader, hasHeader, out result, out headers);
164 if (handler != null) handler(headers);
169 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
170 public object DeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
172 return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallMessage);
175 // shared by DeserializeMethodResponse and UnsafeDeserializeMethodResponse which both involve different security checks
176 private object NoCheckDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
178 if(serializationStream==null) {
179 throw new ArgumentNullException("serializationStream");
181 if(serializationStream.CanSeek &&
182 serializationStream.Length==0) {
183 throw new SerializationException("serializationStream supports seeking, but its length is 0");
186 BinaryReader reader = new BinaryReader (serializationStream);
189 ReadBinaryHeader (reader, out hasHeader);
190 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallMessage, this);
193 public void Serialize(Stream serializationStream, object graph)
195 Serialize (serializationStream, graph, null);
198 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
199 public void Serialize(Stream serializationStream, object graph, Header[] headers)
201 if(serializationStream==null) {
202 throw new ArgumentNullException ("serializationStream");
205 BinaryWriter writer = new BinaryWriter (serializationStream);
206 WriteBinaryHeader (writer, headers!=null);
208 if (graph is IMethodCallMessage) {
209 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
211 else if (graph is IMethodReturnMessage) {
212 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
215 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context, assembly_format, type_format);
216 serializer.WriteObjectGraph (writer, graph, headers);
221 // faster version (under CAS) as this requires a LinkDemand versus full Demand (i.e. a stack-walk)
222 // shouldn't be called unless the code is intended to be executed at full-trust
224 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
225 public object UnsafeDeserialize (Stream serializationStream, HeaderHandler handler)
227 return NoCheckDeserialize (serializationStream, handler);
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
233 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
234 public object UnsafeDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallMessage)
236 return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallMessage);
239 private void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
241 writer.Write ((byte)BinaryElement.Header);
242 writer.Write ((int)1);
243 if (hasHeaders) writer.Write ((int)2);
244 else writer.Write ((int)-1);
245 writer.Write ((int)1);
246 writer.Write ((int)0);
249 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
253 int val = reader.ReadInt32();
254 hasHeaders = (val==2);