diff options
author | phk <phk@FreeBSD.org> | 2001-03-17 10:00:50 +0000 |
---|---|---|
committer | phk <phk@FreeBSD.org> | 2001-03-17 10:00:50 +0000 |
commit | cf0fcea2b7ae26222f4865e9a6df53972a02f443 (patch) | |
tree | 7fe491bdf6b8f8e4c7ca17e38a2e3f0368875806 /lib/libmd/mdXhl.c | |
parent | 670e711dd19510316ecaa1f30d78f16ba66492f6 (diff) | |
download | FreeBSD-src-cf0fcea2b7ae26222f4865e9a6df53972a02f443.zip FreeBSD-src-cf0fcea2b7ae26222f4865e9a6df53972a02f443.tar.gz |
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
Diffstat (limited to 'lib/libmd/mdXhl.c')
-rw-r--r-- | lib/libmd/mdXhl.c | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/lib/libmd/mdXhl.c b/lib/libmd/mdXhl.c index 0af7892..247160c 100644 --- a/lib/libmd/mdXhl.c +++ b/lib/libmd/mdXhl.c @@ -11,6 +11,7 @@ */ #include <sys/types.h> +#include <sys/stat.h> #include <fcntl.h> #include <unistd.h> @@ -43,19 +44,40 @@ 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); } |