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