Add licensing info
[mono.git] / mcs / class / System.Web / System.Web.Mail / Base64AttachmentEncoder.cs
1 //
2 // System.Web.Mail.Base64AttachmentEncoder.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.IO;
31 using System.Text;
32 using System.Security.Cryptography;
33
34 namespace System.Web.Mail {
35     
36     // a class that handles Base64 encoding for attachments
37     internal class Base64AttachmentEncoder : IAttachmentEncoder {
38         
39         // reads bytes from a stream and writes the encoded
40         // as base64 encoded characters. ( 60 chars on each row)
41         public void EncodeStream(  Stream ins , Stream outs ) {
42             
43             if( ( ins == null ) || ( outs == null ) )
44                 throw new ArgumentNullException( "The input and output streams may not " +
45                                                  "be null.");
46             
47             ICryptoTransform base64 = new ToBase64Transform();
48                     
49             // the buffers
50             byte[] plainText = new byte[ base64.InputBlockSize ];
51             byte[] cipherText = new byte[ base64.OutputBlockSize ];
52
53             int readLength = 0;
54             int trLength = 0;
55             int count = 0;
56             byte[] newln = new byte[] { 13 , 10 }; //CR LF with mail
57
58             // read through the stream until there 
59             // are no more bytes left
60             while( true ) {
61                 
62                 // read some bytes
63                 readLength = ins.Read( plainText , 0 , plainText.Length );
64             
65                 // break when there is no more data
66                 if( readLength < 1 ) break;
67             
68                 // transfrom and write the blocks. If the block size
69                 // is less than the InputBlockSize then write the final block
70                 if( readLength == plainText.Length ) {
71                 
72                     trLength = base64.TransformBlock( plainText , 0 , 
73                                                       plainText.Length ,
74                                                       cipherText , 0 );
75                                     
76                     // write the data
77                     outs.Write( cipherText , 0 , cipherText.Length );
78                         
79
80                     // do this to output lines that
81                     // are 60 chars long
82                     count += cipherText.Length;
83                     if( count == 60 ) {
84                         outs.Write( newln , 0 , newln.Length );
85                         count = 0;
86                     }
87                         
88                 } else {
89                     
90                     // convert the final blocks of bytes and write them
91                     cipherText = base64.TransformFinalBlock( plainText , 0 , readLength );
92                     outs.Write( cipherText , 0 , cipherText.Length );
93                     
94                 }
95             
96             } 
97             
98             outs.Write( newln , 0 , newln.Length );
99         }
100         
101         
102     }   
103         
104     
105     
106 }