mdXhl.c revision 1.1
1/*	$NetBSD: mdXhl.c,v 1.1 1997/01/30 01:01:44 thorpej Exp $	*/
2
3/* mdXhl.c
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@login.dkuug.dk> 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 * ----------------------------------------------------------------------------
10 *
11 * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp
12 *
13 */
14
15#include <sys/types.h>
16#include <fcntl.h>
17#include <unistd.h>
18
19#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
22
23#include "mdX.h"
24
25char *
26MDXEnd(MDX_CTX *ctx, char *buf)
27{
28    int i;
29    unsigned char digest[16];
30    static const char hex[]="0123456789abcdef";
31
32    if (!buf)
33        buf = malloc(33);
34    if (!buf)
35	return 0;
36    MDXFinal(digest,ctx);
37    for (i=0;i<16;i++) {
38	buf[i+i] = hex[digest[i] >> 4];
39	buf[i+i+1] = hex[digest[i] & 0x0f];
40    }
41    buf[i+i] = '\0';
42    return buf;
43}
44
45char *
46MDXFile (char *filename, char *buf)
47{
48    unsigned char buffer[BUFSIZ];
49    MDX_CTX ctx;
50    int f,i,j;
51
52    MDXInit(&ctx);
53    f = open(filename,O_RDONLY);
54    if (f < 0) return 0;
55    while ((i = read(f,buffer,sizeof buffer)) > 0) {
56	MDXUpdate(&ctx,buffer,i);
57    }
58    j = errno;
59    close(f);
60    errno = j;
61    if (i < 0) return 0;
62    return MDXEnd(&ctx, buf);
63}
64
65char *
66MDXData (const unsigned char *data, unsigned int len, char *buf)
67{
68    MDX_CTX ctx;
69
70    MDXInit(&ctx);
71    MDXUpdate(&ctx,data,len);
72    return MDXEnd(&ctx, buf);
73}
74