This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
helper.h
192 lines (155 loc) · 4.9 KB
/
helper.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef HELPER_H
#define HELPER_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef ENABLE_SSL
#include <openssl/ssl.h>
#include <openssl/pem.h>
#endif
#include "sds.h"
#include "libircclient-include/strings_utils.h"
#include "xdccget.h"
#include "libircclient.h"
#include "hashmap.h"
#include "argument_parser.h"
#include "dirs.h"
#define LOG_ERR 0
#define LOG_QUIET 1
#define LOG_WARN 2
#define LOG_INFO 3
/* ansi color codes used at the dbg macros for coloured output. */
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
/* define DBG-macros for debugging purposes if DEBUG is defined...*/
#ifdef DEBUG
#define DBG_MSG(color, stream, format, ...) do {\
fprintf(stream, "%sDBG:%s \"", color, KNRM);\
fprintf(stream, format, ##__VA_ARGS__);\
fprintf(stream, "\" function: %s file: %s line: %d\n",(char*) __func__, (char*)__FILE__, __LINE__);} while(0)
#define DBG_OK(format, ...) do {\
DBG_MSG(KGRN, stdout, format, ##__VA_ARGS__);\
} while(0)
#define DBG_WARN(format, ...) do {\
DBG_MSG(KYEL, stderr, format, ##__VA_ARGS__);\
} while(0)
#define DBG_ERR(format, ...) do {\
DBG_MSG(KRED, stderr, format, ##__VA_ARGS__);\
exitPgm(EXIT_FAILURE);\
} while(0)
#else
#define DBG_MSG(color, stream, format, ...) do {} while(0)
#define DBG_OK(format, ...) do {} while(0)
#define DBG_WARN(format, ...) do {} while(0)
#define DBG_ERR(format, ...) do {} while(0)
#endif
#ifdef __GNUC__
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
/* define macro for free that checks if ptr is null and sets ptr after free to null. */
#define FREE(X) \
do {\
if ( (X != NULL) ) {\
DBG_OK("freeing %p now", X);\
free(( X ));\
X = NULL;\
}\
} while(0)
#define bitset_t uint64_t
struct xdccGetConfig {
irc_session_t *session;
uint32_t logLevel;
struct dccDownload **dccDownloadArray;
uint32_t numDownloads;
bitset_t flags;
char *ircServer;
sds *channelsToJoin;
sds targetDir;
sds nick;
sds login_command;
char *args[3];
uint32_t numChannels;
uint16_t port;
};
#define OUTPUT_FLAG 0x01
#define ALLOW_ALL_CERTS_FLAG 0x02
#define USE_IPV4_FLAG 0x03
#define USE_IPV6_FLAG 0x04
#define VERIFY_CHECKSUM_FLAG 0x05
#define SENDED_FLAG 0x06
#define ACCEPT_ALL_NICKS_FLAG 0x07
#define DONT_CONFIRM_OFFSETS_FLAG 0x08
struct terminalDimension {
int rows;
int cols;
};
struct checksumThreadData {
sds completePath;
sds expectedHash;
};
struct dccDownloadContext {
struct dccDownloadProgress *progress;
struct file_io_t *fd;
};
void cfg_clear_bit(struct xdccGetConfig *config, int bitNum);
void cfg_set_bit(struct xdccGetConfig *config, int bitNum);
int cfg_get_bit(struct xdccGetConfig *config, int bitNum);
void logprintf(int logLevel, char *formatString, ...);
/* Wrapper for malloc. Checks if malloc fails and exits pgm if it does. */
static inline void* Malloc(size_t size) {
void *t = malloc(size);
if (unlikely(t == NULL))
{
logprintf(LOG_ERR, "malloc failed. exiting now.\n");
exit(EXIT_FAILURE);
}
return t;
}
/* Mallocs and then nulls the reserved memory. */
static inline void* Safe_Malloc(size_t size) {
void *t = Malloc(size);
memset(t, 0, size);
return t;
}
/* wraps calloc call. */
static inline void* Calloc(size_t numElements, size_t sizeOfElement) {
void *t = calloc(numElements, sizeOfElement);
if (unlikely(t == NULL))
{
logprintf(LOG_ERR, "calloc failed. exiting now.\n");
exit(EXIT_FAILURE);
}
return t;
}
static inline sds getConfigDirectory() {
sds configDir = sdscatprintf(sdsempty(), "%s%s%s%s", getHomeDir(), getPathSeperator(), ".xdccget", getPathSeperator());
return configDir;
}
/* inits the rand-function */
void initRand();
/* reads in the complete content of an text file and returns sds string. string need to be freed with sdsfree*/
sds readTextFile (char *filePath);
/* range-based rand. the returned number will be at least low, but lower than high. */
int rand_range(int low, int high);
/* create a random nickname (e.g. a string) of nicklen chars. result is stored at nick.
function does not malloc, so calling function has to reserve enough space at nick. */
void createRandomNick(int nickLen, char *nick);
struct terminalDimension *getTerminalDimension();
void printProgressBar(const int numBars, const double percentRdy);
int printSize (irc_dcc_size_t size);
void outputProgress(struct dccDownloadProgress *tdp);
#ifdef ENABLE_SSL
int openssl_check_certificate_callback(int preverify_ok, X509_STORE_CTX *ctx);
#endif
#endif