]> gitweb.factorcode.org Git - factor.git/commitdiff
base64: Fix base64 for strings that don't end in equal signs.
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 9 Jun 2017 02:02:13 +0000 (21:02 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 9 Jun 2017 02:03:40 +0000 (21:03 -0500)
Some uses of base64 in the wild do not have trailing ==, such as JWT (JSON Web Token) strings.

basis/base64/base64-tests.factor
basis/base64/base64.factor

index 4e7e887ceb578de6f72b0e2b0b9d10a854eb8c71..d708c1b3d366b6a268724f4adc6ee6e421493e62 100644 (file)
@@ -1,5 +1,5 @@
 USING: base64 io.encodings.ascii io.encodings.string kernel
-sequences strings tools.test ;
+sequences splitting strings tools.test ;
 
 { "abcdefghijklmnopqrstuvwxyz" } [ "abcdefghijklmnopqrstuvwxyz" ascii encode >base64 base64> ascii decode
 ] unit-test
@@ -24,3 +24,18 @@ sequences strings tools.test ;
 
 [ { 33 52 17 40 12 51 33 43 18 33 23 } base64> ]
 [ malformed-base64? ] must-fail-with
+
+{
+    {
+        B{ 123 34 97 108 103 34 58 34 72 83 50 53 54 34 125 }
+        B{ 123 34 115 117 98 34 58 34 74 111 101 34 125 }
+        B{
+            138 151 175 68 219 145 63 161 223 148 111 28 20 169 230
+            80 251 114 166 187 145 11 135 219 212 53 173 160 178 250
+            217 38
+        }
+    }
+} [
+    "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"
+    "." split [ base64> ] map
+] unit-test
\ No newline at end of file
index 500784ef7f89dc219d6036c744bbf9c88faba47b..717a9e0d06262eaeffb05d17f7c377792469f56a 100644 (file)
@@ -84,7 +84,7 @@ PRIVATE>
     4 "\n\r" pick read-ignoring dup length {
         { 0 [ 2drop ] }
         { 4 [ decode4 (decode-base64) ] }
-        [ malformed-base64 ]
+        [ drop 4 CHAR: = pad-tail decode4 (decode-base64) ]
     } case ;
 
 PRIVATE>