[fpc-devel] Base64 decoding stream in the FCL

Bram Kuijvenhoven kuifwaremailinglists at xs4all.nl
Fri Feb 16 13:24:07 CET 2007


Hi!

I've implemented Strict and MIME mode for TBase64DecodingStream as promised. I also created some FPCUnit tests. The following motivates the meaning of TBase64DecodingStream.Size and TBase64DecodingStream.Position; please comment on whether this is the desired behavior of a 'filtering' stream.


TBase64DecodingStream acts as a filter stream on another stream, called InputStream. It starts decoding at InputStream.Position, e.g. it does not set InputStream.Position to 0. This is important so it can decode non-seekable streams as well.

In Strict mode, it will continue decoding until the end of the InputStream; Read will raise an exceptions if any non-base64 character is encountered or when a '=' character is found before the end of the InputStream.

In MIME mode, it will ignore any non-base64 characters, and consider the first '=', or InputStream end, to mark the end of the stream.

Note: Whenever InputStream.Read returns 0, this is considered as a sign that we reached the end of the InputStream; InputStream.GetSize is not used for detecting this.

TBase64DecodingStream.GetPosition returns the number of decoded & Read bytes since the last call to Reset (or Create, which calls Reset). Setting the Position (seeking) is not supported, though this could be implemented.

Now TBase64DecodingStream.GetSize: it returns the number of (expected) decoded bytes since the last call to Reset (or Create) until the end of the Base64 encoded data in the InputStream, as implied by the current Mode. The method will always Seek the InputStream, either to go to the end of the InputStream (Strict mode), or to find the first '=' (MIME mode).

Note: changing the position or content of InputStream while using TBase64DecodingStream is operating on it will probably give very unexpected results.


Regards,

Bram




More information about the fpc-devel mailing list