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: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $");
12 #include <sys/types.h>
24 MD5End(MD5_CTX
*ctx
, char *buf
)
27 unsigned char digest
[MD5_DIGEST_LENGTH
];
28 static const char hex
[]="0123456789abcdef";
31 buf
= malloc(2*MD5_DIGEST_LENGTH
+ 1);
34 MD5Final(digest
, ctx
);
35 for (i
= 0; i
< MD5_DIGEST_LENGTH
; i
++) {
36 buf
[i
+i
] = hex
[digest
[i
] >> 4];
37 buf
[i
+i
+1] = hex
[digest
[i
] & 0x0f];
44 MD5File(const char *filename
, char *buf
)
46 return (MD5FileChunk(filename
, buf
, 0, 0));
50 MD5FileChunk(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 MD5Update(&ctx
, buffer
, i
);
87 return (MD5End(&ctx
, buf
));
91 MD5Data (const void *data
, unsigned int len
, char *buf
)
96 MD5Update(&ctx
,data
,len
);
97 return (MD5End(&ctx
, buf
));