Functions | |
| int | Base64_Encode (char *dest, int dest_size, const char *src, int src_size) |
| Base 64 encode a block of data. | |
| bool | Base64_Decode (char *dest, int *dest_size, const char *src, int src_size) |
| Decode a Base64 string into a block of data. | |
This is often handy when you need to send raw/binary data (as opposed to text) through a text based format, like XML or JSON.
Most code lifted from gnulib - http://savannah.gnu.org/projects/gnulib - and written by Simon Josefsson.
| bool Base64_Decode | ( | char * | dest, | |
| int * | dest_size, | |||
| const char * | src, | |||
| int | src_size | |||
| ) |
Decode a Base64 string into a block of data.
| dest | A pointer to the block of data to write in. | |
| dest_size | A pointer to the maximum number of bytes to write into dest. The number of bytes successfully written will be stored in this value upon return. | |
| src | The base 64 string to decode. | |
| src_size | The size of the base 64 string. |
#define BUFF_SIZE 256 char decode_buf[BUFF_SIZE]; int decode_size = BUFF_SIZE; bool result = Base64_Decode(decode_buf, &decode_size, "dGVzdA==", 8); // we now have "test" in decode_buf, and decode_size is set to 6
| int Base64_Encode | ( | char * | dest, | |
| int | dest_size, | |||
| const char * | src, | |||
| int | src_size | |||
| ) |
Base 64 encode a block of data.
Provide a buffer to write into and to read from. As Base64 encoding results in 4 bytes for every 3 source bytes, ensure your destination buffer is large enough.
| dest | The buffer that the encoded string will be written into. | |
| dest_size | The maximum number of bytes to write into the destination buffer. | |
| src | A block of data to encode. | |
| src_size | The number of bytes from src to encode. |
#define BUFF_SIZE 256 char encode_buf[BUFF_SIZE]; int len = Base64_Encode(encode_buf, BUFF_SIZE, "test", 4); // we now have "dGVzdA==" in encode_buf, and len is 8