~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to .pc/0001-License-Declaration.patch/src/util.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <string.h>
2
 
#include <time.h>
3
 
#include "util.h"
4
 
#ifdef __MINGW32__
5
 
#  include "compat.h"
6
 
#endif
7
 
 
8
 
/* Return the smallest power of 2 not less than n */
9
 
uint32_t ceil2(uint32_t n)
10
 
{
11
 
        --n;
12
 
        n |= n >> 1;
13
 
        n |= n >> 2;
14
 
        n |= n >> 4;
15
 
        n |= n >> 8;
16
 
        n |= n >> 16;
17
 
        return n + 1;
18
 
}
19
 
 
20
 
/* Return the largest power of 2 not greater than n */
21
 
uint32_t floor2(uint32_t n)
22
 
{
23
 
        n |= n >> 1;
24
 
        n |= n >> 2;
25
 
        n |= n >> 4;
26
 
        n |= n >> 8;
27
 
        n |= n >> 16;
28
 
        return n - (n >> 1);
29
 
}
30
 
 
31
 
#include <stdlib.h>
32
 
unsigned long ver2int(const char* version)
33
 
{
34
 
        unsigned long v;
35
 
        char* p;
36
 
 
37
 
        v = (unsigned long)(strtod(version, &p) * 1e7 + 0.5);
38
 
        while (*p)
39
 
                v += *p++;
40
 
 
41
 
        return v;
42
 
}
43
 
/*
44
 
#if !HAVE_STRCASESTR
45
 
#  include <ctype.h>
46
 
// from git 1.6.1.2 compat/strcasestr.c
47
 
char *strcasestr(const char *haystack, const char *needle)
48
 
{
49
 
        int nlen = strlen(needle);
50
 
        int hlen = strlen(haystack) - nlen + 1;
51
 
        int i;
52
 
 
53
 
        for (i = 0; i < hlen; i++) {
54
 
                int j;
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))
59
 
                                goto next;
60
 
                }
61
 
                return (char *) haystack + i;
62
 
        next: ;
63
 
        }
64
 
        return NULL;
65
 
}
66
 
#endif // !HAVE_STRCASESTR
67
 
*/
68
 
 
69
 
#if !HAVE_STRLCPY
70
 
// from git 1.6.1.2 compat/strcasestr.c
71
 
size_t strlcpy(char *dest, const char *src, size_t size)
72
 
{
73
 
        size_t ret = strlen(src);
74
 
 
75
 
        if (size) {
76
 
                size_t len = (ret >= size) ? size - 1 : ret;
77
 
                memcpy(dest, src, len);
78
 
                dest[len] = '\0';
79
 
        }
80
 
        return ret;
81
 
}
82
 
#endif // !HAVE_STRLCPY
83
 
 
84
 
#ifdef __WIN32__
85
 
int set_cloexec(int fd, unsigned char v) { return 0; }
86
 
#else
87
 
#  include <unistd.h>
88
 
#  include <fcntl.h>
89
 
int set_cloexec(int fd, unsigned char v)
90
 
{
91
 
        int f = fcntl(fd, F_GETFD);
92
 
        return f == -1 ? f : fcntl(fd, F_SETFD, (v ? f | FD_CLOEXEC : f & ~FD_CLOEXEC));
93
 
}
94
 
#endif // __WIN32__
95
 
 
96
 
 
97
 
int set_nonblock(int fd, unsigned char v)
98
 
{
99
 
#ifndef __WIN32__
100
 
        int f = fcntl(fd, F_GETFL);
101
 
        return f == -1 ? f : fcntl(fd, F_SETFL, (v ? f | O_NONBLOCK : f & ~O_NONBLOCK));
102
 
#else // __WIN32__
103
 
        u_long v_ = (u_long)v;
104
 
        errno = 0;
105
 
        if (ioctlsocket(fd, FIONBIO, &v_) == SOCKET_ERROR) {
106
 
                errno = WSAGetLastError();
107
 
                return -1;
108
 
        }
109
 
        else
110
 
                return 0;
111
 
#endif // __WIN32__
112
 
}
113
 
 
114
 
#ifndef __WIN32__
115
 
#  include <sys/types.h>
116
 
#  include <sys/socket.h>
117
 
#  include <netinet/in.h>
118
 
#  include <netinet/tcp.h>
119
 
#endif
120
 
int set_nodelay(int fd, unsigned char v)
121
 
{
122
 
        int val = v;
123
 
        return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const char*)&val, sizeof(val));
124
 
}
125
 
 
126
 
 
127
 
#ifdef __WIN32__
128
 
#  include <ws2tcpip.h>
129
 
#endif
130
 
 
131
 
int get_bufsize(int fd, int dir, int* len)
132
 
{
133
 
        socklen_t optlen = sizeof(*len);
134
 
        return getsockopt(fd, SOL_SOCKET, (dir == 0 ? SO_RCVBUF : SO_SNDBUF),
135
 
                          (char*)len, &optlen);
136
 
}
137
 
int set_bufsize(int fd, int dir, int len)
138
 
{
139
 
        return setsockopt(fd, SOL_SOCKET, (dir == 0 ? SO_RCVBUF : SO_SNDBUF),
140
 
                          (const char*)&len, sizeof(len));
141
 
}
142
 
 
143
 
#ifndef __WIN32__
144
 
#include <pthread.h>
145
 
#include <signal.h>
146
 
#ifndef NSIG
147
 
#  define NSIG 64
148
 
#endif
149
 
static size_t nsig = 0;
150
 
static struct sigaction* sigact = 0;
151
 
static pthread_mutex_t sigmutex = PTHREAD_MUTEX_INITIALIZER;
152
 
#endif
153
 
 
154
 
void save_signals(void)
155
 
{
156
 
#ifndef __WIN32__
157
 
        pthread_mutex_lock(&sigmutex);
158
 
        if (!sigact)
159
 
                sigact = new struct sigaction[NSIG];
160
 
        for (nsig = 1; nsig <= NSIG; nsig++)
161
 
                if (sigaction(nsig, NULL, &sigact[nsig-1]) == -1)
162
 
                        break;
163
 
        pthread_mutex_unlock(&sigmutex);
164
 
#endif
165
 
}
166
 
 
167
 
void restore_signals(void)
168
 
{
169
 
#ifndef __WIN32__
170
 
        pthread_mutex_lock(&sigmutex);
171
 
        for (size_t i = 1; i <= nsig; i++)
172
 
                sigaction(i, &sigact[i-1], NULL);
173
 
        delete [] sigact;
174
 
        sigact = 0;
175
 
        nsig = 0;
176
 
        pthread_mutex_unlock(&sigmutex);
177
 
#endif
178
 
}
179
 
 
180
 
 
181
 
uint32_t simple_hash_data(const unsigned char* buf, size_t len, uint32_t code)
182
 
{
183
 
        for (size_t i = 0; i < len; i++)
184
 
                code = ((code << 4) | (code >> (32 - 4))) ^ (uint32_t)buf[i];
185
 
 
186
 
        return code;
187
 
}
188
 
uint32_t simple_hash_str(const unsigned char* str, uint32_t code)
189
 
{
190
 
        while (*str)
191
 
                code = ((code << 4) | (code >> (32 - 4))) ^ (uint32_t)*str++;
192
 
        return code;
193
 
}
194
 
 
195
 
#include <vector>
196
 
#include <climits>
197
 
 
198
 
static const char hexsym[] = "0123456789ABCDEF";
199
 
static std::vector<char>* hexbuf;
200
 
const char* str2hex(const unsigned char* str, size_t len)
201
 
{
202
 
        if (unlikely(len == 0))
203
 
                return "";
204
 
        if (unlikely(!hexbuf)) {
205
 
                hexbuf = new std::vector<char>;
206
 
                hexbuf->reserve(192);
207
 
        }
208
 
        if (unlikely(hexbuf->size() < len * 3))
209
 
                hexbuf->resize(len * 3);
210
 
 
211
 
        char* p = &(*hexbuf)[0];
212
 
        size_t i;
213
 
        for (i = 0; i < len; i++) {
214
 
                *p++ = hexsym[str[i] >> 4];
215
 
                *p++ = hexsym[str[i] & 0xF];
216
 
                *p++ = ' ';
217
 
        }
218
 
        *(p - 1) = '\0';
219
 
 
220
 
        return &(*hexbuf)[0];
221
 
}
222
 
 
223
 
const char* str2hex(const char* str, size_t len)
224
 
{
225
 
        return str2hex((const unsigned char*)str, len ? len : strlen(str));
226
 
}
227
 
 
228
 
static std::vector<char>* binbuf;
229
 
const char* uint2bin(unsigned u, size_t len)
230
 
{
231
 
        if (unlikely(len == 0))
232
 
                len = sizeof(u) * CHAR_BIT;
233
 
 
234
 
        if (unlikely(!binbuf)) {
235
 
                binbuf = new std::vector<char>;
236
 
                binbuf->reserve(sizeof(u) * CHAR_BIT);
237
 
        }
238
 
        if (unlikely(binbuf->size() < len + 1))
239
 
                binbuf->resize(len + 1);
240
 
 
241
 
        for (size_t i = 0; i < len; i++) {
242
 
                (*binbuf)[len - i - 1] = '0' + (u & 1);
243
 
                u >>= 1;
244
 
        }
245
 
        (*binbuf)[len] = '\0';
246
 
 
247
 
        return &(*binbuf)[0];
248
 
}
249
 
 
250
 
void MilliSleep(long msecs)
251
 
{
252
 
#ifndef __WIN32__
253
 
        struct timespec tv;
254
 
        tv.tv_sec = msecs / 1000;
255
 
        tv.tv_nsec = (msecs - tv.tv_sec * 1000) * 1000000L;
256
 
        nanosleep(&tv, NULL);
257
 
#else
258
 
        Sleep(msecs);
259
 
#endif
260
 
 
261
 
}
262