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,46 +43,44 @@
;; 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)
(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
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
(define base64url-alphabet (define base64url-alphabet
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_") "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
(define base64-encode (define base64-encode
(case-lambda (case-lambda
;; Simple interface. Returns a string containing the canonical ;; Simple interface. Returns a string containing the canonical
;; base64 representation of the given bytevector. ;; base64 representation of the given bytevector.
@ -143,7 +142,8 @@
;; 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
(define base64-decode
(case-lambda (case-lambda
((str) ((str)
(base64-decode str base64-alphabet #f)) (base64-decode str base64-alphabet #f))
@ -192,7 +192,7 @@
(error 'base64-decode "invalid input" (error 'base64-decode "invalid input"
(list c1 c2 c3 c4))))))))))) (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))))
@ -201,7 +201,8 @@
;; 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-delimited-base64 port)
(define (get-first-data-line port) (define (get-first-data-line port)
;; Some MIME data has header fields in the same format as mail ;; Some MIME data has header fields in the same format as mail
;; or http. These are ignored. ;; or http. These are ignored.
@ -241,7 +242,7 @@
(else ;skip garbage (like in openssl x509 -in foo -text output). (else ;skip garbage (like in openssl x509 -in foo -text output).
(get-delimited-base64 port))))) (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)
@ -249,4 +250,4 @@
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))))