2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
12 #include <sys/cdefs.h>
13 __FBSDID("$FreeBSD$");
15 #include <sys/types.h>
27 MDXEnd(MDX_CTX *ctx, char *buf)
30 unsigned char digest[LENGTH];
31 static const char hex[]="0123456789abcdef";
34 buf = malloc(2*LENGTH + 1);
37 MDXFinal(digest, ctx);
38 for (i = 0; i < LENGTH; i++) {
39 buf[i+i] = hex[digest[i] >> 4];
40 buf[i+i+1] = hex[digest[i] & 0x0f];
47 MDXFd(int fd, char *buf)
49 return MDXFdChunk(fd, buf, 0, 0);
53 MDXFdChunk(int fd, char *buf, off_t ofs, off_t len)
55 unsigned char buffer[16*1024];
69 if (lseek(fd, ofs, SEEK_SET) != ofs ||
70 (ofs == -1 && errno != 0)) {
77 while (len == 0 || remain > 0) {
78 if (len == 0 || remain > sizeof(buffer))
79 readrv = read(fd, buffer, sizeof(buffer));
81 readrv = read(fd, buffer, remain);
84 MDXUpdate(&ctx, buffer, readrv);
90 return (MDXEnd(&ctx, buf));
94 MDXFile(const char *filename, char *buf)
96 return (MDXFileChunk(filename, buf, 0, 0));
100 MDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len)
105 fd = open(filename, O_RDONLY);
108 ret = MDXFdChunk(fd, buf, ofs, len);
116 MDXData (const void *data, unsigned int len, char *buf)
121 MDXUpdate(&ctx,data,len);
122 return (MDXEnd(&ctx, buf));
126 /* When building libmd, provide weak references. Note: this is not
127 activated in the context of compiling these sources for internal
131 __weak_reference(_libmd_MDXEnd, MDXEnd);
133 __weak_reference(_libmd_MDXFile, MDXFile);
135 __weak_reference(_libmd_MDXFileChunk, MDXFileChunk);
137 __weak_reference(_libmd_MDXData, MDXData);