2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_util_h__
8 #define INCLUDE_util_h__
16 #include "git2/buffer.h"
21 #include "thread-utils.h"
23 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
24 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
25 #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
27 # define min(a,b) ((a) < (b) ? (a) : (b))
30 # define max(a,b) ((a) > (b) ? (a) : (b))
34 # define GIT_CONTAINER_OF(ptr, type, member) \
35 __builtin_choose_expr( \
36 __builtin_offsetof(type, member) == 0 && \
37 __builtin_types_compatible_p(typeof(&((type *) 0)->member), typeof(ptr)), \
41 # define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr)
44 #define GIT_DATE_RFC2822_SZ 32
47 * Return the length of a constant string.
48 * We are aware that `strlen` performs the same task and is usually
49 * optimized away by the compiler, whilst being safer because it returns
50 * valid values when passed a pointer instead of a constant string; however
51 * this macro will transparently work with wide-char and single-char strings.
53 #define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
55 #define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
56 ((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
58 #define CASESELECT(IGNORE_CASE, ICASE, CASE) \
59 ((IGNORE_CASE) ? (ICASE) : (CASE))
61 extern int git__prefixcmp(const char *str
, const char *prefix
);
62 extern int git__prefixcmp_icase(const char *str
, const char *prefix
);
63 extern int git__prefixncmp(const char *str
, size_t str_n
, const char *prefix
);
64 extern int git__prefixncmp_icase(const char *str
, size_t str_n
, const char *prefix
);
65 extern int git__suffixcmp(const char *str
, const char *suffix
);
67 GIT_INLINE(int) git__signum(int val
)
69 return ((val
> 0) - (val
< 0));
72 extern int git__strntol32(int32_t *n
, const char *buff
, size_t buff_len
, const char **end_buf
, int base
);
73 extern int git__strntol64(int64_t *n
, const char *buff
, size_t buff_len
, const char **end_buf
, int base
);
76 extern void git__hexdump(const char *buffer
, size_t n
);
77 extern uint32_t git__hash(const void *key
, int len
, uint32_t seed
);
79 /* 32-bit cross-platform rotl */
80 #ifdef _MSC_VER /* use built-in method in MSVC */
81 # define git__rotl(v, s) (uint32_t)_rotl(v, s)
82 #else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
83 # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
86 extern char *git__strtok(char **end
, const char *sep
);
87 extern char *git__strsep(char **end
, const char *sep
);
89 extern void git__strntolower(char *str
, size_t len
);
90 extern void git__strtolower(char *str
);
93 GIT_INLINE(int) git__tolower(int c
)
95 return (c
>= 'A' && c
<= 'Z') ? (c
+ 32) : c
;
98 # define git__tolower(a) tolower(a)
101 extern size_t git__linenlen(const char *buffer
, size_t buffer_len
);
103 GIT_INLINE(const char *) git__next_line(const char *s
)
105 while (*s
&& *s
!= '\n') s
++;
106 while (*s
== '\n' || *s
== '\r') s
++;
110 GIT_INLINE(const void *) git__memrchr(const void *s
, int c
, size_t n
)
112 const unsigned char *cp
;
115 cp
= (unsigned char *)s
+ n
;
117 if (*(--cp
) == (unsigned char)c
)
125 extern const void * git__memmem(const void *haystack
, size_t haystacklen
,
126 const void *needle
, size_t needlelen
);
128 typedef int (*git__tsort_cmp
)(const void *a
, const void *b
);
130 extern void git__tsort(void **dst
, size_t size
, git__tsort_cmp cmp
);
132 typedef int (*git__sort_r_cmp
)(const void *a
, const void *b
, void *payload
);
134 extern void git__tsort_r(
135 void **dst
, size_t size
, git__sort_r_cmp cmp
, void *payload
);
137 extern void git__qsort_r(
138 void *els
, size_t nel
, size_t elsize
, git__sort_r_cmp cmp
, void *payload
);
141 * @param position If non-NULL, this will be set to the position where the
142 * element is or would be inserted if not found.
143 * @return 0 if found; GIT_ENOTFOUND if not found
145 extern int git__bsearch(
149 int (*compare
)(const void *key
, const void *element
),
152 extern int git__bsearch_r(
156 int (*compare_r
)(const void *key
, const void *element
, void *payload
),
160 #define git__strcmp strcmp
161 #define git__strncmp strncmp
163 extern int git__strcmp_cb(const void *a
, const void *b
);
164 extern int git__strcasecmp_cb(const void *a
, const void *b
);
166 extern int git__strcasecmp(const char *a
, const char *b
);
167 extern int git__strncasecmp(const char *a
, const char *b
, size_t sz
);
169 extern int git__strcasesort_cmp(const char *a
, const char *b
);
176 typedef void (*git_refcount_freeptr
)(void *r
);
178 #define GIT_REFCOUNT_INC(r) { \
179 git_atomic_inc(&(r)->rc.refcount); \
182 #define GIT_REFCOUNT_DEC(_r, do_free) { \
183 git_refcount *r = &(_r)->rc; \
184 int val = git_atomic_dec(&r->refcount); \
185 if (val <= 0 && r->owner == NULL) { do_free(_r); } \
188 #define GIT_REFCOUNT_OWN(r, o) { \
189 (void)git__swap((r)->rc.owner, o); \
192 #define GIT_REFCOUNT_OWNER(r) git__load((r)->rc.owner)
194 #define GIT_REFCOUNT_VAL(r) git_atomic_get((r)->rc.refcount)
197 static signed char from_hex
[] = {
198 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
199 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
200 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
201 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
202 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
203 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
204 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
205 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
206 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
207 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
208 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
209 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
210 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
211 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
212 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
213 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
216 GIT_INLINE(int) git__fromhex(char h
)
218 return from_hex
[(unsigned char) h
];
221 GIT_INLINE(int) git__ishex(const char *str
)
224 for (i
=0; str
[i
] != '\0'; i
++)
225 if (git__fromhex(str
[i
]) < 0)
230 GIT_INLINE(size_t) git__size_t_bitmask(size_t v
)
242 GIT_INLINE(size_t) git__size_t_powerof2(size_t v
)
244 return git__size_t_bitmask(v
) + 1;
247 GIT_INLINE(bool) git__isupper(int c
)
249 return (c
>= 'A' && c
<= 'Z');
252 GIT_INLINE(bool) git__isalpha(int c
)
254 return ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z'));
257 GIT_INLINE(bool) git__isdigit(int c
)
259 return (c
>= '0' && c
<= '9');
262 GIT_INLINE(bool) git__isspace(int c
)
264 return (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\f' || c
== '\r' || c
== '\v');
267 GIT_INLINE(bool) git__isspace_nonlf(int c
)
269 return (c
== ' ' || c
== '\t' || c
== '\f' || c
== '\r' || c
== '\v');
272 GIT_INLINE(bool) git__iswildcard(int c
)
274 return (c
== '*' || c
== '?' || c
== '[');
277 GIT_INLINE(bool) git__isxdigit(int c
)
279 return ((c
>= '0' && c
<= '9') || (c
>= 'a' && c
<= 'f') || (c
>= 'A' && c
<= 'F'));
283 * Parse a string value as a boolean, just like Core Git does.
285 * Valid values for true are: 'true', 'yes', 'on'
286 * Valid values for false are: 'false', 'no', 'off'
288 extern int git__parse_bool(int *out
, const char *value
);
291 * Parse a string into a value as a git_time_t.
293 * Sample valid input:
296 * - "2003-7-17 08:23"
298 extern int git__date_parse(git_time_t
*out
, const char *date
);
301 * Format a git_time as a RFC2822 string
303 * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
304 * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
305 * @param date the date to be formatted
306 * @return 0 if successful; -1 on error
308 extern int git__date_rfc2822_fmt(char *out
, size_t len
, const git_time
*date
);
311 * Unescapes a string in-place.
313 * Edge cases behavior:
314 * - "jackie\" -> "jacky\"
315 * - "chan\\" -> "chan\"
317 extern size_t git__unescape(char *str
);
320 * Iterate through an UTF-8 string, yielding one
321 * codepoint at a time.
323 * @param str current position in the string
324 * @param str_len size left in the string; -1 if the string is NULL-terminated
325 * @param dst pointer where to store the current codepoint
326 * @return length in bytes of the read codepoint; -1 if the codepoint was invalid
328 extern int git__utf8_iterate(const uint8_t *str
, int str_len
, int32_t *dst
);
331 * Iterate through an UTF-8 string and stops after finding any invalid UTF-8
334 * @param str string to scan
335 * @param str_len size of the string
336 * @return length in bytes of the string that contains valid data
338 extern size_t git__utf8_valid_buf_length(const uint8_t *str
, size_t str_len
);
341 * Safely zero-out memory, making sure that the compiler
342 * doesn't optimize away the operation.
344 GIT_INLINE(void) git__memzero(void *data
, size_t size
)
347 SecureZeroMemory((PVOID
)data
, size
);
349 volatile uint8_t *scan
= (volatile uint8_t *)data
;
358 GIT_INLINE(double) git__timer(void)
360 /* GetTickCount64 returns the number of milliseconds that have
361 * elapsed since the system was started. */
362 return (double) GetTickCount64() / (double) 1000;
367 #include <mach/mach_time.h>
369 GIT_INLINE(double) git__timer(void)
371 uint64_t time
= mach_absolute_time();
372 static double scaling_factor
= 0;
374 if (scaling_factor
== 0) {
375 mach_timebase_info_data_t info
;
376 (void)mach_timebase_info(&info
);
377 scaling_factor
= (double)info
.numer
/ (double)info
.denom
;
380 return (double)time
* scaling_factor
/ 1.0E9
;
385 #include <proto/timer.h>
387 GIT_INLINE(double) git__timer(void)
390 ITimer
->GetUpTime(&tv
);
391 return (double)tv
.Seconds
+ (double)tv
.Microseconds
/ 1.0E6
;
396 #include <sys/time.h>
398 GIT_INLINE(double) git__timer(void)
402 if (clock_gettime(CLOCK_MONOTONIC
, &tp
) == 0) {
403 return (double) tp
.tv_sec
+ (double) tp
.tv_nsec
/ 1.0E9
;
405 /* Fall back to using gettimeofday */
408 gettimeofday(&tv
, &tz
);
409 return (double)tv
.tv_sec
+ (double)tv
.tv_usec
/ 1.0E6
;
415 extern int git__getenv(git_buf
*out
, const char *name
);