1294037Sjtl/* mdXhl.c 2294037Sjtl * ---------------------------------------------------------------------------- 31802Sphk * "THE BEER-WARE LICENSE" (Revision 42): 493149Sphk * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you 51802Sphk * can do whatever you want with this stuff. If we meet some day, and you think 61802Sphk * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 71802Sphk * ---------------------------------------------------------------------------- 81802Sphk */ 91802Sphk 1084211Sdillon#include <sys/cdefs.h> 1184211Sdillon__FBSDID("$FreeBSD: stable/11/lib/libmd/mdXhl.c 310372 2016-12-21 18:42:04Z emaste $"); 1284211Sdillon 136684Sphk#include <sys/types.h> 1474385Sphk#include <sys/stat.h> 1519168Sbde#include <fcntl.h> 166684Sphk#include <unistd.h> 171802Sphk 1819168Sbde#include <errno.h> 1919168Sbde#include <stdio.h> 2019168Sbde#include <stdlib.h> 2119168Sbde 2219168Sbde#include "mdX.h" 2319168Sbde 241802Sphkchar * 259488SphkMDXEnd(MDX_CTX *ctx, char *buf) 261802Sphk{ 27103098Sphk int i; 28103098Sphk unsigned char digest[LENGTH]; 29103098Sphk static const char hex[]="0123456789abcdef"; 308870Srgrimes 31103098Sphk if (!buf) 32103098Sphk buf = malloc(2*LENGTH + 1); 33103098Sphk if (!buf) 34103098Sphk return 0; 35103098Sphk MDXFinal(digest, ctx); 36103098Sphk for (i = 0; i < LENGTH; i++) { 37103098Sphk buf[i+i] = hex[digest[i] >> 4]; 38103098Sphk buf[i+i+1] = hex[digest[i] & 0x0f]; 39103098Sphk } 40103098Sphk buf[i+i] = '\0'; 41103098Sphk return buf; 421802Sphk} 431802Sphk 441802Sphkchar * 45310372SemasteMDXFd(int fd, char *buf) 461802Sphk{ 47310372Semaste return MDXFdChunk(fd, buf, 0, 0); 4874385Sphk} 4974385Sphk 5074385Sphkchar * 51310372SemasteMDXFdChunk(int fd, char *buf, off_t ofs, off_t len) 5274385Sphk{ 53285324Sjmg unsigned char buffer[16*1024]; 54103098Sphk MDX_CTX ctx; 55103098Sphk struct stat stbuf; 56310372Semaste int readrv, e; 57294037Sjtl off_t remain; 581802Sphk 59294037Sjtl if (len < 0) { 60294037Sjtl errno = EINVAL; 61294037Sjtl return NULL; 62294037Sjtl } 63294037Sjtl 64103098Sphk MDXInit(&ctx); 65294037Sjtl if (ofs != 0) { 66294037Sjtl errno = 0; 67294037Sjtl if (lseek(fd, ofs, SEEK_SET) != ofs || 68294037Sjtl (ofs == -1 && errno != 0)) { 69294037Sjtl readrv = -1; 70294037Sjtl goto error; 71294037Sjtl } 72292955Sjtl } 73294037Sjtl remain = len; 74294037Sjtl readrv = 0; 75294037Sjtl while (len == 0 || remain > 0) { 76294037Sjtl if (len == 0 || remain > sizeof(buffer)) 77294037Sjtl readrv = read(fd, buffer, sizeof(buffer)); 78103098Sphk else 79294037Sjtl readrv = read(fd, buffer, remain); 80294037Sjtl if (readrv <= 0) 81103098Sphk break; 82294037Sjtl MDXUpdate(&ctx, buffer, readrv); 83294037Sjtl remain -= readrv; 84103098Sphk } 85292955Sjtlerror: 86294037Sjtl if (readrv < 0) 87294037Sjtl return NULL; 88103098Sphk return (MDXEnd(&ctx, buf)); 891802Sphk} 901802Sphk 911802Sphkchar * 92310372SemasteMDXFile(const char *filename, char *buf) 93310372Semaste{ 94310372Semaste return (MDXFileChunk(filename, buf, 0, 0)); 95310372Semaste} 96310372Semaste 97310372Semastechar * 98310372SemasteMDXFileChunk(const char *filename, char *buf, off_t ofs, off_t len) 99310372Semaste{ 100310372Semaste char *ret; 101310372Semaste int e, fd; 102310372Semaste 103310372Semaste fd = open(filename, O_RDONLY); 104310372Semaste if (fd < 0) 105310372Semaste return NULL; 106310372Semaste ret = MDXFdChunk(fd, buf, ofs, len); 107310372Semaste e = errno; 108310372Semaste close (fd); 109310372Semaste errno = e; 110310372Semaste return ret; 111310372Semaste} 112310372Semaste 113310372Semastechar * 114154479SphkMDXData (const void *data, unsigned int len, char *buf) 1151802Sphk{ 116103098Sphk MDX_CTX ctx; 1171802Sphk 118103098Sphk MDXInit(&ctx); 119103098Sphk MDXUpdate(&ctx,data,len); 120103098Sphk return (MDXEnd(&ctx, buf)); 1211802Sphk} 122282726Sthomas 123282774Sthomas#ifdef WEAK_REFS 124282774Sthomas/* When building libmd, provide weak references. Note: this is not 125282774Sthomas activated in the context of compiling these sources for internal 126282774Sthomas use in libcrypt. 127282774Sthomas */ 128282726Sthomas#undef MDXEnd 129282726Sthomas__weak_reference(_libmd_MDXEnd, MDXEnd); 130282726Sthomas#undef MDXFile 131282726Sthomas__weak_reference(_libmd_MDXFile, MDXFile); 132282726Sthomas#undef MDXFileChunk 133282726Sthomas__weak_reference(_libmd_MDXFileChunk, MDXFileChunk); 134282726Sthomas#undef MDXData 135282726Sthomas__weak_reference(_libmd_MDXData, MDXData); 136282774Sthomas#endif 137