1 /* mdXhl.c
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> 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 * libjpeg-turbo Modifications:
9 * Copyright (C)2016, 2018-2019, 2022 D. R. Commander. All Rights Reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither the name of the libjpeg-turbo Project nor the names of its
20 * contributors may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 * ----------------------------------------------------------------------------
35 */
36
37 #ifdef _MSC_VER
38 #define _CRT_SECURE_NO_DEPRECATE
39 #endif
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #ifdef _WIN32
45 #include <io.h>
46 #define close _close
47 #define fstat _fstat
48 #define lseek _lseek
49 #define read _read
50 #define stat _stat
51 #else
52 #include <unistd.h>
53 #endif
54
55 #include <errno.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58
59 #define LENGTH 16
60
61 #include "./md5.h"
62
MD5End(MD5_CTX * ctx,char * buf)63 static char *MD5End(MD5_CTX *ctx, char *buf)
64 {
65 int i;
66 unsigned char digest[LENGTH];
67 static const char hex[] = "0123456789abcdef";
68
69 if (!buf)
70 buf = malloc(2 * LENGTH + 1);
71 if (!buf)
72 return 0;
73 MD5Final(digest, ctx);
74 for (i = 0; i < LENGTH; i++) {
75 buf[i + i] = hex[digest[i] >> 4];
76 buf[i + i + 1] = hex[digest[i] & 0x0f];
77 }
78 buf[i + i] = '\0';
79 return buf;
80 }
81
MD5File(const char * filename,char * buf)82 char *MD5File(const char *filename, char *buf)
83 {
84 return (MD5FileChunk(filename, buf, 0, 0));
85 }
86
MD5FileChunk(const char * filename,char * buf,off_t ofs,off_t len)87 char *MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len)
88 {
89 unsigned char buffer[BUFSIZ];
90 MD5_CTX ctx;
91 struct stat stbuf;
92 int f, i, e;
93 off_t n;
94
95 MD5Init(&ctx);
96 #ifdef _WIN32
97 f = _open(filename, O_RDONLY | O_BINARY);
98 #else
99 f = open(filename, O_RDONLY);
100 #endif
101 if (f < 0)
102 return 0;
103 if (fstat(f, &stbuf) < 0)
104 return 0;
105 if (ofs > stbuf.st_size)
106 ofs = stbuf.st_size;
107 if ((len == 0) || (len > stbuf.st_size - ofs))
108 len = stbuf.st_size - ofs;
109 if (lseek(f, ofs, SEEK_SET) < 0)
110 return 0;
111 n = len;
112 i = 0;
113 while (n > 0) {
114 if (n > sizeof(buffer))
115 i = read(f, buffer, sizeof(buffer));
116 else
117 i = read(f, buffer, n);
118 if (i < 0)
119 break;
120 MD5Update(&ctx, buffer, i);
121 n -= i;
122 }
123 e = errno;
124 close(f);
125 errno = e;
126 if (i < 0)
127 return 0;
128 return (MD5End(&ctx, buf));
129 }
130