summaryrefslogtreecommitdiff
path: root/guix/utils.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2012-06-09 16:34:18 +0200
committerLudovic Courtès <ludo@gnu.org>2012-06-09 16:34:18 +0200
commit6d800a80eaa2a221ee44617fdd702bf7c92f22ed (patch)
treecea3bb6e835a27f0234f838eb1b4c3018e9930c7 /guix/utils.scm
parentc8369caccef256f9e7bfa02ac2cc7fcbd72db04f (diff)
Add `base16-string->bytevector'.
* guix/utils.scm (base16-string->bytevector): New procedure. * tests/utils.scm ("bytevector->base16-string->bytevector"): New test.
Diffstat (limited to 'guix/utils.scm')
-rw-r--r--guix/utils.scm28
1 files changed, 28 insertions, 0 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index 65e89a0e1b..77ed9ce6ee 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -35,6 +35,7 @@
bytevector->base16-string
base32-string->bytevector
nix-base32-string->bytevector
+ base16-string->bytevector
sha256
%nixpkgs-directory
@@ -327,6 +328,33 @@ starting from the right of S."
(loop (+ 1 i)
(cons (vector-ref chars (bytevector-u8-ref bv i)) r))))))
+(define base16-string->bytevector
+ (let ((chars->value (fold (lambda (i r)
+ (vhash-consv (string-ref (number->string i 16)
+ 0)
+ i r))
+ vlist-null
+ (iota 16))))
+ (lambda (s)
+ "Return the bytevector whose hexadecimal representation is string S."
+ (define bv
+ (make-bytevector (quotient (string-length s) 2) 0))
+
+ (string-fold (lambda (chr i)
+ (let ((j (quotient i 2))
+ (v (and=> (vhash-assv chr chars->value) cdr)))
+ (if v
+ (if (zero? (logand i 1))
+ (bytevector-u8-set! bv j
+ (arithmetic-shift v 4))
+ (let ((w (bytevector-u8-ref bv j)))
+ (bytevector-u8-set! bv j (logior v w))))
+ (error "invalid hexadecimal character" chr)))
+ (+ i 1))
+ 0
+ s)
+ bv)))
+
;;;
;;; Hash.