Blame src/util.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_util_h__
Packit Service 20376f
#define INCLUDE_util_h__
Packit Service 20376f
Packit Service 20376f
#include "git2/buffer.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
/* Enable MSVC CRTDBG memory leak reporting.
Packit Service 20376f
 *
Packit Service 20376f
 * We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
Packit Service 20376f
 * documentation because all allocs/frees in libgit2 already go through
Packit Service 20376f
 * the "git__" routines defined in this file.  Simply using the normal
Packit Service 20376f
 * reporting mechanism causes all leaks to be attributed to a routine
Packit Service 20376f
 * here in util.h (ie, the actual call to calloc()) rather than the
Packit Service 20376f
 * caller of git__calloc().
Packit Service 20376f
 *
Packit Service 20376f
 * Therefore, we declare a set of "git__crtdbg__" routines to replace
Packit Service 20376f
 * the corresponding "git__" routines and re-define the "git__" symbols
Packit Service 20376f
 * as macros.  This allows us to get and report the file:line info of
Packit Service 20376f
 * the real caller.
Packit Service 20376f
 *
Packit Service 20376f
 * We DO NOT replace the "git__free" routine because it needs to remain
Packit Service 20376f
 * a function pointer because it is used as a function argument when
Packit Service 20376f
 * setting up various structure "destructors".
Packit Service 20376f
 *
Packit Service 20376f
 * We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
Packit Service 20376f
 * "free" to be remapped to "_free_dbg" and this causes problems for
Packit Service 20376f
 * structures which define a field named "free".
Packit Service 20376f
 *
Packit Service 20376f
 * Finally, CRTDBG must be explicitly enabled and configured at program
Packit Service 20376f
 * startup.  See tests/main.c for an example.
Packit Service 20376f
 */
Packit Service 20376f
#include <stdlib.h>
Packit Service 20376f
#include <crtdbg.h>
Packit Service 20376f
#include "win32/w32_crtdbg_stacktrace.h"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "strnlen.h"
Packit Service 20376f
Packit Service 20376f
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
Packit Service 20376f
#define bitsizeof(x) (CHAR_BIT * sizeof(x))
Packit Service 20376f
#define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits))))
Packit Service 20376f
#ifndef min
Packit Service 20376f
# define min(a,b) ((a) < (b) ? (a) : (b))
Packit Service 20376f
#endif
Packit Service 20376f
#ifndef max
Packit Service 20376f
# define max(a,b) ((a) > (b) ? (a) : (b))
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#define GIT_DATE_RFC2822_SZ  32
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Return the length of a constant string.
Packit Service 20376f
 * We are aware that `strlen` performs the same task and is usually
Packit Service 20376f
 * optimized away by the compiler, whilst being safer because it returns
Packit Service 20376f
 * valid values when passed a pointer instead of a constant string; however
Packit Service 20376f
 * this macro will transparently work with wide-char and single-char strings.
Packit Service 20376f
 */
Packit Service 20376f
#define CONST_STRLEN(x) ((sizeof(x)/sizeof(x[0])) - 1)
Packit Service 20376f
Packit Service 20376f
#if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__crtdbg__malloc(size_t len, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(char *) git__crtdbg__strdup(const char *str, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(char *) git__crtdbg__strndup(const char *str, size_t n, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	size_t length = 0, alloclength;
Packit Service 20376f
	char *ptr;
Packit Service 20376f
Packit Service 20376f
	length = p_strnlen(str, n);
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
Packit Service 20376f
		!(ptr = git__crtdbg__malloc(alloclength, file, line)))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	if (length)
Packit Service 20376f
		memcpy(ptr, str, length);
Packit Service 20376f
Packit Service 20376f
	ptr[length] = '\0';
Packit Service 20376f
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(char *) git__crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	char *ptr;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
Packit Service 20376f
		!(ptr = git__crtdbg__malloc(alloclen, file, line)))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	memcpy(ptr, start, n);
Packit Service 20376f
	ptr[n] = '\0';
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
Packit Service 20376f
	if (!new_ptr) giterr_set_oom();
Packit Service 20376f
	return new_ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	size_t newsize;
Packit Service 20376f
Packit Service 20376f
	return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
Packit Service 20376f
		NULL : _realloc_dbg(ptr, newsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
Packit Service 20376f
{
Packit Service 20376f
	return git__crtdbg__reallocarray(NULL, nelem, elsize, file, line);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define git__malloc(len)                      git__crtdbg__malloc(len, __FILE__, __LINE__)
Packit Service 20376f
#define git__calloc(nelem, elsize)            git__crtdbg__calloc(nelem, elsize, __FILE__, __LINE__)
Packit Service 20376f
#define git__strdup(str)                      git__crtdbg__strdup(str, __FILE__, __LINE__)
Packit Service 20376f
#define git__strndup(str, n)                  git__crtdbg__strndup(str, n, __FILE__, __LINE__)
Packit Service 20376f
#define git__substrdup(str, n)                git__crtdbg__substrdup(str, n, __FILE__, __LINE__)
Packit Service 20376f
#define git__realloc(ptr, size)               git__crtdbg__realloc(ptr, size, __FILE__, __LINE__)
Packit Service 20376f
#define git__reallocarray(ptr, nelem, elsize) git__crtdbg__reallocarray(ptr, nelem, elsize, __FILE__, __LINE__)
Packit Service 20376f
#define git__mallocarray(nelem, elsize)       git__crtdbg__mallocarray(nelem, elsize, __FILE__, __LINE__)
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Custom memory allocation wrappers
Packit Service 20376f
 * that set error code and error message
Packit Service 20376f
 * on allocation failure
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(void *) git__malloc(size_t len)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = malloc(len);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize)
Packit Service 20376f
{
Packit Service 20376f
	void *ptr = calloc(nelem, elsize);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(char *) git__strdup(const char *str)
Packit Service 20376f
{
Packit Service 20376f
	char *ptr = strdup(str);
Packit Service 20376f
	if (!ptr) giterr_set_oom();
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(char *) git__strndup(const char *str, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	size_t length = 0, alloclength;
Packit Service 20376f
	char *ptr;
Packit Service 20376f
Packit Service 20376f
	length = p_strnlen(str, n);
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
Packit Service 20376f
		!(ptr = git__malloc(alloclength)))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	if (length)
Packit Service 20376f
		memcpy(ptr, str, length);
Packit Service 20376f
Packit Service 20376f
	ptr[length] = '\0';
Packit Service 20376f
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* NOTE: This doesn't do null or '\0' checking.  Watch those boundaries! */
Packit Service 20376f
GIT_INLINE(char *) git__substrdup(const char *start, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	char *ptr;
Packit Service 20376f
	size_t alloclen;
Packit Service 20376f
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
Packit Service 20376f
		!(ptr = git__malloc(alloclen)))
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	memcpy(ptr, start, n);
Packit Service 20376f
	ptr[n] = '\0';
Packit Service 20376f
	return ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	void *new_ptr = realloc(ptr, size);
Packit Service 20376f
	if (!new_ptr) giterr_set_oom();
Packit Service 20376f
	return new_ptr;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Similar to `git__realloc`, except that it is suitable for reallocing an
Packit Service 20376f
 * array to a new number of elements of `nelem`, each of size `elsize`.
Packit Service 20376f
 * The total size calculation is checked for overflow.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(void *) git__reallocarray(void *ptr, size_t nelem, size_t elsize)
Packit Service 20376f
{
Packit Service 20376f
	size_t newsize;
Packit Service 20376f
	return GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize) ?
Packit Service 20376f
		NULL : realloc(ptr, newsize);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Similar to `git__calloc`, except that it does not zero memory.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(void *) git__mallocarray(size_t nelem, size_t elsize)
Packit Service 20376f
{
Packit Service 20376f
	return git__reallocarray(NULL, nelem, elsize);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif /* !MSVC_CTRDBG */
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) git__free(void *ptr)
Packit Service 20376f
{
Packit Service 20376f
	free(ptr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define STRCMP_CASESELECT(IGNORE_CASE, STR1, STR2) \
Packit Service 20376f
	((IGNORE_CASE) ? strcasecmp((STR1), (STR2)) : strcmp((STR1), (STR2)))
Packit Service 20376f
Packit Service 20376f
#define CASESELECT(IGNORE_CASE, ICASE, CASE) \
Packit Service 20376f
	((IGNORE_CASE) ? (ICASE) : (CASE))
Packit Service 20376f
Packit Service 20376f
extern int git__prefixcmp(const char *str, const char *prefix);
Packit Service 20376f
extern int git__prefixcmp_icase(const char *str, const char *prefix);
Packit Service 20376f
extern int git__prefixncmp(const char *str, size_t str_n, const char *prefix);
Packit Service 20376f
extern int git__prefixncmp_icase(const char *str, size_t str_n, const char *prefix);
Packit Service 20376f
extern int git__suffixcmp(const char *str, const char *suffix);
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git__signum(int val)
Packit Service 20376f
{
Packit Service 20376f
	return ((val > 0) - (val < 0));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
extern int git__strntol32(int32_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
Packit Service 20376f
extern int git__strntol64(int64_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
extern void git__hexdump(const char *buffer, size_t n);
Packit Service 20376f
extern uint32_t git__hash(const void *key, int len, uint32_t seed);
Packit Service 20376f
Packit Service 20376f
/* 32-bit cross-platform rotl */
Packit Service 20376f
#ifdef _MSC_VER /* use built-in method in MSVC */
Packit Service 20376f
#	define git__rotl(v, s) (uint32_t)_rotl(v, s)
Packit Service 20376f
#else /* use bitops in GCC; with o2 this gets optimized to a rotl instruction */
Packit Service 20376f
#	define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
extern char *git__strtok(char **end, const char *sep);
Packit Service 20376f
extern char *git__strsep(char **end, const char *sep);
Packit Service 20376f
Packit Service 20376f
extern void git__strntolower(char *str, size_t len);
Packit Service 20376f
extern void git__strtolower(char *str);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
GIT_INLINE(int) git__tolower(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
Packit Service 20376f
}
Packit Service 20376f
#else
Packit Service 20376f
# define git__tolower(a) tolower(a)
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
extern size_t git__linenlen(const char *buffer, size_t buffer_len);
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(const char *) git__next_line(const char *s)
Packit Service 20376f
{
Packit Service 20376f
	while (*s && *s != '\n') s++;
Packit Service 20376f
	while (*s == '\n' || *s == '\r') s++;
Packit Service 20376f
	return s;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(const void *) git__memrchr(const void *s, int c, size_t n)
Packit Service 20376f
{
Packit Service 20376f
	const unsigned char *cp;
Packit Service 20376f
Packit Service 20376f
	if (n != 0) {
Packit Service 20376f
		cp = (unsigned char *)s + n;
Packit Service 20376f
		do {
Packit Service 20376f
			if (*(--cp) == (unsigned char)c)
Packit Service 20376f
				return cp;
Packit Service 20376f
		} while (--n != 0);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
extern const void * git__memmem(const void *haystack, size_t haystacklen,
Packit Service 20376f
				const void *needle, size_t needlelen);
Packit Service 20376f
Packit Service 20376f
typedef int (*git__tsort_cmp)(const void *a, const void *b);
Packit Service 20376f
Packit Service 20376f
extern void git__tsort(void **dst, size_t size, git__tsort_cmp cmp);
Packit Service 20376f
Packit Service 20376f
typedef int (*git__sort_r_cmp)(const void *a, const void *b, void *payload);
Packit Service 20376f
Packit Service 20376f
extern void git__tsort_r(
Packit Service 20376f
	void **dst, size_t size, git__sort_r_cmp cmp, void *payload);
Packit Service 20376f
Packit Service 20376f
extern void git__qsort_r(
Packit Service 20376f
	void *els, size_t nel, size_t elsize, git__sort_r_cmp cmp, void *payload);
Packit Service 20376f
Packit Service 20376f
extern void git__insertsort_r(
Packit Service 20376f
	void *els, size_t nel, size_t elsize, void *swapel,
Packit Service 20376f
	git__sort_r_cmp cmp, void *payload);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * @param position If non-NULL, this will be set to the position where the
Packit Service 20376f
 * 		element is or would be inserted if not found.
Packit Service 20376f
 * @return 0 if found; GIT_ENOTFOUND if not found
Packit Service 20376f
 */
Packit Service 20376f
extern int git__bsearch(
Packit Service 20376f
	void **array,
Packit Service 20376f
	size_t array_len,
Packit Service 20376f
	const void *key,
Packit Service 20376f
	int (*compare)(const void *key, const void *element),
Packit Service 20376f
	size_t *position);
Packit Service 20376f
Packit Service 20376f
extern int git__bsearch_r(
Packit Service 20376f
	void **array,
Packit Service 20376f
	size_t array_len,
Packit Service 20376f
	const void *key,
Packit Service 20376f
	int (*compare_r)(const void *key, const void *element, void *payload),
Packit Service 20376f
	void *payload,
Packit Service 20376f
	size_t *position);
Packit Service 20376f
Packit Service 20376f
extern int git__strcmp_cb(const void *a, const void *b);
Packit Service 20376f
extern int git__strcasecmp_cb(const void *a, const void *b);
Packit Service 20376f
Packit Service 20376f
extern int git__strcmp(const char *a, const char *b);
Packit Service 20376f
extern int git__strcasecmp(const char *a, const char *b);
Packit Service 20376f
extern int git__strncmp(const char *a, const char *b, size_t sz);
Packit Service 20376f
extern int git__strncasecmp(const char *a, const char *b, size_t sz);
Packit Service 20376f
Packit Service 20376f
extern int git__strcasesort_cmp(const char *a, const char *b);
Packit Service 20376f
Packit Service 20376f
#include "thread-utils.h"
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_atomic refcount;
Packit Service 20376f
	void *owner;
Packit Service 20376f
} git_refcount;
Packit Service 20376f
Packit Service 20376f
typedef void (*git_refcount_freeptr)(void *r);
Packit Service 20376f
Packit Service 20376f
#define GIT_REFCOUNT_INC(r) { \
Packit Service 20376f
	git_atomic_inc(&((git_refcount *)(r))->refcount);	\
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define GIT_REFCOUNT_DEC(_r, do_free) { \
Packit Service 20376f
	git_refcount *r = (git_refcount *)(_r); \
Packit Service 20376f
	int val = git_atomic_dec(&r->refcount); \
Packit Service 20376f
	if (val <= 0 && r->owner == NULL) { do_free(_r); } \
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define GIT_REFCOUNT_OWN(r, o) { \
Packit Service 20376f
	((git_refcount *)(r))->owner = o; \
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define GIT_REFCOUNT_OWNER(r) (((git_refcount *)(r))->owner)
Packit Service 20376f
Packit Service 20376f
#define GIT_REFCOUNT_VAL(r) git_atomic_get(&((git_refcount *)(r))->refcount)
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static signed char from_hex[] = {
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
Packit Service 20376f
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
Packit Service 20376f
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
Packit Service 20376f
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
Packit Service 20376f
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git__fromhex(char h)
Packit Service 20376f
{
Packit Service 20376f
	return from_hex[(unsigned char) h];
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) git__ishex(const char *str)
Packit Service 20376f
{
Packit Service 20376f
	unsigned i;
Packit Service 20376f
	for (i=0; str[i] != '\0'; i++)
Packit Service 20376f
		if (git__fromhex(str[i]) < 0)
Packit Service 20376f
			return 0;
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(size_t) git__size_t_bitmask(size_t v)
Packit Service 20376f
{
Packit Service 20376f
	v--;
Packit Service 20376f
	v |= v >> 1;
Packit Service 20376f
	v |= v >> 2;
Packit Service 20376f
	v |= v >> 4;
Packit Service 20376f
	v |= v >> 8;
Packit Service 20376f
	v |= v >> 16;
Packit Service 20376f
Packit Service 20376f
	return v;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(size_t) git__size_t_powerof2(size_t v)
Packit Service 20376f
{
Packit Service 20376f
	return git__size_t_bitmask(v) + 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isupper(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c >= 'A' && c <= 'Z');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isalpha(int c)
Packit Service 20376f
{
Packit Service 20376f
	return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isdigit(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c >= '0' && c <= '9');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isspace(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isspace_nonlf(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__iswildcard(int c)
Packit Service 20376f
{
Packit Service 20376f
	return (c == '*' || c == '?' || c == '[');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(bool) git__isxdigit(int c)
Packit Service 20376f
{
Packit Service 20376f
	return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Parse a string value as a boolean, just like Core Git does.
Packit Service 20376f
 *
Packit Service 20376f
 * Valid values for true are: 'true', 'yes', 'on'
Packit Service 20376f
 * Valid values for false are: 'false', 'no', 'off'
Packit Service 20376f
 */
Packit Service 20376f
extern int git__parse_bool(int *out, const char *value);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Parse a string into a value as a git_time_t.
Packit Service 20376f
 *
Packit Service 20376f
 * Sample valid input:
Packit Service 20376f
 * - "yesterday"
Packit Service 20376f
 * - "July 17, 2003"
Packit Service 20376f
 * - "2003-7-17 08:23"
Packit Service 20376f
 */
Packit Service 20376f
extern int git__date_parse(git_time_t *out, const char *date);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Format a git_time as a RFC2822 string
Packit Service 20376f
 *
Packit Service 20376f
 * @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
Packit Service 20376f
 * @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
Packit Service 20376f
 * @param date the date to be formatted
Packit Service 20376f
 * @return 0 if successful; -1 on error
Packit Service 20376f
 */
Packit Service 20376f
extern int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Unescapes a string in-place.
Packit Service 20376f
 *
Packit Service 20376f
 * Edge cases behavior:
Packit Service 20376f
 * - "jackie\" -> "jacky\"
Packit Service 20376f
 * - "chan\\" -> "chan\"
Packit Service 20376f
 */
Packit Service 20376f
extern size_t git__unescape(char *str);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Iterate through an UTF-8 string, yielding one
Packit Service 20376f
 * codepoint at a time.
Packit Service 20376f
 *
Packit Service 20376f
 * @param str current position in the string
Packit Service 20376f
 * @param str_len size left in the string; -1 if the string is NULL-terminated
Packit Service 20376f
 * @param dst pointer where to store the current codepoint
Packit Service 20376f
 * @return length in bytes of the read codepoint; -1 if the codepoint was invalid
Packit Service 20376f
 */
Packit Service 20376f
extern int git__utf8_iterate(const uint8_t *str, int str_len, int32_t *dst);
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Safely zero-out memory, making sure that the compiler
Packit Service 20376f
 * doesn't optimize away the operation.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(void) git__memzero(void *data, size_t size)
Packit Service 20376f
{
Packit Service 20376f
#ifdef _MSC_VER
Packit Service 20376f
	SecureZeroMemory((PVOID)data, size);
Packit Service 20376f
#else
Packit Service 20376f
	volatile uint8_t *scan = (volatile uint8_t *)data;
Packit Service 20376f
Packit Service 20376f
	while (size--)
Packit Service 20376f
		*scan++ = 0x0;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(double) git__timer(void)
Packit Service 20376f
{
Packit Service 20376f
	/* We need the initial tick count to detect if the tick
Packit Service 20376f
	 * count has rolled over. */
Packit Service 20376f
	static DWORD initial_tick_count = 0;
Packit Service 20376f
Packit Service 20376f
	/* GetTickCount returns the number of milliseconds that have
Packit Service 20376f
	 * elapsed since the system was started. */
Packit Service 20376f
	DWORD count = GetTickCount();
Packit Service 20376f
Packit Service 20376f
	if(initial_tick_count == 0) {
Packit Service 20376f
		initial_tick_count = count;
Packit Service 20376f
	} else if (count < initial_tick_count) {
Packit Service 20376f
		/* The tick count has rolled over - adjust for it. */
Packit Service 20376f
		count = (0xFFFFFFFF - initial_tick_count) + count;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return (double) count / (double) 1000;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#elif __APPLE__
Packit Service 20376f
Packit Service 20376f
#include <mach/mach_time.h>
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(double) git__timer(void)
Packit Service 20376f
{
Packit Service 20376f
   uint64_t time = mach_absolute_time();
Packit Service 20376f
   static double scaling_factor = 0;
Packit Service 20376f
Packit Service 20376f
   if (scaling_factor == 0) {
Packit Service 20376f
       mach_timebase_info_data_t info;
Packit Service 20376f
       (void)mach_timebase_info(&info;;
Packit Service 20376f
       scaling_factor = (double)info.numer / (double)info.denom;
Packit Service 20376f
   }
Packit Service 20376f
Packit Service 20376f
   return (double)time * scaling_factor / 1.0E9;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#elif defined(AMIGA)
Packit Service 20376f
Packit Service 20376f
#include <proto/timer.h>
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(double) git__timer(void)
Packit Service 20376f
{
Packit Service 20376f
	struct TimeVal tv;
Packit Service 20376f
	ITimer->GetUpTime(&tv;;
Packit Service 20376f
	return (double)tv.Seconds + (double)tv.Microseconds / 1.0E6;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
#include <sys/time.h>
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(double) git__timer(void)
Packit Service 20376f
{
Packit Service 20376f
	struct timespec tp;
Packit Service 20376f
Packit Service 20376f
	if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
Packit Service 20376f
		return (double) tp.tv_sec + (double) tp.tv_nsec / 1.0E9;
Packit Service 20376f
	} else {
Packit Service 20376f
		/* Fall back to using gettimeofday */
Packit Service 20376f
		struct timeval tv;
Packit Service 20376f
		struct timezone tz;
Packit Service 20376f
		gettimeofday(&tv, &tz;;
Packit Service 20376f
		return (double)tv.tv_sec + (double)tv.tv_usec / 1.0E6;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
extern int git__getenv(git_buf *out, const char *name);
Packit Service 20376f
Packit Service 20376f
#endif /* INCLUDE_util_h__ */