2 // System.Web.UI.LosFormatter
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Configuration;
33 using System.Security.Permissions;
35 using System.Web.Configuration;
37 namespace System.Web.UI {
39 // CAS - no InheritanceDemand here as the class is sealed
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class LosFormatter
43 ObjectStateFormatter osf;
45 public LosFormatter ()
47 osf = new ObjectStateFormatter ();
50 public LosFormatter (bool enableMac, string macKeyModifier)
52 osf = new ObjectStateFormatter ();
53 if (enableMac && !String.IsNullOrEmpty (macKeyModifier)) {
54 SetMacKey (Convert.FromBase64String (macKeyModifier));
58 public LosFormatter (bool enableMac, byte[] macKeyModifier)
60 osf = new ObjectStateFormatter ();
61 if (enableMac && (macKeyModifier != null)) {
62 SetMacKey (macKeyModifier);
66 private void SetMacKey (byte[] macKeyModifier)
69 osf.Section.ValidationKey = MachineKeySectionUtils.GetHexString (macKeyModifier);
71 catch (ArgumentException) {
73 catch (ConfigurationErrorsException) {
74 // bad key (e.g. size), default key will be used
78 public object Deserialize (Stream stream)
81 throw new ArgumentNullException ("stream");
83 using (StreamReader sr = new StreamReader (stream)) {
84 return Deserialize (sr.ReadToEnd ());
87 long streamLength = -1;
89 streamLength = stream.Length;
90 MemoryStream ms = null;
91 if (streamLength != -1 && (stream is MemoryStream) && stream.Position == 0) {
92 // We save allocating a new stream and reading in this case.
93 ms = (MemoryStream) stream;
95 byte [] bytes = new byte [streamLength >= 0 ? streamLength : 2048];
96 ms = new MemoryStream ();
98 while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
99 ms.Write (bytes, 0, n);
100 streamLength = ms.Length;
102 string b64 = Encoding.ASCII.GetString (ms.GetBuffer (),
103 0, (int) streamLength);
104 return Deserialize (b64);
108 public object Deserialize (TextReader input)
111 throw new ArgumentNullException ("input");
113 return Deserialize (input.ReadToEnd ());
116 public object Deserialize (string input)
121 return osf.Deserialize (input);
124 internal string SerializeToBase64 (object value)
126 return osf.Serialize (value);
129 public void Serialize (Stream stream, object value)
132 throw new ArgumentNullException ("stream");
135 throw new NotSupportedException ();
137 string b64 = SerializeToBase64 (value);
138 byte [] bytes = Encoding.ASCII.GetBytes (b64);
139 stream.Write (bytes, 0, bytes.Length);
142 public void Serialize (TextWriter output, object value)
145 throw new ArgumentNullException ("output");
147 output.Write (SerializeToBase64 (value));