Unit implementing base64 encoding

base64 implements base64 encoding (as used for instance in MIME encoding) based on streams. It implements 2 streams which encode or decode anything written or read from it. The source or the destination of the encoded data is another stream. 2 classes are implemented for this: for encoding, and for decoding.

The streams are designed as plug-in streams, which can be placed between other streams, to provide base64 encoding and decoding on-the-fly...

Stream support Decoding mode for stream

TBase64DecodingMode determines the decoding algorithm used by . There are 2 modes:

bdmStrict
Strict mode, which follows RFC3548 and rejects any characters outside of base64 alphabet. In this mode only up to two '=' characters are accepted at the end. It requires the input to have a Size being a multiple of 4, otherwise an exception is raised.
bdmMime
MIME mode, which follows RFC2045 and ignores any characters outside of base64 alphabet. In this mode any '=' is seen as the end of string, it handles apparently truncated input streams gracefully.
MIME encoding Strict encoding Decoding mode Mode is the mode in which the stream is read. It can be set when creating the stream or at any time afterwards. Exception raised on errors EBase64DecodeException is raised when the stream contains errors against the encoding format. Whether or not this exception is raised depends on the mode in which the stream is decoded. Base64 encoding stream.

TBase64EncodingStream can be used to encode data using the base64 algorithm. At creation time, a destination stream is specified. Any data written to the TBase64EncodingStream instance will be base64 encoded, and subsequently written to the destination stream.

The TBase64EncodingStream stream is a write-only stream. Obviously it is also not seekable. It is meant to be included in a chain of streams.

By the nature of base64 encoding, when a buffer is written to the stream, the output stream does not yet contain all output: input must be a multiple of 3. In order to be sure that the output contains all encoded bytes, the Flush method can be used. The destructor will automatically call Flush, so all data is written to the destination stream when the decodes is destroyed.

Create a new instance of the TBase64EncodingStream class. Create instantiates a new TBase64EncodingStream class. The ASource stream is stored and used to write the encoded data to. Destination stream for encoded data Encoding mode to be used when encoding Flush the remaining bytes to the output stream. Flush writes the remaining bytes from the internal encoding buffer to the output stream and pads the output with "=" signs. It returns True if padding was necessary, and False if not. Remove a TBase64EncodingStream instance from memory Destroy flushes any remaining output and then removes the TBase64EncodingStream instance from memory by calling the inherited destructor. An exception may be raised if the destination stream no longer exists or is closed. Read data from the stream Read always raises an exception, because the encoding stream is write-only. TStream.Read Number of bytes actually read Buffer to receive read data Number of bytes to read Write data to the stream. Write encodes Count bytes from Buffer using the Base64 mechanism, and then writes the encoded data to the destination stream. It returns the number of bytes from Buffer that were actually written. Note that this is not the number of bytes written to the destination stream: the base64 mechanism writes more bytes to the destination stream. If there is an error writing to the destination stream, an error may occur. TStream.Write Number of bytes from buffer actually used Buffer with data to write Number of bytes from buffer to write Position the stream Seek always raises an EStreamError exception unless the arguments it received it don't change the current file pointer position. The encryption stream is not seekable. An EStreamError error is raised. TStream.Seek New position in the stream Offset from origin Origin for the seek operation. Base64 Decoding stream

TBase64DecodingStream can be used to read data from a stream (the source stream) that contains Base64 encoded data. The data is read and decoded on-the-fly.

The decoding stream is read-only, and provides a limited forward-seek capability.

Create a new instance of the TBase64DecodingStream class

Create creates a new instance of the TBase64DecodingStream class. It stores the source stream ASource for reading the data from.

The optional AMode parameter determines the mode in which the decoding will be done. If omitted, bdmMIME is used.

Source stream from which to read encoded data Reset the stream Reset resets the data as if it was again on the start of the decoding stream. None. Read and decrypt data from the source stream

Read reads encrypted data from the source stream and stores this data in Buffer. At most Count bytes will be stored in the buffer, but more bytes will be read from the source stream: the encoding algorithm multiplies the number of bytes.

The function returns the number of bytes stored in the buffer.

If an error occurs during the read from the source stream, an exception may occur. TStream.Read
Number of bytes stored in buffer. Buffer to store data in. Maxmimum number of bytes to store in buffer. Write data to the stream

Write always raises an EStreamError exception, because the decoding stream is read-only. To write to an encrypted stream, use a instance.

TStream.Write
Number of bytes written Buffer with data to write to the stream Number of bytes to write Set stream position.

Seek sets the position of the stream. In the TBase64DecodingStream class, the seek operation is forward only, it does not support backward seeks. The forward seek is emulated by reading and discarding data till the desired position is reached.

For an explanation of the parameters, see TStream.Seek

In case of an unsupported operation, an EStreamError exception is raised. TStream.Seek
New absolute position in the stream. Offset, relative to origin. Origin of the seek operation. Decodes a Base64 encoded string and returns the decoded data as a string. DecodeStringBase64 decodes the string s (containing Base 64 encoded data) returns the decoded data as a string. It uses a to do this. The Strict parameter is passed on to the constructor as bdmStrict or bdmMIME Choose between strict base64 decoding or MIME decoding Encode a string with Base64 encoding and return the result as a string. EncodeStringBase64 encodes the string s using Base 64 encoding and returns the result. It uses a to do this. True if additional bytes were written. The Base64 encoded version of S The string to encode with base64 The base64 decoded version of S The base64 encoded string to decode