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