~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/NU-Smtp/.svn/text-base/ToUUEncodingTransform.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************************
3
 
 |
4
 
 | Copyright (c) 2007 Novell, Inc.
5
 
 | All Rights Reserved.
6
 
 |
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.
10
 
 |
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.
15
 
 |
16
 
 | You should have received a copy of the GNU General Public License
17
 
 | along with this program; if not, contact Novell, Inc.
18
 
 |
19
 
 | To contact Novell about this file by physical or electronic mail,
20
 
 | you may find current contact information at www.novell.com 
21
 
 |
22
 
 | Author: Per Arneng <pt99par@student.bth.se>
23
 
 |***************************************************************************/
24
 
 
25
 
 
26
 
using System;
27
 
using System.Security.Cryptography;
28
 
 
29
 
namespace Simias.Mail {
30
 
 
31
 
 
32
 
    // This class transforms blocks of plaintext to UU encoding
33
 
    internal class ToUUEncodingTransform : ICryptoTransform
34
 
    {
35
 
 
36
 
        public int InputBlockSize { get { return 45;}}
37
 
        public int OutputBlockSize { get { return 61;}}
38
 
 
39
 
        public bool CanTransformMultipleBlocks { get { return true;}}
40
 
        public bool CanReuseTransform { get { return true;}}
41
 
 
42
 
    // transforms a block of bytes to UU encoding
43
 
        public int TransformBlock( byte[] inputBuffer,
44
 
                int inputOffset,
45
 
                int inputCount,
46
 
                byte[] outputBuffer,
47
 
                int outputOffset
48
 
                )
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
 
 
58
 
                TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
59
 
                        outputBuffer , outputOffset + i * 4 + 1);
60
 
 
61
 
            }
62
 
 
63
 
 
64
 
            return OutputBlockSize;
65
 
        }
66
 
 
67
 
    // make a final uu transformations
68
 
        public byte[] TransformFinalBlock(byte[] inputBuffer,
69
 
                int inputOffset,
70
 
                int inputCount
71
 
                )
72
 
        {
73
 
 
74
 
        // calculate how many 4-byte blocks there are
75
 
            int tripletBlocks = inputCount / 3 + 1;
76
 
 
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);
80
 
 
81
 
        // create the outpur buffer and set the first byte
82
 
        // to the length+0x20
83
 
            byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
84
 
            outputBuffer[ 0 ] = (byte)(inputCount+0x20);
85
 
 
86
 
        // transform the block 3bytes at a time
87
 
            for( int i =0 ; i < tripletBlocks ; i++ )
88
 
            {
89
 
                TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
90
 
                        outputBuffer , i * 4 + 1);
91
 
            }
92
 
 
93
 
 
94
 
 
95
 
            return outputBuffer;
96
 
 
97
 
        }
98
 
 
99
 
    // transforms a 3byte buffer to a 4byte uuencoded buffer
100
 
        protected int TransformTriplet( byte[] inputBuffer,
101
 
                int inputOffset,
102
 
                int inputCount,
103
 
                byte[] outputBuffer,
104
 
                int outputOffset
105
 
                )
106
 
        {
107
 
 
108
 
            byte a = inputBuffer[ inputOffset + 0 ];
109
 
            byte b = inputBuffer[ inputOffset + 1 ];
110
 
            byte c = inputBuffer[ inputOffset + 2 ];
111
 
 
112
 
            outputBuffer[ outputOffset + 0 ] = 
113
 
                    (byte)(0x20 + (( a >> 2                    ) & 0x3F));
114
 
 
115
 
            outputBuffer[ outputOffset + 1 ] = 
116
 
                    (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
117
 
 
118
 
            outputBuffer[ outputOffset + 2 ] = 
119
 
                    (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
120
 
 
121
 
            outputBuffer[ outputOffset + 3 ] = 
122
 
                    (byte)(0x20 + (( c                         ) & 0x3F));
123
 
 
124
 
        // tanslate all 0x20 to 0x60 according to specs
125
 
            for( int i = 0; i < 4; i++ )
126
 
            {
127
 
                if( outputBuffer[ outputOffset + i ] == 0x20 )
128
 
                {
129
 
                    outputBuffer[ outputOffset + i ] = 0x60;
130
 
                }
131
 
            }
132
 
 
133
 
            return 4;
134
 
        }
135
 
 
136
 
        public void Dispose()
137
 
        {
138
 
        }
139
 
    }
140
 
 
141
 
}