mdXhl.c revision 1.1
11.1Sthorpej/*	$NetBSD: mdXhl.c,v 1.1 1997/01/30 01:01:44 thorpej Exp $	*/
21.1Sthorpej
31.1Sthorpej/* mdXhl.c
41.1Sthorpej * ----------------------------------------------------------------------------
51.1Sthorpej * "THE BEER-WARE LICENSE" (Revision 42):
61.1Sthorpej * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
71.1Sthorpej * can do whatever you want with this stuff. If we meet some day, and you think
81.1Sthorpej * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
91.1Sthorpej * ----------------------------------------------------------------------------
101.1Sthorpej *
111.1Sthorpej * from FreeBSD Id: mdXhl.c,v 1.8 1996/10/25 06:48:12 bde Exp
121.1Sthorpej *
131.1Sthorpej */
141.1Sthorpej
151.1Sthorpej#include <sys/types.h>
161.1Sthorpej#include <fcntl.h>
171.1Sthorpej#include <unistd.h>
181.1Sthorpej
191.1Sthorpej#include <errno.h>
201.1Sthorpej#include <stdio.h>
211.1Sthorpej#include <stdlib.h>
221.1Sthorpej
231.1Sthorpej#include "mdX.h"
241.1Sthorpej
251.1Sthorpejchar *
261.1SthorpejMDXEnd(MDX_CTX *ctx, char *buf)
271.1Sthorpej{
281.1Sthorpej    int i;
291.1Sthorpej    unsigned char digest[16];
301.1Sthorpej    static const char hex[]="0123456789abcdef";
311.1Sthorpej
321.1Sthorpej    if (!buf)
331.1Sthorpej        buf = malloc(33);
341.1Sthorpej    if (!buf)
351.1Sthorpej	return 0;
361.1Sthorpej    MDXFinal(digest,ctx);
371.1Sthorpej    for (i=0;i<16;i++) {
381.1Sthorpej	buf[i+i] = hex[digest[i] >> 4];
391.1Sthorpej	buf[i+i+1] = hex[digest[i] & 0x0f];
401.1Sthorpej    }
411.1Sthorpej    buf[i+i] = '\0';
421.1Sthorpej    return buf;
431.1Sthorpej}
441.1Sthorpej
451.1Sthorpejchar *
461.1SthorpejMDXFile (char *filename, char *buf)
471.1Sthorpej{
481.1Sthorpej    unsigned char buffer[BUFSIZ];
491.1Sthorpej    MDX_CTX ctx;
501.1Sthorpej    int f,i,j;
511.1Sthorpej
521.1Sthorpej    MDXInit(&ctx);
531.1Sthorpej    f = open(filename,O_RDONLY);
541.1Sthorpej    if (f < 0) return 0;
551.1Sthorpej    while ((i = read(f,buffer,sizeof buffer)) > 0) {
561.1Sthorpej	MDXUpdate(&ctx,buffer,i);
571.1Sthorpej    }
581.1Sthorpej    j = errno;
591.1Sthorpej    close(f);
601.1Sthorpej    errno = j;
611.1Sthorpej    if (i < 0) return 0;
621.1Sthorpej    return MDXEnd(&ctx, buf);
631.1Sthorpej}
641.1Sthorpej
651.1Sthorpejchar *
661.1SthorpejMDXData (const unsigned char *data, unsigned int len, char *buf)
671.1Sthorpej{
681.1Sthorpej    MDX_CTX ctx;
691.1Sthorpej
701.1Sthorpej    MDXInit(&ctx);
711.1Sthorpej    MDXUpdate(&ctx,data,len);
721.1Sthorpej    return MDXEnd(&ctx, buf);
731.1Sthorpej}
74