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