Next round of fixing all kinds of spelling mistakes.
[dragonfly.git] / lib / libmd / mdXhl.c
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  * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.13 1999/08/28 00:05:07 peter Exp $
10  * $DragonFly: src/lib/libmd/mdXhl.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
11  *
12  */
13
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "mdX.h"
23
24 char *
25 MDXEnd(MDX_CTX *ctx, char *buf)
26 {
27     int i;
28     unsigned char digest[LENGTH];
29     static const char hex[]="0123456789abcdef";
30
31     if (!buf)
32         buf = malloc(2*LENGTH + 1);
33     if (!buf)
34         return 0;
35     MDXFinal(digest, ctx);
36     for (i = 0; i < LENGTH; i++) {
37         buf[i+i] = hex[digest[i] >> 4];
38         buf[i+i+1] = hex[digest[i] & 0x0f];
39     }
40     buf[i+i] = '\0';
41     return buf;
42 }
43
44 char *
45 MDXFile(const char *filename, char *buf)
46 {
47     unsigned char buffer[BUFSIZ];
48     MDX_CTX ctx;
49     int f,i,j;
50
51     MDXInit(&ctx);
52     f = open(filename,O_RDONLY);
53     if (f < 0) return 0;
54     while ((i = read(f,buffer,sizeof buffer)) > 0) {
55         MDXUpdate(&ctx,buffer,i);
56     }
57     j = errno;
58     close(f);
59     errno = j;
60     if (i < 0) return 0;
61     return MDXEnd(&ctx, buf);
62 }
63
64 char *
65 MDXData (const unsigned char *data, unsigned int len, char *buf)
66 {
67     MDX_CTX ctx;
68
69     MDXInit(&ctx);
70     MDXUpdate(&ctx,data,len);
71     return MDXEnd(&ctx, buf);
72 }