2
/****************************************************************************
4
| Copyright (c) 2007 Novell, Inc.
7
| This program is free software; you can redistribute it and/or
8
| modify it under the terms of version 2 of the GNU General Public License as
9
| published by the Free Software Foundation.
11
| This program is distributed in the hope that it will be useful,
12
| but WITHOUT ANY WARRANTY; without even the implied warranty of
13
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
| GNU General Public License for more details.
16
| You should have received a copy of the GNU General Public License
17
| along with this program; if not, contact Novell, Inc.
19
| To contact Novell about this file by physical or electronic mail,
20
| you may find current contact information at www.novell.com
22
| Author: Per Arneng <pt99par@student.bth.se>
23
|***************************************************************************/
27
using System.Security.Cryptography;
29
namespace Simias.Mail {
32
// This class transforms blocks of plaintext to UU encoding
33
internal class ToUUEncodingTransform : ICryptoTransform
36
public int InputBlockSize { get { return 45;}}
37
public int OutputBlockSize { get { return 61;}}
39
public bool CanTransformMultipleBlocks { get { return true;}}
40
public bool CanReuseTransform { get { return true;}}
42
// transforms a block of bytes to UU encoding
43
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++ )
58
TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
59
outputBuffer , outputOffset + i * 4 + 1);
64
return OutputBlockSize;
67
// make a final uu transformations
68
public byte[] TransformFinalBlock(byte[] inputBuffer,
74
// calculate how many 4-byte blocks there are
75
int tripletBlocks = inputCount / 3 + 1;
77
// create a new buffer and copy the input data into that
78
byte[] buffer = new byte[ tripletBlocks * 3 ];
79
Buffer.BlockCopy( inputBuffer,inputOffset, buffer,0,inputCount);
81
// create the outpur buffer and set the first byte
83
byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
84
outputBuffer[ 0 ] = (byte)(inputCount+0x20);
86
// transform the block 3bytes at a time
87
for( int i =0 ; i < tripletBlocks ; i++ )
89
TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
90
outputBuffer , i * 4 + 1);
99
// transforms a 3byte buffer to a 4byte uuencoded buffer
100
protected int TransformTriplet( byte[] inputBuffer,
108
byte a = inputBuffer[ inputOffset + 0 ];
109
byte b = inputBuffer[ inputOffset + 1 ];
110
byte c = inputBuffer[ inputOffset + 2 ];
112
outputBuffer[ outputOffset + 0 ] =
113
(byte)(0x20 + (( a >> 2 ) & 0x3F));
115
outputBuffer[ outputOffset + 1 ] =
116
(byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
118
outputBuffer[ outputOffset + 2 ] =
119
(byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
121
outputBuffer[ outputOffset + 3 ] =
122
(byte)(0x20 + (( c ) & 0x3F));
124
// tanslate all 0x20 to 0x60 according to specs
125
for( int i = 0; i < 4; i++ )
127
if( outputBuffer[ outputOffset + i ] == 0x20 )
129
outputBuffer[ outputOffset + i ] = 0x60;
136
public void Dispose()