aboutsummaryrefslogtreecommitdiff
path: root/lib/base64.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-11 15:50:46 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-11 15:50:46 -0800
commit4efe44ed271aeed8e25e909344788d92a0d9f82b (patch)
tree73766440b53031d4fc8210dbe3b0aece47e6b852 /lib/base64.nom
parentba03cb67c3c8ba53451eba25dd2186f095cd1db2 (diff)
Fully upgraded to 4.10.12.7, including deprecating the old list/dict
comprehension methods, in favor of the new native support.
Diffstat (limited to 'lib/base64.nom')
-rw-r--r--lib/base64.nom12
1 files changed, 5 insertions, 7 deletions
diff --git a/lib/base64.nom b/lib/base64.nom
index 2091615..01d4f2f 100644
--- a/lib/base64.nom
+++ b/lib/base64.nom
@@ -1,18 +1,18 @@
-#!/usr/bin/env nomsu -V4.8.10
+#!/usr/bin/env nomsu -V4.10.12.7
#
This file defines actions for encoding/decoding base 64, as specified in:
https://tools.ietf.org/html/rfc4648
-
+
%b64_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-%reverse_b64 = (%b64_str.%i = (%i - 1) for %i in 1 to (size of %b64_str))
+%reverse_b64 = {: for %i in 1 to (size of %b64_str): add %b64_str.%i = (%i - 1)}
%reverse_b64."=" = 0
-
test:
%cases = ["", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"]
for %len = %encoded in %cases:
%plain = "foobar".[1, %len - 1]
assume (base64 %plain) == %encoded
assume (base64 decode %encoded) == %plain
+
externally [base64 %str, base64 encode %str, %str base64] all mean:
%chars = []
for %i in 1 to (size of %str) via 3:
@@ -33,18 +33,16 @@ externally [base64 %str, base64 encode %str, %str base64] all mean:
%chars::add %b64_str.(((%bytes.1 & 3) << 4) + 1)
%chars::add "="
%chars::add "="
-
return (%chars::joined)
externally (chr %) means (=lua "string.char(\%)")
externally [decode base64 %str, %str base64 decoded, base64 decode %str] all mean:
%chars = []
for %i in 1 to (size of %str) via 4:
- %indices = (%reverse_b64.(%str.%) for % in %i to (%i + 3))
+ %indices = [: for % in %i to (%i + 3): add %reverse_b64.(%str.%)]
%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)