gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-scheme] 02/03: hashcode: Complete translation to bytevector slic


From: gnunet
Subject: [gnunet-scheme] 02/03: hashcode: Complete translation to bytevector slices.
Date: Sun, 20 Nov 2022 20:50:40 +0100

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 63f3892f4f343a330595f995e3f8f76474fb9a8b
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sun Nov 20 19:21:09 2022 +0100

    hashcode: Complete translation to bytevector slices.
    
    * gnu/gnunet/hashcode.scm
    (<hashcode>,make-hashcode,<short-hashcode>,bv->hashcode,bv->short-hashcode,
    hashcode->bv,short-hashcode->bv): Adjust.
    * Makefile.am (modules): Add it.
---
 Makefile.am             |  1 +
 gnu/gnunet/hashcode.scm | 74 ++++++++++++++++++++++++++++---------------------
 2 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index dc4badb..79a5354 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,6 +50,7 @@ modules = \
   gnu/gnunet/concurrency/lost-and-found.scm \
   \
   gnu/gnunet/data-string.scm \
+  gnu/gnunet/hashcode.scm \
   \
   gnu/gnunet/mq/envelope.scm \
   gnu/gnunet/mq/error-reporting.scm \
diff --git a/gnu/gnunet/hashcode.scm b/gnu/gnunet/hashcode.scm
index 8ef9a06..a19884c 100644
--- a/gnu/gnunet/hashcode.scm
+++ b/gnu/gnunet/hashcode.scm
@@ -1,6 +1,6 @@
 ;#!r6rs
 ;;   This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
-;;   Copyright (C) 2006--2020 GNUnet e.V.
+;;   Copyright (C) 2006--2020, 2022 GNUnet e.V.
 ;;
 ;;   GNUnet is free software: you can redistribute it and/or modify it
 ;;   under the terms of the GNU Affero General Public License as published
@@ -23,12 +23,12 @@
   (export hashcode-bit-length hashcode-u8-length
           short-hashcode-bit-length short-hashcode-u8-length
           hashcode? short-hashcode?
-          bv->hashcode bv->short-hashcode
-          hashcode->bv short-hashcode->bv)
+          make-hashcode/share make-hashcode
+         make-short-hashcode/share make-short-hashcode
+          hashcode->slice short-hashcode->slice)
   (import (rnrs base)
-         (rnrs control)
-          (rnrs records syntactic)
-         (rnrs bytevectors))
+         (gnu gnunet utils bv-slice)
+          (rnrs records syntactic))
 
   (define hashcode-bit-length 512)
   (define short-hashcode-bit-length 256)
@@ -37,43 +37,53 @@
 
   ;; A 512-bit hashcode.  These are the default length for GNUnet,
   ;; using SHA-512.
-  (define-record-type (<hashcode> %make-hashcode hashcode?)
-    (fields (immutable bv %hashcode-bv))
+  (define-record-type (<hashcode> make-hashcode/share hashcode?)
+    (fields (immutable slice hashcode-slice))
     (opaque #t)
-    (sealed #t))
+    (sealed #t)
+    (protocol
+     (lambda (%make)
+       (lambda (slice)
+        "Make a hashcode, containing @var{slice} (a readable
+@code{/hashcode:512} bytevector slice).  @var{slice} may not be mutated
+while the constructed hashcode is in use."
+        (assert (= (slice-length slice) hashcode-u8-length))
+        (slice/read-only slice)))))
+
+  (define (make-hashcode slice)
+    "Make a hashcode, containing @var{slice} (a readable @code{/hashcode:512}
+bytevector slice).  @var{slice} may not be mutated while the constructed
+hashcode is in use."
+    (make-hashcode/share (slice-copy/read-only slice)))
 
   ;; A 256-bit hashcode.  Used under special conditions, like when space
   ;; is critical and security is not impacted by it.
-  (define-record-type (<short-hashcode> %make-short-hashcode short-hashcode?)
-    (fields (immutable bv %short-hashcode-bv))
+  (define-record-type (<short-hashcode> make-short-hashcode/share 
short-hashcode?)
+    (fields (immutable slice short-hashcode-slice))
     (opaque #t)
-    (sealed #t))
-
-  (define (bv->something %wrap length)
-    (case-lambda
-      "Read something from a bytevector"
-      ((bv) ; whole bytevector
-       (assert (= (bytevector-length bv) length))
-       (%wrap (bytevector-copy bv)))
-      ((bv offset) ; part of a bytevector, starting at some offset
-       (assert (<= (+ offset length) (bytevector-length bv)))
-       (let ((bv-new (make-bytevector length)))
-         (bytevector-copy! bv 0 bv-new 0 length)
-         (%wrap bv-new)))))
+    (sealed #t)
+    (protocol
+     (lambda (%make)
+       (lambda (slice)
+        "Make a short hashcode, containing @var{slice} (a readable
+@code{/hashcode:256} bytevector slice).  @var{slice} may not be mutated
+while the constructed short hashcode is in use."
+        (assert (= (slice-length slice) short-hashcode-u8-length))
+        (slice/read-only slice)))))
 
-  (define bv->hashcode
-    (bv->something %make-hashcode hashcode-u8-length))
-  (define bv->short-hashcode
-    (bv->something %make-short-hashcode short-hashcode-u8-length))
+  (define (bv->hashcode bv)
+    "Read a hashcode from a bytevector (deprecated)."
+    (make-hashcode (bv-slice/read-only bv)))
+  (define (bv->short-hashcode bv)
+    "Read a short hashcode from a bytevector (deprecated)."
+    (make-short-hashcode (bv-slice/read-only bv)))
 
   (define (hashcode->bv hashcode)
     "Extract the bytevector corresponding to @var{hashcode}
 (read-only)"
-    (bytevector-copy (%hashcode-bv hashcode)))
+    (slice-copy/bytevector (hashcode-slice hashcode)))
 
   (define (short-hashcode->bv hashcode)
     "Extract the bytevector corresponding to @var{short-hashcode}
 (read-only)"
-    (bytevector-copy (%short-hashcode-bv hashcode))))
-
-
+    (slice-copy/bytevector (short-hashcode-slice hashcode))))

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]