nomsu/lib/base64.nom

51 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-09-14 19:17:09 -07:00
#!/usr/bin/env nomsu -V4.8.8.6
#
This file defines actions for encoding/decoding base 64, as specified in:
https://tools.ietf.org/html/rfc4648
%b64_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
2018-08-30 14:16:09 -07:00
%reverse_b64 = (%b64_str.%i = (%i - 1) for %i in 1 to (size of %b64_str))
%reverse_b64."=" = 0
test:
%cases = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"]
for %len = %encoded in %cases:
%plain = "foobar".[1, %len - 1]
2018-09-14 19:17:09 -07:00
assume (base64 %plain) == %encoded
assume (base64 decode %encoded) == %plain
action [base64 %str, base64 encode %str, %str base64]:
%chars = []
2018-08-30 14:16:09 -07:00
for %i in 1 to (size of %str) via 3:
%bytes = [=lua "\%str:byte(\%i, \(%i + 2))"]
%chars::add %b64_str.(((%bytes.1 & 252) >> 2) + 1)
2018-08-30 14:16:09 -07:00
if (size of %bytes) is:
3:
%chars::add %b64_str.(((%bytes.1 & 3) << 4) + ((%bytes.2 & 240) >> 4) + 1)
%chars::add %b64_str.(((%bytes.2 & 15) << 2) + ((%bytes.3 & 192) >> 6) + 1)
%chars::add %b64_str.((%bytes.3 & 63) + 1)
2:
%chars::add %b64_str.(((%bytes.1 & 3) << 4) + ((%bytes.2 & 240) >> 4) + 1)
%chars::add %b64_str.(((%bytes.2 & 15) << 2) + 1)
%chars::add "="
1:
%chars::add %b64_str.(((%bytes.1 & 3) << 4) + 1)
%chars::add "="
%chars::add "="
return (%chars::joined)
action [chr %] (=lua "string.char(\%)")
action [decode base64 %str, %str base64 decoded, base64 decode %str]:
%chars = []
2018-08-30 14:16:09 -07:00
for %i in 1 to (size of %str) via 4:
%indices = (%reverse_b64.(%str.%) for % in %i to (%i + 3))
%chars::add (chr ((%indices.1 << 2) + ((%indices.2 & 48) >> 4)))
if (%str.(%i + 2) == "="): stop
%chars::add (chr (((%indices.2 & 15) << 4) + ((%indices.3 & 60) >> 2)))
if (%str.(%i + 3) == "="): stop
%chars::add (chr (((%indices.3 & 3) << 6) + %indices.4))
return (%chars::joined)