]> git.proxmox.com Git - libgit2.git/blob - src/util.h
Merge pull request #2284 from jacquesg/push-progress-callback
[libgit2.git] / src / util.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_util_h__
8 #define INCLUDE_util_h__
9
10 #include "common.h"
11 #include "strnlen.h"
12
13 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
14 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
15 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
16 #ifndef min
17 # define min(a,b) ((a) < (b) ? (a) : (b))
18 #endif
19 #ifndef max
20 # define max(a,b) ((a) > (b) ? (a) : (b))
21 #endif
22
23 #define GIT_DATE_RFC2822_SZ 32
24
25 /**
26 * Return the length of a constant string.
27 * We are aware that `strlen` performs the same task and is usually
28 * optimized away by the compiler, whilst being safer because it returns
29 * valid values when passed a pointer instead of a constant string; however
30 * this macro will transparently work with wide-char and single-char strings.
31 */
32 #define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
33
34 /*
35 * Custom memory allocation wrappers
36 * that set error code and error message
37 * on allocation failure
38 */
39 GIT_INLINE(void *) git__malloc(size_t len)
40 {
41 void *ptr = malloc(len);
42 if (!ptr) giterr_set_oom();
43 return ptr;
44 }
45
46 GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
47 {
48 void *ptr = calloc(nelem, elsize);
49 if (!ptr) giterr_set_oom();
50 return ptr;
51 }
52
53 GIT_INLINE(char *) git__strdup(const char *str)
54 {
55 char *ptr = strdup(str);
56 if (!ptr) giterr_set_oom();
57 return ptr;
58 }
59
60 GIT_INLINE(char *) git__strndup(const char *str, size_t n)
61 {
62 size_t length = 0;
63 char *ptr;
64
65 length = p_strnlen(str, n);
66
67 ptr = (char*)git__malloc(length + 1);
68
69 if (!ptr)
70 return NULL;
71
72 if (length)
73 memcpy(ptr, str, length);
74
75 ptr[length] = '\0';
76
77 return ptr;
78 }
79
80 /* NOTE: This doesn't do null or '\0' checking. Watch those boundaries! */
81 GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
82 {
83 char *ptr = (char*)git__malloc(n+1);
84 memcpy(ptr, start, n);
85 ptr[n] = '\0';
86 return ptr;
87 }
88
89 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
90 {
91 void *new_ptr = realloc(ptr, size);
92 if (!new_ptr) giterr_set_oom();
93 return new_ptr;
94 }
95
96 GIT_INLINE(void) git__free(void *ptr)
97 {
98 free(ptr);
99 }
100
101 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
102 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
103
104 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
105 ((IGNORE_CASE) ? (ICASE) : (CASE))
106
107 extern int git__prefixcmp(const char *str, const char *prefix);
108 extern int git__prefixcmp_icase(const char *str, const char *prefix);
109 extern int git__suffixcmp(const char *str, const char *suffix);
110
111 GIT_INLINE(int) git__signum(int val)
112 {
113 return ((val > 0) - (val < 0));
114 }
115
116 extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
117 extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
118
119 extern void git__hexdump(const char *buffer, size_t n);
120 extern uint32_t git__hash(const void *key, int len, uint32_t seed);
121
122 /** @return true if p fits into the range of a size_t */
123 GIT_INLINE(int) git__is_sizet(git_off_t p)
124 {
125 size_t r = (size_t)p;
126 return p == (git_off_t)r;
127 }
128
129 /** @return true if p fits into the range of a uint32_t */
130 GIT_INLINE(int) git__is_uint32(size_t p)
131 {
132 uint32_t r = (uint32_t)p;
133 return p == (size_t)r;
134 }
135
136 /* 32-bit cross-platform rotl */
137 #ifdef _MSC_VER /* use built-in method in MSVC */
138 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
139 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
140 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
141 #endif
142
143 extern char *git__strtok(char **end, const char *sep);
144 extern char *git__strsep(char **end, const char *sep);
145
146 extern void git__strntolower(char *str, size_t len);
147 extern void git__strtolower(char *str);
148
149 GIT_INLINE(const char *) git__next_line(const char *s)
150 {
151 while (*s && *s != '\n') s++;
152 while (*s == '\n' || *s == '\r') s++;
153 return s;
154 }
155
156 GIT_INLINE(const void *) git__memrchr(const void *s, int c, size_t n)
157 {
158 const unsigned char *cp;
159
160 if (n != 0) {
161 cp = (unsigned char *)s + n;
162 do {
163 if (*(--cp) == (unsigned char)c)
164 return cp;
165 } while (--n != 0);
166 }
167
168 return NULL;
169 }
170
171 typedef int (*git__tsort_cmp)(const void *a, const void *b);
172
173 extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
174
175 typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload);
176
177 extern void git__tsort_r(
178 void **dst, size_t size, git__sort_r_cmp cmp, void *payload);
179
180 extern void git__qsort_r(
181 void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload);
182
183 extern void git__insertsort_r(
184 void *els, size_t nel, size_t elsize, void *swapel,
185 git__sort_r_cmp cmp, void *payload);
186
187 /**
188 * @param position If non-NULL, this will be set to the position where the
189 * element is or would be inserted if not found.
190 * @return 0 if found; GIT_ENOTFOUND if not found
191 */
192 extern int git__bsearch(
193 void **array,
194 size_t array_len,
195 const void *key,
196 int (*compare)(const void *key, const void *element),
197 size_t *position);
198
199 extern int git__bsearch_r(
200 void **array,
201 size_t array_len,
202 const void *key,
203 int (*compare_r)(const void *key, const void *element, void *payload),
204 void *payload,
205 size_t *position);
206
207 extern int git__strcmp_cb(const void *a, const void *b);
208 extern int git__strcasecmp_cb(const void *a, const void *b);
209
210 extern int git__strcmp(const char *a, const char *b);
211 extern int git__strcasecmp(const char *a, const char *b);
212 extern int git__strncmp(const char *a, const char *b, size_t sz);
213 extern int git__strncasecmp(const char *a, const char *b, size_t sz);
214
215 extern int git__strcasesort_cmp(const char *a, const char *b);
216
217 #include "thread-utils.h"
218
219 typedef struct {
220 git_atomic refcount;
221 void *owner;
222 } git_refcount;
223
224 typedef void (*git_refcount_freeptr)(void *r);
225
226 #define GIT_REFCOUNT_INC(r) { \
227 git_atomic_inc(&((git_refcount *)(r))->refcount); \
228 }
229
230 #define GIT_REFCOUNT_DEC(_r, do_free) { \
231 git_refcount *r = (git_refcount *)(_r); \
232 int val = git_atomic_dec(&r->refcount); \
233 if (val <= 0 && r->owner == NULL) { do_free(_r); } \
234 }
235
236 #define GIT_REFCOUNT_OWN(r, o) { \
237 ((git_refcount *)(r))->owner = o; \
238 }
239
240 #define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
241
242 #define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
243
244
245 static signed char from_hex[] = {
246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
249 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
250 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
251 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
252 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
253 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
254 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
255 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
256 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
257 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
258 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
259 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
260 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
261 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
262 };
263
264 GIT_INLINE(int) git__fromhex(char h)
265 {
266 return from_hex[(unsigned char) h];
267 }
268
269 GIT_INLINE(int) git__ishex(const char *str)
270 {
271 unsigned i;
272 for (i=0; i<strlen(str); i++)
273 if (git__fromhex(str[i]) < 0)
274 return 0;
275 return 1;
276 }
277
278 GIT_INLINE(size_t) git__size_t_bitmask(size_t v)
279 {
280 v--;
281 v |= v >> 1;
282 v |= v >> 2;
283 v |= v >> 4;
284 v |= v >> 8;
285 v |= v >> 16;
286
287 return v;
288 }
289
290 GIT_INLINE(size_t) git__size_t_powerof2(size_t v)
291 {
292 return git__size_t_bitmask(v) + 1;
293 }
294
295 GIT_INLINE(bool) git__isupper(int c)
296 {
297 return (c >= 'A' && c <= 'Z');
298 }
299
300 GIT_INLINE(bool) git__isalpha(int c)
301 {
302 return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
303 }
304
305 GIT_INLINE(bool) git__isdigit(int c)
306 {
307 return (c >= '0' && c <= '9');
308 }
309
310 GIT_INLINE(bool) git__isspace(int c)
311 {
312 return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
313 }
314
315 GIT_INLINE(bool) git__isspace_nonlf(int c)
316 {
317 return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */);
318 }
319
320 GIT_INLINE(bool) git__iswildcard(int c)
321 {
322 return (c == '*' || c == '?' || c == '[');
323 }
324
325 /*
326 * Parse a string value as a boolean, just like Core Git does.
327 *
328 * Valid values for true are: 'true', 'yes', 'on'
329 * Valid values for false are: 'false', 'no', 'off'
330 */
331 extern int git__parse_bool(int *out, const char *value);
332
333 /*
334 * Parse a string into a value as a git_time_t.
335 *
336 * Sample valid input:
337 * - "yesterday"
338 * - "July 17, 2003"
339 * - "2003-7-17 08:23"
340 */
341 extern int git__date_parse(git_time_t *out, const char *date);
342
343 /*
344 * Format a git_time as a RFC2822 string
345 *
346 * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
347 * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
348 * @param date the date to be formatted
349 * @return 0 if successful; -1 on error
350 */
351 extern int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date);
352
353 /*
354 * Unescapes a string in-place.
355 *
356 * Edge cases behavior:
357 * - "jackie\" -> "jacky\"
358 * - "chan\\" -> "chan\"
359 */
360 extern size_t git__unescape(char *str);
361
362 /*
363 * Safely zero-out memory, making sure that the compiler
364 * doesn't optimize away the operation.
365 */
366 GIT_INLINE(void) git__memzero(void *data, size_t size)
367 {
368 #ifdef _MSC_VER
369 SecureZeroMemory((PVOID)data, size);
370 #else
371 volatile uint8_t *scan = (volatile uint8_t *)data;
372
373 while (size--)
374 *scan++ = 0x0;
375 #endif
376 }
377
378 #ifdef GIT_WIN32
379
380 GIT_INLINE(double) git__timer(void)
381 {
382 /* We need the initial tick count to detect if the tick
383 * count has rolled over. */
384 static DWORD initial_tick_count = 0;
385
386 /* GetTickCount returns the number of milliseconds that have
387 * elapsed since the system was started. */
388 DWORD count = GetTickCount();
389
390 if(initial_tick_count == 0) {
391 initial_tick_count = count;
392 } else if (count < initial_tick_count) {
393 /* The tick count has rolled over - adjust for it. */
394 count = (0xFFFFFFFF - initial_tick_count) + count;
395 }
396
397 return (double) count / (double) 1000;
398 }
399
400 #elif __APPLE__
401
402 #include <mach/mach_time.h>
403
404 GIT_INLINE(double) git__timer(void)
405 {
406 uint64_t time = mach_absolute_time();
407 static double scaling_factor = 0;
408
409 if (scaling_factor == 0) {
410 mach_timebase_info_data_t info;
411 (void)mach_timebase_info(&info);
412 scaling_factor = (double)info.numer / (double)info.denom;
413 }
414
415 return (double)time * scaling_factor / 1.0E-9;
416 }
417
418 #else
419
420 #include <sys/time.h>
421
422 GIT_INLINE(double) git__timer(void)
423 {
424 struct timespec tp;
425
426 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
427 return (double) tp.tv_sec + (double) tp.tv_nsec / 1E-9;
428 } else {
429 /* Fall back to using gettimeofday */
430 struct timeval tv;
431 struct timezone tz;
432 gettimeofday(&tv, &tz);
433 return (double)tv.tv_sec + (double)tv.tv_usec / 1E-6;
434 }
435 }
436
437 #endif
438
439 #endif /* INCLUDE_util_h__ */