]> git.proxmox.com Git - libgit2.git/blame - src/oid.c
tree: Add traversal in post-order
[libgit2.git] / src / oid.c
CommitLineData
c15648cb 1/*
bb742ede 2 * Copyright (C) 2009-2011 the libgit2 contributors
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
b3be0fc7 8#include "common.h"
44908fe7 9#include "git2/oid.h"
58519018 10#include "repository.h"
c15648cb 11#include <string.h>
26022f07 12#include <limits.h>
c15648cb
SP
13
14static signed char from_hex[] = {
15-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
16-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
17-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
87d9869f 18 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
c15648cb
SP
19-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
20-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
21-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
22-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
23-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
24-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
25-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
26-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
27-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
28-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
29-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
30-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
31};
af795e49 32static char to_hex[] = "0123456789abcdef";
c15648cb 33
d5afc039 34int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
c15648cb 35{
0ef9d2aa 36 size_t p;
e724b058 37 int v;
d5afc039 38
b9caa185
DI
39 if (length < 4)
40 return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short");
41
d5afc039
VM
42 if (length > GIT_OID_HEXSZ)
43 length = GIT_OID_HEXSZ;
44
e724b058
DI
45 for (p = 0; p < length - 1; p += 2) {
46 v = (from_hex[(unsigned char)str[p + 0]] << 4)
87d9869f 47 | from_hex[(unsigned char)str[p + 1]];
d5afc039 48
c15648cb 49 if (v < 0)
bea54842 50 return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
d5afc039
VM
51
52 out->id[p / 2] = (unsigned char)v;
c15648cb 53 }
d5afc039 54
e724b058
DI
55 if (length % 2) {
56 v = (from_hex[(unsigned char)str[p + 0]] << 4);
0e058e78
DI
57 if (v < 0)
58 return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
59
e724b058
DI
60 out->id[p / 2] = (unsigned char)v;
61 p += 2;
62 }
63
6d8d3f19 64 memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
d5afc039 65
c15648cb
SP
66 return GIT_SUCCESS;
67}
af795e49 68
d5afc039
VM
69int git_oid_fromstr(git_oid *out, const char *str)
70{
71 return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
72}
73
213e720c 74GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
af795e49
SP
75{
76 *str++ = to_hex[val >> 4];
77 *str++ = to_hex[val & 0xf];
78 return str;
79}
80
81void git_oid_fmt(char *str, const git_oid *oid)
82{
0ef9d2aa 83 size_t i;
af795e49
SP
84
85 for (i = 0; i < sizeof(oid->id); i++)
86 str = fmt_one(str, oid->id[i]);
87}
88
89void git_oid_pathfmt(char *str, const git_oid *oid)
90{
0ef9d2aa 91 size_t i;
af795e49
SP
92
93 str = fmt_one(str, oid->id[0]);
94 *str++ = '/';
95 for (i = 1; i < sizeof(oid->id); i++)
96 str = fmt_one(str, oid->id[i]);
97}
98
99char *git_oid_allocfmt(const git_oid *oid)
100{
64a47c01 101 char *str = git__malloc(GIT_OID_HEXSZ + 1);
af795e49
SP
102 if (!str)
103 return NULL;
104 git_oid_fmt(str, oid);
105 str[GIT_OID_HEXSZ] = '\0';
106 return str;
107}
960ca1d7
RJ
108
109char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
110{
111 char str[GIT_OID_HEXSZ];
112
113 if (!out || n == 0 || !oid)
114 return "";
115
87d9869f 116 n--; /* allow room for terminating NUL */
960ca1d7
RJ
117
118 if (n > 0) {
119 git_oid_fmt(str, oid);
552e23ba
RJ
120 if (n > GIT_OID_HEXSZ)
121 n = GIT_OID_HEXSZ;
122 memcpy(out, str, n);
960ca1d7
RJ
123 }
124
125 out[n] = '\0';
126
127 return out;
128}
129
06c43821 130int git_oid__parse(git_oid *oid, const char **buffer_out,
58519018
VM
131 const char *buffer_end, const char *header)
132{
133 const size_t sha_len = GIT_OID_HEXSZ;
134 const size_t header_len = strlen(header);
135
720d5472 136 const char *buffer = *buffer_out;
58519018
VM
137
138 if (buffer + (header_len + sha_len + 1) > buffer_end)
bea54842 139 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer too small");
58519018
VM
140
141 if (memcmp(buffer, header, header_len) != 0)
bea54842 142 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer and header do not match");
58519018
VM
143
144 if (buffer[header_len + sha_len] != '\n')
bea54842 145 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer not terminated correctly");
58519018 146
fa48608e 147 if (git_oid_fromstr(oid, buffer + header_len) < GIT_SUCCESS)
bea54842 148 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Failed to generate sha1");
58519018
VM
149
150 *buffer_out = buffer + (header_len + sha_len + 1);
151
6f02c3ba 152 return GIT_SUCCESS;
58519018
VM
153}
154
afeecf4f
VM
155void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
156{
157 char hex_oid[GIT_OID_HEXSZ];
158
159 git_oid_fmt(hex_oid, oid);
160 git_buf_puts(buf, header);
161 git_buf_put(buf, hex_oid, GIT_OID_HEXSZ);
162 git_buf_putc(buf, '\n');
163}
164
fa48608e 165void git_oid_fromraw(git_oid *out, const unsigned char *raw)
d12299fe
VM
166{
167 memcpy(out->id, raw, sizeof(out->id));
168}
169
170void git_oid_cpy(git_oid *out, const git_oid *src)
171{
172 memcpy(out->id, src->id, sizeof(out->id));
173}
174
175int git_oid_cmp(const git_oid *a, const git_oid *b)
176{
177 return memcmp(a->id, b->id, sizeof(a->id));
178}
26022f07 179
f1d01851 180int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len)
53c0bd81 181{
f1d01851
VM
182 const unsigned char *a = oid_a->id;
183 const unsigned char *b = oid_b->id;
184
185 do {
186 if (*a != *b)
187 return 1;
188 a++;
189 b++;
190 len -= 2;
191 } while (len > 1);
192
193 if (len)
194 if ((*a ^ *b) & 0xf0)
195 return 1;
196
197 return 0;
53c0bd81
MP
198}
199
34aff010 200int git_oid_streq(const git_oid *a, const char *str)
201{
202 git_oid id;
203 int error;
204
205 if ((error = git_oid_fromstr(&id, str)) < GIT_SUCCESS)
206 return git__rethrow(error, "Failed to convert '%s' to oid.", str);
207
208 return git_oid_cmp(a, &id) == 0 ? GIT_SUCCESS : GIT_ERROR;
209}
210
26022f07
VM
211typedef short node_index;
212
213typedef union {
214 const char *tail;
215 node_index children[16];
216} trie_node;
217
218struct git_oid_shorten {
219 trie_node *nodes;
220 size_t node_count, size;
221 int min_length, full;
222};
223
224static int resize_trie(git_oid_shorten *self, size_t new_size)
225{
226 self->nodes = realloc(self->nodes, new_size * sizeof(trie_node));
227 if (self->nodes == NULL)
228 return GIT_ENOMEM;
229
230 if (new_size > self->size) {
231 memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
232 }
233
234 self->size = new_size;
235 return GIT_SUCCESS;
236}
237
238static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
239{
240 trie_node *node, *leaf;
241 node_index idx_leaf;
242
243 if (os->node_count >= os->size) {
244 if (resize_trie(os, os->size * 2) < GIT_SUCCESS)
245 return NULL;
246 }
247
248 idx_leaf = (node_index)os->node_count++;
249
250 if (os->node_count == SHRT_MAX)
251 os->full = 1;
252
253 node = &os->nodes[idx];
254 node->children[push_at] = -idx_leaf;
255
256 leaf = &os->nodes[idx_leaf];
257 leaf->tail = oid;
258
259 return node;
260}
261
262git_oid_shorten *git_oid_shorten_new(size_t min_length)
263{
264 git_oid_shorten *os;
265
266 os = git__malloc(sizeof(git_oid_shorten));
267 if (os == NULL)
268 return NULL;
269
270 memset(os, 0x0, sizeof(git_oid_shorten));
271
272 if (resize_trie(os, 16) < GIT_SUCCESS) {
273 free(os);
274 return NULL;
275 }
276
277 os->node_count = 1;
278 os->min_length = min_length;
279
280 return os;
281}
282
283void git_oid_shorten_free(git_oid_shorten *os)
284{
285 free(os->nodes);
286 free(os);
287}
288
289
290/*
291 * What wizardry is this?
292 *
293 * This is just a memory-optimized trie: basically a very fancy
294 * 16-ary tree, which is used to store the prefixes of the OID
295 * strings.
296 *
297 * Read more: http://en.wikipedia.org/wiki/Trie
298 *
299 * Magic that happens in this method:
300 *
301 * - Each node in the trie is an union, so it can work both as
302 * a normal node, or as a leaf.
303 *
304 * - Each normal node points to 16 children (one for each possible
305 * character in the oid). This is *not* stored in an array of
932d1baf 306 * pointers, because in a 64-bit arch this would be sucking
26022f07
VM
307 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
308 * insane. What we do is store Node Indexes, and use these indexes
309 * to look up each node in the om->index array. These indexes are
310 * signed shorts, so this limits the amount of unique OIDs that
311 * fit in the structure to about 20000 (assuming a more or less uniform
312 * distribution).
313 *
314 * - All the nodes in om->index array are stored contiguously in
315 * memory, and each of them is 32 bytes, so we fit 2x nodes per
316 * cache line. Convenient for speed.
317 *
318 * - To differentiate the leafs from the normal nodes, we store all
319 * the indexes towards a leaf as a negative index (indexes to normal
320 * nodes are positives). When we find that one of the children for
321 * a node has a negative value, that means it's going to be a leaf.
322 * This reduces the amount of indexes we have by two, but also reduces
323 * the size of each node by 1-4 bytes (the amount we would need to
324 * add a `is_leaf` field): this is good because it allows the nodes
325 * to fit cleanly in cache lines.
326 *
327 * - Once we reach an empty children, instead of continuing to insert
328 * new nodes for each remaining character of the OID, we store a pointer
329 * to the tail in the leaf; if the leaf is reached again, we turn it
330 * into a normal node and use the tail to create a new leaf.
331 *
332 * This is a pretty good balance between performance and memory usage.
333 */
334int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
335{
336 int i, is_leaf;
337 node_index idx;
338
339 if (os->full)
340 return GIT_ENOMEM;
341
f0ab9fda
VM
342 if (text_oid == NULL)
343 return os->min_length;
344
26022f07
VM
345 idx = 0;
346 is_leaf = 0;
347
348 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
349 int c = from_hex[(int)text_oid[i]];
350 trie_node *node;
351
352 if (c == -1)
bea54842 353 return git__throw(GIT_ENOTOID, "Failed to shorten OID. Invalid hex value");
26022f07
VM
354
355 node = &os->nodes[idx];
356
357 if (is_leaf) {
358 const char *tail;
359
360 tail = node->tail;
361 node->tail = NULL;
362
363 node = push_leaf(os, idx, from_hex[(int)tail[0]], &tail[1]);
364 if (node == NULL)
365 return GIT_ENOMEM;
366 }
367
368 if (node->children[c] == 0) {
369 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
370 return GIT_ENOMEM;
371 break;
372 }
373
374 idx = node->children[c];
375 is_leaf = 0;
376
377 if (idx < 0) {
378 node->children[c] = idx = -idx;
379 is_leaf = 1;
380 }
381 }
382
383 if (++i > os->min_length)
384 os->min_length = i;
385
386 return os->min_length;
387}
388