Add a new entrypoint to the hashes in libmd:

char *
  FooFileChunk(const char *filename, char *buf, off_t offset, off_t length)
Which only hashes part of a file.
Implement FooFile() in terms of this function.

Submitted by:	roam
This commit is contained in:
Poul-Henning Kamp 2001-03-17 10:00:50 +00:00
parent 6eb39ac8fc
commit 8a24546c85
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=74385
11 changed files with 115 additions and 14 deletions

View File

@ -8,18 +8,24 @@ INCS= md2.h md4.h md5.h ripemd.h sha.h
MAN3+= md2.3 md4.3 md5.3 ripemd.3 sha.3
MLINKS+=md2.3 MD2Init.3 md2.3 MD2Update.3 md2.3 MD2Final.3
MLINKS+=md2.3 MD2End.3 md2.3 MD2File.3 md2.3 MD2Data.3
MLINKS+=md2.3 MD2End.3 md2.3 MD2File.3 md2.3 MD2FileChunk.3
MLINKS+=md2.3 MD2Data.3
MLINKS+=md4.3 MD4Init.3 md4.3 MD4Update.3 md4.3 MD4Final.3
MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4Data.3
MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4FileChunk.3
MLINKS+=md4.3 MD4Data.3
MLINKS+=md5.3 MD5Init.3 md5.3 MD5Update.3 md5.3 MD5Final.3
MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5Data.3
MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5FileChunk.3
MLINKS+=md5.3 MD5Data.3
MLINKS+=ripemd.3 RIPEMD160_Init.3 ripemd.3 RIPEMD160_Update.3
MLINKS+=ripemd.3 RIPEMD160_Final.3 ripemd.3 RIPEMD160_Data.3
MLINKS+=ripemd.3 RIPEMD160_End.3 ripemd.3 RIPEMD160_File.3
MLINKS+=ripemd.3 RIPEMD160_FileChunk.3
MLINKS+=sha.3 SHA_Init.3 sha.3 SHA_Update.3 sha.3 SHA_Final.3
MLINKS+=sha.3 SHA_End.3 sha.3 SHA_File.3 sha.3 SHA_Data.3
MLINKS+=sha.3 SHA_End.3 sha.3 SHA_File.3 sha.3 SHA_FileChunk.3
MLINKS+=sha.3 SHA_Data.3
MLINKS+=sha.3 SHA1_Init.3 sha.3 SHA1_Update.3 sha.3 SHA1_Final.3
MLINKS+=sha.3 SHA1_End.3 sha.3 SHA1_File.3 sha.3 SHA1_Data.3
MLINKS+=sha.3 SHA1_End.3 sha.3 SHA1_File.3 sha.3 SHA1_FileChunk.3
MLINKS+=sha.3 SHA1_Data.3
CLEANFILES+= md[245]hl.c md[245].ref md[245].3 mddriver \
rmd160.ref rmd160hl.c rmddriver \
sha0.ref sha0hl.c sha1.ref sha1hl.c shadriver

View File

@ -39,6 +39,7 @@ void MD2Pad(MD2_CTX *);
void MD2Final(unsigned char [16], MD2_CTX *);
char * MD2End(MD2_CTX *, char *);
char * MD2File(const char *, char *);
char * MD2FileChunk(const char *, char *, off_t, off_t);
char * MD2Data(const unsigned char *, unsigned int, char *);
__END_DECLS

View File

@ -20,9 +20,9 @@
documentation and/or software.
*/
#include "md2.h"
#include <string.h>
#include <sys/types.h>
#include <string.h>
#include "md2.h"
typedef unsigned char *POINTER;

View File

@ -41,6 +41,7 @@ void MD4Pad(MD4_CTX *);
void MD4Final(unsigned char [16], MD4_CTX *);
char * MD4End(MD4_CTX *, char *);
char * MD4File(const char *, char *);
char * MD4FileChunk(const char *, char *, off_t, off_t);
char * MD4Data(const unsigned char *, unsigned int, char *);
__END_DECLS

View File

@ -18,6 +18,7 @@
.Nm MDXFinal ,
.Nm MDXEnd ,
.Nm MDXFile ,
.Nm MDXFileChunk ,
.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 MDXFileChunk "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 @@ calculates the digest of a file, and uses
.Fn MDXEnd
to return the result.
If the file cannot be opened, a null pointer is returned.
.Fn MDXFileChunk
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 MDXFileChunk
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

View File

@ -11,6 +11,7 @@
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
@ -42,20 +43,41 @@ MDXEnd(MDX_CTX *ctx, char *buf)
char *
MDXFile(const char *filename, char *buf)
{
return MDXFileChunk(filename, buf, 0, 0);
}
char *
MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
{
unsigned char buffer[BUFSIZ];
MDX_CTX ctx;
int f,i,j;
struct stat stbuf;
int f, i, e;
off_t n;
MDXInit(&ctx);
f = open(filename,O_RDONLY);
f = open(filename, O_RDONLY);
if (f < 0) return 0;
while ((i = read(f,buffer,sizeof buffer)) > 0) {
MDXUpdate(&ctx,buffer,i);
}
j = errno;
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;
}
e = errno;
close(f);
errno = j;
errno = e;
if (i < 0) return 0;
return MDXEnd(&ctx, buf);
}

View File

@ -18,6 +18,7 @@
.Nm RIPEMD160_Final ,
.Nm RIPEMD160_End ,
.Nm RIPEMD160_File ,
.Nm RIPEMD160_FileChunk ,
.Nm RIPEMD160_Data
.Nd calculate the RIPEMD160 message digest
.Sh LIBRARY
@ -36,6 +37,8 @@
.Ft "char *"
.Fn RIPEMD160_File "const char *filename" "char *buf"
.Ft "char *"
.Fn RIPEMD160_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn RIPEMD160_Data "const unsigned char *data" "unsigned int len" "char *buf"
.Sh DESCRIPTION
The
@ -72,6 +75,23 @@ calculates the digest of a file, and uses
.Fn RIPEMD160_End
to return the result.
If the file cannot be opened, a null pointer is returned.
.Fn RIPEMD160_FileChunk
is similar to
.Fn RIPEMD160_File ,
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 RIPEMD160_FileChunk
calculates the digest from
.Ar offset
to the end of file.
.Fn RIPEMD160_Data
calculates the digest of a chunk of data in memory, and uses
.Fn RIPEMD160_End

View File

@ -56,6 +56,10 @@
* [including the GNU Public Licence.]
*/
/*
* $FreeBSD$
*/
#ifndef HEADER_RIPEMD_H
#define HEADER_RIPEMD_H
@ -83,6 +87,7 @@ void RIPEMD160_Update(RIPEMD160_CTX *c, const unsigned char *data,
void RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c);
char *RIPEMD160_End(RIPEMD160_CTX *, char *);
char *RIPEMD160_File(const char *, char *);
char *RIPEMD160_FileChunk(const char *, char *, off_t, off_t);
char *RIPEMD160_Data(const unsigned char *, unsigned int, char *);
__END_DECLS

View File

@ -18,12 +18,14 @@
.Nm SHA_Final ,
.Nm SHA_End ,
.Nm SHA_File ,
.Nm SHA_FileChunk ,
.Nm SHA_Data ,
.Nm SHA1_Init ,
.Nm SHA1_Update ,
.Nm SHA1_Final ,
.Nm SHA1_End ,
.Nm SHA1_File ,
.Nm SHA1_FileChunk ,
.Nm SHA1_Data
.Nd calculate the FIPS 160 and 160-1 ``SHA'' message digests
.Sh LIBRARY
@ -42,6 +44,8 @@
.Ft "char *"
.Fn SHA_File "const char *filename" "char *buf"
.Ft "char *"
.Fn SHA_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA_Data "const unsigned char *data" "unsigned int len" "char *buf"
.Ft void
.Fn SHA1_Init "SHA_CTX *context"
@ -54,6 +58,8 @@
.Ft "char *"
.Fn SHA1_File "const char *filename" "char *buf"
.Ft "char *"
.Fn SHA1_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA1_Data "const unsigned char *data" "unsigned int len" "char *buf"
.Sh DESCRIPTION
The
@ -101,6 +107,23 @@ calculates the digest of a file, and uses
.Fn SHA1_End
to return the result.
If the file cannot be opened, a null pointer is returned.
.Fn SHA1_FileChunk
is similar to
.Fn SHA1_File ,
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 SHA1_FileChunk
calculates the digest from
.Ar offset
to the end of file.
.Fn SHA1_Data
calculates the digest of a chunk of data in memory, and uses
.Fn SHA1_End

View File

@ -84,12 +84,14 @@ void SHA_Update(SHA_CTX *c, const unsigned char *data, size_t len);
void SHA_Final(unsigned char *md, SHA_CTX *c);
char *SHA_End(SHA_CTX *, char *);
char *SHA_File(const char *, char *);
char *SHA_FileChunk(const char *, char *, off_t, off_t);
char *SHA_Data(const unsigned char *, unsigned int, char *);
void SHA1_Init(SHA_CTX *c);
void SHA1_Update(SHA_CTX *c, const unsigned char *data, size_t len);
void SHA1_Final(unsigned char *md, SHA_CTX *c);
char *SHA1_End(SHA_CTX *, char *);
char *SHA1_File(const char *, char *);
char *SHA1_FileChunk(const char *, char *, off_t, off_t);
char *SHA1_Data(const unsigned char *, unsigned int, char *);
__END_DECLS

View File

@ -42,6 +42,7 @@ void MD5Pad (MD5_CTX *);
void MD5Final (unsigned char [16], MD5_CTX *);
char * MD5End(MD5_CTX *, char *);
char * MD5File(const char *, char *);
char * MD5FileChunk(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]));