oid.c 9.5 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9
#include "oid.h"

10
#include "git2/oid.h"
11
#include "repository.h"
12
#include "threadstate.h"
13
#include <string.h>
14
#include <limits.h>
15

16
static char to_hex[] = "0123456789abcdef";
17

18 19
static int oid_error_invalid(const char *msg)
{
20
	git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg);
21 22 23
	return -1;
}

24
int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
25
{
26
	size_t p;
27
	int v;
28

Edward Thomson committed
29 30
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(str);
31

32 33
	if (!length)
		return oid_error_invalid("too short");
34

35 36
	if (length > GIT_OID_HEXSZ)
		return oid_error_invalid("too long");
37

38
	memset(out->id, 0, GIT_OID_RAWSZ);
39

40 41
	for (p = 0; p < length; p++) {
		v = git__fromhex(str[p]);
42
		if (v < 0)
43
			return oid_error_invalid("contains invalid characters");
44

45
		out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4));
46 47
	}

48
	return 0;
49
}
50

51 52
int git_oid_fromstrp(git_oid *out, const char *str)
{
53
	return git_oid_fromstrn(out, str, strlen(str));
54 55
}

56 57 58 59 60
int git_oid_fromstr(git_oid *out, const char *str)
{
	return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
}

61
GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
62 63 64 65 66 67
{
	*str++ = to_hex[val >> 4];
	*str++ = to_hex[val & 0xf];
	return str;
}

68
int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
69
{
70 71 72 73
	size_t i, max_i;

	if (!oid) {
		memset(str, 0, n);
74
		return 0;
75 76 77 78 79 80 81
	}
	if (n > GIT_OID_HEXSZ) {
		memset(&str[GIT_OID_HEXSZ], 0, n - GIT_OID_HEXSZ);
		n = GIT_OID_HEXSZ;
	}

	max_i = n / 2;
82

83
	for (i = 0; i < max_i; i++)
84
		str = fmt_one(str, oid->id[i]);
85 86 87

	if (n & 1)
		*str++ = to_hex[oid->id[i] >> 4];
88 89

	return 0;
90 91
}

92
int git_oid_fmt(char *str, const git_oid *oid)
93
{
94
	return git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
95 96
}

97
int git_oid_pathfmt(char *str, const git_oid *oid)
98
{
99
	size_t i;
100 101 102 103 104

	str = fmt_one(str, oid->id[0]);
	*str++ = '/';
	for (i = 1; i < sizeof(oid->id); i++)
		str = fmt_one(str, oid->id[i]);
105 106

	return 0;
107 108
}

109 110
char *git_oid_tostr_s(const git_oid *oid)
{
111
	char *str = GIT_THREADSTATE->oid_fmt;
112 113 114 115
	git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
	return str;
}

116 117
char *git_oid_allocfmt(const git_oid *oid)
{
118
	char *str = git__malloc(GIT_OID_HEXSZ + 1);
119 120
	if (!str)
		return NULL;
121
	git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
122 123
	return str;
}
124

125
char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
126
{
127
	if (!out || n == 0)
128 129
		return "";

130 131
	if (n > GIT_OID_HEXSZ + 1)
		n = GIT_OID_HEXSZ + 1;
132

133 134
	git_oid_nfmt(out, n - 1, oid); /* allow room for terminating NUL */
	out[n - 1] = '\0';
135 136 137 138

	return out;
}

139 140 141
int git_oid__parse(
	git_oid *oid, const char **buffer_out,
	const char *buffer_end, const char *header)
142 143 144 145
{
	const size_t sha_len = GIT_OID_HEXSZ;
	const size_t header_len = strlen(header);

146
	const char *buffer = *buffer_out;
147 148

	if (buffer + (header_len + sha_len + 1) > buffer_end)
149
		return -1;
150 151

	if (memcmp(buffer, header, header_len) != 0)
152
		return -1;
153 154

	if (buffer[header_len + sha_len] != '\n')
155
		return -1;
156

157 158
	if (git_oid_fromstr(oid, buffer + header_len) < 0)
		return -1;
159 160 161

	*buffer_out = buffer + (header_len + sha_len + 1);

162
	return 0;
163 164
}

165 166 167 168 169 170 171 172 173 174
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
{
	char hex_oid[GIT_OID_HEXSZ];

	git_oid_fmt(hex_oid, oid);
	git_buf_puts(buf, header);
	git_buf_put(buf, hex_oid, GIT_OID_HEXSZ);
	git_buf_putc(buf, '\n');
}

175
int git_oid_fromraw(git_oid *out, const unsigned char *raw)
176 177
{
	memcpy(out->id, raw, sizeof(out->id));
178
	return 0;
179 180
}

181
int git_oid_cpy(git_oid *out, const git_oid *src)
182 183
{
	memcpy(out->id, src->id, sizeof(out->id));
184
	return 0;
185 186
}

187
int git_oid_cmp(const git_oid *a, const git_oid *b)
188
{
189
	return git_oid__cmp(a, b);
190 191
}

192 193 194 195 196
int git_oid_equal(const git_oid *a, const git_oid *b)
{
	return (git_oid__cmp(a, b) == 0);
}

197
int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
198
{
199 200 201
	const unsigned char *a = oid_a->id;
	const unsigned char *b = oid_b->id;

202 203 204 205
	if (len > GIT_OID_HEXSZ)
		len = GIT_OID_HEXSZ;

	while (len > 1) {
206 207 208 209 210
		if (*a != *b)
			return 1;
		a++;
		b++;
		len -= 2;
211
	};
212 213 214 215 216 217

	if (len)
		if ((*a ^ *b) & 0xf0)
			return 1;

	return 0;
218 219
}

220
int git_oid_strcmp(const git_oid *oid_a, const char *str)
221
{
222
	const unsigned char *a;
223 224 225 226 227 228
	unsigned char strval;
	int hexval;

	for (a = oid_a->id; *str && (a - oid_a->id) < GIT_OID_RAWSZ; ++a) {
		if ((hexval = git__fromhex(*str++)) < 0)
			return -1;
Linquize committed
229
		strval = (unsigned char)(hexval << 4);
230 231 232 233 234 235 236 237
		if (*str) {
			if ((hexval = git__fromhex(*str++)) < 0)
				return -1;
			strval |= hexval;
		}
		if (*a != strval)
			return (*a - strval);
	}
238

239 240
	return 0;
}
241

242 243 244
int git_oid_streq(const git_oid *oid_a, const char *str)
{
	return git_oid_strcmp(oid_a, str) == 0 ? 0 : -1;
245 246
}

247
int git_oid_is_zero(const git_oid *oid_a)
248 249 250 251 252 253 254 255 256
{
	const unsigned char *a = oid_a->id;
	unsigned int i;
	for (i = 0; i < GIT_OID_RAWSZ; ++i, ++a)
		if (*a != 0)
			return 0;
	return 1;
}

257
#ifndef GIT_DEPRECATE_HARD
258 259 260 261
int git_oid_iszero(const git_oid *oid_a)
{
	return git_oid_is_zero(oid_a);
}
262
#endif
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
typedef short node_index;

typedef union {
	const char *tail;
	node_index children[16];
} trie_node;

struct git_oid_shorten {
	trie_node *nodes;
	size_t node_count, size;
	int min_length, full;
};

static int resize_trie(git_oid_shorten *self, size_t new_size)
{
279
	self->nodes = git__reallocarray(self->nodes, new_size, sizeof(trie_node));
280
	GIT_ERROR_CHECK_ALLOC(self->nodes);
281 282 283 284 285 286

	if (new_size > self->size) {
		memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
	}

	self->size = new_size;
287
	return 0;
288 289 290 291 292 293 294 295
}

static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
{
	trie_node *node, *leaf;
	node_index idx_leaf;

	if (os->node_count >= os->size) {
296
		if (resize_trie(os, os->size * 2) < 0)
297 298 299 300 301
			return NULL;
	}

	idx_leaf = (node_index)os->node_count++;

302
	if (os->node_count == SHRT_MAX) {
303
		os->full = 1;
304 305
        return NULL;
    }
306 307 308 309 310 311 312 313 314 315 316 317 318 319

	node = &os->nodes[idx];
	node->children[push_at] = -idx_leaf;

	leaf = &os->nodes[idx_leaf];
	leaf->tail = oid;

	return node;
}

git_oid_shorten *git_oid_shorten_new(size_t min_length)
{
	git_oid_shorten *os;

Edward Thomson committed
320
	GIT_ASSERT_ARG_WITH_RETVAL((size_t)((int)min_length) == min_length, NULL);
321

322
	os = git__calloc(1, sizeof(git_oid_shorten));
323 324 325
	if (os == NULL)
		return NULL;

326
	if (resize_trie(os, 16) < 0) {
327
		git__free(os);
328 329 330 331
		return NULL;
	}

	os->node_count = 1;
332
	os->min_length = (int)min_length;
333 334 335 336 337 338

	return os;
}

void git_oid_shorten_free(git_oid_shorten *os)
{
339 340 341
	if (os == NULL)
		return;

342 343
	git__free(os->nodes);
	git__free(os);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
}


/*
 * What wizardry is this?
 *
 * This is just a memory-optimized trie: basically a very fancy
 * 16-ary tree, which is used to store the prefixes of the OID
 * strings.
 *
 * Read more: http://en.wikipedia.org/wiki/Trie
 *
 * Magic that happens in this method:
 *
 *	- Each node in the trie is an union, so it can work both as
 *	a normal node, or as a leaf.
 *
 *	- Each normal node points to 16 children (one for each possible
 *	character in the oid). This is *not* stored in an array of
363
 *	pointers, because in a 64-bit arch this would be sucking
364
 *	16*sizeof(void*) = 128 bytes of memory per node, which is
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
 *	insane. What we do is store Node Indexes, and use these indexes
 *	to look up each node in the om->index array. These indexes are
 *	signed shorts, so this limits the amount of unique OIDs that
 *	fit in the structure to about 20000 (assuming a more or less uniform
 *	distribution).
 *
 *	- All the nodes in om->index array are stored contiguously in
 *	memory, and each of them is 32 bytes, so we fit 2x nodes per
 *	cache line. Convenient for speed.
 *
 *	- To differentiate the leafs from the normal nodes, we store all
 *	the indexes towards a leaf as a negative index (indexes to normal
 *	nodes are positives). When we find that one of the children for
 *	a node has a negative value, that means it's going to be a leaf.
 *	This reduces the amount of indexes we have by two, but also reduces
 *	the size of each node by 1-4 bytes (the amount we would need to
 *	add a `is_leaf` field): this is good because it allows the nodes
 *	to fit cleanly in cache lines.
 *
 *	- Once we reach an empty children, instead of continuing to insert
 *	new nodes for each remaining character of the OID, we store a pointer
 *	to the tail in the leaf; if the leaf is reached again, we turn it
 *	into a normal node and use the tail to create a new leaf.
 *
 *	This is a pretty good balance between performance and memory usage.
 */
int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
{
393 394
	int i;
	bool is_leaf;
395 396
	node_index idx;

397
	if (os->full) {
398
		git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
399
		return -1;
400
	}
401

402 403 404
	if (text_oid == NULL)
		return os->min_length;

405
	idx = 0;
406
	is_leaf = false;
407 408

	for (i = 0; i < GIT_OID_HEXSZ; ++i) {
nulltoken committed
409
		int c = git__fromhex(text_oid[i]);
410 411
		trie_node *node;

412
		if (c == -1) {
413
			git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - invalid hex value");
414 415
			return -1;
		}
416 417 418 419 420 421 422 423 424

		node = &os->nodes[idx];

		if (is_leaf) {
			const char *tail;

			tail = node->tail;
			node->tail = NULL;

nulltoken committed
425
			node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
426 427
			if (node == NULL) {
				if (os->full)
428
					git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
429 430
				return -1;
			}
431 432 433
		}

		if (node->children[c] == 0) {
434 435
			if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) {
				if (os->full)
436
					git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
437
				return -1;
438
			}
439 440 441 442
			break;
		}

		idx = node->children[c];
443
		is_leaf = false;
444 445 446

		if (idx < 0) {
			node->children[c] = idx = -idx;
447
			is_leaf = true;
448 449 450 451 452 453 454 455 456
		}
	}

	if (++i > os->min_length)
		os->min_length = i;

	return os->min_length;
}