LosFormatter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // System.Web.UI.LosFormatter
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  10. //
  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:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  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.
  29. //
  30. using System.Configuration;
  31. using System.IO;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. using System.Web.Util;
  35. namespace System.Web.UI {
  36. // CAS - no InheritanceDemand here as the class is sealed
  37. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public sealed class LosFormatter
  39. {
  40. ObjectStateFormatter osf;
  41. public LosFormatter ()
  42. {
  43. osf = new ObjectStateFormatter ();
  44. }
  45. public LosFormatter (bool enableMac, string macKeyModifier)
  46. {
  47. osf = new ObjectStateFormatter ();
  48. if (enableMac && !String.IsNullOrEmpty (macKeyModifier)) {
  49. SetMacKey (Convert.FromBase64String (macKeyModifier));
  50. }
  51. }
  52. public LosFormatter (bool enableMac, byte[] macKeyModifier)
  53. {
  54. osf = new ObjectStateFormatter ();
  55. if (enableMac && (macKeyModifier != null)) {
  56. SetMacKey (macKeyModifier);
  57. }
  58. }
  59. private void SetMacKey (byte[] macKeyModifier)
  60. {
  61. try {
  62. osf.Section.ValidationKey = MachineKeySectionUtils.GetHexString (macKeyModifier);
  63. }
  64. catch (ArgumentException) {
  65. }
  66. catch (ConfigurationErrorsException) {
  67. // bad key (e.g. size), default key will be used
  68. }
  69. }
  70. public object Deserialize (Stream stream)
  71. {
  72. if (stream == null)
  73. throw new ArgumentNullException ("stream");
  74. #if NET_4_0
  75. using (StreamReader sr = new StreamReader (stream)) {
  76. return Deserialize (sr.ReadToEnd ());
  77. }
  78. #else
  79. long streamLength = -1;
  80. if (stream.CanSeek)
  81. streamLength = stream.Length;
  82. MemoryStream ms = null;
  83. if (streamLength != -1 && (stream is MemoryStream) && stream.Position == 0) {
  84. // We save allocating a new stream and reading in this case.
  85. ms = (MemoryStream) stream;
  86. } else {
  87. byte [] bytes = new byte [streamLength >= 0 ? streamLength : 2048];
  88. ms = new MemoryStream ();
  89. int n;
  90. while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
  91. ms.Write (bytes, 0, n);
  92. streamLength = ms.Length;
  93. }
  94. string b64 = Encoding.ASCII.GetString (ms.GetBuffer (),
  95. 0, (int) streamLength);
  96. return Deserialize (b64);
  97. #endif
  98. }
  99. public object Deserialize (TextReader input)
  100. {
  101. if (input == null)
  102. throw new ArgumentNullException ("input");
  103. return Deserialize (input.ReadToEnd ());
  104. }
  105. public object Deserialize (string input)
  106. {
  107. if (input == null)
  108. return null;
  109. return osf.Deserialize (input);
  110. }
  111. internal string SerializeToBase64 (object value)
  112. {
  113. return osf.Serialize (value);
  114. }
  115. public void Serialize (Stream stream, object value)
  116. {
  117. if (stream == null)
  118. throw new ArgumentNullException ("stream");
  119. #if NET_4_0
  120. if (!stream.CanSeek)
  121. throw new NotSupportedException ();
  122. #endif
  123. string b64 = SerializeToBase64 (value);
  124. byte [] bytes = Encoding.ASCII.GetBytes (b64);
  125. stream.Write (bytes, 0, bytes.Length);
  126. }
  127. public void Serialize (TextWriter output, object value)
  128. {
  129. if (output == null)
  130. throw new ArgumentNullException ("output");
  131. output.Write (SerializeToBase64 (value));
  132. }
  133. }
  134. }