File: DeflateCookieTransform.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (124 lines) | stat: -rw-r--r-- 4,936 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.IdentityModel
{
    using System.Diagnostics;
    using System.IdentityModel.Diagnostics;
    using System.IdentityModel.Tokens;
    using System.IO;
    using System.IO.Compression;

    /// <summary>
    /// Provides cookie compression using <see cref="DeflateStream"/>.
    /// </summary>
    public sealed class DeflateCookieTransform : CookieTransform
    {
        int _maxDecompressedSize = 1024 * 1024;     // Default maximum of 1MB

        /// <summary>
        /// Creates a new instance of <see cref="DeflateCookieTransform"/>.
        /// </summary>
        public DeflateCookieTransform()
        {
        }

        /// <summary>
        /// Gets or sets the maximum size (in bytes) of a decompressed cookie.
        /// </summary>
        public int MaxDecompressedSize
        {
            get { return _maxDecompressedSize; }
            set { _maxDecompressedSize = value; }
        }

        /// <summary>
        /// Inflates data.
        /// </summary>
        /// <param name="encoded">Data previously returned from <see cref="Encode"/></param>
        /// <returns>Decoded data.</returns>
        /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception>
        /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception>
        /// <exception cref="SecurityTokenException">The decompressed length is larger than MaxDecompressedSize.</exception>
        public override byte[] Decode( byte[] encoded )
        {
            if ( null == encoded )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "encoded" );
            }

            if ( 0 == encoded.Length )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( "encoded", SR.GetString( SR.ID6045 ) );
            }

            MemoryStream compressedStream = new MemoryStream( encoded );
            using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Decompress, false ) )
            {
                using ( MemoryStream decompressedStream = new MemoryStream() )
                {
                    byte[] buffer = new byte[1024];
                    int bytesRead;

                    do
                    {
                        bytesRead = deflateStream.Read( buffer, 0, buffer.Length );
                        decompressedStream.Write( buffer, 0, bytesRead );

                        // check length against configured maximum to prevevent decompression bomb attacks
                        if ( decompressedStream.Length > MaxDecompressedSize )
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityTokenException( SR.GetString( SR.ID1068, MaxDecompressedSize ) ) );
                        }
                    } while ( bytesRead > 0 );

                    return decompressedStream.ToArray();
                }
            }
        }

        /// <summary>
        /// Deflates data.
        /// </summary>
        /// <param name="value">Data to be compressed.</param>
        /// <returns>Compressed data.</returns>
        /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception>
        /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception>
        public override byte[] Encode( byte[] value )
        {
            if ( null == value )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "value" );
            }

            if ( 0 == value.Length )
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( "value", SR.GetString( SR.ID6044 ) );
            }

            using ( MemoryStream compressedStream = new MemoryStream() )
            {
                using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Compress, true ) )
                {
                    deflateStream.Write( value, 0, value.Length );
                }

                byte[] encoded = compressedStream.ToArray();

                if ( DiagnosticUtility.ShouldTrace( TraceEventType.Information ) )
                {
                    TraceUtility.TraceEvent( 
                            TraceEventType.Information,
                            TraceCode.Diagnostics,
                            SR.GetString(SR.TraceDeflateCookieEncode),
                            new DeflateCookieTraceRecord( value.Length, encoded.Length ),
                            null,
                            null );
                }

                return encoded;
            }
        }
    }
}