2 // System.Web.Mail.ToUUEncodingTransform.cs
5 // Per Arneng <pt99par@student.bth.se>
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Security.Cryptography;
32 namespace System.Web.Mail {
34 // This class transforms blocks of plaintext to UU encoding
35 internal class ToUUEncodingTransform : ICryptoTransform {
37 public int InputBlockSize { get { return 45; } }
38 public int OutputBlockSize { get { return 61; } }
40 public bool CanTransformMultipleBlocks { get { return true; } }
41 public bool CanReuseTransform { get { return true; } }
43 // transforms a block of bytes to UU encoding
44 public int TransformBlock( byte[] inputBuffer,
51 // write the line length length+0x20
52 outputBuffer[ 0 ] = (byte)'M';
54 // transform the block 3bytes at a time
55 for( int i=0;i<15;i++ ) {
57 TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
58 outputBuffer , outputOffset + i * 4 + 1);
63 return OutputBlockSize;
66 // make a final uu transformations
67 public byte[] TransformFinalBlock(byte[] inputBuffer,
72 // calculate how many 4-byte blocks there are
73 int tripletBlocks = inputCount / 3 + 1;
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);
79 // create the outpur buffer and set the first byte
81 byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
82 outputBuffer[ 0 ] = (byte)(inputCount+0x20);
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);
96 // transforms a 3byte buffer to a 4byte uuencoded buffer
97 protected int TransformTriplet( byte[] inputBuffer,
104 byte a = inputBuffer[ inputOffset + 0 ];
105 byte b = inputBuffer[ inputOffset + 1 ];
106 byte c = inputBuffer[ inputOffset + 2 ];
108 outputBuffer[ outputOffset + 0 ] =
109 (byte)(0x20 + (( a >> 2 ) & 0x3F));
111 outputBuffer[ outputOffset + 1 ] =
112 (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
114 outputBuffer[ outputOffset + 2 ] =
115 (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
117 outputBuffer[ outputOffset + 3 ] =
118 (byte)(0x20 + (( c ) & 0x3F));
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;
130 public void Dispose() {}