From c19ba72fc241b19423e5dd54b02d7b2f441ced97 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 9 Nov 2024 12:56:34 -0500 Subject: base64: Test for invalid characters when decoding. --- chickadee/base64.scm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/chickadee/base64.scm b/chickadee/base64.scm index 89fc4bb..c12fa39 100644 --- a/chickadee/base64.scm +++ b/chickadee/base64.scm @@ -45,9 +45,9 @@ (let ((cache (make-hash-table))) (lambda (alphabet) (or (hash-ref cache alphabet) - (let ((bv (make-bytevector 128))) + (let ((bv (make-bytevector 128 -1))) (alphabet-for-each (lambda (i x) - (bytevector-u8-set! bv x i)) + (bytevector-s8-set! bv x i)) alphabet) (hash-set! cache alphabet bv) bv))))) @@ -56,7 +56,11 @@ "Decode the base64 encoded string @var{str} using @var{alphabet} and return a bytevector containing the decoded data." (define decoder (alphabet->decoder alphabet)) - (define (decode char) (bytevector-u8-ref decoder (char->integer char))) + (define (decode char) + (let ((x (bytevector-s8-ref decoder (char->integer char)))) + (when (eq? x -1) + (error "invalid base64 character" char)) + x)) (call-with-input-string str (lambda (in) (call-with-output-bytevector -- cgit v1.2.3