A POSIX shell-only Base64 decoder

TL;DR

Implementing a fallback Base64 in POSIX shell - only.

Sometimes you might need to decode a Base64-encoded file and your environment does not contain a decoder for it. What can you do?

Taking inspiration from this clever script, mixed with the evergreen Rich’s sh (POSIX shell) tricks, I’ve come up with the following:

Local version here

The initial part (lines 2 through 7) ensure that the base64-encoded input is processed in batches of 64 characters. Any multiple of 4 should do here, because each character is capable of emitting at most 6 bits and we eventually need them to be groupable as octets (i.e. multiples of 8 bits); each group of 4 inputs resolves to providing 3 bytes, some of which might be padding that is eliminated.

The transformation (lines 8 through 28) is clever in my opinion: it makes sure to transform each input character in a series of bit sequence representations, using characters @ and ,. They are chosen because they do not fall in the allowed range for regular base64-encoded data (line 0 and 1 would), hence it’s possible to leverage them to transform each input character in a succession of sed operations.

Afterwards, these bit strings are transformed into hexadecimal nibbles (line 29 through 33) and eventually turned into binary data (lines 34 through 36).

This generally works… although not everywhere. As an example, there is a bug in older versions of dash that cuts all NUL values (i.e. the 0x00), making it… less than useful, at least for generic binary data.


Comments? Octodon, , GitHub, Reddit, or drop me a line!