]> git.proxmox.com Git - libgit2.git/blob - src/oid.c
8d45e4d7d52c236fc6be221e5a13444391b835f2
[libgit2.git] / src / oid.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "oid.h"
9
10 #include "git2/oid.h"
11 #include "repository.h"
12 #include "global.h"
13 #include <string.h>
14 #include <limits.h>
15
16 static char to_hex[] = "0123456789abcdef";
17
18 static int oid_error_invalid(const char *msg)
19 {
20 git_error_set(GIT_ERROR_INVALID, "unable to parse OID - %s", msg);
21 return -1;
22 }
23
24 int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
25 {
26 size_t p;
27 int v;
28
29 assert(out && str);
30
31 if (!length)
32 return oid_error_invalid("too short");
33
34 if (length > GIT_OID_HEXSZ)
35 return oid_error_invalid("too long");
36
37 memset(out->id, 0, GIT_OID_RAWSZ);
38
39 for (p = 0; p < length; p++) {
40 v = git__fromhex(str[p]);
41 if (v < 0)
42 return oid_error_invalid("contains invalid characters");
43
44 out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4));
45 }
46
47 return 0;
48 }
49
50 int git_oid_fromstrp(git_oid *out, const char *str)
51 {
52 return git_oid_fromstrn(out, str, strlen(str));
53 }
54
55 int git_oid_fromstr(git_oid *out, const char *str)
56 {
57 return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
58 }
59
60 GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
61 {
62 *str++ = to_hex[val >> 4];
63 *str++ = to_hex[val & 0xf];
64 return str;
65 }
66
67 void git_oid_nfmt(char *str, size_t n, const git_oid *oid)
68 {
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;
81
82 for (i = 0; i < max_i; i++)
83 str = fmt_one(str, oid->id[i]);
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);
92 }
93
94 void git_oid_pathfmt(char *str, const git_oid *oid)
95 {
96 size_t i;
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
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
111 char *git_oid_allocfmt(const git_oid *oid)
112 {
113 char *str = git__malloc(GIT_OID_HEXSZ + 1);
114 if (!str)
115 return NULL;
116 git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
117 return str;
118 }
119
120 char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
121 {
122 if (!out || n == 0)
123 return "";
124
125 if (n > GIT_OID_HEXSZ + 1)
126 n = GIT_OID_HEXSZ + 1;
127
128 git_oid_nfmt(out, n - 1, oid); /* allow room for terminating NUL */
129 out[n - 1] = '\0';
130
131 return out;
132 }
133
134 int git_oid__parse(
135 git_oid *oid, const char **buffer_out,
136 const char *buffer_end, const char *header)
137 {
138 const size_t sha_len = GIT_OID_HEXSZ;
139 const size_t header_len = strlen(header);
140
141 const char *buffer = *buffer_out;
142
143 if (buffer + (header_len + sha_len + 1) > buffer_end)
144 return -1;
145
146 if (memcmp(buffer, header, header_len) != 0)
147 return -1;
148
149 if (buffer[header_len + sha_len] != '\n')
150 return -1;
151
152 if (git_oid_fromstr(oid, buffer + header_len) < 0)
153 return -1;
154
155 *buffer_out = buffer + (header_len + sha_len + 1);
156
157 return 0;
158 }
159
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
170 void git_oid_fromraw(git_oid *out, const unsigned char *raw)
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
180 int git_oid_cmp(const git_oid *a, const git_oid *b)
181 {
182 return git_oid__cmp(a, b);
183 }
184
185 int git_oid_equal(const git_oid *a, const git_oid *b)
186 {
187 return (git_oid__cmp(a, b) == 0);
188 }
189
190 int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
191 {
192 const unsigned char *a = oid_a->id;
193 const unsigned char *b = oid_b->id;
194
195 if (len > GIT_OID_HEXSZ)
196 len = GIT_OID_HEXSZ;
197
198 while (len > 1) {
199 if (*a != *b)
200 return 1;
201 a++;
202 b++;
203 len -= 2;
204 };
205
206 if (len)
207 if ((*a ^ *b) & 0xf0)
208 return 1;
209
210 return 0;
211 }
212
213 int git_oid_strcmp(const git_oid *oid_a, const char *str)
214 {
215 const unsigned char *a;
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;
222 strval = (unsigned char)(hexval << 4);
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 }
231
232 return 0;
233 }
234
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;
238 }
239
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
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 {
265 self->nodes = git__reallocarray(self->nodes, new_size, sizeof(trie_node));
266 GIT_ERROR_CHECK_ALLOC(self->nodes);
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;
273 return 0;
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) {
282 if (resize_trie(os, os->size * 2) < 0)
283 return NULL;
284 }
285
286 idx_leaf = (node_index)os->node_count++;
287
288 if (os->node_count == SHRT_MAX) {
289 os->full = 1;
290 return NULL;
291 }
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
306 assert((size_t)((int)min_length) == min_length);
307
308 os = git__calloc(1, sizeof(git_oid_shorten));
309 if (os == NULL)
310 return NULL;
311
312 if (resize_trie(os, 16) < 0) {
313 git__free(os);
314 return NULL;
315 }
316
317 os->node_count = 1;
318 os->min_length = (int)min_length;
319
320 return os;
321 }
322
323 void git_oid_shorten_free(git_oid_shorten *os)
324 {
325 if (os == NULL)
326 return;
327
328 git__free(os->nodes);
329 git__free(os);
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
349 * pointers, because in a 64-bit arch this would be sucking
350 * 16*sizeof(void*) = 128 bytes of memory per node, which is
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 {
379 int i;
380 bool is_leaf;
381 node_index idx;
382
383 if (os->full) {
384 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
385 return -1;
386 }
387
388 if (text_oid == NULL)
389 return os->min_length;
390
391 idx = 0;
392 is_leaf = false;
393
394 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
395 int c = git__fromhex(text_oid[i]);
396 trie_node *node;
397
398 if (c == -1) {
399 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - invalid hex value");
400 return -1;
401 }
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
411 node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
412 if (node == NULL) {
413 if (os->full)
414 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
415 return -1;
416 }
417 }
418
419 if (node->children[c] == 0) {
420 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL) {
421 if (os->full)
422 git_error_set(GIT_ERROR_INVALID, "unable to shorten OID - OID set full");
423 return -1;
424 }
425 break;
426 }
427
428 idx = node->children[c];
429 is_leaf = false;
430
431 if (idx < 0) {
432 node->children[c] = idx = -idx;
433 is_leaf = true;
434 }
435 }
436
437 if (++i > os->min_length)
438 os->min_length = i;
439
440 return os->min_length;
441 }
442