8
/* Return the smallest power of 2 not less than n */
9
uint32_t ceil2(uint32_t n)
20
/* Return the largest power of 2 not greater than n */
21
uint32_t floor2(uint32_t n)
32
unsigned long ver2int(const char* version)
37
v = (unsigned long)(strtod(version, &p) * 1e7 + 0.5);
46
// from git 1.6.1.2 compat/strcasestr.c
47
char *strcasestr(const char *haystack, const char *needle)
49
int nlen = strlen(needle);
50
int hlen = strlen(haystack) - nlen + 1;
53
for (i = 0; i < hlen; i++) {
55
for (j = 0; j < nlen; j++) {
56
unsigned char c1 = haystack[i+j];
57
unsigned char c2 = needle[j];
58
if (toupper(c1) != toupper(c2))
61
return (char *) haystack + i;
66
#endif // !HAVE_STRCASESTR
70
// from git 1.6.1.2 compat/strcasestr.c
71
size_t strlcpy(char *dest, const char *src, size_t size)
73
size_t ret = strlen(src);
76
size_t len = (ret >= size) ? size - 1 : ret;
77
memcpy(dest, src, len);
82
#endif // !HAVE_STRLCPY
85
int set_cloexec(int fd, unsigned char v) { return 0; }
89
int set_cloexec(int fd, unsigned char v)
91
int f = fcntl(fd, F_GETFD);
92
return f == -1 ? f : fcntl(fd, F_SETFD, (v ? f | FD_CLOEXEC : f & ~FD_CLOEXEC));
97
int set_nonblock(int fd, unsigned char v)
100
int f = fcntl(fd, F_GETFL);
101
return f == -1 ? f : fcntl(fd, F_SETFL, (v ? f | O_NONBLOCK : f & ~O_NONBLOCK));
103
u_long v_ = (u_long)v;
105
if (ioctlsocket(fd, FIONBIO, &v_) == SOCKET_ERROR) {
106
errno = WSAGetLastError();
115
# include <sys/types.h>
116
# include <sys/socket.h>
117
# include <netinet/in.h>
118
# include <netinet/tcp.h>
120
int set_nodelay(int fd, unsigned char v)
123
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const char*)&val, sizeof(val));
128
# include <ws2tcpip.h>
131
int get_bufsize(int fd, int dir, int* len)
133
socklen_t optlen = sizeof(*len);
134
return getsockopt(fd, SOL_SOCKET, (dir == 0 ? SO_RCVBUF : SO_SNDBUF),
135
(char*)len, &optlen);
137
int set_bufsize(int fd, int dir, int len)
139
return setsockopt(fd, SOL_SOCKET, (dir == 0 ? SO_RCVBUF : SO_SNDBUF),
140
(const char*)&len, sizeof(len));
149
static size_t nsig = 0;
150
static struct sigaction* sigact = 0;
151
static pthread_mutex_t sigmutex = PTHREAD_MUTEX_INITIALIZER;
154
void save_signals(void)
157
pthread_mutex_lock(&sigmutex);
159
sigact = new struct sigaction[NSIG];
160
for (nsig = 1; nsig <= NSIG; nsig++)
161
if (sigaction(nsig, NULL, &sigact[nsig-1]) == -1)
163
pthread_mutex_unlock(&sigmutex);
167
void restore_signals(void)
170
pthread_mutex_lock(&sigmutex);
171
for (size_t i = 1; i <= nsig; i++)
172
sigaction(i, &sigact[i-1], NULL);
176
pthread_mutex_unlock(&sigmutex);
181
uint32_t simple_hash_data(const unsigned char* buf, size_t len, uint32_t code)
183
for (size_t i = 0; i < len; i++)
184
code = ((code << 4) | (code >> (32 - 4))) ^ (uint32_t)buf[i];
188
uint32_t simple_hash_str(const unsigned char* str, uint32_t code)
191
code = ((code << 4) | (code >> (32 - 4))) ^ (uint32_t)*str++;
198
static const char hexsym[] = "0123456789ABCDEF";
199
static std::vector<char>* hexbuf;
200
const char* str2hex(const unsigned char* str, size_t len)
202
if (unlikely(len == 0))
204
if (unlikely(!hexbuf)) {
205
hexbuf = new std::vector<char>;
206
hexbuf->reserve(192);
208
if (unlikely(hexbuf->size() < len * 3))
209
hexbuf->resize(len * 3);
211
char* p = &(*hexbuf)[0];
213
for (i = 0; i < len; i++) {
214
*p++ = hexsym[str[i] >> 4];
215
*p++ = hexsym[str[i] & 0xF];
220
return &(*hexbuf)[0];
223
const char* str2hex(const char* str, size_t len)
225
return str2hex((const unsigned char*)str, len ? len : strlen(str));
228
static std::vector<char>* binbuf;
229
const char* uint2bin(unsigned u, size_t len)
231
if (unlikely(len == 0))
232
len = sizeof(u) * CHAR_BIT;
234
if (unlikely(!binbuf)) {
235
binbuf = new std::vector<char>;
236
binbuf->reserve(sizeof(u) * CHAR_BIT);
238
if (unlikely(binbuf->size() < len + 1))
239
binbuf->resize(len + 1);
241
for (size_t i = 0; i < len; i++) {
242
(*binbuf)[len - i - 1] = '0' + (u & 1);
245
(*binbuf)[len] = '\0';
247
return &(*binbuf)[0];
250
void MilliSleep(long msecs)
254
tv.tv_sec = msecs / 1000;
255
tv.tv_nsec = (msecs - tv.tv_sec * 1000) * 1000000L;
256
nanosleep(&tv, NULL);