IdCoderUUE.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10099: IdCoderUUE.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:33:16 PM czhower
  13. }
  14. unit IdCoderUUE;
  15. interface
  16. uses
  17. Classes,
  18. IdCoder3to4;
  19. type
  20. TIdDecoderUUEBase = class(TIdDecoder4to3)
  21. public
  22. procedure DecodeToStream(AIn: string; ADest: TStream); override;
  23. end;
  24. TIdDecoderUUE = class(TIdDecoderUUEBase)
  25. public
  26. constructor Create(AOwner: TComponent); override;
  27. procedure DecodeToStream(AIn: string; ADest: TStream); override;
  28. end;
  29. TIdEncoderUUEBase = class(TIdEncoder3to4)
  30. public
  31. function Encode(ASrcStream: TStream; const ABytes: integer = MaxInt): string; override;
  32. end;
  33. TIdEncoderUUE = class(TIdEncoderUUEBase)
  34. public
  35. constructor Create(AOwner: TComponent); override;
  36. end;
  37. const
  38. GUUECodeTable: string = '`!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_'; {Do not Localize}
  39. var
  40. GUUEDecodeTable: TIdDecodeTable;
  41. implementation
  42. uses
  43. IdGlobal,
  44. SysUtils;
  45. { TIdEncoderUUE }
  46. constructor TIdEncoderUUE.Create(AOwner: TComponent);
  47. begin
  48. inherited;
  49. FCodingTable := GUUECodeTable;
  50. FFillChar := FCodingTable[1];
  51. end;
  52. { TIdDecoderUUE }
  53. constructor TIdDecoderUUE.Create(AOwner: TComponent);
  54. begin
  55. inherited;
  56. FDecodeTable := GUUEDecodeTable;
  57. FFillChar := '~'; {Do not Localize}
  58. end;
  59. procedure TIdDecoderUUE.DecodeToStream(AIn: string; ADest: TStream);
  60. begin
  61. // Older UUEncoders use space instead of `
  62. inherited DecodeToStream(StringReplace(AIn, ' ' , '`', [rfReplaceAll]), ADest); {Do not Localize}
  63. end;
  64. { TIdDecoderUUEBase }
  65. procedure TIdDecoderUUEBase.DecodeToStream(AIn: string; ADest: TStream);
  66. var
  67. LLength: integer;
  68. begin
  69. if Length(AIn) > 0 then begin
  70. LLength := FDecodeTable[Ord(AIn[1])];
  71. Delete(AIn, 1, 1);
  72. case (LLength mod 3) of
  73. 0: begin
  74. SetLength(AIn,(LLength div 3)*4);
  75. // SetLength(AIn, (LLength div 3) * 4 + 4);
  76. // AIn[Length(AIn) - 2] := FillChar;
  77. // AIn[Length(AIn) - 1] := FillChar;
  78. // AIn[Length(AIn)] := FillChar;
  79. end;
  80. 1: begin
  81. SetLength(AIn, (LLength div 3) * 4 + 4);
  82. AIn[Length(AIn) - 1] := FillChar;
  83. AIn[Length(AIn)] := FillChar;
  84. end;
  85. 2: begin
  86. SetLength(AIn, (LLength div 3) * 4 + 4);
  87. AIn[Length(AIn)] := FillChar;
  88. end;
  89. end;
  90. inherited DecodeToStream(AIn, ADest);
  91. end;
  92. end;
  93. { TIdEncoderUUEBase }
  94. function TIdEncoderUUEBase.Encode(ASrcStream: TStream; const ABytes: integer): string;
  95. var
  96. LStart: Integer;
  97. begin
  98. LStart := ASrcStream.Position;
  99. Result := inherited Encode(ASrcStream, ABytes);
  100. Result := FCodingTable[ASrcStream.Position - LStart + 1] + Result;
  101. end;
  102. initialization
  103. TIdDecoder4to3.ConstructDecodeTable(GUUECodeTable, GUUEDecodeTable);
  104. end.