2 // System.Web.Mail.ToUUEncodingTransform.cs
5 // Per Arneng <pt99par@student.bth.se>
9 using System.Security.Cryptography;
11 namespace System.Web.Mail {
13 // This class transforms blocks of plaintext to UU encoding
14 internal class ToUUEncodingTransform : ICryptoTransform {
16 public int InputBlockSize { get { return 45; } }
17 public int OutputBlockSize { get { return 61; } }
19 public bool CanTransformMultipleBlocks { get { return true; } }
20 public bool CanReuseTransform { get { return true; } }
22 // transforms a block of bytes to UU encoding
23 public int TransformBlock( byte[] inputBuffer,
30 // write the line length length+0x20
31 outputBuffer[ 0 ] = (byte)'M';
33 // transform the block 3bytes at a time
34 for( int i=0;i<15;i++ ) {
36 TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
37 outputBuffer , outputOffset + i * 4 + 1);
42 return OutputBlockSize;
45 // make a final uu transformations
46 public byte[] TransformFinalBlock(byte[] inputBuffer,
51 // calculate how many 4-byte blocks there are
52 int tripletBlocks = inputCount / 3 + 1;
54 // create a new buffer and copy the input data into that
55 byte[] buffer = new byte[ tripletBlocks * 3 ];
56 Buffer.BlockCopy( inputBuffer,inputOffset, buffer,0,inputCount);
58 // create the outpur buffer and set the first byte
60 byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
61 outputBuffer[ 0 ] = (byte)(inputCount+0x20);
63 // transform the block 3bytes at a time
64 for( int i =0 ; i < tripletBlocks ; i++ ) {
65 TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
66 outputBuffer , i * 4 + 1);
75 // transforms a 3byte buffer to a 4byte uuencoded buffer
76 protected int TransformTriplet( byte[] inputBuffer,
83 byte a = inputBuffer[ inputOffset + 0 ];
84 byte b = inputBuffer[ inputOffset + 1 ];
85 byte c = inputBuffer[ inputOffset + 2 ];
87 outputBuffer[ outputOffset + 0 ] =
88 (byte)(0x20 + (( a >> 2 ) & 0x3F));
90 outputBuffer[ outputOffset + 1 ] =
91 (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
93 outputBuffer[ outputOffset + 2 ] =
94 (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
96 outputBuffer[ outputOffset + 3 ] =
97 (byte)(0x20 + (( c ) & 0x3F));
99 // tanslate all 0x20 to 0x60 according to specs
100 for( int i = 0; i < 4; i++ ) {
101 if( outputBuffer[ outputOffset + i ] == 0x20 ) {
102 outputBuffer[ outputOffset + i ] = 0x60;
109 public void Dispose() {}