[bcl] Rename variables to avoid conflict with later renames
[mono.git] / mcs / class / System.Web / System.Web.Mail / ToUUEncodingTransform.cs
1 //
2 // System.Web.Mail.ToUUEncodingTransform.cs
3 //
4 // Author(s):
5 //   Per Arneng <pt99par@student.bth.se>
6 //
7 //
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29 using System;
30 using System.Security.Cryptography;
31
32 namespace System.Web.Mail {
33
34     // This class transforms blocks of plaintext to UU encoding
35     internal class ToUUEncodingTransform : ICryptoTransform {
36         
37         public int InputBlockSize { get { return 45; } }
38         public int OutputBlockSize { get { return 61; } }
39
40         public bool CanTransformMultipleBlocks { get { return true; } }
41         public bool CanReuseTransform { get { return true; } }
42     
43         // transforms a block of bytes to UU encoding
44         public int TransformBlock( byte[] inputBuffer,
45                                    int inputOffset,
46                                    int inputCount,
47                                    byte[] outputBuffer,
48                                    int outputOffset
49                                    ) {
50             
51             // write the line length length+0x20
52             outputBuffer[ 0 ] = (byte)'M';
53             
54             // transform the block 3bytes at a time
55             for( int i=0;i<15;i++ ) {
56                 
57                 TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
58                                   outputBuffer , outputOffset + i * 4 + 1);
59             
60             }
61             
62             
63             return OutputBlockSize;
64         }
65         
66         // make a final uu transformations
67         public byte[] TransformFinalBlock(byte[] inputBuffer,
68                                           int inputOffset,
69                                           int inputCount
70                                           ) {
71             
72             // calculate how many 4-byte blocks there are
73             int tripletBlocks = inputCount / 3 + 1;
74             
75             // create a new buffer and copy the input data into that
76             byte[] buffer = new byte[ tripletBlocks * 3 ];
77             Buffer.BlockCopy( inputBuffer,inputOffset, buffer,0,inputCount);
78             
79             // create the outpur buffer and set the first byte
80             // to the length+0x20
81             byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
82             outputBuffer[ 0 ] = (byte)(inputCount+0x20);
83             
84             // transform the block 3bytes at a time
85             for( int i =0 ; i < tripletBlocks ; i++ ) {
86                 TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
87                                   outputBuffer , i * 4 + 1);
88             }
89             
90             
91
92             return outputBuffer;
93                     
94         }
95         
96         // transforms a 3byte buffer to a 4byte uuencoded buffer
97         protected int TransformTriplet( byte[] inputBuffer,
98                                         int inputOffset,
99                                         int inputCount,
100                                         byte[] outputBuffer,
101                                         int outputOffset
102                                         ) {
103             
104             byte a = inputBuffer[ inputOffset + 0 ];
105             byte b = inputBuffer[ inputOffset + 1 ];
106             byte c = inputBuffer[ inputOffset + 2 ];
107             
108             outputBuffer[ outputOffset + 0 ] = 
109                 (byte)(0x20 + (( a >> 2                    ) & 0x3F));
110             
111             outputBuffer[ outputOffset + 1 ] = 
112                 (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
113             
114             outputBuffer[ outputOffset + 2 ] = 
115                 (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
116             
117             outputBuffer[ outputOffset + 3 ] = 
118                 (byte)(0x20 + (( c                         ) & 0x3F));
119             
120             // tanslate all 0x20 to 0x60 according to specs
121             for( int i = 0; i < 4; i++ ) {
122                 if( outputBuffer[ outputOffset + i ] == 0x20 ) {
123                     outputBuffer[ outputOffset + i ] = 0x60;
124                 }
125             }
126             
127             return 4;
128         }
129
130         public void Dispose() {}
131     }
132
133 }