base64: Turn into a regular Guile module.

* guix/base64.scm: Replace 'library' form with 'define-module'.
This commit is contained in:
Ludovic Courtès 2017-05-25 14:11:22 +02:00
parent faa6bdf8ad
commit 4862a98be4
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5

View file

@ -5,6 +5,7 @@
;; February 12, 2014. ;; February 12, 2014.
;; ;;
;; Some optimizations made by Ludovic Courtès <ludo@gnu.org>, 2015. ;; Some optimizations made by Ludovic Courtès <ludo@gnu.org>, 2015.
;; Turned into a Guile module (instead of R6RS).
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by ;; it under the terms of the GNU General Public License as published by
@ -42,211 +43,211 @@
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE. ;; DEALINGS IN THE SOFTWARE.
#!r6rs
;; RFC 4648 Base-N Encodings ;; RFC 4648 Base-N Encodings
(library (guix base64) (define-module (guix base64)
(export base64-encode #:export (base64-encode
base64-decode base64-decode
base64-alphabet base64-alphabet
base64url-alphabet base64url-alphabet
get-delimited-base64 get-delimited-base64
put-delimited-base64) put-delimited-base64)
(import (rnrs) #:use-module (rnrs)
(only (srfi :13 strings) #:use-module ((srfi srfi-13)
string-index #:select (string-index
string-prefix? string-suffix? string-prefix? string-suffix?
string-concatenate string-trim-both) string-concatenate string-trim-both)))
(only (guile) ash logior))
(define-syntax define-alias (define-syntax define-alias
(syntax-rules () (syntax-rules ()
((_ new old) ((_ new old)
(define-syntax new (identifier-syntax old))))) (define-syntax new (identifier-syntax old)))))
;; Force the use of Guile's own primitives to avoid the overhead of its 'fx' ;; Force the use of Guile's own primitives to avoid the overhead of its 'fx'
;; procedures. ;; procedures.
(define-alias fxbit-field bitwise-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 base64-alphabet (define-alias fxbit-field bitwise-bit-field)
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") (define-alias fxarithmetic-shift ash)
(define-alias fxarithmetic-shift-left ash)
(define-alias fxand logand)
(define-alias fxior logior)
(define-alias fxxor logxor)
(define base64url-alphabet (define base64-alphabet
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define base64-encode (define base64url-alphabet
(case-lambda "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
;; Simple interface. Returns a string containing the canonical
;; base64 representation of the given bytevector. (define base64-encode
((bv) (case-lambda
(base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f)) ;; Simple interface. Returns a string containing the canonical
((bv start) ;; base64 representation of the given bytevector.
(base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f)) ((bv)
((bv start end) (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f))
(base64-encode bv start end #f #f base64-alphabet #f)) ((bv start)
((bv start end line-length) (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f))
(base64-encode bv start end line-length #f base64-alphabet #f)) ((bv start end)
((bv start end line-length no-padding) (base64-encode bv start end #f #f base64-alphabet #f))
(base64-encode bv start end line-length no-padding base64-alphabet #f)) ((bv start end line-length)
((bv start end line-length no-padding alphabet) (base64-encode bv start end line-length #f base64-alphabet #f))
(base64-encode bv start end line-length no-padding alphabet #f)) ((bv start end line-length no-padding)
;; Base64 encodes the bytes [start,end[ in the given bytevector. (base64-encode bv start end line-length no-padding base64-alphabet #f))
;; Lines are limited to line-length characters (unless #f), ((bv start end line-length no-padding alphabet)
;; which must be a multiple of four. To omit the padding (base64-encode bv start end line-length no-padding alphabet #f))
;; characters (#\=) set no-padding to a true value. If port is ;; Base64 encodes the bytes [start,end[ in the given bytevector.
;; #f, returns a string. ;; Lines are limited to line-length characters (unless #f),
((bv start end line-length no-padding alphabet port) ;; which must be a multiple of four. To omit the padding
(assert (or (not line-length) (zero? (mod line-length 4)))) ;; characters (#\=) set no-padding to a true value. If port is
(let-values (((p extract) (if port ;; #f, returns a string.
(values port (lambda () (values))) ((bv start end line-length no-padding alphabet port)
(open-string-output-port)))) (assert (or (not line-length) (zero? (mod line-length 4))))
(letrec ((put (if line-length (let-values (((p extract) (if port
(let ((chars 0)) (values port (lambda () (values)))
(lambda (p c) (open-string-output-port))))
(when (fx=? chars line-length) (letrec ((put (if line-length
(set! chars 0) (let ((chars 0))
(put-char p #\linefeed)) (lambda (p c)
(set! chars (fx+ chars 1)) (when (fx=? chars line-length)
(put-char p c))) (set! chars 0)
put-char))) (put-char p #\linefeed))
(let lp ((i start)) (set! chars (fx+ chars 1))
(cond ((= i end)) (put-char p c)))
((<= (+ i 3) end) put-char)))
(let ((x (bytevector-uint-ref bv i (endianness big) 3))) (let lp ((i start))
(put p (string-ref alphabet (fxbit-field x 18 24))) (cond ((= i end))
(put p (string-ref alphabet (fxbit-field x 12 18))) ((<= (+ i 3) end)
(put p (string-ref alphabet (fxbit-field x 6 12))) (let ((x (bytevector-uint-ref bv i (endianness big) 3)))
(put p (string-ref alphabet (fxbit-field x 0 6))) (put p (string-ref alphabet (fxbit-field x 18 24)))
(lp (+ i 3)))) (put p (string-ref alphabet (fxbit-field x 12 18)))
((<= (+ i 2) end) (put p (string-ref alphabet (fxbit-field x 6 12)))
(let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8))) (put p (string-ref alphabet (fxbit-field x 0 6)))
(put p (string-ref alphabet (fxbit-field x 18 24))) (lp (+ i 3))))
(put p (string-ref alphabet (fxbit-field x 12 18))) ((<= (+ i 2) end)
(put p (string-ref alphabet (fxbit-field x 6 12))) (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8)))
(unless no-padding (put p (string-ref alphabet (fxbit-field x 18 24)))
(put p #\=)))) (put p (string-ref alphabet (fxbit-field x 12 18)))
(else (put p (string-ref alphabet (fxbit-field x 6 12)))
(let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16))) (unless no-padding
(put p (string-ref alphabet (fxbit-field x 18 24))) (put p #\=))))
(put p (string-ref alphabet (fxbit-field x 12 18))) (else
(unless no-padding (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16)))
(put p #\=) (put p (string-ref alphabet (fxbit-field x 18 24)))
(put p #\=))))))) (put p (string-ref alphabet (fxbit-field x 12 18)))
(extract))))) (unless no-padding
(put p #\=)
(put p #\=)))))))
(extract)))))
;; Decodes a base64 string. The string must contain only pure ;; Decodes a base64 string. The string must contain only pure
;; unpadded base64 data. ;; unpadded base64 data.
(define base64-decode
(case-lambda (define base64-decode
((str) (case-lambda
(base64-decode str base64-alphabet #f)) ((str)
((str alphabet) (base64-decode str base64-alphabet #f))
(base64-decode str alphabet #f)) ((str alphabet)
((str alphabet port) (base64-decode str alphabet #f))
(unless (zero? (mod (string-length str) 4)) ((str alphabet port)
(error 'base64-decode (unless (zero? (mod (string-length str) 4))
"input string must be a multiple of four characters")) (error 'base64-decode
(let-values (((p extract) (if port "input string must be a multiple of four characters"))
(values port (lambda () (values))) (let-values (((p extract) (if port
(open-bytevector-output-port)))) (values port (lambda () (values)))
(do ((i 0 (+ i 4))) (open-bytevector-output-port))))
((= i (string-length str)) (do ((i 0 (+ i 4)))
(extract)) ((= i (string-length str))
(let ((c1 (string-ref str i)) (extract))
(c2 (string-ref str (+ i 1))) (let ((c1 (string-ref str i))
(c3 (string-ref str (+ i 2))) (c2 (string-ref str (+ i 1)))
(c4 (string-ref str (+ i 3)))) (c3 (string-ref str (+ i 2)))
;; TODO: be more clever than string-index (c4 (string-ref str (+ i 3))))
(let ((i1 (string-index alphabet c1)) ;; TODO: be more clever than string-index
(i2 (string-index alphabet c2)) (let ((i1 (string-index alphabet c1))
(i3 (string-index alphabet c3)) (i2 (string-index alphabet c2))
(i4 (string-index alphabet c4))) (i3 (string-index alphabet c3))
(cond ((and i1 i2 i3 i4) (i4 (string-index alphabet c4)))
(let ((x (fxior (fxarithmetic-shift-left i1 18) (cond ((and i1 i2 i3 i4)
(fxarithmetic-shift-left i2 12) (let ((x (fxior (fxarithmetic-shift-left i1 18)
(fxarithmetic-shift-left i3 6) (fxarithmetic-shift-left i2 12)
i4))) (fxarithmetic-shift-left i3 6)
(put-u8 p (fxbit-field x 16 24)) i4)))
(put-u8 p (fxbit-field x 8 16)) (put-u8 p (fxbit-field x 16 24))
(put-u8 p (fxbit-field x 0 8)))) (put-u8 p (fxbit-field x 8 16))
((and i1 i2 i3 (char=? c4 #\=) (put-u8 p (fxbit-field x 0 8))))
(= i (- (string-length str) 4))) ((and i1 i2 i3 (char=? c4 #\=)
(let ((x (fxior (fxarithmetic-shift-left i1 18) (= i (- (string-length str) 4)))
(fxarithmetic-shift-left i2 12) (let ((x (fxior (fxarithmetic-shift-left i1 18)
(fxarithmetic-shift-left i3 6)))) (fxarithmetic-shift-left i2 12)
(put-u8 p (fxbit-field x 16 24)) (fxarithmetic-shift-left i3 6))))
(put-u8 p (fxbit-field x 8 16)))) (put-u8 p (fxbit-field x 16 24))
((and i1 i2 (char=? c3 #\=) (char=? c4 #\=) (put-u8 p (fxbit-field x 8 16))))
(= i (- (string-length str) 4))) ((and i1 i2 (char=? c3 #\=) (char=? c4 #\=)
(let ((x (fxior (fxarithmetic-shift-left i1 18) (= i (- (string-length str) 4)))
(fxarithmetic-shift-left i2 12)))) (let ((x (fxior (fxarithmetic-shift-left i1 18)
(put-u8 p (fxbit-field x 16 24)))) (fxarithmetic-shift-left i2 12))))
(else (put-u8 p (fxbit-field x 16 24))))
(error 'base64-decode "invalid input" (else
(list c1 c2 c3 c4))))))))))) (error 'base64-decode "invalid input"
(list c1 c2 c3 c4)))))))))))
(define (get-line-comp f port) (define (get-line-comp f port)
(if (port-eof? port) (if (port-eof? port)
(eof-object) (eof-object)
(f (get-line port)))) (f (get-line port))))
;; Reads the common -----BEGIN/END type----- delimited format from ;; Reads the common -----BEGIN/END type----- delimited format from
;; the given port. Returns two values: a string with the type and a ;; the given port. Returns two values: a string with the type and a
;; bytevector containing the base64 decoded data. The second value ;; bytevector containing the base64 decoded data. The second value
;; is the eof object if there is an eof before the BEGIN delimiter. ;; is the eof object if there is an eof before the BEGIN delimiter.
(define (get-delimited-base64 port)
(define (get-first-data-line port) (define (get-delimited-base64 port)
;; Some MIME data has header fields in the same format as mail (define (get-first-data-line port)
;; or http. These are ignored. ;; Some MIME data has header fields in the same format as mail
(let ((line (get-line-comp string-trim-both port))) ;; or http. These are ignored.
(cond ((eof-object? line) line)
((string-index line #\:)
(let lp () ;read until empty line
(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))) (let ((line (get-line-comp string-trim-both port)))
(cond ((eof-object? line) (cond ((eof-object? line) line)
(values "" (eof-object))) ((string-index line #\:)
((string=? line "") (let lp () ;read until empty line
(get-delimited-base64 port)) (let ((line (get-line-comp string-trim-both port)))
((and (string-prefix? "-----BEGIN " line) (if (string=? line "")
(string-suffix? "-----" line)) (get-line-comp string-trim-both port)
(let* ((type (substring line 11 (- (string-length line) 5))) (lp)))))
(endline (string-append "-----END " type "-----"))) (else line))))
(let-values (((outp extract) (open-bytevector-output-port))) (let ((line (get-line-comp string-trim-both port)))
(let lp ((line (get-first-data-line port))) (cond ((eof-object? line)
(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 (error 'get-delimited-base64
"unexpected end of file")) "bad end delimiter" type line))
((string-prefix? "-" line) (values type (extract)))
(unless (string=? line endline) (else
(error 'get-delimited-base64 (unless (and (= (string-length line) 5)
"bad end delimiter" type line)) (string-prefix? "=" line)) ;Skip Radix-64 checksum
(values type (extract))) (base64-decode line base64-alphabet outp))
(else (lp (get-line-comp string-trim-both port))))))))
(unless (and (= (string-length line) 5) (else ;skip garbage (like in openssl x509 -in foo -text output).
(string-prefix? "=" line)) ;Skip Radix-64 checksum (get-delimited-base64 port)))))
(base64-decode line base64-alphabet outp))
(lp (get-line-comp string-trim-both port))))))))
(else ;skip garbage (like in openssl x509 -in foo -text output).
(get-delimited-base64 port)))))
(define put-delimited-base64 (define put-delimited-base64
(case-lambda (case-lambda
((port type bv line-length) ((port type bv line-length)
(display (string-append "-----BEGIN " type "-----\n") port) (display (string-append "-----BEGIN " type "-----\n") port)
(base64-encode bv 0 (bytevector-length bv) (base64-encode bv 0 (bytevector-length bv)
line-length #f base64-alphabet port) line-length #f base64-alphabet port)
(display (string-append "\n-----END " type "-----\n") port)) (display (string-append "\n-----END " type "-----\n") port))
((port type bv) ((port type bv)
(put-delimited-base64 port type bv 76))))) (put-delimited-base64 port type bv 76))))