util.c 7.36 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7
#include <git2.h>
8
#include "common.h"
9 10
#include <stdarg.h>
#include <stdio.h>
11
#include <ctype.h>
12
#include "posix.h"
13

14
#ifdef _MSC_VER
15 16 17
# include <Shlwapi.h>
#endif

18 19 20 21 22 23 24
void git_libgit2_version(int *major, int *minor, int *rev)
{
	*major = LIBGIT2_VER_MAJOR;
	*minor = LIBGIT2_VER_MINOR;
	*rev = LIBGIT2_VER_REVISION;
}

25 26 27 28
void git_strarray_free(git_strarray *array)
{
	size_t i;
	for (i = 0; i < array->count; ++i)
29
		git__free(array->strings[i]);
30

31
	git__free(array->strings);
32 33
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
{
	size_t i;

	assert(tgt && src);

	memset(tgt, 0, sizeof(*tgt));

	if (!src->count)
		return 0;

	tgt->strings = git__calloc(src->count, sizeof(char *));
	GITERR_CHECK_ALLOC(tgt->strings);

	for (i = 0; i < src->count; ++i) {
		tgt->strings[tgt->count] = git__strdup(src->strings[i]);

		if (!tgt->strings[tgt->count]) {
			git_strarray_free(tgt);
			memset(tgt, 0, sizeof(*tgt));
			return -1;
		}

		tgt->count++;
	}

	return 0;
}

63 64 65 66
int git__fnmatch(const char *pattern, const char *name, int flags)
{
	int ret;

67
	ret = p_fnmatch(pattern, name, flags);
68 69
	switch (ret) {
	case 0:
70
		return 0;
71 72 73
	case FNM_NOMATCH:
		return GIT_ENOMATCH;
	default:
74 75
		giterr_set(GITERR_OS, "Error trying to match path");
		return -1;
76 77 78
	}
}

79
int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base)
80 81
{
	const char *p;
82
	int64_t n, nn;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	int c, ovfl, v, neg, ndig;

	p = nptr;
	neg = 0;
	n = 0;
	ndig = 0;
	ovfl = 0;

	/*
	 * White space
	 */
	while (isspace(*p))
		p++;

	/*
	 * Sign
	 */
	if (*p == '-' || *p == '+')
		if (*p++ == '-')
			neg = 1;

	/*
	 * Base
	 */
	if (base == 0) {
		if (*p != '0')
			base = 10;
		else {
			base = 8;
			if (p[1] == 'x' || p[1] == 'X') {
				p += 2;
				base = 16;
			}
		}
	} else if (base == 16 && *p == '0') {
		if (p[1] == 'x' || p[1] == 'X')
			p += 2;
	} else if (base < 0 || 36 < base)
		goto Return;

	/*
	 * Non-empty sequence of digits
	 */
	for (;; p++,ndig++) {
		c = *p;
		v = base;
		if ('0'<=c && c<='9')
			v = c - '0';
		else if ('a'<=c && c<='z')
			v = c - 'a' + 10;
		else if ('A'<=c && c<='Z')
			v = c - 'A' + 10;
		if (v >= base)
			break;
		nn = n*base + v;
		if (nn < n)
			ovfl = 1;
		n = nn;
	}

Return:
144 145 146 147
	if (ndig == 0) {
		giterr_set(GITERR_INVALID, "Failed to convert string to long. Not a number");
		return -1;
	}
148 149 150 151

	if (endptr)
		*endptr = p;

152 153 154 155
	if (ovfl) {
		giterr_set(GITERR_INVALID, "Failed to convert string to long. Overflow error");
		return -1;
	}
156 157

	*result = neg ? -n : n;
158
	return 0;
159 160
}

161
int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
162
{
163
	int error;
164 165
	int32_t tmp_int;
	int64_t tmp_long;
166

167
	if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0)
168 169 170
		return error;

	tmp_int = tmp_long & 0xFFFFFFFF;
171 172 173 174
	if (tmp_int != tmp_long) {
		giterr_set(GITERR_INVALID, "Failed to convert. '%s' is too large", nptr);
		return -1;
	}
175 176

	*result = tmp_int;
177

178 179 180
	return error;
}

181
void git__strntolower(char *str, size_t len)
182
{
183
	size_t i;
184 185

	for (i = 0; i < len; ++i) {
186
		str[i] = (char) tolower(str[i]);
187 188 189 190 191 192 193 194
	}
}

void git__strtolower(char *str)
{
	git__strntolower(str, strlen(str));
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
int git__prefixcmp(const char *str, const char *prefix)
{
	for (;;) {
		char p = *(prefix++), s;
		if (!p)
			return 0;
		if ((s = *(str++)) != p)
			return s - p;
	}
}

int git__suffixcmp(const char *str, const char *suffix)
{
	size_t a = strlen(str);
	size_t b = strlen(suffix);
	if (a < b)
		return -1;
	return strcmp(str + (a - b), suffix);
}
214

215
char *git__strtok(char **end, const char *sep)
216
{
217
	char *ptr = *end;
218

219 220
	while (*ptr && strchr(sep, *ptr))
		++ptr;
221

222 223 224
	if (*ptr) {
		char *start = ptr;
		*end = start + 1;
225

226 227
		while (**end && !strchr(sep, **end))
			++*end;
228

229 230 231 232 233 234 235 236 237
		if (**end) {
			**end = '\0';
			++*end;
		}

		return start;
	}

	return NULL;
238 239
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
void git__hexdump(const char *buffer, size_t len)
{
	static const size_t LINE_WIDTH = 16;

	size_t line_count, last_line, i, j;
	const char *line;

	line_count = (len / LINE_WIDTH);
	last_line = (len % LINE_WIDTH);

	for (i = 0; i < line_count; ++i) {
		line = buffer + (i * LINE_WIDTH);
		for (j = 0; j < LINE_WIDTH; ++j, ++line)
			printf("%02X ", (unsigned char)*line & 0xFF);

		printf("| ");

		line = buffer + (i * LINE_WIDTH);
		for (j = 0; j < LINE_WIDTH; ++j, ++line)
			printf("%c", (*line >= 32 && *line <= 126) ? *line : '.');

		printf("\n");
	}

	if (last_line > 0) {

		line = buffer + (line_count * LINE_WIDTH);
		for (j = 0; j < last_line; ++j, ++line)
			printf("%02X ", (unsigned char)*line & 0xFF);

		for (j = 0; j < (LINE_WIDTH - last_line); ++j)
Vicent Marti committed
271
			printf("	");
272 273 274 275 276 277 278 279 280 281 282 283

		printf("| ");

		line = buffer + (line_count * LINE_WIDTH);
		for (j = 0; j < last_line; ++j, ++line)
			printf("%c", (*line >= 32 && *line <= 126) ? *line : '.');

		printf("\n");
	}

	printf("\n");
}
284 285 286 287 288 289 290 291 292 293 294 295 296

#ifdef GIT_LEGACY_HASH
uint32_t git__hash(const void *key, int len, unsigned int seed)
{
	const uint32_t m = 0x5bd1e995;
	const int r = 24;
	uint32_t h = seed ^ len;

	const unsigned char *data = (const unsigned char *)key;

	while(len >= 4) {
		uint32_t k = *(uint32_t *)data;

297 298 299 300 301
		k *= m;
		k ^= k >> r;
		k *= m;

		h *= m;
302 303 304 305 306
		h ^= k;

		data += 4;
		len -= 4;
	}
307

308 309 310 311
	switch(len) {
	case 3: h ^= data[2] << 16;
	case 2: h ^= data[1] << 8;
	case 1: h ^= data[0];
Vicent Marti committed
312
			h *= m;
313 314 315 316 317 318 319
	};

	h ^= h >> 13;
	h *= m;
	h ^= h >> 15;

	return h;
320
}
321 322 323 324 325 326 327 328 329 330 331 332
#else
/*
	Cross-platform version of Murmurhash3
	http://code.google.com/p/smhasher/wiki/MurmurHash3
	by Austin Appleby (aappleby@gmail.com)

	This code is on the public domain.
*/
uint32_t git__hash(const void *key, int len, uint32_t seed)
{

#define MURMUR_BLOCK() {\
Vicent Marti committed
333 334 335 336 337 338 339
	k1 *= c1; \
	k1 = git__rotl(k1,11);\
	k1 *= c2;\
	h1 ^= k1;\
	h1 = h1*3 + 0x52dce729;\
	c1 = c1*5 + 0x7b7d159c;\
	c2 = c2*5 + 0x6bce6396;\
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
}

	const uint8_t *data = (const uint8_t*)key;
	const int nblocks = len / 4;

	const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
	const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);

	uint32_t h1 = 0x971e137b ^ seed;
	uint32_t k1;

	uint32_t c1 = 0x95543787;
	uint32_t c2 = 0x2ad7eb25;

	int i;

	for (i = -nblocks; i; i++) {
		k1 = blocks[i];
		MURMUR_BLOCK();
	}

	k1 = 0;

	switch(len & 3) {
	case 3: k1 ^= tail[2] << 16;
	case 2: k1 ^= tail[1] << 8;
	case 1: k1 ^= tail[0];
			MURMUR_BLOCK();
	}

	h1 ^= len;
	h1 ^= h1 >> 16;
	h1 *= 0x85ebca6b;
	h1 ^= h1 >> 13;
	h1 *= 0xc2b2ae35;
	h1 ^= h1 >> 16;

	return h1;
378
}
379
#endif
380

381 382 383 384 385
/**
 * A modified `bsearch` from the BSD glibc.
 *
 * Copyright (c) 1990 Regents of the University of California.
 * All rights reserved.
386
 */
387 388 389 390 391 392
int git__bsearch(
	void **array,
	size_t array_len,
	const void *key,
	int (*compare)(const void *, const void *),
	size_t *position)
393
{
394 395
	unsigned int lim;
	int cmp = -1;
396 397
	void **part, **base = array;

398
	for (lim = (unsigned int)array_len; lim != 0; lim >>= 1) {
399 400 401
		part = base + (lim >> 1);
		cmp = (*compare)(key, *part);
		if (cmp == 0) {
402 403 404 405
			base = part;
			break;
		}
		if (cmp > 0) { /* key > p; take right partition */
406 407 408 409 410
			base = part + 1;
			lim--;
		} /* else take left partition */
	}

411 412 413 414
	if (position)
		*position = (base - array);

	return (cmp == 0) ? 0 : -1;
415 416
}

417 418 419 420 421 422 423 424 425 426 427 428
/**
 * A strcmp wrapper
 * 
 * We don't want direct pointers to the CRT on Windows, we may
 * get stdcall conflicts.
 */
int git__strcmp_cb(const void *a, const void *b)
{
	const char *stra = (const char *)a;
	const char *strb = (const char *)b;

	return strcmp(stra, strb);
429
}