md5hl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* mdXhl.c * ----------------------------------------------------------------------------
  2. * "THE BEER-WARE LICENSE" (Revision 42):
  3. * <[email protected]> wrote this file. As long as you retain this notice you
  4. * can do whatever you want with this stuff. If we meet some day, and you think
  5. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  6. * ----------------------------------------------------------------------------
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2016, D. R. Commander.
  9. * Modifications are under the same license as the original code (see above)
  10. * ----------------------------------------------------------------------------
  11. */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>
  15. #ifdef _WIN32
  16. #include <io.h>
  17. #define close _close
  18. #define fstat _fstat
  19. #define lseek _lseek
  20. #define read _read
  21. #define stat _stat
  22. #else
  23. #include <unistd.h>
  24. #endif
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #define LENGTH 16
  29. #include "./md5.h"
  30. char *
  31. MD5End(MD5_CTX *ctx, char *buf)
  32. {
  33. int i;
  34. unsigned char digest[LENGTH];
  35. static const char hex[]="0123456789abcdef";
  36. if (!buf)
  37. buf = malloc(2*LENGTH + 1);
  38. if (!buf)
  39. return 0;
  40. MD5Final(digest, ctx);
  41. for (i = 0; i < LENGTH; i++) {
  42. buf[i+i] = hex[digest[i] >> 4];
  43. buf[i+i+1] = hex[digest[i] & 0x0f];
  44. }
  45. buf[i+i] = '\0';
  46. return buf;
  47. }
  48. char *
  49. MD5File(const char *filename, char *buf)
  50. {
  51. return (MD5FileChunk(filename, buf, 0, 0));
  52. }
  53. char *
  54. MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len)
  55. {
  56. unsigned char buffer[BUFSIZ];
  57. MD5_CTX ctx;
  58. struct stat stbuf;
  59. int f, i, e;
  60. off_t n;
  61. MD5Init(&ctx);
  62. #if _WIN32
  63. f = _open(filename, O_RDONLY|O_BINARY);
  64. #else
  65. f = open(filename, O_RDONLY);
  66. #endif
  67. if (f < 0)
  68. return 0;
  69. if (fstat(f, &stbuf) < 0)
  70. return 0;
  71. if (ofs > stbuf.st_size)
  72. ofs = stbuf.st_size;
  73. if ((len == 0) || (len > stbuf.st_size - ofs))
  74. len = stbuf.st_size - ofs;
  75. if (lseek(f, ofs, SEEK_SET) < 0)
  76. return 0;
  77. n = len;
  78. i = 0;
  79. while (n > 0) {
  80. if (n > sizeof(buffer))
  81. i = read(f, buffer, sizeof(buffer));
  82. else
  83. i = read(f, buffer, n);
  84. if (i < 0)
  85. break;
  86. MD5Update(&ctx, buffer, i);
  87. n -= i;
  88. }
  89. e = errno;
  90. close(f);
  91. errno = e;
  92. if (i < 0)
  93. return 0;
  94. return (MD5End(&ctx, buf));
  95. }
  96. char *
  97. MD5Data (const void *data, unsigned int len, char *buf)
  98. {
  99. MD5_CTX ctx;
  100. MD5Init(&ctx);
  101. MD5Update(&ctx,data,len);
  102. return (MD5End(&ctx, buf));
  103. }