aboutsummaryrefslogblamecommitdiffstats
path: root/common.h
blob: f601ae5690678421a5dbd85f0ef638c47ea39422 (plain) (tree)
1
2
3
4
5
6
7
                                                         





                                       













                                                            












                                    







                    











                               





                                 

                  





                                     

                  


                       
















                                                                 
                     























                                     

                                                                                            

                                                                                              





                                                                                                     
                                             

                                                                           

                                                                                                   

            

                                                                      
                                                                                         







































                                                                                                    
/* See LICENSE file for copyright and license details. */
#ifndef SINGLE_THREADED
# include <pthread.h>
#else
# define pthread_barrier_t PhonyBarrier
#endif
#include <libhashsum.h>
#include <libsimple.h>
#include <libsimple-arg.h>
LIBSIMPLE_NORETURN__ void usage(void);


#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
# pragma clang diagnostic ignored "-Wcovered-switch-default"
# pragma clang diagnostic ignored "-Wpadded"
# pragma clang diagnostic ignored "-Wcomma"
# pragma clang diagnostic ignored "-Wassign-enum"
#elif defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
#endif


struct thread_data;


#ifdef SINGLE_THREADED
typedef struct {
	struct thread_data *threads;
	size_t nthreads;
} pthread_barrier_t;
#endif


enum command {
	SPECIALISED,
	ANYSUM,
	SHA3SUM,
	BSUM
};


enum format {
	BINARY        = 0,
	LOWERCASE_HEX = 1,
	UPPERCASE_HEX = 2,
#define FORMAT_MASK     0x00FF
	WITH_ALGOSTR  = 0x0100,
	WITH_FILENAME = 0x0200,
	WITH_NUL      = 0x0400,
	WITH_LF       = 0x0800
};


struct config {
	enum format format;
	int verify;
	int warn_improper_format;
	int hexinput;
	int recursive;
	int xdev;
	int xlink;
	struct algorithm *algorithms;
	size_t nalgorithms;
	size_t threads;
};


struct buffer {
	char *buf;
	size_t ready;
	size_t procoff;
	size_t offset;
	size_t size;
};


struct algorithm {
	const char *algostr;
	char *result;
	size_t result_size;
	size_t result_length;
	size_t offset;
	struct libhashsum_hasher hasher;
};


struct global_data {
	void (*action)(struct algorithm *, struct global_data *);
	enum format format;
	int hexinput;
	const char *file;
	struct buffer *buffer;
	struct algorithm *algorithms;
	size_t nalgorithms;
};


struct barrier_group {
	pthread_barrier_t barrier;
	struct thread_data *threads;
	size_t nthreads;
};


struct thread_data {
	struct barrier_group *group;
	struct global_data *global;
	size_t index;
#ifndef SINGLE_THREADED
	pthread_t thread;
#endif
};


/* get.c */
int calculate_and_print_each(char **files, struct algorithm *algorithms, size_t nalgorithms,
                             size_t nthreads, enum format format, int hexinput, int recursive,
                             int xdev, int xlink);

/* check.c */
int verify_checksums(char **files, struct algorithm *algorithms, size_t nalgorithms, size_t nthreads,
                     enum format format, int warn_improper_format, int hexinput);

/* barrier.c */
void barrierwait(pthread_barrier_t *barrier);
void barriersend(struct barrier_group *group, struct global_data *global,
                 void (*action)(struct algorithm *, struct global_data *));
void createbarriergroup(struct barrier_group *group_out, size_t count, struct global_data *global);
void killbarriergroup(struct barrier_group *group, struct global_data *global);

/* hash.c */
size_t inithashers(struct algorithm *algorithms, size_t nalgorithms);
void destroyhashers(struct algorithm *algorithms, size_t nalgorithms);
int calculate(const char *file, struct barrier_group *group, struct global_data *global);

/* write.c */
void writeall(int fd, const void *data, size_t n, const char *fname);
char *hex(char *out, const unsigned char *in, size_t n, const char *xdigits);
void format_result(struct algorithm *algorithm, const char *file, enum format format, int hexinput);

/* open.c */
int patheq(const char *have, const char *want, const char **end_out);
int openfile(const char *path, int *is_new_fd_out, const char **fname_out);

/* read.c */
int feedbuffer(int fd, struct buffer *buffer, const char *fname);
void shiftbuffer(struct algorithm *algorithms, size_t nalgorithms, struct buffer *buffer);
int unhex(struct buffer *buffer);

/* proc.c */
size_t getnproc(size_t default_count);
size_t getautonthreads(void);

/* command.c */
enum command getcommand(const char **name_out, enum libhashsum_algorithm *algorithm_out);
enum command getsupercommand(enum command cmd, enum libhashsum_algorithm algo);

/* opts.c */
int parseopt_vendor(void /* (struct config) */ *config, char *opt, char *val);
char *parseopts(void *config, char *string, int (*parseopt)(void *, char *opt, char *val));

/* cmdline_bsum.c */
int cmdline_bsum(int argc, char **argv, enum command command, struct config *config,
                 const char **algostr, enum libhashsum_algorithm *algorithm, char **algostrbuf);

/* cmdline_sha3sum.c */
int cmdline_sha3sum(int argc, char **argv, enum command command, struct config *config,
                    const char **algostr, enum libhashsum_algorithm *algorithm, char **algostrbuf);

/* cmdline_other.c */
int cmdline_other(int argc, char **argv, enum command command, struct config *config);

/* cmdline.c */
void cmdline(int *argcp, char ***argvp, struct config *config);