[ccan] [PATCH] base64: implements rfc4648, the base64 encoding

David Gibson david at gibson.dropbear.id.au
Thu Feb 12 21:04:12 AEDT 2015


On Thu, Jan 22, 2015 at 11:53:26AM +1100, Peter Barker wrote:
> Encode buffers into base64 according to rfc4648.
> Decode base64-encoded buffers according to the same standard.

Sorry, I should have replied to this earlier.  I've reviewed a couple
of earlier versions of this patch before Peter posted it.

I'd really like to see base64 support in ccan - I have a project I'm
working on that could use it.

So,

Reviewed-by: David Gibson <david at gibson.dropbear.id.au>

Though I did spot a couple of small nits in this latest version:

> +/**
> + * sixbit_to_b64 - maps a 6-bit value to the base64 alphabet
> + * @param alphabet A base 64 alphabet (see base64_init_alphabet)
> + * @param sixbit Six-bit value to map
> + * @return a base 64 character
> + */
> +static char sixbit_to_b64(const base64_alphabet_t *alphabet, const char sixbit)

I'd actually suggest making your sixbit values type int, rather than
char.  It helps to emphasise that they're the numerical side, rather
than the encoded side.

> +{
> +	assert(sixbit >= 0);
> +	assert(sixbit <= 63);
> +
> +	return alphabet->encode_map[(unsigned char)sixbit];
> +}
> +
> +/**
> + * sixbit_from_b64 - maps a base64-alphabet character to its 6-bit value
> + * @param alphabet A base 64 alphabet (see base64_init_alphabet)
> + * @param sixbit Six-bit value to map
> + * @return a six-bit value
> + */
> +static signed char sixbit_from_b64(const base64_alphabet_t *alphabet,
> +				   const unsigned char b64letter)
> +{
> +	signed char ret;
> +
> +	ret = alphabet->decode_map[(unsigned char)b64letter];
> +	if (ret == '~') {

Using '~' on the *decoded* side seems rather odd.  the decode_map
could just use -1 as the error value.

> +		errno = ERANGE;
> +		return -1;
> +	}
> +
> +	return ret;
> +}
> +
> +int base64_char_in_alphabet(const base64_alphabet_t *alphabet,
> +			    const char b64char) {
> +	return (alphabet->decode_map[(unsigned char)b64char] != '~');
> +}
> +
> +void base64_init_alphabet(base64_alphabet_t *dest, const char src[64]) {

Another Perl style brace has crept in here.

> +	unsigned char i;
> +
> +	memcpy(dest->encode_map,src,64);
> +	memset(dest->decode_map,'~',256);
> +	for (i=0; i<64; i++) {
> +	  dest->decode_map[(unsigned char)src[i]] = i;

Looks like a nonstandard indent here.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20150212/6ed1a464/attachment.sig>


More information about the ccan mailing list