1*04819365SAxel Dörfler /* mdXhl.c * ----------------------------------------------------------------------------
2*04819365SAxel Dörfler * "THE BEER-WARE LICENSE" (Revision 42):
3*04819365SAxel Dörfler * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
4*04819365SAxel Dörfler * can do whatever you want with this stuff. If we meet some day, and you think
5*04819365SAxel Dörfler * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
6*04819365SAxel Dörfler * ----------------------------------------------------------------------------
7*04819365SAxel Dörfler */
8*04819365SAxel Dörfler
9*04819365SAxel Dörfler #include <sys/cdefs.h>
10*04819365SAxel Dörfler __FBSDID("$FreeBSD: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $");
11*04819365SAxel Dörfler
12*04819365SAxel Dörfler #include <sys/types.h>
13*04819365SAxel Dörfler #include <sys/stat.h>
14*04819365SAxel Dörfler #include <fcntl.h>
15*04819365SAxel Dörfler #include <unistd.h>
16*04819365SAxel Dörfler
17*04819365SAxel Dörfler #include <errno.h>
18*04819365SAxel Dörfler #include <stdio.h>
19*04819365SAxel Dörfler #include <stdlib.h>
20*04819365SAxel Dörfler
21*04819365SAxel Dörfler #include "md5.h"
22*04819365SAxel Dörfler
23*04819365SAxel Dörfler char *
MD5End(MD5_CTX * ctx,char * buf)24*04819365SAxel Dörfler MD5End(MD5_CTX *ctx, char *buf)
25*04819365SAxel Dörfler {
26*04819365SAxel Dörfler int i;
27*04819365SAxel Dörfler unsigned char digest[MD5_DIGEST_LENGTH];
28*04819365SAxel Dörfler static const char hex[]="0123456789abcdef";
29*04819365SAxel Dörfler
30*04819365SAxel Dörfler if (!buf)
31*04819365SAxel Dörfler buf = malloc(2*MD5_DIGEST_LENGTH + 1);
32*04819365SAxel Dörfler if (!buf)
33*04819365SAxel Dörfler return 0;
34*04819365SAxel Dörfler MD5Final(digest, ctx);
35*04819365SAxel Dörfler for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
36*04819365SAxel Dörfler buf[i+i] = hex[digest[i] >> 4];
37*04819365SAxel Dörfler buf[i+i+1] = hex[digest[i] & 0x0f];
38*04819365SAxel Dörfler }
39*04819365SAxel Dörfler buf[i+i] = '\0';
40*04819365SAxel Dörfler return buf;
41*04819365SAxel Dörfler }
42*04819365SAxel Dörfler
43*04819365SAxel Dörfler char *
MD5File(const char * filename,char * buf)44*04819365SAxel Dörfler MD5File(const char *filename, char *buf)
45*04819365SAxel Dörfler {
46*04819365SAxel Dörfler return (MD5FileChunk(filename, buf, 0, 0));
47*04819365SAxel Dörfler }
48*04819365SAxel Dörfler
49*04819365SAxel Dörfler char *
MD5FileChunk(const char * filename,char * buf,off_t ofs,off_t len)50*04819365SAxel Dörfler MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len)
51*04819365SAxel Dörfler {
52*04819365SAxel Dörfler unsigned char buffer[BUFSIZ];
53*04819365SAxel Dörfler MD5_CTX ctx;
54*04819365SAxel Dörfler struct stat stbuf;
55*04819365SAxel Dörfler int f, i, e;
56*04819365SAxel Dörfler off_t n;
57*04819365SAxel Dörfler
58*04819365SAxel Dörfler MD5Init(&ctx);
59*04819365SAxel Dörfler f = open(filename, O_RDONLY);
60*04819365SAxel Dörfler if (f < 0)
61*04819365SAxel Dörfler return 0;
62*04819365SAxel Dörfler if (fstat(f, &stbuf) < 0)
63*04819365SAxel Dörfler return 0;
64*04819365SAxel Dörfler if (ofs > stbuf.st_size)
65*04819365SAxel Dörfler ofs = stbuf.st_size;
66*04819365SAxel Dörfler if ((len == 0) || (len > stbuf.st_size - ofs))
67*04819365SAxel Dörfler len = stbuf.st_size - ofs;
68*04819365SAxel Dörfler if (lseek(f, ofs, SEEK_SET) < 0)
69*04819365SAxel Dörfler return 0;
70*04819365SAxel Dörfler n = len;
71*04819365SAxel Dörfler i = 0;
72*04819365SAxel Dörfler while (n > 0) {
73*04819365SAxel Dörfler if (n > sizeof(buffer))
74*04819365SAxel Dörfler i = read(f, buffer, sizeof(buffer));
75*04819365SAxel Dörfler else
76*04819365SAxel Dörfler i = read(f, buffer, n);
77*04819365SAxel Dörfler if (i < 0)
78*04819365SAxel Dörfler break;
79*04819365SAxel Dörfler MD5Update(&ctx, buffer, i);
80*04819365SAxel Dörfler n -= i;
81*04819365SAxel Dörfler }
82*04819365SAxel Dörfler e = errno;
83*04819365SAxel Dörfler close(f);
84*04819365SAxel Dörfler errno = e;
85*04819365SAxel Dörfler if (i < 0)
86*04819365SAxel Dörfler return 0;
87*04819365SAxel Dörfler return (MD5End(&ctx, buf));
88*04819365SAxel Dörfler }
89*04819365SAxel Dörfler
90*04819365SAxel Dörfler char *
MD5Data(const void * data,unsigned int len,char * buf)91*04819365SAxel Dörfler MD5Data (const void *data, unsigned int len, char *buf)
92*04819365SAxel Dörfler {
93*04819365SAxel Dörfler MD5_CTX ctx;
94*04819365SAxel Dörfler
95*04819365SAxel Dörfler MD5Init(&ctx);
96*04819365SAxel Dörfler MD5Update(&ctx,data,len);
97*04819365SAxel Dörfler return (MD5End(&ctx, buf));
98*04819365SAxel Dörfler }
99