Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Base64Encoder.cs « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e5db862d303f8a77e08410bab42f9f9ebc198df (plain)
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
125

//------------------------------------------------------------------------------
// <copyright file="Base64Encoder.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------

using System.Text;
using System.Diagnostics;

namespace System.Xml {

    internal abstract partial class Base64Encoder {

        byte[]  leftOverBytes;
        int     leftOverBytesCount;
        char[]  charsLine;

        internal const int Base64LineSize = 76;  
        internal const int LineSizeInBytes = Base64LineSize/4*3;    

        internal Base64Encoder() {
            charsLine = new char[Base64LineSize];
        }

        internal abstract void WriteChars( char[] chars, int index, int count );

        internal void Encode( byte[] buffer, int index, int count ) {
            if ( buffer == null ) {
                throw new ArgumentNullException( "buffer" );
            }
            if ( index < 0 ) {
                throw new ArgumentOutOfRangeException( "index" );
            }
            if ( count < 0 ) {
                throw new ArgumentOutOfRangeException( "count" );
            }
            if ( count > buffer.Length - index ) {
                throw new ArgumentOutOfRangeException( "count" );
            }

            // encode left-over buffer
            if( leftOverBytesCount > 0 ) {
                int i = leftOverBytesCount;
                while ( i < 3 && count > 0 ) {
                    leftOverBytes[i++] = buffer[index++];
                    count--;
                }

                // the total number of buffer we have is less than 3 -> return
                if ( count == 0 && i < 3 ) {
                    leftOverBytesCount = i;
                    return;
                }

                // encode the left-over buffer and write out
                int leftOverChars = Convert.ToBase64CharArray( leftOverBytes, 0, 3, charsLine, 0 );
                WriteChars( charsLine, 0, leftOverChars );
            }

            // store new left-over buffer
            leftOverBytesCount = count % 3;
            if ( leftOverBytesCount > 0 )  {
                count -= leftOverBytesCount;
                if ( leftOverBytes == null ) {
                    leftOverBytes = new byte[3];
                }
                for( int i = 0; i < leftOverBytesCount; i++ ) {
                    leftOverBytes[i] = buffer[ index + count + i ];
                }
            }

            // encode buffer in 76 character long chunks
            int endIndex = index + count;
            int chunkSize = LineSizeInBytes;
            while( index < endIndex ) {
                if ( index + chunkSize > endIndex ) {
                    chunkSize = endIndex - index;
                }
                int charCount = Convert.ToBase64CharArray( buffer, index, chunkSize, charsLine, 0 );
                WriteChars( charsLine, 0, charCount );
    
                index += chunkSize;
            }
        }

        internal void Flush() {
            if ( leftOverBytesCount > 0 ) {
                int leftOverChars = Convert.ToBase64CharArray( leftOverBytes, 0, leftOverBytesCount, charsLine, 0 );
                WriteChars( charsLine, 0, leftOverChars );
                leftOverBytesCount = 0;
            }
        }
    } 

    internal partial class XmlRawWriterBase64Encoder : Base64Encoder {

        XmlRawWriter rawWriter;
        
        internal XmlRawWriterBase64Encoder( XmlRawWriter rawWriter ) {
            this.rawWriter = rawWriter;
        }

        internal override void WriteChars( char[] chars, int index, int count ) {
            rawWriter.WriteRaw( chars, index, count );
        }
    }

#if !SILVERLIGHT || FEATURE_NETCORE
    internal partial class XmlTextWriterBase64Encoder : Base64Encoder {

        XmlTextEncoder xmlTextEncoder;
        
        internal XmlTextWriterBase64Encoder( XmlTextEncoder xmlTextEncoder ) {
            this.xmlTextEncoder = xmlTextEncoder;
        }

        internal override void WriteChars( char[] chars, int index, int count ) {
            xmlTextEncoder.WriteRaw( chars, index, count );
        }
    }
#endif

}