ToUUEncodingTransform.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Web.Mail.ToUUEncodingTransform.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.Security.Cryptography;
  10. namespace System.Web.Mail {
  11. // This class transforms blocks of plaintext to UU encoding
  12. internal class ToUUEncodingTransform : ICryptoTransform {
  13. public int InputBlockSize { get { return 45; } }
  14. public int OutputBlockSize { get { return 61; } }
  15. public bool CanTransformMultipleBlocks { get { return true; } }
  16. public bool CanReuseTransform { get { return true; } }
  17. // transforms a block of bytes to UU encoding
  18. public int TransformBlock( byte[] inputBuffer,
  19. int inputOffset,
  20. int inputCount,
  21. byte[] outputBuffer,
  22. int outputOffset
  23. ) {
  24. // write the line length length+0x20
  25. outputBuffer[ 0 ] = (byte)'M';
  26. // transform the block 3bytes at a time
  27. for( int i=0;i<15;i++ ) {
  28. TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
  29. outputBuffer , outputOffset + i * 4 + 1);
  30. }
  31. return OutputBlockSize;
  32. }
  33. // make a final uu transformations
  34. public byte[] TransformFinalBlock(byte[] inputBuffer,
  35. int inputOffset,
  36. int inputCount
  37. ) {
  38. // calculate how many 4-byte blocks there are
  39. int tripletBlocks = inputCount / 3 + 1;
  40. // create a new buffer and copy the input data into that
  41. byte[] buffer = new byte[ tripletBlocks * 3 ];
  42. Buffer.BlockCopy( inputBuffer,inputOffset, buffer,0,inputCount);
  43. // create the outpur buffer and set the first byte
  44. // to the length+0x20
  45. byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
  46. outputBuffer[ 0 ] = (byte)(inputCount+0x20);
  47. // transform the block 3bytes at a time
  48. for( int i =0 ; i < tripletBlocks ; i++ ) {
  49. TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
  50. outputBuffer , i * 4 + 1);
  51. }
  52. return outputBuffer;
  53. }
  54. // transforms a 3byte buffer to a 4byte uuencoded buffer
  55. protected int TransformTriplet( byte[] inputBuffer,
  56. int inputOffset,
  57. int inputCount,
  58. byte[] outputBuffer,
  59. int outputOffset
  60. ) {
  61. byte a = inputBuffer[ inputOffset + 0 ];
  62. byte b = inputBuffer[ inputOffset + 1 ];
  63. byte c = inputBuffer[ inputOffset + 2 ];
  64. outputBuffer[ outputOffset + 0 ] =
  65. (byte)(0x20 + (( a >> 2 ) & 0x3F));
  66. outputBuffer[ outputOffset + 1 ] =
  67. (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
  68. outputBuffer[ outputOffset + 2 ] =
  69. (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
  70. outputBuffer[ outputOffset + 3 ] =
  71. (byte)(0x20 + (( c ) & 0x3F));
  72. // tanslate all 0x20 to 0x60 according to specs
  73. for( int i = 0; i < 4; i++ ) {
  74. if( outputBuffer[ outputOffset + i ] == 0x20 ) {
  75. outputBuffer[ outputOffset + i ] = 0x60;
  76. }
  77. }
  78. return 4;
  79. }
  80. public void Dispose() {}
  81. }
  82. }