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 {
39 public sealed class BinaryFormatter : IRemotingFormatter, IFormatter
42 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Simple;
44 private FormatterAssemblyStyle assembly_format = FormatterAssemblyStyle.Full;
46 private SerializationBinder binder;
47 private StreamingContext context;
48 private ISurrogateSelector surrogate_selector;
49 private FormatterTypeStyle type_format = FormatterTypeStyle.TypesAlways;
52 private TypeFilterLevel filter_level = TypeFilterLevel.Full;
55 public BinaryFormatter()
57 surrogate_selector=null;
58 context=new StreamingContext(StreamingContextStates.All);
61 public BinaryFormatter(ISurrogateSelector selector, StreamingContext context)
63 surrogate_selector=selector;
67 public FormatterAssemblyStyle AssemblyFormat
70 return(assembly_format);
73 assembly_format=value;
77 public SerializationBinder Binder
87 public StreamingContext Context
97 public ISurrogateSelector SurrogateSelector
100 return(surrogate_selector);
103 surrogate_selector=value;
107 public FormatterTypeStyle TypeFormat
118 [System.Runtime.InteropServices.ComVisible (false)]
119 public TypeFilterLevel FilterLevel
121 get { return filter_level; }
122 set { filter_level = value; }
126 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
127 public object Deserialize (Stream serializationStream)
129 return NoCheckDeserialize (serializationStream, null);
132 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
133 public object Deserialize (Stream serializationStream, HeaderHandler handler)
135 return NoCheckDeserialize (serializationStream, handler);
138 // shared by Deserialize and UnsafeDeserialize which both involve different security checks
139 private object NoCheckDeserialize (Stream serializationStream, HeaderHandler handler)
141 if(serializationStream==null)
143 throw new ArgumentNullException("serializationStream");
145 if(serializationStream.CanSeek &&
146 serializationStream.Length==0)
148 throw new SerializationException("serializationStream supports seeking, but its length is 0");
151 BinaryReader reader = new BinaryReader (serializationStream);
154 ReadBinaryHeader (reader, out hasHeader);
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.
159 BinaryElement elem = (BinaryElement) reader.PeekChar();
161 if (elem == BinaryElement.MethodCall) {
162 return MessageFormatter.ReadMethodCall (reader, hasHeader, handler, this);
164 else if (elem == BinaryElement.MethodResponse) {
165 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, null, this);
168 ObjectReader serializer = new ObjectReader (this);
172 serializer.ReadObjectGraph (reader, hasHeader, out result, out headers);
173 if (handler != null) handler(headers);
178 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
179 public object DeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
181 return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallmessage);
184 // shared by DeserializeMethodResponse and UnsafeDeserializeMethodResponse which both involve different security checks
185 private object NoCheckDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
187 if(serializationStream==null) {
188 throw new ArgumentNullException("serializationStream");
190 if(serializationStream.CanSeek &&
191 serializationStream.Length==0) {
192 throw new SerializationException("serializationStream supports seeking, but its length is 0");
195 BinaryReader reader = new BinaryReader (serializationStream);
198 ReadBinaryHeader (reader, out hasHeader);
199 return MessageFormatter.ReadMethodResponse (reader, hasHeader, handler, methodCallmessage, this);
202 public void Serialize(Stream serializationStream, object graph)
204 Serialize (serializationStream, graph, null);
207 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
208 public void Serialize(Stream serializationStream, object graph, Header[] headers)
210 if(serializationStream==null) {
211 throw new ArgumentNullException ("serializationStream");
214 BinaryWriter writer = new BinaryWriter (serializationStream);
215 WriteBinaryHeader (writer, headers!=null);
217 if (graph is IMethodCallMessage) {
218 MessageFormatter.WriteMethodCall (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
220 else if (graph is IMethodReturnMessage) {
221 MessageFormatter.WriteMethodResponse (writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
224 ObjectWriter serializer = new ObjectWriter (surrogate_selector, context, assembly_format, type_format);
225 serializer.WriteObjectGraph (writer, graph, headers);
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 UnsafeDeserialize (Stream serializationStream, HeaderHandler handler)
236 return NoCheckDeserialize (serializationStream, handler);
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
242 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
243 public object UnsafeDeserializeMethodResponse (Stream serializationStream, HeaderHandler handler, IMethodCallMessage methodCallmessage)
245 return NoCheckDeserializeMethodResponse (serializationStream, handler, methodCallmessage);
248 private void WriteBinaryHeader (BinaryWriter writer, bool hasHeaders)
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);
258 private void ReadBinaryHeader (BinaryReader reader, out bool hasHeaders)
262 int val = reader.ReadInt32();
263 hasHeaders = (val==2);