11.13Schristos/* $NetBSD: mdXhl.c,v 1.13 2014/09/24 13:18:52 christos Exp $ */ 21.1Sthorpej 31.2Sthorpej/* 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.2Sthorpej */ 131.2Sthorpej 141.2Sthorpej/* 151.7Schristos * Modified April 29, 1997 by Jason R. Thorpe <thorpej@NetBSD.org> 161.1Sthorpej */ 171.1Sthorpej 181.8Sapb#if HAVE_NBTOOL_CONFIG_H 191.8Sapb#include "nbtool_config.h" 201.8Sapb#endif 211.8Sapb 221.7Schristos#define CONCAT(x,y) __CONCAT(x,y) 231.7Schristos#define MDNAME(x) CONCAT(MDALGORITHM,x) 241.7Schristos 251.8Sapb#if !defined(_KERNEL) && defined(__weak_alias) && !defined(HAVE_NBTOOL_CONFIG_H) 261.7Schristos#define WA(a,b) __weak_alias(a,b) 271.7SchristosWA(MDNAME(End),CONCAT(_,MDNAME(End))) 281.7SchristosWA(MDNAME(File),CONCAT(_,MDNAME(File))) 291.7SchristosWA(MDNAME(Data),CONCAT(_,MDNAME(Data))) 301.7Schristos#undef WA 311.7Schristos#endif 321.7Schristos 331.7Schristos#include "namespace.h" 341.7Schristos 351.1Sthorpej#include <sys/types.h> 361.4Slukem 371.7Schristos#include MDINCLUDE 381.4Slukem#include <assert.h> 391.1Sthorpej#include <fcntl.h> 401.1Sthorpej#include <errno.h> 411.1Sthorpej#include <stdio.h> 421.1Sthorpej#include <stdlib.h> 431.4Slukem#include <unistd.h> 441.1Sthorpej 451.1Sthorpejchar * 461.10SabsMDNAME(End)(MDNAME(_CTX) *ctx, char *buf) 471.1Sthorpej{ 481.2Sthorpej int i; 491.2Sthorpej unsigned char digest[16]; 501.2Sthorpej static const char hex[]="0123456789abcdef"; 511.2Sthorpej 521.4Slukem _DIAGASSERT(ctx != 0); 531.4Slukem 541.2Sthorpej if (buf == NULL) 551.2Sthorpej buf = malloc(33); 561.2Sthorpej if (buf == NULL) 571.2Sthorpej return (NULL); 581.2Sthorpej 591.2Sthorpej MDNAME(Final)(digest, ctx); 601.2Sthorpej 611.2Sthorpej for (i = 0; i < 16; i++) { 621.3Schristos buf[i+i] = hex[(u_int32_t)digest[i] >> 4]; 631.2Sthorpej buf[i+i+1] = hex[digest[i] & 0x0f]; 641.2Sthorpej } 651.2Sthorpej 661.2Sthorpej buf[i+i] = '\0'; 671.2Sthorpej return (buf); 681.1Sthorpej} 691.1Sthorpej 701.1Sthorpejchar * 711.10SabsMDNAME(File)(const char *filename, char *buf) 721.1Sthorpej{ 731.2Sthorpej unsigned char buffer[BUFSIZ]; 741.2Sthorpej MDNAME(_CTX) ctx; 751.9Schristos int f, j; 761.9Schristos ssize_t i; 771.2Sthorpej 781.4Slukem _DIAGASSERT(filename != 0); 791.4Slukem /* buf may be NULL */ 801.4Slukem 811.2Sthorpej MDNAME(Init)(&ctx); 821.11Schristos f = open(filename, O_RDONLY | O_CLOEXEC, 0666); 831.2Sthorpej if (f < 0) 841.2Sthorpej return NULL; 851.2Sthorpej 861.2Sthorpej while ((i = read(f, buffer, sizeof(buffer))) > 0) 871.3Schristos MDNAME(Update)(&ctx, buffer, (unsigned int)i); 881.2Sthorpej 891.2Sthorpej j = errno; 901.2Sthorpej close(f); 911.2Sthorpej errno = j; 921.2Sthorpej 931.2Sthorpej if (i < 0) 941.2Sthorpej return NULL; 951.2Sthorpej 961.2Sthorpej return (MDNAME(End)(&ctx, buf)); 971.1Sthorpej} 981.1Sthorpej 991.1Sthorpejchar * 1001.10SabsMDNAME(Data)(const unsigned char *data, unsigned int len, char *buf) 1011.1Sthorpej{ 1021.2Sthorpej MDNAME(_CTX) ctx; 1031.4Slukem 1041.4Slukem _DIAGASSERT(data != 0); 1051.1Sthorpej 1061.2Sthorpej MDNAME(Init)(&ctx); 1071.2Sthorpej MDNAME(Update)(&ctx, data, len); 1081.2Sthorpej return (MDNAME(End)(&ctx, buf)); 1091.1Sthorpej} 110