aboutsummaryrefslogtreecommitdiffstats
path: root/common.h
blob: 00aae3e04c6866d51f70d0a424f85457907a6ddb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* 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, char **algostrbufp);