| 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 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 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <fcntl.h> |
| 40 | #ifdef _WIN32 |
| 41 | #include <io.h> |
| 42 | #define close _close |
| 43 | #define fstat _fstat |
| 44 | #define lseek _lseek |
| 45 | #define read _read |
| 46 | #define stat _stat |
| 47 | #else |
| 48 | #include <unistd.h> |
| 49 | #endif |
| 50 |
| 51 | #include <errno.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 |
| 55 | #define LENGTH 16 |
| 56 |
| 57 | #include "./md5.h" |
| 58 |
| 59 | static char *MD5End(MD5_CTX *ctx, char *buf) |
| 60 | { |
| 61 | int i; |
| 62 | unsigned char digest[LENGTH]; |
| 63 | static const char hex[] = "0123456789abcdef"; |
| 64 |
| 65 | if (!buf) |
| 66 | buf = malloc(2 * LENGTH + 1); |
| 67 | if (!buf) |
| 68 | return 0; |
| 69 | MD5Final(digest, ctx); |
| 70 | for (i = 0; i < LENGTH; i++) { |
| 71 | buf[i + i] = hex[digest[i] >> 4]; |
| 72 | buf[i + i + 1] = hex[digest[i] & 0x0f]; |
| 73 | } |
| 74 | buf[i + i] = '\0'; |
| 75 | return buf; |
| 76 | } |
| 77 |
| 78 | char *MD5File(const char *filename, char *buf) |
| 79 | { |
| 80 | return (MD5FileChunk(filename, buf, 0, 0)); |
| 81 | } |
| 82 |
| 83 | char *MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len) |
| 84 | { |
| 85 | unsigned char buffer[BUFSIZ]; |
| 86 | MD5_CTX ctx; |
| 87 | struct stat stbuf; |
| 88 | int f, i, e; |
| 89 | off_t n; |
| 90 |
| 91 | MD5Init(&ctx); |
| 92 | #ifdef _WIN32 |
| 93 | f = _open(filename, O_RDONLY | O_BINARY); |
| 94 | #else |
| 95 | f = open(filename, O_RDONLY); |
| 96 | #endif |
| 97 | if (f < 0) |
| 98 | return 0; |
| 99 | if (fstat(f, &stbuf) < 0) |
| 100 | return 0; |
| 101 | if (ofs > stbuf.st_size) |
| 102 | ofs = stbuf.st_size; |
| 103 | if ((len == 0) || (len > stbuf.st_size - ofs)) |
| 104 | len = stbuf.st_size - ofs; |
| 105 | if (lseek(f, ofs, SEEK_SET) < 0) |
| 106 | return 0; |
| 107 | n = len; |
| 108 | i = 0; |
| 109 | while (n > 0) { |
| 110 | if (n > sizeof(buffer)) |
| 111 | i = read(f, buffer, sizeof(buffer)); |
| 112 | else |
| 113 | i = read(f, buffer, n); |
| 114 | if (i < 0) |
| 115 | break; |
| 116 | MD5Update(&ctx, buffer, i); |
| 117 | n -= i; |
| 118 | } |
| 119 | e = errno; |
| 120 | close(f); |
| 121 | errno = e; |
| 122 | if (i < 0) |
| 123 | return 0; |
| 124 | return (MD5End(&ctx, buf)); |
| 125 | } |
| 126 |