We have to create a command named base64 that will encode or decode strings into/from Base64.
The command must have to following flags:
-d: active decode mode
-e: enable encode mode (this is the default value)
-i: specify which file to read from
-o: specify which file to write to
-b: specify the maximum length of each line
If -i flag is not specified, read from stdin.
If -o flag is not specified, write to sdout.
Parameters can be overridden: if -i flag is specified two times, we keep the value of the last one.
This should also apply to -e and -d flags. -e sets the mode to encoding, while -d sets the mode to decoding. One way to implement that could be to have a field of enumeration type.
The objective of this algorithm is to transform binary data into printable text data.
To do that, the algorithm expands three 8-bit words into four 6-bit words. The relation is defined by 3 8 = 4 6.
As resulting words can not physically be stored on 6-bit words, they are put in 8-bit words.
The message bit length has then been multiplied by 4/3.
If the last input group can not contain three bytes, the third sextet will be padded with two 0 and the last output byte will be the padding character =.
If the last input group can only contain the first byte, the second sextet will be padded with four 0 and the two last output bytes will be the padding character =.
According to -b (b for break) argument, we may need to add newlines
We have to create a command named
base64
that will encode or decode strings into/from Base64.The command must have to following flags:
-d
: active decode mode-e
: enable encode mode (this is the default value)-i
: specify which file to read from-o
: specify which file to write to-b
: specify the maximum length of each lineIf
-i
flag is not specified, read from stdin. If-o
flag is not specified, write to sdout.Parameters can be overridden: if
-i
flag is specified two times, we keep the value of the last one. This should also apply to-e
and-d
flags.-e
sets the mode toencoding
, while-d
sets the mode todecoding
. One way to implement that could be to have a field of enumeration type.Base64 specifications
The Base64 algorithm uses a dictionary of 64 characters: https://en.wikipedia.org/wiki/Base64#Base64_table
Encoding
The objective of this algorithm is to transform binary data into printable text data. To do that, the algorithm expands three 8-bit words into four 6-bit words. The relation is defined by 3 8 = 4 6. As resulting words can not physically be stored on 6-bit words, they are put in 8-bit words. The message bit length has then been multiplied by 4/3.
If the last input group can not contain three bytes, the third sextet will be padded with two
0
and the last output byte will be the padding character=
. If the last input group can only contain the first byte, the second sextet will be padded with four0
and the two last output bytes will be the padding character=
.According to
-b
(b for break) argument, we may need to add newlinesDecoding
See https://en.wikipedia.org/wiki/Base64#Decoding_Base64_with_padding
As macOS
base64
command we will handle only decoding with padding:Space characters are simply discarded. Characters not present in Base64 dictionary throw an error.