]> git.proxmox.com Git - libgit2.git/blob - src/oid.c
Remove use of English expletives
[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 > GIT_OID_HEXSZ)
28 length = GIT_OID_HEXSZ;
29
30 for (p = 0; p < length - 1; p += 2) {
31 v = (git__fromhex(str[p + 0]) << 4)
32 | git__fromhex(str[p + 1]);
33
34 if (v < 0)
35 return oid_error_invalid("contains invalid characters");
36
37 out->id[p / 2] = (unsigned char)v;
38 }
39
40 if (length % 2) {
41 v = (git__fromhex(str[p + 0]) << 4);
42 if (v < 0)
43 return oid_error_invalid("contains invalid characters");
44
45 out->id[p / 2] = (unsigned char)v;
46 p += 2;
47 }
48
49 memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
50
51 return 0;
52 }
53
54 int git_oid_fromstr(git_oid *out, const char *str)
55 {
56 return git_oid_fromstrn(out, str, GIT_OID_HEXSZ);
57 }
58
59 GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
60 {
61 *str++ = to_hex[val >> 4];
62 *str++ = to_hex[val & 0xf];
63 return str;
64 }
65
66 void git_oid_fmt(char *str, const git_oid *oid)
67 {
68 size_t i;
69
70 for (i = 0; i < sizeof(oid->id); i++)
71 str = fmt_one(str, oid->id[i]);
72 }
73
74 void git_oid_pathfmt(char *str, const git_oid *oid)
75 {
76 size_t i;
77
78 str = fmt_one(str, oid->id[0]);
79 *str++ = '/';
80 for (i = 1; i < sizeof(oid->id); i++)
81 str = fmt_one(str, oid->id[i]);
82 }
83
84 char *git_oid_allocfmt(const git_oid *oid)
85 {
86 char *str = git__malloc(GIT_OID_HEXSZ + 1);
87 if (!str)
88 return NULL;
89 git_oid_fmt(str, oid);
90 str[GIT_OID_HEXSZ] = '\0';
91 return str;
92 }
93
94 char *git_oid_tostr(char *out, size_t n, const git_oid *oid)
95 {
96 char str[GIT_OID_HEXSZ];
97
98 if (!out || n == 0 || !oid)
99 return "";
100
101 n--; /* allow room for terminating NUL */
102
103 if (n > 0) {
104 git_oid_fmt(str, oid);
105 if (n > GIT_OID_HEXSZ)
106 n = GIT_OID_HEXSZ;
107 memcpy(out, str, n);
108 }
109
110 out[n] = '\0';
111
112 return out;
113 }
114
115 int git_oid__parse(
116 git_oid *oid, const char **buffer_out,
117 const char *buffer_end, const char *header)
118 {
119 const size_t sha_len = GIT_OID_HEXSZ;
120 const size_t header_len = strlen(header);
121
122 const char *buffer = *buffer_out;
123
124 if (buffer + (header_len + sha_len + 1) > buffer_end)
125 return -1;
126
127 if (memcmp(buffer, header, header_len) != 0)
128 return -1;
129
130 if (buffer[header_len + sha_len] != '\n')
131 return -1;
132
133 if (git_oid_fromstr(oid, buffer + header_len) < 0)
134 return -1;
135
136 *buffer_out = buffer + (header_len + sha_len + 1);
137
138 return 0;
139 }
140
141 void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid)
142 {
143 char hex_oid[GIT_OID_HEXSZ];
144
145 git_oid_fmt(hex_oid, oid);
146 git_buf_puts(buf, header);
147 git_buf_put(buf, hex_oid, GIT_OID_HEXSZ);
148 git_buf_putc(buf, '\n');
149 }
150
151 void git_oid_fromraw(git_oid *out, const unsigned char *raw)
152 {
153 memcpy(out->id, raw, sizeof(out->id));
154 }
155
156 void git_oid_cpy(git_oid *out, const git_oid *src)
157 {
158 memcpy(out->id, src->id, sizeof(out->id));
159 }
160
161 int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
162 {
163 const unsigned char *a = oid_a->id;
164 const unsigned char *b = oid_b->id;
165
166 do {
167 if (*a != *b)
168 return 1;
169 a++;
170 b++;
171 len -= 2;
172 } while (len > 1);
173
174 if (len)
175 if ((*a ^ *b) & 0xf0)
176 return 1;
177
178 return 0;
179 }
180
181 int git_oid_streq(const git_oid *a, const char *str)
182 {
183 git_oid id;
184
185 if (git_oid_fromstr(&id, str) < 0)
186 return -1;
187
188 return git_oid_cmp(a, &id) == 0 ? 0 : -1;
189 }
190
191 int git_oid_iszero(const git_oid *oid_a)
192 {
193 const unsigned char *a = oid_a->id;
194 unsigned int i;
195 for (i = 0; i < GIT_OID_RAWSZ; ++i, ++a)
196 if (*a != 0)
197 return 0;
198 return 1;
199 }
200
201 typedef short node_index;
202
203 typedef union {
204 const char *tail;
205 node_index children[16];
206 } trie_node;
207
208 struct git_oid_shorten {
209 trie_node *nodes;
210 size_t node_count, size;
211 int min_length, full;
212 };
213
214 static int resize_trie(git_oid_shorten *self, size_t new_size)
215 {
216 self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node));
217 GITERR_CHECK_ALLOC(self->nodes);
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 0;
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) < 0)
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 assert((size_t)((int)min_length) == min_length);
256
257 os = git__calloc(1, sizeof(git_oid_shorten));
258 if (os == NULL)
259 return NULL;
260
261 if (resize_trie(os, 16) < 0) {
262 git__free(os);
263 return NULL;
264 }
265
266 os->node_count = 1;
267 os->min_length = (int)min_length;
268
269 return os;
270 }
271
272 void git_oid_shorten_free(git_oid_shorten *os)
273 {
274 git__free(os->nodes);
275 git__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
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;
326 bool is_leaf;
327 node_index idx;
328
329 if (os->full)
330 return -1;
331
332 if (text_oid == NULL)
333 return os->min_length;
334
335 idx = 0;
336 is_leaf = false;
337
338 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
339 int c = git__fromhex(text_oid[i]);
340 trie_node *node;
341
342 if (c == -1) {
343 giterr_set(GITERR_INVALID, "Unable to shorten OID - invalid hex value");
344 return -1;
345 }
346
347 node = &os->nodes[idx];
348
349 if (is_leaf) {
350 const char *tail;
351
352 tail = node->tail;
353 node->tail = NULL;
354
355 node = push_leaf(os, idx, git__fromhex(tail[0]), &tail[1]);
356 GITERR_CHECK_ALLOC(node);
357 }
358
359 if (node->children[c] == 0) {
360 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
361 return -1;
362 break;
363 }
364
365 idx = node->children[c];
366 is_leaf = false;
367
368 if (idx < 0) {
369 node->children[c] = idx = -idx;
370 is_leaf = true;
371 }
372 }
373
374 if (++i > os->min_length)
375 os->min_length = i;
376
377 return os->min_length;
378 }
379