2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
13 #include <sys/types.h>
25 MDXEnd(MDX_CTX *ctx, char *buf)
28 unsigned char digest[LENGTH];
29 static const char hex[]="0123456789abcdef";
32 buf = malloc(2*LENGTH + 1);
35 MDXFinal(digest, ctx);
36 for (i = 0; i < LENGTH; i++) {
37 buf[i+i] = hex[digest[i] >> 4];
38 buf[i+i+1] = hex[digest[i] & 0x0f];
45 MDXFile(const char *filename, char *buf)
47 return (MDXFileChunk(filename, buf, 0, 0));
51 MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
53 unsigned char buffer[16*1024];
65 fd = open(filename, O_RDONLY);
70 if (lseek(fd, ofs, SEEK_SET) != ofs ||
71 (ofs == -1 && errno != 0)) {
78 while (len == 0 || remain > 0) {
79 if (len == 0 || remain > sizeof(buffer))
80 readrv = read(fd, buffer, sizeof(buffer));
82 readrv = read(fd, buffer, remain);
85 MDXUpdate(&ctx, buffer, readrv);
94 return (MDXEnd(&ctx, buf));
98 MDXData (const void *data, unsigned int len, char *buf)
103 MDXUpdate(&ctx,data,len);
104 return (MDXEnd(&ctx, buf));
108 /* When building libmd, provide weak references. Note: this is not
109 activated in the context of compiling these sources for internal
113 __weak_reference(_libmd_MDXEnd, MDXEnd);
115 __weak_reference(_libmd_MDXFile, MDXFile);
117 __weak_reference(_libmd_MDXFileChunk, MDXFileChunk);
119 __weak_reference(_libmd_MDXData, MDXData);