#include #include #include #include #include #include "../util.h" char *hashfile(char *path) { struct AVMD5 *ctx; int i, fd; char *ptr, *buf; size_t len, size; FILE *stream; if ((fd = open(path, O_RDONLY)) < 0) job_failed("Could not open file for hashing: %s", strerror(errno)); if (!(ctx = av_md5_alloc())) exit(99); av_md5_init(ctx); buf = xmalloc(4096); while ((len = read(fd, buf, 4096)) > 0) av_md5_update(ctx, buf, len); if (len < 0) job_failed("Could not read file for hashing: %s", strerror(errno)); close(fd); av_md5_final(ctx, buf); av_free(ctx); if (!(stream = open_memstream(&ptr, &size))) exit(99); for (i = 0; i < 16; i ++) fprintf(stream, "%02hhx", buf[i]); free(buf); fclose(stream); return ptr; }