A SERVICE OF

logo

Chapter 5 Non-Cryptographic Operations 173
Converting Data Between Binary and ASCII
Step 4: Update
Enter the data to encode through B_EncodeUpdate. The application is responsible for
allocating the space for the output of this routine. When encoding, for each three bytes
of input there are four bytes of output. So when allocating space, multiply the input
size by 4/3 and round up. If memory is not an issue, you can make the output buffer
twice the size of the input length.
Given pre-existing binary input, your calls to the Update functions would be as
follows:
if ((status = B_EncodeInit (asciiEncoder)) != 0)
break;
/* We are assuming binaryData already points to allocated
space and contains the data to encode into ASCII.
*/
unsigned char *binaryData;
unsigned int binaryDataLen;
unsigned char *asciiEncoding = NULL_PTR;
unsigned int asciiEncodingLenUpdate;
/* Allocate a buffer twice the size of the binary data */
asciiEncoding = T_malloc (binaryDataLen * 2);
if ((status = (asciiEncoding == NULL_PTR)) != 0)
break;
if ((status = B_EncodeUpdate
(asciiEncoder, asciiEncoding, &asciiEncodingLenUpdate,
(binaryDataLen * 2), binaryData, binaryDataLen)) != 0)
break;