]> git.proxmox.com Git - libgit2.git/blob - src/oid.c
I broke your bindings
[libgit2.git] / src / oid.c
1 /*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
26 #include "common.h"
27 #include "git2/oid.h"
28 #include "repository.h"
29 #include <string.h>
30 #include <limits.h>
31
32 static signed char from_hex[] = {
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
36 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
37 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
39 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
49 };
50 static char to_hex[] = "0123456789abcdef";
51
52 int git_oid_mkstr(git_oid *out, const char *str)
53 {
54 size_t p;
55 for (p = 0; p < sizeof(out->id); p++, str += 2) {
56 int v = (from_hex[(unsigned char)str[0]] << 4)
57 | from_hex[(unsigned char)str[1]];
58 if (v < 0)
59 return GIT_ENOTOID;
60 out->id[p] = (unsigned char)v;
61 }
62 return GIT_SUCCESS;
63 }
64
65 GIT_INLINE(char) *fmt_one(char *str, unsigned int val)
66 {
67 *str++ = to_hex[val >> 4];
68 *str++ = to_hex[val & 0xf];
69 return str;
70 }
71
72 void git_oid_fmt(char *str, const git_oid *oid)
73 {
74 size_t i;
75
76 for (i = 0; i < sizeof(oid->id); i++)
77 str = fmt_one(str, oid->id[i]);
78 }
79
80 void git_oid_pathfmt(char *str, const git_oid *oid)
81 {
82 size_t i;
83
84 str = fmt_one(str, oid->id[0]);
85 *str++ = '/';
86 for (i = 1; i < sizeof(oid->id); i++)
87 str = fmt_one(str, oid->id[i]);
88 }
89
90 char *git_oid_allocfmt(const git_oid *oid)
91 {
92 char *str = git__malloc(GIT_OID_HEXSZ + 1);
93 if (!str)
94 return NULL;
95 git_oid_fmt(str, oid);
96 str[GIT_OID_HEXSZ] = '\0';
97 return str;
98 }
99
100 char *git_oid_to_string(char *out, size_t n, const git_oid *oid)
101 {
102 char str[GIT_OID_HEXSZ];
103
104 if (!out || n == 0 || !oid)
105 return "";
106
107 n--; /* allow room for terminating NUL */
108
109 if (n > 0) {
110 git_oid_fmt(str, oid);
111 if (n > GIT_OID_HEXSZ)
112 n = GIT_OID_HEXSZ;
113 memcpy(out, str, n);
114 }
115
116 out[n] = '\0';
117
118 return out;
119 }
120
121 int git__parse_oid(git_oid *oid, char **buffer_out,
122 const char *buffer_end, const char *header)
123 {
124 const size_t sha_len = GIT_OID_HEXSZ;
125 const size_t header_len = strlen(header);
126
127 char *buffer = *buffer_out;
128
129 if (buffer + (header_len + sha_len + 1) > buffer_end)
130 return GIT_EOBJCORRUPTED;
131
132 if (memcmp(buffer, header, header_len) != 0)
133 return GIT_EOBJCORRUPTED;
134
135 if (buffer[header_len + sha_len] != '\n')
136 return GIT_EOBJCORRUPTED;
137
138 if (git_oid_mkstr(oid, buffer + header_len) < GIT_SUCCESS)
139 return GIT_EOBJCORRUPTED;
140
141 *buffer_out = buffer + (header_len + sha_len + 1);
142
143 return GIT_SUCCESS;
144 }
145
146 int git__write_oid(git_odb_stream *stream, const char *header, const git_oid *oid)
147 {
148 char hex_oid[42];
149
150 git_oid_fmt(hex_oid + 1, oid);
151
152 hex_oid[0] = ' ';
153 hex_oid[41] = '\n';
154
155 stream->write(stream, header, strlen(header));
156 stream->write(stream, hex_oid, 42);
157 return GIT_SUCCESS;
158 }
159
160 void git_oid_mkraw(git_oid *out, const unsigned char *raw)
161 {
162 memcpy(out->id, raw, sizeof(out->id));
163 }
164
165 void git_oid_cpy(git_oid *out, const git_oid *src)
166 {
167 memcpy(out->id, src->id, sizeof(out->id));
168 }
169
170 int git_oid_cmp(const git_oid *a, const git_oid *b)
171 {
172 return memcmp(a->id, b->id, sizeof(a->id));
173 }
174
175
176 typedef short node_index;
177
178 typedef union {
179 const char *tail;
180 node_index children[16];
181 } trie_node;
182
183 struct git_oid_shorten {
184 trie_node *nodes;
185 size_t node_count, size;
186 int min_length, full;
187 };
188
189 static int resize_trie(git_oid_shorten *self, size_t new_size)
190 {
191 self->nodes = realloc(self->nodes, new_size * sizeof(trie_node));
192 if (self->nodes == NULL)
193 return GIT_ENOMEM;
194
195 if (new_size > self->size) {
196 memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
197 }
198
199 self->size = new_size;
200 return GIT_SUCCESS;
201 }
202
203 static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
204 {
205 trie_node *node, *leaf;
206 node_index idx_leaf;
207
208 if (os->node_count >= os->size) {
209 if (resize_trie(os, os->size * 2) < GIT_SUCCESS)
210 return NULL;
211 }
212
213 idx_leaf = (node_index)os->node_count++;
214
215 if (os->node_count == SHRT_MAX)
216 os->full = 1;
217
218 node = &os->nodes[idx];
219 node->children[push_at] = -idx_leaf;
220
221 leaf = &os->nodes[idx_leaf];
222 leaf->tail = oid;
223
224 return node;
225 }
226
227 git_oid_shorten *git_oid_shorten_new(size_t min_length)
228 {
229 git_oid_shorten *os;
230
231 os = git__malloc(sizeof(git_oid_shorten));
232 if (os == NULL)
233 return NULL;
234
235 memset(os, 0x0, sizeof(git_oid_shorten));
236
237 if (resize_trie(os, 16) < GIT_SUCCESS) {
238 free(os);
239 return NULL;
240 }
241
242 os->node_count = 1;
243 os->min_length = min_length;
244
245 return os;
246 }
247
248 void git_oid_shorten_free(git_oid_shorten *os)
249 {
250 free(os->nodes);
251 free(os);
252 }
253
254
255 /*
256 * What wizardry is this?
257 *
258 * This is just a memory-optimized trie: basically a very fancy
259 * 16-ary tree, which is used to store the prefixes of the OID
260 * strings.
261 *
262 * Read more: http://en.wikipedia.org/wiki/Trie
263 *
264 * Magic that happens in this method:
265 *
266 * - Each node in the trie is an union, so it can work both as
267 * a normal node, or as a leaf.
268 *
269 * - Each normal node points to 16 children (one for each possible
270 * character in the oid). This is *not* stored in an array of
271 * pointers, because in a 64-bit arch this would be sucking
272 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
273 * insane. What we do is store Node Indexes, and use these indexes
274 * to look up each node in the om->index array. These indexes are
275 * signed shorts, so this limits the amount of unique OIDs that
276 * fit in the structure to about 20000 (assuming a more or less uniform
277 * distribution).
278 *
279 * - All the nodes in om->index array are stored contiguously in
280 * memory, and each of them is 32 bytes, so we fit 2x nodes per
281 * cache line. Convenient for speed.
282 *
283 * - To differentiate the leafs from the normal nodes, we store all
284 * the indexes towards a leaf as a negative index (indexes to normal
285 * nodes are positives). When we find that one of the children for
286 * a node has a negative value, that means it's going to be a leaf.
287 * This reduces the amount of indexes we have by two, but also reduces
288 * the size of each node by 1-4 bytes (the amount we would need to
289 * add a `is_leaf` field): this is good because it allows the nodes
290 * to fit cleanly in cache lines.
291 *
292 * - Once we reach an empty children, instead of continuing to insert
293 * new nodes for each remaining character of the OID, we store a pointer
294 * to the tail in the leaf; if the leaf is reached again, we turn it
295 * into a normal node and use the tail to create a new leaf.
296 *
297 * This is a pretty good balance between performance and memory usage.
298 */
299 int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
300 {
301 int i, is_leaf;
302 node_index idx;
303
304 if (os->full)
305 return GIT_ENOMEM;
306
307 idx = 0;
308 is_leaf = 0;
309
310 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
311 int c = from_hex[(int)text_oid[i]];
312 trie_node *node;
313
314 if (c == -1)
315 return GIT_ENOTOID;
316
317 node = &os->nodes[idx];
318
319 if (is_leaf) {
320 const char *tail;
321
322 tail = node->tail;
323 node->tail = NULL;
324
325 node = push_leaf(os, idx, from_hex[(int)tail[0]], &tail[1]);
326 if (node == NULL)
327 return GIT_ENOMEM;
328 }
329
330 if (node->children[c] == 0) {
331 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
332 return GIT_ENOMEM;
333 break;
334 }
335
336 idx = node->children[c];
337 is_leaf = 0;
338
339 if (idx < 0) {
340 node->children[c] = idx = -idx;
341 is_leaf = 1;
342 }
343 }
344
345 if (++i > os->min_length)
346 os->min_length = i;
347
348 return os->min_length;
349 }
350