/* * Copyright 2010. Ivan Voras * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Helper library for comparing files through hashing. */ #include #include #include #include #include #include #include #include "hashjob.h" static void * hashjob_md5(void *arg) { struct hashjob *job = arg; assert(job->filename != NULL); job->hash_len = 32; memset(job->hash, 0, HASH_MAX_LEN); assert(MD5File(job->filename, job->hash) != NULL); return (job); } static void * hashjob_sha256(void *arg) { struct hashjob *job = arg; assert(job->filename != NULL); job->hash_len = 64; memset(job->hash, 0, HASH_MAX_LEN); assert(SHA256_File(job->filename, job->hash) != NULL); return (job); } int hashjob_start(struct hashjob *job, char *filename, enum HASH_TYPE type) { job->filename = filename; job->finished = 0; if (type == HASH_MD5) return (pthread_create(&job->thread, NULL, hashjob_md5, job)); else if (type == HASH_SHA256) return (pthread_create(&job->thread, NULL, hashjob_sha256, job)); else return (-1); } int hashjob_finish(struct hashjob *job) { void *ret; int rval; if (job->finished) return 0; rval = pthread_join(job->thread, &ret); job->finished = 1; return (rval); }