diff --git a/basis/base64/base64-tests.factor b/basis/base64/base64-tests.factor index a63f6b3bc6..e3ab35d250 100644 --- a/basis/base64/base64-tests.factor +++ b/basis/base64/base64-tests.factor @@ -39,3 +39,9 @@ sequences splitting strings tools.test ; "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY" "." split [ base64> ] map ] unit-test + +{ "01a+b/cd" } [ "\xd3V\xbeo\xf7\x1d" >base64 "" like ] unit-test +{ "\xd3V\xbeo\xf7\x1d" } [ "01a+b/cd" base64> "" like ] unit-test + +{ "01a-b_cd" } [ "\xd3V\xbeo\xf7\x1d" >urlsafe-base64 "" like ] unit-test +{ "\xd3V\xbeo\xf7\x1d" } [ "01a-b_cd" urlsafe-base64> "" like ] unit-test diff --git a/basis/base64/base64.factor b/basis/base64/base64.factor index 717a9e0d06..aff50fb230 100644 --- a/basis/base64/base64.factor +++ b/basis/base64/base64.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2008 Doug Coleman, Daniel Ehrenberg. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays combinators fry io io.binary io.encodings.binary -io.streams.byte-array kernel literals math namespaces sbufs -sequences ; +USING: arrays assocs byte-arrays combinators fry io io.binary +io.encodings.binary io.streams.byte-array kernel literals math +namespaces sbufs sequences ; IN: base64 ERROR: malformed-base64 ; @@ -10,8 +10,10 @@ ERROR: malformed-base64 ; byte-array +] : alphabet-inverse ( alphabet -- seq ) dup supremum 1 + f [ @@ -100,3 +102,21 @@ PRIVATE> : >base64-lines ( seq -- base64 ) binary [ binary [ encode-base64-lines ] with-byte-reader ] with-byte-writer ; + +: >urlsafe-base64 ( seq -- base64 ) + >base64 H{ + { CHAR: + CHAR: - } + { CHAR: / CHAR: _ } + } substitute ; + +: urlsafe-base64> ( base64 -- seq ) + H{ + { CHAR: - CHAR: + } + { CHAR: _ CHAR: / } + } substitute base64> ; + +: >urlsafe-base64-lines ( seq -- base64 ) + >base64-lines H{ + { CHAR: + CHAR: - } + { CHAR: / CHAR: _ } + } substitute ; diff --git a/basis/base64/summary.txt b/basis/base64/summary.txt index 89950e2a7b..487909bb72 100644 --- a/basis/base64/summary.txt +++ b/basis/base64/summary.txt @@ -1 +1 @@ -Base64 encoding/decoding +Base64 encoding/decoding (RFC 3548)