]> gitweb.factorcode.org Git - factor.git/commitdiff
base64: adding urlsafe base64 and specify RFC 3548.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 5 Apr 2019 19:03:00 +0000 (12:03 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 5 Apr 2019 19:03:00 +0000 (12:03 -0700)
basis/base64/base64-tests.factor
basis/base64/base64.factor
basis/base64/summary.txt

index a63f6b3bc63e900f065e309775536b5dfb2d3159..e3ab35d250bdd69988135ee01a03c6a0e48a5480 100644 (file)
@@ -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
index 717a9e0d06262eaeffb05d17f7c377792469f56a..aff50fb2309b946bab75ab1b4cdbbff2c62831c2 100644 (file)
@@ -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 ;
 <PRIVATE
 
 <<
-CONSTANT: alphabet
+CONSTANT: alphabet $[
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+    >byte-array
+]
 
 : alphabet-inverse ( alphabet -- seq )
     dup supremum 1 + f <array> [
@@ -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 ;
index 89950e2a7b9cc4339533e81a3acf0030f981cd39..487909bb72ec9f9c60e043d96a5724281d19cdcf 100644 (file)
@@ -1 +1 @@
-Base64 encoding/decoding
+Base64 encoding/decoding (RFC 3548)