summary refs log tree commit diff
path: root/base64.joy
blob: 10827c7cf3fb68541c082ac99a12bad8430f24d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
DEFINE
shl 	== 2 *;
shr 	== 2 /;
and63 	== dup 64 / 64 * -;
b64enc1 == ord 16 [shl] times [ord 8 [shl] times] dip + [ord] dip +;
shrat   == [shr] times and63 at;
b64enc2 == ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" swap] [
	# [18 12 6 0 ] [[shrat] cons] map crashes
		[18 shrat]
		[12 shrat]
		[6  shrat]
		[0  shrat]
	   ] construct "" 4 [cons] times popd;

s2l 	== [null]  [pop []]  [uncons]  [cons]  linrec;
b64enc 	==
	s2l
	"" swap
	dup size 3 rem 3 swap - 3 rem
	[0 chr [] cons concat] times
	dup size 3 /
	[dup 3 take s2l [b64enc1 b64enc2] infra first rollup [swap concat] dip 3 drop] times
	pop
	"\n" concat;
END
stdin [feof not] [fgets b64enc putchars] while fclose.