ToUUEncodingTransform.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // System.Web.Mail.ToUUEncodingTransform.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Security.Cryptography;
  30. namespace System.Web.Mail {
  31. // This class transforms blocks of plaintext to UU encoding
  32. internal class ToUUEncodingTransform : ICryptoTransform {
  33. public int InputBlockSize { get { return 45; } }
  34. public int OutputBlockSize { get { return 61; } }
  35. public bool CanTransformMultipleBlocks { get { return true; } }
  36. public bool CanReuseTransform { get { return true; } }
  37. // transforms a block of bytes to UU encoding
  38. public int TransformBlock( byte[] inputBuffer,
  39. int inputOffset,
  40. int inputCount,
  41. byte[] outputBuffer,
  42. int outputOffset
  43. ) {
  44. // write the line length length+0x20
  45. outputBuffer[ 0 ] = (byte)'M';
  46. // transform the block 3bytes at a time
  47. for( int i=0;i<15;i++ ) {
  48. TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
  49. outputBuffer , outputOffset + i * 4 + 1);
  50. }
  51. return OutputBlockSize;
  52. }
  53. // make a final uu transformations
  54. public byte[] TransformFinalBlock(byte[] inputBuffer,
  55. int inputOffset,
  56. int inputCount
  57. ) {
  58. // calculate how many 4-byte blocks there are
  59. int tripletBlocks = inputCount / 3 + 1;
  60. // create a new buffer and copy the input data into that
  61. byte[] buffer = new byte[ tripletBlocks * 3 ];
  62. Buffer.BlockCopy( inputBuffer,inputOffset, buffer,0,inputCount);
  63. // create the outpur buffer and set the first byte
  64. // to the length+0x20
  65. byte[] outputBuffer = new byte[ tripletBlocks * 4 + 1 ];
  66. outputBuffer[ 0 ] = (byte)(inputCount+0x20);
  67. // transform the block 3bytes at a time
  68. for( int i =0 ; i < tripletBlocks ; i++ ) {
  69. TransformTriplet( inputBuffer , inputOffset + i * 3 , 3,
  70. outputBuffer , i * 4 + 1);
  71. }
  72. return outputBuffer;
  73. }
  74. // transforms a 3byte buffer to a 4byte uuencoded buffer
  75. protected int TransformTriplet( byte[] inputBuffer,
  76. int inputOffset,
  77. int inputCount,
  78. byte[] outputBuffer,
  79. int outputOffset
  80. ) {
  81. byte a = inputBuffer[ inputOffset + 0 ];
  82. byte b = inputBuffer[ inputOffset + 1 ];
  83. byte c = inputBuffer[ inputOffset + 2 ];
  84. outputBuffer[ outputOffset + 0 ] =
  85. (byte)(0x20 + (( a >> 2 ) & 0x3F));
  86. outputBuffer[ outputOffset + 1 ] =
  87. (byte)(0x20 + (((a << 4) | ((b >> 4) & 0xF)) & 0x3F));
  88. outputBuffer[ outputOffset + 2 ] =
  89. (byte)(0x20 + (((b << 2) | ((c >> 6) & 0x3)) & 0x3F));
  90. outputBuffer[ outputOffset + 3 ] =
  91. (byte)(0x20 + (( c ) & 0x3F));
  92. // tanslate all 0x20 to 0x60 according to specs
  93. for( int i = 0; i < 4; i++ ) {
  94. if( outputBuffer[ outputOffset + i ] == 0x20 ) {
  95. outputBuffer[ outputOffset + i ] = 0x60;
  96. }
  97. }
  98. return 4;
  99. }
  100. public void Dispose() {}
  101. }
  102. }