home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / afx_511.exe / DEVELOP.ZIP / UUENCODE.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1998-02-08  |  3.9 KB  |  167 lines

  1. (******************************************************************************)
  2. (*                          ALLFIX sample procedures                          *)
  3. (*    Copyright (C) 1992,98 Harms Software Engineering, all rights reserved   *)
  4. (*                                                                            *)
  5. (*                 All information in this documentation is                   *)
  6. (*                  subject to change without prior notice                    *)
  7. (******************************************************************************)
  8.  
  9. UNIT UUencode;
  10.  
  11. Interface
  12.  
  13. const enBase64 : array[0..63] of char = '`!"#$%&'#39'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
  14. var   deBase64 : array[0..255] of byte;
  15.  
  16. const uu_bufsize   = 60;
  17.       EncodeFactor = 8/6;
  18.  
  19.       (* This unit was developed for creating uuencoded data for email   *)
  20.       (* messages in ALLFIX. By changing the uu_bufsize to 45, it will   *)
  21.       (* create standard uuencoded data which can be decoded by other    *)
  22.       (* applications. In order to do that, simply preceed the uuencoded *)
  23.       (* data with the string "begin 644 <filename>", where <filename>   *)
  24.       (* is the name of the file. And, at the end of the uuencoded data  *)
  25.       (* put the character ` on a line by itself, followed by the string *)
  26.       (* "end". That's all there is to it.                               *)
  27.  
  28. type uu_inbuffer  = array[0..uu_bufsize-1] of byte;    { binary data    }
  29.      uu_outbuffer = array[0..2*uu_bufsize-1] of byte;  { uuencoded data }
  30.  
  31. function  uu_encode(input : uu_inbuffer; size: word): string;
  32.  
  33. procedure uu_decode(       str: string;
  34.                     var output: uu_inbuffer;
  35.                     var size  : word);
  36.  
  37. Implementation
  38.  
  39. Procedure Build_deBase64;
  40.  
  41.   var cnt : word;
  42.  
  43.   begin
  44.     for cnt := 0 to 63 do
  45.       deBase64[Byte(enBase64[cnt])] := cnt;
  46.   end;
  47.  
  48. {$R-}
  49. function PullBits(a: byte; var c: byte; s: byte; var n: byte): byte;
  50.  
  51.   var mask : byte;
  52.  
  53.   begin
  54.     mask := $FF shl (8-n);
  55.  
  56.     PullBits := c or ((a and mask) shr (8-n));
  57.  
  58.     n := s-(8-n);
  59.  
  60.     c := (a and not mask) shl n;
  61.   end;
  62. {$R+}
  63.  
  64. {$R-}
  65. function PushBits(a: byte; var b: byte; s: byte; var n: byte): byte;
  66.  
  67.   var mask : byte;
  68.  
  69.   begin
  70.     mask := $FF shl (s-n);
  71.  
  72.     PushBits := a shl n or ((b and mask) shr (s-n));
  73.  
  74.     b := b and not mask;
  75.  
  76.     n := 8-(s-n);
  77.   end;
  78. {$R+}
  79.  
  80. function  uu_encode(input : uu_inbuffer; size: word): string;
  81.  
  82.   var cnt : word;
  83.       s   : string;
  84.  
  85.       c   : byte;
  86.       n   : byte;
  87.       t   : byte;
  88.  
  89.   begin
  90.     { uuencode input buffer and store in output buffer }
  91.  
  92.     cnt  := 0;
  93.     s    := enBase64[size];
  94.  
  95.     c := 0;
  96.     n := 6;
  97.     while cnt < size do
  98.     begin
  99.       if n > 0 then
  100.         t := PullBits(input[cnt], c, 6, n) else
  101.       begin
  102.         t := c;
  103.         c := 0;
  104.         n := 6;
  105.       end;
  106.  
  107.       if n > 0 then inc(cnt);
  108.  
  109.       s := s+enBase64[t];
  110.     end;
  111.  
  112.  
  113.     if n <> 6 {size > (length(s)*6/8)} then
  114.       s := s+enBase64[c];
  115.  
  116.     uu_encode := s;
  117.   end;
  118.  
  119. procedure uu_decode(       str: string;
  120.                     var output: uu_inbuffer;
  121.                     var size  : word);
  122.  
  123.   var len,
  124.       s,
  125.       n,
  126.       t1,
  127.       t2,
  128.       cnt : byte;
  129.  
  130.   begin
  131.     { uudecode input buffer and store in output buffer }
  132.  
  133.     len  := deBase64[byte(str[1])];
  134.  
  135.     if len > sizeof(output) then
  136.       len := sizeof(output);
  137.     size := 0;
  138.  
  139.     s := 6;
  140.     n := 2;
  141.  
  142.     t1 := deBase64[byte(str[2])];
  143.  
  144.     cnt := 3;
  145.     while (size < len) do
  146.     begin
  147.       t2 := deBase64[byte(str[cnt])];
  148.       inc(cnt);
  149.  
  150.       output[size] := PushBits(t1,t2,s,n);
  151.  
  152.       if n = 8 then
  153.       begin
  154.         t1 := deBase64[byte(str[cnt])];
  155.         inc(cnt);
  156.         n  := 2;
  157.       end else
  158.         t1 := t2;
  159.  
  160.       inc(size);
  161.     end;
  162.   end;
  163.  
  164. begin
  165.   Build_deBase64;
  166. end.
  167.