Base-64

Base-64 — Base-64 Encoding and Decoding

Synopsis




#define     GSK_BASE64_GET_ENCODED_LEN      (length)
#define     GSK_BASE64_GET_MAX_DECODED_LEN  (length)
guint       gsk_base64_decode               (char *dst,
                                             guint dst_len,
                                             const char *src,
                                             gssize src_len);
GByteArray* gsk_base64_decode_alloc         (const char *src);
void        gsk_base64_encode               (char *dst,
                                             const char *src,
                                             guint src_len);
char*       gsk_base64_encode_alloc         (const char *src,
                                             gssize src_len);

Description

A collection of methods to deal with base-64 encoding and decoding. Base-64 is defined in RFC 2045, Section 6.8.

The number of terminal = characters is specified in the FAQ such that a base-64 encoded string is always a multiple of 4 bytes long.

XXX: The encoding end of this class probably needs some fixing in light of RFC 2045.

Details

GSK_BASE64_GET_ENCODED_LEN()

#define     GSK_BASE64_GET_ENCODED_LEN(length)

Get the number of bytes needed to encode in base-64 a binary block of data of a given length. This includes space for a terminal = sign, but does not include space for a NUL.

length : the length of the raw binary string.

GSK_BASE64_GET_MAX_DECODED_LEN()

#define     GSK_BASE64_GET_MAX_DECODED_LEN(length)

Get the maximum number of bytes a base-64 encoded string may require. This assumes that there is no terminal = character, but there may be one, causing the actual number of bytes once decoded to be smaller. The actual number is returned by gsk_base64_decode().

length : the length of the base64 encoded string.

gsk_base64_decode ()

guint       gsk_base64_decode               (char *dst,
                                             guint dst_len,
                                             const char *src,
                                             gssize src_len);

Decode a base64-encoded string into binary.

dst : output area for binary data. Should be GSK_BASE64_GET_MAX_DECODED_LEN(src) long at least.
dst_len : length of buffer allocated for dst.
src : base64 encoded data.
src_len : length of the binary data, or -1 to assume that src is NUL-terminated.
Returns : number of bytes decoded.

gsk_base64_decode_alloc ()

GByteArray* gsk_base64_decode_alloc         (const char *src);

Decode a base64-encoded string into binary.

GSK_BASE64_GET_MAX_DECODED_LEN might not be the return value, because it doesn't take a terminate '=' sign into account. The return value should be exactly that if src is not = terminated, or GSK_BASE64_GET_MAX_DECODED_LEN() is only called on the length which precedes the = sign.

src : base64 encoded data, NUL terminated.
Returns : the byte-array with the binary data.

gsk_base64_encode ()

void        gsk_base64_encode               (char *dst,
                                             const char *src,
                                             guint src_len);

base64 encodes binary data.

dst : output base64 encoded string. The result is NOT nul-terminated, but is terminated with an = sign. dst should be exactly GSK_BASE64_GET_ENCODED_LEN(src_len) bytes long.
src : input binary data.
src_len : length of src.

gsk_base64_encode_alloc ()

char*       gsk_base64_encode_alloc         (const char *src,
                                             gssize src_len);

base64 encodes binary data (that does not contain a NULL).

src : data to base64 encode.
src_len : length of binary data to encode, or -1 to take src as a NUL-terminated string.
Returns : an newly allocated base64 encoded NUL-terminated ASCII string.