1 /* mdXhl.c * ----------------------------------------------------------------------------
2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
4 * can do whatever you want with this stuff. If we meet some day, and you think
5 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
6 * ----------------------------------------------------------------------------
10 __FBSDID("$FreeBSD$");
12 #include <sys/types.h>
24 MDXEnd(MDX_CTX *ctx, char *buf)
27 unsigned char digest[LENGTH];
28 static const char hex[]="0123456789abcdef";
31 buf = malloc(2*LENGTH + 1);
34 MDXFinal(digest, ctx);
35 for (i = 0; i < LENGTH; i++) {
36 buf[i+i] = hex[digest[i] >> 4];
37 buf[i+i+1] = hex[digest[i] & 0x0f];
44 MDXFile(const char *filename, char *buf)
46 return (MDXFileChunk(filename, buf, 0, 0));
50 MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
52 unsigned char buffer[BUFSIZ];
59 f = open(filename, O_RDONLY);
62 if (fstat(f, &stbuf) < 0)
64 if (ofs > stbuf.st_size)
66 if ((len == 0) || (len > stbuf.st_size - ofs))
67 len = stbuf.st_size - ofs;
68 if (lseek(f, ofs, SEEK_SET) < 0)
73 if (n > sizeof(buffer))
74 i = read(f, buffer, sizeof(buffer));
76 i = read(f, buffer, n);
79 MDXUpdate(&ctx, buffer, i);
87 return (MDXEnd(&ctx, buf));
91 MDXData (const void *data, unsigned int len, char *buf)
96 MDXUpdate(&ctx,data,len);
97 return (MDXEnd(&ctx, buf));