forked from archiecobbs/libnbcompat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdXhl.c
105 lines (84 loc) · 2.06 KB
/
mdXhl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* $NetBSD: mdXhl.c,v 1.2 2008/10/06 12:36:20 joerg Exp $ */
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp
*/
/*
* Modifed April 29, 1997 by Jason R. Thorpe <[email protected]>
*/
#include <nbcompat.h>
#include <nbcompat/types.h>
#include <nbcompat/assert.h>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <nbcompat/stdio.h>
#include <nbcompat/stdlib.h>
#include <nbcompat/unistd.h>
#define CONCAT(x,y) __CONCAT(x,y)
#define MDNAME(x) CONCAT(MDALGORITHM,x)
char *
MDNAME(End)(ctx, buf)
MDNAME(_CTX) *ctx;
char *buf;
{
int i;
unsigned char digest[16];
static const char hex[]="0123456789abcdef";
_DIAGASSERT(ctx != 0);
if (buf == NULL)
buf = malloc(33);
if (buf == NULL)
return (NULL);
MDNAME(Final)(digest, ctx);
for (i = 0; i < 16; i++) {
buf[i+i] = hex[(uint32_t)digest[i] >> 4];
buf[i+i+1] = hex[digest[i] & 0x0f];
}
buf[i+i] = '\0';
return (buf);
}
char *
MDNAME(File)(filename, buf)
const char *filename;
char *buf;
{
unsigned char buffer[BUFSIZ];
MDNAME(_CTX) ctx;
int f, i, j;
_DIAGASSERT(filename != 0);
/* buf may be NULL */
MDNAME(Init)(&ctx);
f = open(filename, O_RDONLY, 0666);
if (f < 0)
return NULL;
while ((i = read(f, buffer, sizeof(buffer))) > 0)
MDNAME(Update)(&ctx, buffer, (unsigned int)i);
j = errno;
close(f);
errno = j;
if (i < 0)
return NULL;
return (MDNAME(End)(&ctx, buf));
}
char *
MDNAME(Data)(data, len, buf)
const unsigned char *data;
unsigned int len;
char *buf;
{
MDNAME(_CTX) ctx;
_DIAGASSERT(data != 0);
MDNAME(Init)(&ctx);
MDNAME(Update)(&ctx, data, len);
return (MDNAME(End)(&ctx, buf));
}