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 * ----------------------------------------------------------------------------
8 * $FreeBSD: src/lib/libmd/mdXhl.c,v 1.19 2006/01/17 15:35:56 phk Exp $
9 * $DragonFly: src/lib/libmd/mdXhl.c,v 1.3 2008/09/11 20:25:34 swildner Exp $
13 #include <sys/types.h>
25 MDXEnd(MDX_CTX
*ctx
, char *buf
)
28 unsigned char digest
[LENGTH
];
29 static const char hex
[]="0123456789abcdef";
32 buf
= malloc(2*LENGTH
+ 1);
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];
45 MDXFile(const char *filename
, char *buf
)
47 return (MDXFileChunk(filename
, buf
, 0, 0));
51 MDXFileChunk(const char *filename
, char *buf
, off_t ofs
, off_t len
)
53 unsigned char buffer
[8192];
60 f
= open(filename
, O_RDONLY
);
63 if (fstat(f
, &stbuf
) < 0)
65 if (ofs
> stbuf
.st_size
)
67 if ((len
== 0) || (len
> stbuf
.st_size
- ofs
))
68 len
= stbuf
.st_size
- ofs
;
69 if (lseek(f
, ofs
, SEEK_SET
) < 0)
74 if (n
> sizeof(buffer
))
75 i
= read(f
, buffer
, sizeof(buffer
));
77 i
= read(f
, buffer
, n
);
80 MDXUpdate(&ctx
, buffer
, i
);
88 return (MDXEnd(&ctx
, buf
));
92 MDXData (const void *data
, unsigned int len
, char *buf
)
97 MDXUpdate(&ctx
,data
,len
);
98 return (MDXEnd(&ctx
, buf
));