123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- (define-module (guix base64)
- #:export (base64-encode
- base64-decode
- base64-alphabet
- base64url-alphabet
- get-delimited-base64
- put-delimited-base64)
- #:use-module (srfi srfi-11)
- #:use-module (srfi srfi-60)
- #:use-module (rnrs bytevectors)
- #:use-module (rnrs io ports))
- (define-syntax define-alias
- (syntax-rules ()
- ((_ new old)
- (define-syntax new (identifier-syntax old)))))
- (define-alias fxbit-field bit-field)
- (define-alias fxarithmetic-shift ash)
- (define-alias fxarithmetic-shift-left ash)
- (define-alias fxand logand)
- (define-alias fxior logior)
- (define-alias fxxor logxor)
- (define-alias fx=? =)
- (define-alias fx+ +)
- (define-alias mod modulo)
- (define-syntax-rule (assert exp)
- (unless exp
- (throw 'assertion-failure 'exp)))
- (define base64-alphabet
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
- (define base64url-alphabet
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
- (define base64-encode
- (case-lambda
-
-
- ((bv)
- (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f))
- ((bv start)
- (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f))
- ((bv start end)
- (base64-encode bv start end #f #f base64-alphabet #f))
- ((bv start end line-length)
- (base64-encode bv start end line-length #f base64-alphabet #f))
- ((bv start end line-length no-padding)
- (base64-encode bv start end line-length no-padding base64-alphabet #f))
- ((bv start end line-length no-padding alphabet)
- (base64-encode bv start end line-length no-padding alphabet #f))
-
-
-
-
-
- ((bv start end line-length no-padding alphabet port)
- (assert (or (not line-length) (zero? (mod line-length 4))))
- (let-values (((p extract) (if port
- (values port (lambda () (values)))
- (open-string-output-port))))
- (letrec ((put (if line-length
- (let ((chars 0))
- (lambda (p c)
- (when (fx=? chars line-length)
- (set! chars 0)
- (put-char p #\linefeed))
- (set! chars (fx+ chars 1))
- (put-char p c)))
- put-char)))
- (let lp ((i start))
- (cond ((= i end))
- ((<= (+ i 3) end)
- (let ((x (bytevector-uint-ref bv i (endianness big) 3)))
- (put p (string-ref alphabet (fxbit-field x 18 24)))
- (put p (string-ref alphabet (fxbit-field x 12 18)))
- (put p (string-ref alphabet (fxbit-field x 6 12)))
- (put p (string-ref alphabet (fxbit-field x 0 6)))
- (lp (+ i 3))))
- ((<= (+ i 2) end)
- (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8)))
- (put p (string-ref alphabet (fxbit-field x 18 24)))
- (put p (string-ref alphabet (fxbit-field x 12 18)))
- (put p (string-ref alphabet (fxbit-field x 6 12)))
- (unless no-padding
- (put p #\=))))
- (else
- (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16)))
- (put p (string-ref alphabet (fxbit-field x 18 24)))
- (put p (string-ref alphabet (fxbit-field x 12 18)))
- (unless no-padding
- (put p #\=)
- (put p #\=)))))))
- (extract)))))
-
-
-
- (define base64-decode
- (case-lambda
- ((str)
- (base64-decode str base64-alphabet #f))
- ((str alphabet)
- (base64-decode str alphabet #f))
- ((str alphabet port)
- (unless (zero? (mod (string-length str) 4))
- (error 'base64-decode
- "input string must be a multiple of four characters"))
- (let-values (((p extract) (if port
- (values port (lambda () (values)))
- (open-bytevector-output-port))))
- (do ((i 0 (+ i 4)))
- ((= i (string-length str))
- (extract))
- (let ((c1 (string-ref str i))
- (c2 (string-ref str (+ i 1)))
- (c3 (string-ref str (+ i 2)))
- (c4 (string-ref str (+ i 3))))
-
- (let ((i1 (string-index alphabet c1))
- (i2 (string-index alphabet c2))
- (i3 (string-index alphabet c3))
- (i4 (string-index alphabet c4)))
- (cond ((and i1 i2 i3 i4)
- (let ((x (fxior (fxarithmetic-shift-left i1 18)
- (fxarithmetic-shift-left i2 12)
- (fxarithmetic-shift-left i3 6)
- i4)))
- (put-u8 p (fxbit-field x 16 24))
- (put-u8 p (fxbit-field x 8 16))
- (put-u8 p (fxbit-field x 0 8))))
- ((and i1 i2 i3 (char=? c4 #\=)
- (= i (- (string-length str) 4)))
- (let ((x (fxior (fxarithmetic-shift-left i1 18)
- (fxarithmetic-shift-left i2 12)
- (fxarithmetic-shift-left i3 6))))
- (put-u8 p (fxbit-field x 16 24))
- (put-u8 p (fxbit-field x 8 16))))
- ((and i1 i2 (char=? c3 #\=) (char=? c4 #\=)
- (= i (- (string-length str) 4)))
- (let ((x (fxior (fxarithmetic-shift-left i1 18)
- (fxarithmetic-shift-left i2 12))))
- (put-u8 p (fxbit-field x 16 24))))
- (else
- (error 'base64-decode "invalid input"
- (list c1 c2 c3 c4)))))))))))
- (define (get-line-comp f port)
- (if (port-eof? port)
- (eof-object)
- (f (get-line port))))
-
-
-
-
-
- (define (get-delimited-base64 port)
- (define (get-first-data-line port)
-
-
- (let ((line (get-line-comp string-trim-both port)))
- (cond ((eof-object? line) line)
- ((string-index line #\:)
- (let lp ()
- (let ((line (get-line-comp string-trim-both port)))
- (if (string=? line "")
- (get-line-comp string-trim-both port)
- (lp)))))
- (else line))))
- (let ((line (get-line-comp string-trim-both port)))
- (cond ((eof-object? line)
- (values "" (eof-object)))
- ((string=? line "")
- (get-delimited-base64 port))
- ((and (string-prefix? "-----BEGIN " line)
- (string-suffix? "-----" line))
- (let* ((type (substring line 11 (- (string-length line) 5)))
- (endline (string-append "-----END " type "-----")))
- (let-values (((outp extract) (open-bytevector-output-port)))
- (let lp ((line (get-first-data-line port)))
- (cond ((eof-object? line)
- (error 'get-delimited-base64
- "unexpected end of file"))
- ((string-prefix? "-" line)
- (unless (string=? line endline)
- (error 'get-delimited-base64
- "bad end delimiter" type line))
- (values type (extract)))
- (else
- (unless (and (= (string-length line) 5)
- (string-prefix? "=" line))
- (base64-decode line base64-alphabet outp))
- (lp (get-line-comp string-trim-both port))))))))
- (else
- (get-delimited-base64 port)))))
- (define put-delimited-base64
- (case-lambda
- ((port type bv line-length)
- (display (string-append "-----BEGIN " type "-----\n") port)
- (base64-encode bv 0 (bytevector-length bv)
- line-length #f base64-alphabet port)
- (display (string-append "\n-----END " type "-----\n") port))
- ((port type bv)
- (put-delimited-base64 port type bv 76))))
|