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.6 1995/07/12 09:13:47 phk 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 22 char * 23 MDXEnd(MDX_CTX *ctx, char *buf) 24 { 25 int i; 26 unsigned char digest[16]; 27 static const char hex[]="0123456789abcdef"; 28 29 if (!buf) 30 buf = malloc(33); 31 if (!buf) 32 return 0; 33 MDXFinal(digest,ctx); 34 for (i=0;i<16;i++) { 35 buf[i+i] = hex[digest[i] >> 4]; 36 buf[i+i+1] = hex[digest[i] & 0x0f]; 37 } 38 buf[i+i] = '\0'; 39 return buf; 40 } 41 42 char * 43 MDXFile (char *filename, char *buf) 44 { 45 unsigned char buffer[BUFSIZ]; 46 MDX_CTX ctx; 47 int f,i,j; 48 49 MDXInit(&ctx); 50 f = open(filename,O_RDONLY); 51 if (f < 0) return 0; 52 while ((i = read(f,buffer,sizeof buffer)) > 0) { 53 MDXUpdate(&ctx,buffer,i); 54 } 55 j = errno; 56 close(f); 57 errno = j; 58 if (i < 0) return 0; 59 return MDXEnd(&ctx, buf); 60 } 61 62 char * 63 MDXData (const unsigned char *data, unsigned int len, char *buf) 64 { 65 MDX_CTX ctx; 66 67 MDXInit(&ctx); 68 MDXUpdate(&ctx,data,len); 69 return MDXEnd(&ctx, buf); 70 } 71