summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--base64.joy29
1 files changed, 29 insertions, 0 deletions
diff --git a/base64.joy b/base64.joy
new file mode 100644
index 0000000..0aa97b4
--- /dev/null
+++ b/base64.joy
@@ -0,0 +1,29 @@
+DEFINE
+shl == 2 *;
+shr == 2 /;
+and63 == [64 / 64 *] nullary -;
+b64enc1 == ord 16 [shl] times [ord 8 [shl] times] dip + [ord] dip +;
+b64enc2 == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" swap [] [
+		[18 [shr] times and63 at]
+		[12 [shr] times and63 at]
+		[6  [shr] times and63 at]
+		[and63 at]
+	      ] construct "" cons cons cons cons [pop] dip;
+
+s2l == [null]  [pop []]  [uncons]  [cons]  linrec;
+b64enc ==
+	s2l
+	"" swap
+	# pad with null bytes
+	# calculate how many bytes to pad
+	[size 3 rem 3 swap - 3 rem] nullary
+	# i cant use null bytes in strings (hence s2l)
+	[0 chr [] cons concat] times
+	# len(s)/3 times
+	[size 3 /] nullary
+	[[3 take] nullary s2l [b64enc1 b64enc2] infra first rollup [swap concat] dip 3 drop] times
+	# remove the now-empty string
+	pop
+	"\n" concat
+END
+stdin [feof not] [fgets b64enc putchars] while fclose.