libmd: introduce functions that operate on an fd instead of filename

Reviewed by:	allanjude, cem
MFC after:	2 months
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D8264
This commit is contained in:
emaste 2016-10-17 13:47:22 +00:00
parent 10774e73f3
commit b76d29a58e
12 changed files with 122 additions and 10 deletions

View File

@ -53,6 +53,12 @@ __BEGIN_DECLS
#ifndef MD4End
#define MD4End _libmd_MD4End
#endif
#ifndef MD4Fd
#define MD4Fd _libmd_MD4Fd
#endif
#ifndef MD4FdChunk
#define MD4FdChunk _libmd_MD4FdChunk
#endif
#ifndef MD4File
#define MD4File _libmd_MD4File
#endif
@ -68,6 +74,8 @@ void MD4Update(MD4_CTX *, const void *, unsigned int);
void MD4Pad(MD4_CTX *);
void MD4Final(unsigned char [16], MD4_CTX *);
char * MD4End(MD4_CTX *, char *);
char * MD4Fd(int, char *);
char * MD4FdChunk(int, char *, off_t, off_t);
char * MD4File(const char *, char *);
char * MD4FileChunk(const char *, char *, off_t, off_t);
char * MD4Data(const void *, unsigned int, char *);

View File

@ -25,6 +25,12 @@
#ifndef MD5End
#define MD5End _libmd_MD5End
#endif
#ifndef MD5Fd
#define MD5Fd _libmd_MD5Fd
#endif
#ifndef MD5FdChunk
#define MD5FdChunk _libmd_MD5FdChunk
#endif
#ifndef MD5File
#define MD5File _libmd_MD5File
#endif

View File

@ -42,18 +42,18 @@ MDXEnd(MDX_CTX *ctx, char *buf)
}
char *
MDXFile(const char *filename, char *buf)
MDXFd(int fd, char *buf)
{
return (MDXFileChunk(filename, buf, 0, 0));
return MDXFdChunk(fd, buf, 0, 0);
}
char *
MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
MDXFdChunk(int fd, char *buf, off_t ofs, off_t len)
{
unsigned char buffer[16*1024];
MDX_CTX ctx;
struct stat stbuf;
int fd, readrv, e;
int readrv, e;
off_t remain;
if (len < 0) {
@ -62,9 +62,6 @@ MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
}
MDXInit(&ctx);
fd = open(filename, O_RDONLY);
if (fd < 0)
return NULL;
if (ofs != 0) {
errno = 0;
if (lseek(fd, ofs, SEEK_SET) != ofs ||
@ -86,14 +83,33 @@ MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
remain -= readrv;
}
error:
e = errno;
close(fd);
errno = e;
if (readrv < 0)
return NULL;
return (MDXEnd(&ctx, 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)
{
char *ret;
int e, fd;
fd = open(filename, O_RDONLY);
if (fd < 0)
return NULL;
ret = MDXFdChunk(fd, buf, ofs, len);
e = errno;
close (fd);
errno = e;
return ret;
}
char *
MDXData (const void *data, unsigned int len, char *buf)
{

View File

@ -96,6 +96,12 @@ __BEGIN_DECLS
#ifndef RIPEMD160_End
#define RIPEMD160_End _libmd_RIPEMD160_End
#endif
#ifndef RIPEMD160_Fd
#define RIPEMD160_Fd _libmd_RIPEMD160_Fd
#endif
#ifndef RIPEMD160_FdChunk
#define RIPEMD160_FdChunk _libmd_RIPEMD160_FdChunk
#endif
#ifndef RIPEMD160_File
#define RIPEMD160_File _libmd_RIPEMD160_File
#endif
@ -121,6 +127,8 @@ void RIPEMD160_Update(RIPEMD160_CTX *c, const void *data,
size_t len);
void RIPEMD160_Final(unsigned char *md, RIPEMD160_CTX *c);
char *RIPEMD160_End(RIPEMD160_CTX *, char *);
char *RIPEMD160_Fd(int, char *);
char *RIPEMD160_FdChunk(int, char *, off_t, off_t);
char *RIPEMD160_File(const char *, char *);
char *RIPEMD160_FileChunk(const char *, char *, off_t, off_t);
char *RIPEMD160_Data(const void *, unsigned int, char *);

View File

@ -94,6 +94,12 @@ __BEGIN_DECLS
#ifndef SHA_End
#define SHA_End _libmd_SHA_End
#endif
#ifndef SHA_Fd
#define SHA_Fd _libmd_SHA_Fd
#endif
#ifndef SHA_FdChunk
#define SHA_FdChunk _libmd_SHA_FdChunk
#endif
#ifndef SHA_File
#define SHA_File _libmd_SHA_File
#endif
@ -126,6 +132,12 @@ __BEGIN_DECLS
#ifndef SHA1_End
#define SHA1_End _libmd_SHA1_End
#endif
#ifndef SHA1_Fd
#define SHA1_Fd _libmd_SHA1_Fd
#endif
#ifndef SHA1_FdChunk
#define SHA1_FdChunk _libmd_SHA1_FdChunk
#endif
#ifndef SHA1_File
#define SHA1_File _libmd_SHA1_File
#endif
@ -150,6 +162,8 @@ void SHA_Init(SHA_CTX *c);
void SHA_Update(SHA_CTX *c, const void *data, size_t len);
void SHA_Final(unsigned char *md, SHA_CTX *c);
char *SHA_End(SHA_CTX *, char *);
char *SHA_Fd(int, char *);
char *SHA_FdChunk(int, char *, off_t, off_t);
char *SHA_File(const char *, char *);
char *SHA_FileChunk(const char *, char *, off_t, off_t);
char *SHA_Data(const void *, unsigned int, char *);
@ -158,6 +172,8 @@ void SHA1_Init(SHA_CTX *c);
void SHA1_Update(SHA_CTX *c, const void *data, size_t len);
void SHA1_Final(unsigned char *md, SHA_CTX *c);
char *SHA1_End(SHA_CTX *, char *);
char *SHA1_Fd(int, char *);
char *SHA1_FdChunk(int, char *, off_t, off_t);
char *SHA1_File(const char *, char *);
char *SHA1_FileChunk(const char *, char *, off_t, off_t);
char *SHA1_Data(const void *, unsigned int, char *);

View File

@ -59,6 +59,12 @@ __BEGIN_DECLS
#ifndef SHA256_End
#define SHA256_End _libmd_SHA256_End
#endif
#ifndef SHA256_Fd
#define SHA256_Fd _libmd_SHA256_Fd
#endif
#ifndef SHA256_FdChunk
#define SHA256_FdChunk _libmd_SHA256_FdChunk
#endif
#ifndef SHA256_File
#define SHA256_File _libmd_SHA256_File
#endif
@ -82,6 +88,8 @@ void SHA256_Final(unsigned char [static SHA256_DIGEST_LENGTH], SHA256_CTX *);
#ifndef _KERNEL
char *SHA256_End(SHA256_CTX *, char *);
char *SHA256_Data(const void *, unsigned int, char *);
char *SHA256_Fd(int, char *);
char *SHA256_FdChunk(int, char *, off_t, off_t);
char *SHA256_File(const char *, char *);
char *SHA256_FileChunk(const char *, char *, off_t, off_t);
#endif

View File

@ -58,6 +58,12 @@ __BEGIN_DECLS
#ifndef SHA384_End
#define SHA384_End _libmd_SHA384_End
#endif
#ifndef SHA384_Fd
#define SHA384_Fd _libmd_SHA384_Fd
#endif
#ifndef SHA384_FdChunk
#define SHA384_FdChunk _libmd_SHA384_FdChunk
#endif
#ifndef SHA384_File
#define SHA384_File _libmd_SHA384_File
#endif
@ -78,6 +84,8 @@ void SHA384_Final(unsigned char [static SHA384_DIGEST_LENGTH], SHA384_CTX *);
#ifndef _KERNEL
char *SHA384_End(SHA384_CTX *, char *);
char *SHA384_Data(const void *, unsigned int, char *);
char *SHA384_Fd(int, char *);
char *SHA384_FdChunk(int, char *, off_t, off_t);
char *SHA384_File(const char *, char *);
char *SHA384_FileChunk(const char *, char *, off_t, off_t);
#endif

View File

@ -58,6 +58,12 @@ __BEGIN_DECLS
#ifndef SHA512_End
#define SHA512_End _libmd_SHA512_End
#endif
#ifndef SHA512_Fd
#define SHA512_Fd _libmd_SHA512_Fd
#endif
#ifndef SHA512_FdChunk
#define SHA512_FdChunk _libmd_SHA512_FdChunk
#endif
#ifndef SHA512_File
#define SHA512_File _libmd_SHA512_File
#endif
@ -81,6 +87,8 @@ void SHA512_Final(unsigned char [static SHA512_DIGEST_LENGTH], SHA512_CTX *);
#ifndef _KERNEL
char *SHA512_End(SHA512_CTX *, char *);
char *SHA512_Data(const void *, unsigned int, char *);
char *SHA512_Fd(int, char *);
char *SHA512_FdChunk(int, char *, off_t, off_t);
char *SHA512_File(const char *, char *);
char *SHA512_FileChunk(const char *, char *, off_t, off_t);
#endif

View File

@ -55,6 +55,12 @@ __BEGIN_DECLS
#ifndef SHA512_224_End
#define SHA512_224_End _libmd_SHA512_224_End
#endif
#ifndef SHA512_224_Fd
#define SHA512_224_Fd _libmd_SHA512_224_Fd
#endif
#ifndef SHA512_224_FdChunk
#define SHA512_224_FdChunk _libmd_SHA512_224_FdChunk
#endif
#ifndef SHA512_224_File
#define SHA512_224_File _libmd_SHA512_224_File
#endif
@ -84,6 +90,12 @@ __BEGIN_DECLS
#ifndef SHA512_256_End
#define SHA512_256_End _libmd_SHA512_256_End
#endif
#ifndef SHA512_256_Fd
#define SHA512_256_Fd _libmd_SHA512_256_Fd
#endif
#ifndef SHA512_256_FdChunk
#define SHA512_256_FdChunk _libmd_SHA512_256_FdChunk
#endif
#ifndef SHA512_256_File
#define SHA512_256_File _libmd_SHA512_256_File
#endif
@ -107,6 +119,8 @@ void SHA512_224_Final(unsigned char [static SHA512_224_DIGEST_LENGTH], SHA512_CT
#ifndef _KERNEL
char *SHA512_224_End(SHA512_CTX *, char *);
char *SHA512_224_Data(const void *, unsigned int, char *);
char *SHA512_224_Fd(int, char *);
char *SHA512_224_FdChunk(int, char *, off_t, off_t);
char *SHA512_224_File(const char *, char *);
char *SHA512_224_FileChunk(const char *, char *, off_t, off_t);
#endif
@ -116,6 +130,8 @@ void SHA512_256_Final(unsigned char [static SHA512_256_DIGEST_LENGTH], SHA512_CT
#ifndef _KERNEL
char *SHA512_256_End(SHA512_CTX *, char *);
char *SHA512_256_Data(const void *, unsigned int, char *);
char *SHA512_256_Fd(int, char *);
char *SHA512_256_FdChunk(int, char *, off_t, off_t);
char *SHA512_256_File(const char *, char *);
char *SHA512_256_FileChunk(const char *, char *, off_t, off_t);
#endif

View File

@ -68,6 +68,12 @@ char *SKEIN1024_End(SKEIN1024_CTX *, char *);
char *SKEIN256_Data(const void *, unsigned int, char *);
char *SKEIN512_Data(const void *, unsigned int, char *);
char *SKEIN1024_Data(const void *, unsigned int, char *);
char *SKEIN256_Fd(int, char *);
char *SKEIN512_Fd(int, char *);
char *SKEIN1024_Fd(int, char *);
char *SKEIN256_FdChunk(int, char *, off_t, off_t);
char *SKEIN512_FdChunk(int, char *, off_t, off_t);
char *SKEIN1024_FdChunk(int, char *, off_t, off_t);
char *SKEIN256_File(const char *, char *);
char *SKEIN512_File(const char *, char *);
char *SKEIN1024_File(const char *, char *);

View File

@ -137,6 +137,16 @@ void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
#define SKEIN512_End _libmd_SKEIN512_End
#define SKEIN1024_End _libmd_SKEIN1024_End
#endif
#ifndef SKEIN256_Fd
#define SKEIN256_Fd _libmd_SKEIN256_Fd
#define SKEIN512_Fd _libmd_SKEIN512_Fd
#define SKEIN1024_Fd _libmd_SKEIN1024_Fd
#endif
#ifndef SKEIN256_FdChunk
#define SKEIN256_FdChunk _libmd_SKEIN256_FdChunk
#define SKEIN512_FdChunk _libmd_SKEIN512_FdChunk
#define SKEIN1024_FdChunk _libmd_SKEIN1024_FdChunk
#endif
#ifndef SKEIN256_File
#define SKEIN256_File _libmd_SKEIN256_File
#define SKEIN512_File _libmd_SKEIN512_File

View File

@ -47,6 +47,8 @@ void MD5Update (MD5_CTX *, const void *, unsigned int);
void MD5Final (unsigned char[static MD5_DIGEST_LENGTH], MD5_CTX *);
#ifndef _KERNEL
char * MD5End(MD5_CTX *, char *);
char * MD5Fd(int, char *);
char * MD5FdChunk(int, char *, off_t, off_t);
char * MD5File(const char *, char *);
char * MD5FileChunk(const char *, char *, off_t, off_t);
char * MD5Data(const void *, unsigned int, char *);