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
1 changed files with 189 additions and 188 deletions

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
((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))))
;; TODO: be more clever than string-index
(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) (define base64-decode
(if (port-eof? port) (case-lambda
(eof-object) ((str)
(f (get-line port)))) (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))))
;; TODO: be more clever than string-index
(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))))
;; 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)
;; Some MIME data has header fields in the same format as mail
;; or http. These are ignored.
(let ((line (get-line-comp string-trim-both port)))
(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)))
(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)) ;Skip Radix-64 checksum
(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 (get-delimited-base64 port)
(case-lambda (define (get-first-data-line port)
((port type bv line-length) ;; Some MIME data has header fields in the same format as mail
(display (string-append "-----BEGIN " type "-----\n") port) ;; or http. These are ignored.
(base64-encode bv 0 (bytevector-length bv) (let ((line (get-line-comp string-trim-both port)))
line-length #f base64-alphabet port) (cond ((eof-object? line) line)
(display (string-append "\n-----END " type "-----\n") port)) ((string-index line #\:)
((port type bv) (let lp () ;read until empty line
(put-delimited-base64 port type bv 76))))) (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)) ;Skip Radix-64 checksum
(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
(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))))