5 ms·
Fun small educational project: If you want to learn how to work with units smaller than a byte, implement Base64 from scratch. It’s really fun!
by cpach 9y ago
Fun small educational project: If you want to learn how to work with units smaller than a byte, implement Base64 from scratch. It’s really fun!
- psykotic 9y agoIn case anyone might be mislead given the topic of this article, you shouldn't do base64 encoding with a general purpose routine like getbits(). Since 3 * 8 / 6 = 4 you can process the input bytes in groups of 3 to avoid straddling issues (ideally with an unaligned 32-bit load if they're fast on your platform). Then your inner loop will look like this: uint32_t triple = *(uint32_t *)in; *out++ = base64[triple & MASK]; *out++ = base64[(triple >> 6) & MASK]; *out++ = base64[(triple >> 12) & MASK]; *out++ = base64[(triple >> 18) & MASK]; in += 3; (The 32-bit load can read one byte past the end of the buffer, so allocate extra space or stop the loop early.)
- wmu 9y agoBetter would be the Huffman coding; one has to deal with variable bitfield widths.
- zmodem 9y agoIn fact, that appears to be where the author is coming from. Part 2 mentions techniques coming from the Kraken Huffman decoder (http://www.radgametools.com/oodlecompressors.htm http://www.radgametools.com/oodlecompressors.htm)