Commit c0802519 authored by Julian Rother's avatar Julian Rother

Added hashfile function

parent 5e3d6d02
......@@ -12,6 +12,7 @@ char *vmprintf(const char *fmt, va_list ap);
char *mprintf(const char *fmt, ...);
void *xmalloc(size_t size);
char *hashfile(char *path);
/* JSON av parsing */
void parse_dict(AVDictionary **d, char *s);
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libavutil/md5.h>
#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);
fprintf(stream, "md5:");
for (i = 0; i < 16; i ++)
fprintf(stream, "%02hhx", buf[i]);
free(buf);
fclose(stream);
return ptr;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment