]> git.proxmox.com Git - libgit2.git/blame - src/oid.c
Refresh patches
[libgit2.git] / src / oid.c
CommitLineData
c15648cb 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
c15648cb 3 *
bb742ede
VM
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.
c15648cb
SP
6 */
7
eae0bfdc
PP
8#include "oid.h"
9
44908fe7 10#include "git2/oid.h"
58519018 11#include "repository.h"
c25aa7cd 12#include "threadstate.h"
c15648cb 13#include <string.h>
26022f07 14#include <limits.h>
c15648cb 15
af795e49 16static char to_hex[] = "0123456789abcdef";
c15648cb 17
7c7ff7d1
RB
18static int oid_error_invalid(const char *msg)
19{
ac3d33df 20 git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg);
7c7ff7d1
RB
21 return -1;
22}
23
d5afc039 24int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
c15648cb 25{
0ef9d2aa 26 size_t p;
e724b058 27 int v;
d5afc039 28
c25aa7cd
PP
29 GIT_ASSERT_ARG(out);
30 GIT_ASSERT_ARG(str);
d5afc039 31
00258cc0
JP
32 if (!length)
33 return oid_error_invalid("too short");
d5afc039 34
00258cc0
JP
35 if (length > GIT_OID_HEXSZ)
36 return oid_error_invalid("too long");
d5afc039 37
00258cc0 38 memset(out->id, 0, GIT_OID_RAWSZ);
d5afc039 39
00258cc0
JP
40 for (p = 0; p < length; p++) {
41 v = git__fromhex(str[p]);
0e058e78 42 if (v < 0)
7c7ff7d1 43 return oid_error_invalid("contains invalid characters");
0e058e78 44
00258cc0 45 out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4));
e724b058
DI
46 }
47
7c7ff7d1 48 return 0;
c15648cb 49}
af795e49 50
0c8efb38
X
51int git_oid_fromstrp(git_oid *out, const char *str)
52{
1e7b7523 53 return git_oid_fromstrn(out, str, strlen(str));
0c8efb38
X
54}
55
d5afc039
VM
56int git_oid_fromstr(git_oid *out, const char *str)
57{
58 return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
59}
60
213e720c 61GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
af795e49
SP
62{
63 *str++ = to_hex[val >> 4];
64 *str++ = to_hex[val & 0xf];
65 return str;
66}
67
22a2d3d5 68int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
af795e49 69{
660d59ca
RB
70 size_t i, max_i;
71
72 if (!oid) {
73 memset(str, 0, n);
22a2d3d5 74 return 0;
660d59ca
RB
75 }
76 if (n > GIT_OID_HEXSZ) {
77 memset(&str[GIT_OID_HEXSZ], 0, n - GIT_OID_HEXSZ);
78 n = GIT_OID_HEXSZ;
79 }
80
81 max_i = n / 2;
af795e49 82
660d59ca 83 for (i = 0; i < max_i; i++)
af795e49 84 str = fmt_one(str, oid->id[i]);
660d59ca
RB
85
86 if (n & 1)
87 *str++ = to_hex[oid->id[i] >> 4];
22a2d3d5
UG
88
89 return 0;
660d59ca
RB
90}
91
22a2d3d5 92int git_oid_fmt(char *str, const git_oid *oid)
660d59ca 93{
22a2d3d5 94 return git_oid_nfmt(str, GIT_OID_HEXSZ, oid);
af795e49
SP
95}
96
22a2d3d5 97int git_oid_pathfmt(char *str, const git_oid *oid)
af795e49 98{
0ef9d2aa 99 size_t i;
af795e49
SP
100
101 str = fmt_one(str, oid->id[0]);
102 *str++ = '/';
103 for (i = 1; i < sizeof(oid->id); i++)
104 str = fmt_one(str, oid->id[i]);
22a2d3d5
UG
105
106 return 0;
af795e49
SP
107}
108
4ca0b566
VM
109char *git_oid_tostr_s(const git_oid *oid)
110{
c25aa7cd 111 char *str = GIT_THREADSTATE->oid_fmt;
4ca0b566
VM
112 git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
113 return str;
114}
115
af795e49
SP
116char *git_oid_allocfmt(const git_oid *oid)
117{
64a47c01 118 char *str = git__malloc(GIT_OID_HEXSZ + 1);
af795e49
SP
119 if (!str)
120 return NULL;
660d59ca 121 git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
af795e49
SP
122 return str;
123}
960ca1d7 124
5621d809 125char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
960ca1d7 126{
8fe713cc 127 if (!out || n == 0)
960ca1d7
RJ
128 return "";
129
660d59ca
RB
130 if (n > GIT_OID_HEXSZ + 1)
131 n = GIT_OID_HEXSZ + 1;
960ca1d7 132
660d59ca
RB
133 git_oid_nfmt(out, n - 1, oid); /* allow room for terminating NUL */
134 out[n - 1] = '\0';
960ca1d7
RJ
135
136 return out;
137}
138
7c7ff7d1
RB
139int git_oid__parse(
140 git_oid *oid, const char **buffer_out,
141 const char *buffer_end, const char *header)
58519018
VM
142{
143 const size_t sha_len = GIT_OID_HEXSZ;
144 const size_t header_len = strlen(header);
145
720d5472 146 const char *buffer = *buffer_out;
58519018
VM
147
148 if (buffer + (header_len + sha_len + 1) > buffer_end)
73fe6a8e 149 return -1;
58519018
VM
150
151 if (memcmp(buffer, header, header_len) != 0)
73fe6a8e 152 return -1;
58519018
VM
153
154 if (buffer[header_len + sha_len] != '\n')
73fe6a8e 155 return -1;
58519018 156
7c7ff7d1
RB
157 if (git_oid_fromstr(oid, buffer + header_len) < 0)
158 return -1;
58519018
VM
159
160 *buffer_out = buffer + (header_len + sha_len + 1);
161
7c7ff7d1 162 return 0;
58519018
VM
163}
164
afeecf4f
VM
165void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
166{
167 char hex_oid[GIT_OID_HEXSZ];
168
169 git_oid_fmt(hex_oid, oid);
170 git_buf_puts(buf, header);
171 git_buf_put(buf, hex_oid, GIT_OID_HEXSZ);
172 git_buf_putc(buf, '\n');
173}
174
22a2d3d5 175int git_oid_fromraw(git_oid *out, const unsigned char *raw)
d12299fe
VM
176{
177 memcpy(out->id, raw, sizeof(out->id));
22a2d3d5 178 return 0;
d12299fe
VM
179}
180
22a2d3d5 181int git_oid_cpy(git_oid *out, const git_oid *src)
d12299fe
VM
182{
183 memcpy(out->id, src->id, sizeof(out->id));
22a2d3d5 184 return 0;
d12299fe
VM
185}
186
b7f167da 187int git_oid_cmp(const git_oid *a, const git_oid *b)
0c72248b 188{
b7f167da 189 return git_oid__cmp(a, b);
0c72248b
RB
190}
191
978a4ed5
RB
192int git_oid_equal(const git_oid *a, const git_oid *b)
193{
194 return (git_oid__cmp(a, b) == 0);
195}
196
b8457baa 197int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
53c0bd81 198{
f1d01851
VM
199 const unsigned char *a = oid_a->id;
200 const unsigned char *b = oid_b->id;
201
8564a022
RB
202 if (len > GIT_OID_HEXSZ)
203 len = GIT_OID_HEXSZ;
204
205 while (len > 1) {
f1d01851
VM
206 if (*a != *b)
207 return 1;
208 a++;
209 b++;
210 len -= 2;
8564a022 211 };
f1d01851
VM
212
213 if (len)
214 if ((*a ^ *b) & 0xf0)
215 return 1;
216
217 return 0;
53c0bd81
MP
218}
219
aa8f0101 220int git_oid_strcmp(const git_oid *oid_a, const char *str)
34aff010 221{
ea66215d 222 const unsigned char *a;
aa8f0101
RB
223 unsigned char strval;
224 int hexval;
225
226 for (a = oid_a->id; *str && (a - oid_a->id) < GIT_OID_RAWSZ; ++a) {
227 if ((hexval = git__fromhex(*str++)) < 0)
228 return -1;
66566516 229 strval = (unsigned char)(hexval << 4);
aa8f0101
RB
230 if (*str) {
231 if ((hexval = git__fromhex(*str++)) < 0)
232 return -1;
233 strval |= hexval;
234 }
235 if (*a != strval)
236 return (*a - strval);
237 }
34aff010 238
aa8f0101
RB
239 return 0;
240}
34aff010 241
aa8f0101
RB
242int git_oid_streq(const git_oid *oid_a, const char *str)
243{
244 return git_oid_strcmp(oid_a, str) == 0 ? 0 : -1;
34aff010 245}
246
22a2d3d5 247int git_oid_is_zero(const git_oid *oid_a)
74fa4bfa
RB
248{
249 const unsigned char *a = oid_a->id;
250 unsigned int i;
251 for (i = 0; i < GIT_OID_RAWSZ; ++i, ++a)
252 if (*a != 0)
253 return 0;
254 return 1;
255}
256
22a2d3d5
UG
257#ifndef GIT_DEPRECATE_HARD
258int git_oid_iszero(const git_oid *oid_a)
259{
260 return git_oid_is_zero(oid_a);
261}
262#endif
263
26022f07
VM
264typedef short node_index;
265
266typedef union {
267 const char *tail;
268 node_index children[16];
269} trie_node;
270
271struct git_oid_shorten {
272 trie_node *nodes;
273 size_t node_count, size;
274 int min_length, full;
275};
276
277static int resize_trie(git_oid_shorten *self, size_t new_size)
278{
3603cb09 279 self->nodes = git__reallocarray(self->nodes, new_size, sizeof(trie_node));
ac3d33df 280 GIT_ERROR_CHECK_ALLOC(self->nodes);
26022f07
VM
281
282 if (new_size > self->size) {
283 memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
284 }
285
286 self->size = new_size;
7c7ff7d1 287 return 0;
26022f07
VM
288}
289
290static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
291{
292 trie_node *node, *leaf;
293 node_index idx_leaf;
294
295 if (os->node_count >= os->size) {
7c7ff7d1 296 if (resize_trie(os, os->size * 2) < 0)
26022f07
VM
297 return NULL;
298 }
299
300 idx_leaf = (node_index)os->node_count++;
301
52f537e9 302 if (os->node_count == SHRT_MAX) {
26022f07 303 os->full = 1;
52f537e9
AW
304 return NULL;
305 }
26022f07
VM
306
307 node = &os->nodes[idx];
308 node->children[push_at] = -idx_leaf;
309
310 leaf = &os->nodes[idx_leaf];
311 leaf->tail = oid;
312
313 return node;
314}
315
316git_oid_shorten *git_oid_shorten_new(size_t min_length)
317{
318 git_oid_shorten *os;
319
c25aa7cd 320 GIT_ASSERT_ARG_WITH_RETVAL((size_t)((int)min_length) == min_length, NULL);
44ef8b1b 321
7c7ff7d1 322 os = git__calloc(1, sizeof(git_oid_shorten));
26022f07
VM
323 if (os == NULL)
324 return NULL;
325
7c7ff7d1 326 if (resize_trie(os, 16) < 0) {
3286c408 327 git__free(os);
26022f07
VM
328 return NULL;
329 }
330
331 os->node_count = 1;
44ef8b1b 332 os->min_length = (int)min_length;
26022f07
VM
333
334 return os;
335}
336
337void git_oid_shorten_free(git_oid_shorten *os)
338{
32b7e84e
BR
339 if (os == NULL)
340 return;
341
3286c408
VM
342 git__free(os->nodes);
343 git__free(os);
26022f07
VM
344}
345
346
347/*
348 * What wizardry is this?
349 *
350 * This is just a memory-optimized trie: basically a very fancy
351 * 16-ary tree, which is used to store the prefixes of the OID
352 * strings.
353 *
354 * Read more: http://en.wikipedia.org/wiki/Trie
355 *
356 * Magic that happens in this method:
357 *
358 * - Each node in the trie is an union, so it can work both as
359 * a normal node, or as a leaf.
360 *
361 * - Each normal node points to 16 children (one for each possible
362 * character in the oid). This is *not* stored in an array of
932d1baf 363 * pointers, because in a 64-bit arch this would be sucking
826bc4a8 364 * 16*sizeof(void*) = 128 bytes of memory per node, which is
26022f07
VM
365 * insane. What we do is store Node Indexes, and use these indexes
366 * to look up each node in the om->index array. These indexes are
367 * signed shorts, so this limits the amount of unique OIDs that
368 * fit in the structure to about 20000 (assuming a more or less uniform
369 * distribution).
370 *
371 * - All the nodes in om->index array are stored contiguously in
372 * memory, and each of them is 32 bytes, so we fit 2x nodes per
373 * cache line. Convenient for speed.
374 *
375 * - To differentiate the leafs from the normal nodes, we store all
376 * the indexes towards a leaf as a negative index (indexes to normal
377 * nodes are positives). When we find that one of the children for
378 * a node has a negative value, that means it's going to be a leaf.
379 * This reduces the amount of indexes we have by two, but also reduces
380 * the size of each node by 1-4 bytes (the amount we would need to
381 * add a `is_leaf` field): this is good because it allows the nodes
382 * to fit cleanly in cache lines.
383 *
384 * - Once we reach an empty children, instead of continuing to insert
385 * new nodes for each remaining character of the OID, we store a pointer
386 * to the tail in the leaf; if the leaf is reached again, we turn it
387 * into a normal node and use the tail to create a new leaf.
388 *
389 * This is a pretty good balance between performance and memory usage.
390 */
391int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
392{
44ef8b1b
RB
393 int i;
394 bool is_leaf;
26022f07
VM
395 node_index idx;
396
d45e9480 397 if (os->full) {
ac3d33df 398 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
7c7ff7d1 399 return -1;
d45e9480 400 }
26022f07 401
f0ab9fda
VM
402 if (text_oid == NULL)
403 return os->min_length;
404
26022f07 405 idx = 0;
44ef8b1b 406 is_leaf = false;
26022f07
VM
407
408 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
eb8de747 409 int c = git__fromhex(text_oid[i]);
26022f07
VM
410 trie_node *node;
411
7c7ff7d1 412 if (c == -1) {
ac3d33df 413 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - invalid hex value");
7c7ff7d1
RB
414 return -1;
415 }
26022f07
VM
416
417 node = &os->nodes[idx];
418
419 if (is_leaf) {
420 const char *tail;
421
422 tail = node->tail;
423 node->tail = NULL;
424
eb8de747 425 node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
d45e9480
L
426 if (node == NULL) {
427 if (os->full)
ac3d33df 428 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
d45e9480
L
429 return -1;
430 }
26022f07
VM
431 }
432
433 if (node->children[c] == 0) {
d45e9480
L
434 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) {
435 if (os->full)
ac3d33df 436 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
7c7ff7d1 437 return -1;
d45e9480 438 }
26022f07
VM
439 break;
440 }
441
442 idx = node->children[c];
44ef8b1b 443 is_leaf = false;
26022f07
VM
444
445 if (idx < 0) {
446 node->children[c] = idx = -idx;
44ef8b1b 447 is_leaf = true;
26022f07
VM
448 }
449 }
450
451 if (++i > os->min_length)
452 os->min_length = i;
453
454 return os->min_length;
455}
456