> > > What's the usecase ?
> > >
> > > If regular file: Why not mmap and save the kernel/userland copies ?
> >
> > Because the code from which I extracted this function was doing a copy
> > while computing the checksum on the fly, so I didn't consider mmap
> > when I extracted the read part of the code.
>
> But what is the use-case ?
My bad, in my mind replying about mmap implied that this was indeed
about having a convenience functtion to compute the hash of a regular
file.
Would the attached patch be welcome?
Slightly off topic, why have both definitions?
#define VSHA256_LEN 32
#define VSHA256_DIGEST_LENGTH 32
Dridi
diff --git a/include/vsha256.h b/include/vsha256.h
index 8f20505fce..b7c9872da7 100644
--- a/include/vsha256.h
+++ b/include/vsha256.h
@@ -41,6 +41,7 @@ typedef struct VSHA256Context {
void VSHA256_Init(VSHA256_CTX *);
void VSHA256_Update(VSHA256_CTX *, const void *, size_t);
void VSHA256_Final(unsigned char [VSHA256_LEN], VSHA256_CTX *);
+int VSHA256_Fd(unsigned char [static VSHA256_LEN], int);
void VSHA256_Test(void);
#define SHA256_LEN VSHA256_LEN
diff --git a/lib/libvarnish/vsha256.c b/lib/libvarnish/vsha256.c
index 73efb0b2bd..24ab130938 100644
--- a/lib/libvarnish/vsha256.c
+++ b/lib/libvarnish/vsha256.c
@@ -40,6 +40,9 @@
# define VBIG_ENDIAN __BIG_ENDIAN
#endif
+#include <sys/mman.h>
+#include <sys/stat.h>
+
#include <stdint.h>
#include <string.h>
@@ -318,6 +321,24 @@ VSHA256_Final(unsigned char digest[static VSHA256_DIGEST_LENGTH],
memset((void *)ctx, 0, sizeof(*ctx));
}
+int
+VSHA256_Fd(unsigned char digest[static VSHA256_DIGEST_LENGTH], int fd)
+{
+ struct VSHA256Context c;
+ struct stat st;
+ void *ptr;
+
+ if (fstat(fd, &st) == -1)
+ return (-1);
+ ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (ptr == MAP_FAILED)
+ return (-1);
+ VSHA256_Init(&c);
+ VSHA256_Update(&c, ptr, st.st_size);
+ VSHA256_Final(digest, &c);
+ return (0);
+}
+
/*
* A few test-vectors, just in case
*/
_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev