From 62678390c170b607363b876276a26ec52f16ea1f Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 9 Aug 1999 16:12:26 +0000 Subject: [PATCH] * Fixes and new examples from Sebastian Guenther --- fcl/inc/base64.pp | 19 +++++++++---------- fcl/tests/Makefile | 8 ++++++-- fcl/tests/README | 3 +++ fcl/tests/b64dec.pp | 37 +++++++++++++++++++++++++++++++++++++ fcl/tests/b64enc.pp | 39 +++++++++++++++++++++++++++++++++++++++ fcl/tests/b64test2.pp | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 fcl/tests/b64dec.pp create mode 100644 fcl/tests/b64enc.pp create mode 100644 fcl/tests/b64test2.pp diff --git a/fcl/inc/base64.pp b/fcl/inc/base64.pp index 5c059740ec..4ac4e7d810 100644 --- a/fcl/inc/base64.pp +++ b/fcl/inc/base64.pp @@ -81,7 +81,7 @@ destructor TBase64EncodingStream.Destroy; var WriteBuf: array[0..3] of Char; begin - // Fill output to multiple of 3 + // Fill output to multiple of 4 case (TotalBytesProcessed mod 3) of 1: begin WriteBuf[0] := EncodingTable[Buf[0] shr 2]; @@ -109,19 +109,18 @@ end; function TBase64EncodingStream.Write(const Buffer; Count: Longint): Longint; var ReadNow: LongInt; - p: PChar; + p: Pointer; WriteBuf: array[0..3] of Char; begin Inc(TotalBytesProcessed, Count); Result := Count; - p := PChar(Buffer); + p := @Buffer; while count > 0 do begin // Fetch data into the Buffer ReadNow := 3 - BufSize; if ReadNow > Count then break; // Not enough data available - - Move(p, Buf[BufSize], ReadNow); + Move(p^, Buf[BufSize], ReadNow); Inc(p, ReadNow); Dec(Count, ReadNow); @@ -134,7 +133,7 @@ begin Inc(BytesWritten, 4); BufSize := 0; end; - Move(p, Buf[BufSize], count); + Move(p^, Buf[BufSize], count); Inc(BufSize, count); end; @@ -173,7 +172,6 @@ begin if endbytes[0] = '=' then Dec(DataLen); end; - // WriteLn('DataLen = ', DataLen); end; function TBase64DecodingStream.Read(var Buffer; Count: Longint): Longint; @@ -211,11 +209,9 @@ begin end; end; end; - // WriteLn('ReadBuf: ', ReadBuf[0], ' ', ReadBuf[1], ' ', ReadBuf[2], ' ', ReadBuf[3]); Buf[0] := ReadBuf[0] shl 2 or ReadBuf[1] shr 4; Buf[1] := (ReadBuf[1] and 15) shl 4 or ReadBuf[2] shr 2; Buf[2] := (ReadBuf[2] and 3) shl 6 or ReadBuf[3]; - // WriteLn('Gelesen: ', Buf[0], ' ', Buf[1], ' ', Buf[2]); end; p[0] := Chr(Buf[BufPos]); @@ -250,7 +246,10 @@ end. { $Log$ - Revision 1.1 1999-08-03 17:02:38 michael + Revision 1.2 1999-08-09 16:12:28 michael + * Fixes and new examples from Sebastian Guenther + + Revision 1.1 1999/08/03 17:02:38 michael * Base64 en/de cdeing streams added } diff --git a/fcl/tests/Makefile b/fcl/tests/Makefile index 38fb6cc6ff..08b76d5aef 100644 --- a/fcl/tests/Makefile +++ b/fcl/tests/Makefile @@ -36,7 +36,8 @@ NEEDOPT=-S2 UNITOBJECTS= EXEOBJECTS=stringl dparser fstream mstream list threads testrtf\ - cfgtest testz testz2 xmldump htdump testcgi tidea b64test + cfgtest testz testz2 xmldump htdump testcgi tidea\ + b64test b64test2 b64enc b64dec ##################################################################### @@ -113,7 +114,10 @@ endif # # $Log$ -# Revision 1.10 1999-08-03 17:02:36 michael +# Revision 1.11 1999-08-09 16:12:26 michael +# * Fixes and new examples from Sebastian Guenther +# +# Revision 1.10 1999/08/03 17:02:36 michael # * Base64 en/de cdeing streams added # # Revision 1.9 1999/07/25 14:30:39 michael diff --git a/fcl/tests/README b/fcl/tests/README index 34f5b42221..d870044469 100644 --- a/fcl/tests/README +++ b/fcl/tests/README @@ -29,3 +29,6 @@ htdump.pp htdump dumps XL IDL definition as ObjectPascal classes (MVC) testcgi.pp test program for ezcgi class (MH) tidea.pp test program for IDEA encryption/decryption streams (MVC) b64test.pp test program for base64 encoding streams (SG) +b64test2.pp test program for base64 encoding streams (SG) +b64enc.pp base64-encodes StdIn to StdOut (SG) +b64dec.pp base64-decodes file given as 1st argument to StdOut (SG) \ No newline at end of file diff --git a/fcl/tests/b64dec.pp b/fcl/tests/b64dec.pp new file mode 100644 index 0000000000..a08697c4ed --- /dev/null +++ b/fcl/tests/b64dec.pp @@ -0,0 +1,37 @@ +// $Id$ + +// base64-decodes a file (argument #1) and writes the output to StdOut +// (c) 1999 Sebastian Guenther + +{$MODE objfpc} + +program b64dec; +uses classes, base64, sysutils; +var + b64decoder: TBase64DecodingStream; + InputStream: TStream; + IsEnd: Boolean; +begin + + InputStream := TFileStream.Create(ParamStr(1), fmOpenRead); + + b64decoder := TBase64DecodingStream.Create(InputStream); + + while not IsEnd do + try + Write(Chr(b64decoder.ReadByte)); + except + on e: EStreamError do IsEnd := True; + end; + + b64decoder.Free; + InputStream.Free; +end. + + +{ + $Log$ + Revision 1.1 1999-08-09 16:12:26 michael + * Fixes and new examples from Sebastian Guenther + +} diff --git a/fcl/tests/b64enc.pp b/fcl/tests/b64enc.pp new file mode 100644 index 0000000000..11a5120ad0 --- /dev/null +++ b/fcl/tests/b64enc.pp @@ -0,0 +1,39 @@ +// $Id$ + +// base64-encodes data from StdIn and writes the output to StdOut +// (c) 1999 Sebastian Guenther + +{$MODE objfpc} + +program b64enc; +uses classes, base64, sysutils; +var + b64encoder: TBase64EncodingStream; + InputStream, OutputStream: TStream; + IsEnd: Boolean; +begin + + InputStream := THandleStream.Create(StdInputHandle); + OutputStream := THandleStream.Create(StdOutputHandle); + + b64encoder := TBase64EncodingStream.Create(OutputStream); + + while not IsEnd do + try + b64encoder.WriteByte(InputStream.ReadByte); + except + on e: EStreamError do IsEnd := True; + end; + + b64encoder.Free; + InputStream.Free; + OutputStream.Free; +end. + + +{ + $Log$ + Revision 1.1 1999-08-09 16:12:26 michael + * Fixes and new examples from Sebastian Guenther + +} diff --git a/fcl/tests/b64test2.pp b/fcl/tests/b64test2.pp new file mode 100644 index 0000000000..85c9a40ab4 --- /dev/null +++ b/fcl/tests/b64test2.pp @@ -0,0 +1,37 @@ +{$MODE objfpc} + +program b64test; +uses classes, base64, sysutils; +var + b64encoder: TBase64EncodingStream; + b64decoder: TBase64DecodingStream; + BaseStream: TStream; + i, j: Integer; + buf: array[1..23] of Char; +begin + BaseStream := TMemoryStream.Create; + + WriteLn('Encoded Size / Decoded Size / Data:'); + + for i := 1 to 22 do begin + BaseStream.Position := 0; + + b64encoder := TBase64EncodingStream.Create(BaseStream); + for j := 1 to i do + buf[j] := Chr(i - j + 65); + b64encoder.Write(buf, i); + Write(b64encoder.Size: 2, ' '); + b64encoder.Free; + + BaseStream.Position := 0; + + b64decoder := TBase64DecodingStream.Create(BaseStream); + Write(b64decoder.Size: 2, ' '); + b64decoder.Read(buf, i); + buf[i + 1] := #0; + WriteLn(buf); + b64decoder.Free; + end; + + BaseStream.Free; +end.