shun-iwasawa 82a8f5
/* mdXhl.c
shun-iwasawa 82a8f5
 * ----------------------------------------------------------------------------
shun-iwasawa 82a8f5
 * "THE BEER-WARE LICENSE" (Revision 42):
shun-iwasawa 82a8f5
 * <phk@freebsd.org> wrote this file.  As long as you retain this notice you</phk@freebsd.org>
shun-iwasawa 82a8f5
 * can do whatever you want with this stuff. If we meet some day, and you think
shun-iwasawa 82a8f5
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
shun-iwasawa 82a8f5
 * ----------------------------------------------------------------------------
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C)2016, 2018-2019 D. R. Commander.  All Rights Reserved.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Redistribution and use in source and binary forms, with or without
shun-iwasawa 82a8f5
 * modification, are permitted provided that the following conditions are met:
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * - Redistributions of source code must retain the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer.
shun-iwasawa 82a8f5
 * - Redistributions in binary form must reproduce the above copyright notice,
shun-iwasawa 82a8f5
 *   this list of conditions and the following disclaimer in the documentation
shun-iwasawa 82a8f5
 *   and/or other materials provided with the distribution.
shun-iwasawa 82a8f5
 * - Neither the name of the libjpeg-turbo Project nor the names of its
shun-iwasawa 82a8f5
 *   contributors may be used to endorse or promote products derived from this
shun-iwasawa 82a8f5
 *   software without specific prior written permission.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
shun-iwasawa 82a8f5
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
shun-iwasawa 82a8f5
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
shun-iwasawa 82a8f5
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
shun-iwasawa 82a8f5
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
shun-iwasawa 82a8f5
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
shun-iwasawa 82a8f5
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
shun-iwasawa 82a8f5
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
shun-iwasawa 82a8f5
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
shun-iwasawa 82a8f5
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
shun-iwasawa 82a8f5
 * POSSIBILITY OF SUCH DAMAGE.
shun-iwasawa 82a8f5
 * ----------------------------------------------------------------------------
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <sys types.h=""></sys>
shun-iwasawa 82a8f5
#include <sys stat.h=""></sys>
shun-iwasawa 82a8f5
#include <fcntl.h></fcntl.h>
shun-iwasawa 82a8f5
#ifdef _WIN32
shun-iwasawa 82a8f5
#include <io.h></io.h>
shun-iwasawa 82a8f5
#define close  _close
shun-iwasawa 82a8f5
#define fstat  _fstat
shun-iwasawa 82a8f5
#define lseek  _lseek
shun-iwasawa 82a8f5
#define read  _read
shun-iwasawa 82a8f5
#define stat  _stat
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
#include <unistd.h></unistd.h>
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include <errno.h></errno.h>
shun-iwasawa 82a8f5
#include <stdio.h></stdio.h>
shun-iwasawa 82a8f5
#include <stdlib.h></stdlib.h>
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define LENGTH  16
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "./md5.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static char *MD5End(MD5_CTX *ctx, char *buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
  unsigned char digest[LENGTH];
shun-iwasawa 82a8f5
  static const char hex[] = "0123456789abcdef";
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!buf)
shun-iwasawa 82a8f5
    buf = malloc(2 * LENGTH + 1);
shun-iwasawa 82a8f5
  if (!buf)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  MD5Final(digest, ctx);
shun-iwasawa 82a8f5
  for (i = 0; i < LENGTH; i++) {
shun-iwasawa 82a8f5
    buf[i + i] = hex[digest[i] >> 4];
shun-iwasawa 82a8f5
    buf[i + i + 1] = hex[digest[i] & 0x0f];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  buf[i + i] = '\0';
shun-iwasawa 82a8f5
  return buf;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
char *MD5File(const char *filename, char *buf)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return (MD5FileChunk(filename, buf, 0, 0));
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
char *MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  unsigned char buffer[BUFSIZ];
shun-iwasawa 82a8f5
  MD5_CTX ctx;
shun-iwasawa 82a8f5
  struct stat stbuf;
shun-iwasawa 82a8f5
  int f, i, e;
shun-iwasawa 82a8f5
  off_t n;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  MD5Init(&ctx);
shun-iwasawa 82a8f5
#ifdef _WIN32
shun-iwasawa 82a8f5
  f = _open(filename, O_RDONLY | O_BINARY);
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
  f = open(filename, O_RDONLY);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
  if (f < 0)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  if (fstat(f, &stbuf) < 0)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  if (ofs > stbuf.st_size)
shun-iwasawa 82a8f5
    ofs = stbuf.st_size;
shun-iwasawa 82a8f5
  if ((len == 0) || (len > stbuf.st_size - ofs))
shun-iwasawa 82a8f5
    len = stbuf.st_size - ofs;
shun-iwasawa 82a8f5
  if (lseek(f, ofs, SEEK_SET) < 0)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  n = len;
shun-iwasawa 82a8f5
  i = 0;
shun-iwasawa 82a8f5
  while (n > 0) {
shun-iwasawa 82a8f5
    if (n > sizeof(buffer))
shun-iwasawa 82a8f5
      i = read(f, buffer, sizeof(buffer));
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      i = read(f, buffer, n);
shun-iwasawa 82a8f5
    if (i < 0)
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    MD5Update(&ctx, buffer, i);
shun-iwasawa 82a8f5
    n -= i;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  e = errno;
shun-iwasawa 82a8f5
  close(f);
shun-iwasawa 82a8f5
  errno = e;
shun-iwasawa 82a8f5
  if (i < 0)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  return (MD5End(&ctx, buf));
shun-iwasawa 82a8f5
}