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