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