md5hl.c 3.8 KB

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