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.
;;
;; 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
;; 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
;; DEALINGS IN THE SOFTWARE.
#!r6rs
;; RFC 4648 Base-N Encodings
(library (guix base64)
(export base64-encode
(define-module (guix base64)
#:export (base64-encode
base64-decode
base64-alphabet
base64url-alphabet
get-delimited-base64
put-delimited-base64)
(import (rnrs)
(only (srfi :13 strings)
string-index
#:use-module (rnrs)
#:use-module ((srfi srfi-13)
#:select (string-index
string-prefix? string-suffix?
string-concatenate string-trim-both)
(only (guile) ash logior))
string-concatenate string-trim-both)))
(define-syntax define-alias
(define-syntax define-alias
(syntax-rules ()
((_ new old)
(define-syntax new (identifier-syntax old)))))
;; Force the use of Guile's own primitives to avoid the overhead of its 'fx'
;; 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)
;; Force the use of Guile's own primitives to avoid the overhead of its 'fx'
;; procedures.
(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+/")
(define base64url-alphabet
(define base64url-alphabet
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
(define base64-encode
(define base64-encode
(case-lambda
;; Simple interface. Returns a string containing the canonical
;; base64 representation of the given bytevector.
@ -143,7 +142,8 @@
;; Decodes a base64 string. The string must contain only pure
;; unpadded base64 data.
(define base64-decode
(define base64-decode
(case-lambda
((str)
(base64-decode str base64-alphabet #f))
@ -192,7 +192,7 @@
(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)
(eof-object)
(f (get-line port))))
@ -201,7 +201,8 @@
;; the given port. Returns two values: a string with the type and a
;; bytevector containing the base64 decoded data. The second value
;; 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)
;; Some MIME data has header fields in the same format as mail
;; or http. These are ignored.
@ -241,7 +242,7 @@
(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
((port type bv line-length)
(display (string-append "-----BEGIN " type "-----\n") port)
@ -249,4 +250,4 @@
line-length #f base64-alphabet port)
(display (string-append "\n-----END " type "-----\n") port))
((port type bv)
(put-delimited-base64 port type bv 76)))))
(put-delimited-base64 port type bv 76))))