views:

132

answers:

1

As I understand the spec, a Base64 encoder

a) takes the source binary, and pads it out with zeroes to be a multiple of 24 bytes long.

b) it then transcodes it, six bits at a time, to the target set of 64 characters (A..Z, a..z, 0..9, +, -). If it finds the last two bytes (16 bits) have been zero-padded, the last two characters are transcoded as '=='. If it finds that the last one byte (8 bits) have been zero-padded, the last character is transcoded as '='.

My question is, in step (b), how does it know that the last bytes are zeroes because they have been padded vs. they are zeroes because thay are part of the valid binary source data?

Is it that the subsystem that is responsible for part (b) has to know what took place during part (a)

+3  A: 

The encoder (as opposed to the decoder) will know the length of the input data and be able to figure out whether to output nothing, "=" or "==" at the end. Your question assumes there is no connect between the two stages you mention but that is not true in the implementations I've seen.

The implementation I had to write didn't do the first stage at all since it had routines to extract 6-bit groups from the input stream one at a time, incrementing byteCount each time. Then at the end, the expression "byteCount%3" was used to decide which string to append to the output stream.

paxdiablo