md5hl.c 3.7 KB

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