Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Mar 2001 21:43:10 +0200
From:      Peter Pentchev <roam@orbitel.bg>
To:        Poul-Henning Kamp <phk@critter.freebsd.dk>
Cc:        arch@FreeBSD.ORG
Subject:   Re: add MD5Chunk(filename, .., offset, length) to libmd
Message-ID:  <20010316214310.A8968@ringworld.oblivion.bg>
In-Reply-To: <14989.984770783@critter>; from phk@critter.freebsd.dk on Fri, Mar 16, 2001 at 08:26:23PM %2B0100
References:  <20010316210833.C8245@ringworld.oblivion.bg> <14989.984770783@critter>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Mar 16, 2001 at 08:26:23PM +0100, Poul-Henning Kamp wrote:
> In message <20010316210833.C8245@ringworld.oblivion.bg>, Peter Pentchev writes:
> >Hi,
> >
> >(I wonder if I'm starting another bikeshed, but oh well ;)
> >
> >As phk pointed out in the SITE MD5 thread, it is sometimes useful
> >to compute the MD5 hash over a range of a file.  I agree this is trivial
> >to implement, but why not have it in our standard toolbox?
> 
> If you include manpage patches I'll commit it.

Thanks! :)

OK, here's the libmd part.  How about /sbin/md5?  ALT_RFLAG or not?
Or not at all? :)

G'luck,
Peter

-- 
.siht ekil ti gnidaer eb d'uoy ,werbeH ni erew ecnetnes siht fI

Index: src/sys/sys/md5.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/md5.h,v
retrieving revision 1.13
diff -u -r1.13 md5.h
--- src/sys/sys/md5.h	1999/12/29 04:24:44	1.13
+++ src/sys/sys/md5.h	2001/03/16 19:38:33
@@ -34,6 +34,7 @@
 } MD5_CTX;
 
 #include <sys/cdefs.h>
+#include <sys/types.h>
 
 __BEGIN_DECLS
 void   MD5Init (MD5_CTX *);
@@ -42,6 +43,7 @@
 void   MD5Final (unsigned char [16], MD5_CTX *);
 char * MD5End(MD5_CTX *, char *);
 char * MD5File(const char *, char *);
+char * MD5Chunk(const char *, char *, off_t, off_t);
 char * MD5Data(const unsigned char *, unsigned int, char *);
 #ifdef _KERNEL
 void MD5Transform __P((u_int32_t [4], const unsigned char [64]));
Index: src/lib/libmd/md2.h
===================================================================
RCS file: /home/ncvs/src/lib/libmd/md2.h,v
retrieving revision 1.8
diff -u -r1.8 md2.h
--- src/lib/libmd/md2.h	1999/08/28 00:05:04	1.8
+++ src/lib/libmd/md2.h	2001/03/16 19:38:33
@@ -31,6 +31,7 @@
 } MD2_CTX;
 
 #include <sys/cdefs.h>
+#include <sys/types.h>
 
 __BEGIN_DECLS
 void   MD2Init(MD2_CTX *);
@@ -39,6 +40,7 @@
 void   MD2Final(unsigned char [16], MD2_CTX *);
 char * MD2End(MD2_CTX *, char *);
 char * MD2File(const char *, char *);
+char * MD2Chunk(const char *, char *, off_t, off_t);
 char * MD2Data(const unsigned char *, unsigned int, char *);
 __END_DECLS
 
Index: src/lib/libmd/md4.h
===================================================================
RCS file: /home/ncvs/src/lib/libmd/md4.h,v
retrieving revision 1.9
diff -u -r1.9 md4.h
--- src/lib/libmd/md4.h	1999/08/28 00:05:05	1.9
+++ src/lib/libmd/md4.h	2001/03/16 19:38:33
@@ -33,6 +33,7 @@
 } MD4_CTX;
 
 #include <sys/cdefs.h>
+#include <sys/types.h>
 
 __BEGIN_DECLS
 void   MD4Init(MD4_CTX *);
@@ -41,6 +42,7 @@
 void   MD4Final(unsigned char [16], MD4_CTX *);
 char * MD4End(MD4_CTX *, char *);
 char * MD4File(const char *, char *);
+char * MD4Chunk(const char *, char *, off_t, off_t);
 char * MD4Data(const unsigned char *, unsigned int, char *);
 __END_DECLS
 
Index: src/lib/libmd/mdX.3
===================================================================
RCS file: /home/ncvs/src/lib/libmd/mdX.3,v
retrieving revision 1.18
diff -u -r1.18 mdX.3
--- src/lib/libmd/mdX.3	2000/12/14 11:45:18	1.18
+++ src/lib/libmd/mdX.3	2001/03/16 19:38:33
@@ -18,6 +18,7 @@
 .Nm MDXFinal ,
 .Nm MDXEnd ,
 .Nm MDXFile ,
+.Nm MDXChunk ,
 .Nm MDXData
 .Nd calculate the RSA Data Security, Inc., ``MDX'' message digest
 .Sh LIBRARY
@@ -38,6 +39,8 @@
 .Ft "char *"
 .Fn MDXFile "const char *filename" "char *buf"
 .Ft "char *"
+.Fn MDXChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
+.Ft "char *"
 .Fn MDXData "const unsigned char *data" "unsigned int len" "char *buf"
 .Sh DESCRIPTION
 The MDX functions calculate a 128-bit cryptographic checksum (digest)
@@ -88,6 +91,23 @@
 .Fn MDXEnd
 to return the result.
 If the file cannot be opened, a null pointer is returned.
+.Fn MDXChunk
+is similar to
+.Fn MDXFile ,
+but it only calculates the digest over a byte-range of the file specified,
+starting at
+.Ar offset
+and spanning
+.Ar length
+bytes.
+If the
+.Ar length
+parameter is specified as 0, or more than the length of the remaining part
+of the file,
+.Fn MDXChunk
+calculates the digest from
+.Ar offset
+to the end of file.
 .Fn MDXData
 calculates the digest of a chunk of data in memory, and uses
 .Fn MDXEnd
Index: src/lib/libmd/mdXhl.c
===================================================================
RCS file: /home/ncvs/src/lib/libmd/mdXhl.c,v
retrieving revision 1.13
diff -u -r1.13 mdXhl.c
--- src/lib/libmd/mdXhl.c	1999/08/28 00:05:07	1.13
+++ src/lib/libmd/mdXhl.c	2001/03/16 19:38:33
@@ -11,6 +11,7 @@
  */
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 
@@ -53,6 +54,41 @@
     while ((i = read(f,buffer,sizeof buffer)) > 0) {
 	MDXUpdate(&ctx,buffer,i);
     }
+    j = errno;
+    close(f);
+    errno = j;
+    if (i < 0) return 0;
+    return MDXEnd(&ctx, buf);
+}
+
+char *
+MDXChunk(const char *filename, char *buf, off_t ofs, off_t len)
+{
+    unsigned char buffer[BUFSIZ];
+    MDX_CTX ctx;
+    struct stat stbuf;
+    int f, i, j;
+    off_t n;
+
+    MDXInit(&ctx);
+    f = open(filename, O_RDONLY);
+    if (f < 0) return 0;
+    if (fstat(f, &stbuf) < 0) return 0;
+    if (ofs > stbuf.st_size)
+	ofs = stbuf.st_size;
+    if ((len == 0) || (len > stbuf.st_size - ofs))
+	len = stbuf.st_size - ofs;
+    if (lseek(f, ofs, SEEK_SET) < 0) return 0;
+    n = len;
+    while (n > 0) {
+	if (n > sizeof(buffer))
+	    i = read(f, buffer, sizeof(buffer));
+	else
+	    i = read(f, buffer, n);
+	if (i < 0) break;
+	MDXUpdate(&ctx, buffer, i);
+	n -= i;
+    } 
     j = errno;
     close(f);
     errno = j;

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010316214310.A8968>