The Base64Encoder module allows you to encode binary data one buffer at a time, thus allowing you to encode data of unlimited size using a limited amount of memory. For example, if you want to encode data from an input file to an output file, you may read from the input file one buffer at a time, present each buffer to the encoder, and write to the output file one buffer at a time.
Encoder Options
The following list enumerates the options that affect the behavior of a Base64Encoder object. These options are set by calling functions in the Base64Encoder module.
Maximum Line Length You may change this option to set the maximum length of a line in the encoder's output. If you don't want any line breaks at all, you may set the maximum line length to a very large value (for example, 0x7FFFFFFF). The allowed values must be a multiple of 4, and the encoder enforces this restriction by rounding down any value that is not a multiple of 4 (for example, 73, 74, or 75 will be rounded down to 72). The default value is 72.
Output CR LF If this option is true, then the encoder uses CR LF as the end-of-line characters in the encoded output. If the option is false, then the encoder uses LF alone. The default value is false.
Suppress Final Newline If this option is true, then the encoder does not put a final newline (CR LF, or LF) at the end of the encoded output, unless the last line of output is a full line. If the option is false, then the encoder always adds a newline to the end of the encoded output, even if the last line is a partial line. If you don't want any end-of-line characters in the output, you may set this option to true and set the Maximum Line Length to a very large value. The default value is false, meaning that a final newline is always added.
Using Base64Encoder
The encode procedure uses three functions: Base64Encoder_start(), Base64Encoder_encode(), and Base64Encoder_finish(). The procedure is described here:
Call Base64Encoder_initialize() to initialize the encoder object.
Set any encoder options.
Call Base64Encoder_start() to start a new encode operation.
Initialize an input buffer and an output buffer. These buffers are instances of ByteBuffer and Char8Buffer, respectively. To initialize an input buffer named inBuf, you set inBuf.bytes to a char array that contains the data to be encoded, set inBuf.pos to the offset of the beginning of the data in inBuf.bytes, and set inBuf.endPos to the offset of the first byte past the end of the data in inBuf.bytes. To initialize an output buffer named outBuf, set outBuf.chars to a char array of any size, set outBuf.pos to zero, and set outBuf.endPos to the length of the array referenced by outBuf.chars.
Call Base64Encoder_encode() with the input buffer and output buffer as arguments.
Check to see if the output buffer is full or if the input buffer is empty. If outBuf.pos == outBuf.endPos, then the output buffer is full, and you must make room in the output buffer before you call Base64Encoder_encode() again. If inBuf.pos == inBuf.endPos, then the input buffer is empty, and you must supply the input buffer with more data before you call Base64Encoder_encode() again.
Repeat steps 5 and 6 as many times as necessary until you have processed all the input data.
Call Base64Encoder_finish() to flush any internally buffered data to the output buffer. If the output buffer is full after Base64Encoder_finish() returns, you must make room in the output buffer and call Base64Encoder_finish() again. If Base64Encoder_finish() returns and the output buffer is not full, then the encode operation is finished.
You may repeat steps 2 through 8 multiple times to perform multiple encode operations.
Tips
There is a certain amount of overhead associated with saving and restoring the encoder's state with each call to Base64Encoder_encode(). If you use small buffers, the overhead may have an impact on performance. For larger buffers, the overhead is negligible. A good buffer size is 8192, which also happens to be a good size for I/O to the file system on many machines. If you use 8K buffers for I/O to the file system, make sure your reads and writes are properly aligned.
The operation of the encoder itself never causes an error, therefore error handling is not necessary.
Functions | |
| void | Base64Encoder_setMaxLineLen (Base64Encoder *encoder, size_t n) |
| Sets the maximum line length of the encoded output. | |
| size_t | Base64Encoder_getMaxLineLen (Base64Encoder *encoder) |
| Gets the maximum line length of the encoded output. | |
| void | Base64Encoder_setOutputCrLf (Base64Encoder *encoder, int b) |
| Sets the CRLF end-of-line characters option. | |
| int | Base64Encoder_getOutputCrLf (Base64Encoder *encoder) |
| Gets the CRLF end-of-line characters option. | |
| void | Base64Encoder_setSuppressFinalNewline (Base64Encoder *encoder, int b) |
| Sets the option to suppress a final newline in the output. | |
| int | Base64Encoder_getSuppressFinalNewline (Base64Encoder *encoder) |
| Gets the option to suppress a final newline in the output. | |
| void | Base64Encoder_initialize (Base64Encoder *encoder) |
| Initializes the encoder object. | |
| void | Base64Encoder_start (Base64Encoder *encoder) |
| Starts an encode operation. | |
| void | Base64Encoder_encode (Base64Encoder *encoder, ByteBuffer *inBuf, Char8Buffer *outBuf) |
| Encodes data from the input buffer to the output buffer. | |
| void | Base64Encoder_finish (Base64Encoder *encoder, Char8Buffer *outBuf) |
| Finishes an encode operation. | |
|
||||||||||||||||
|
Encodes data from the input buffer to the output buffer. This function takes an input buffer and an output buffer as parameters, and encodes data from the input buffer to the output buffer until either (1) the input buffer is empty, or (2) the output buffer is full. Therefore, one of the following conditions is always true when the function returns:
You may call the function multiple times to encode multiple buffers of input data. However, both of the following conditions should be true before you call the function:
|
|
||||||||||||
|
Finishes an encode operation by flushing any internally buffered data to the output buffer. When you perform an encode operation, the encoder buffers some data internally. Therefore, after you have processed all the input data, you must call this function to flush the internal buffer. The following condition must be true when the function is called:
The above condition must also be true when the function returns in order to guarantee that all output data has been written to the output buffer. If the condition is false when the function returns, you must make room in the output buffer and call the function again.
|
|
|
Gets the maximum line length of the encoded output.
|
|
|
Gets the CRLF end-of-line characters option.
|
|
|
Gets the option to suppress a final newline in the output.
|
|
|
Initializes the encoder object. You must initialize the encoder object before you use it. After you have initialized it, you may use the encoder object for multiple encode operations.
|
|
||||||||||||
|
Sets the maximum line length of the encoded output.
For MIME-compliant Internet mail, lines may be no longer than 76 characters. However, applications other than mail might allow lines longer than 76 characters. You may call This function enforces the restriction that the maximum line length be a multiple of 4. If the argument is not a multiple of 4, then it is rounded down to the nearest multiple of 4. The default value is 72.
|
|
||||||||||||
|
Sets the CRLF end-of-line characters option. If this option is true, then the encoder uses CR LF as the end-of-line characters in the encoded output. If the option is false, then the encoder uses LF alone. The default value is false.
|
|
||||||||||||
|
Sets the option to suppress a final newline in the output. If this option is true, then the encoder does not put a final newline (CR LF, or LF) at the end of the encoded output, unless the last line of output is a full line. If the option is false, then the encoder always adds a newline to the end of the encoded output, even if the last line is a partial line. If you don't want any end-of-line characters in the output, you may set this option to true and set the Maximum Line Length to a very large value. The default value is false.
|
|
|
Starts an encode operation.
After you call
|
Copyright © 2001-2006 Hunny Software, Inc. All rights reserved.