]> git.proxmox.com Git - libgit2.git/blob - src/oid.c
Merge pull request #238 from pegonma/git_oid_ncmp
[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__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
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, const 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 const char *buffer = *buffer_out;
128
129 if (buffer + (header_len + sha_len + 1) > buffer_end)
130 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer too small");
131
132 if (memcmp(buffer, header, header_len) != 0)
133 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer and header do not match");
134
135 if (buffer[header_len + sha_len] != '\n')
136 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Buffer not terminated correctly");
137
138 if (git_oid_mkstr(oid, buffer + header_len) < GIT_SUCCESS)
139 return git__throw(GIT_EOBJCORRUPTED, "Failed to parse OID. Failed to generate sha1");
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 int git_oid_ncmp_raw(unsigned int len, const unsigned char *a, const unsigned char *b)
177 {
178 do {
179 if (*a != *b)
180 return 1;
181 a++;
182 b++;
183 len -= 2;
184 } while (len > 1);
185 if (len)
186 if ((*a ^ *b) & 0xf0)
187 return 1;
188 return 0;
189 }
190
191 int git_oid_ncmp_hex(unsigned int len, const unsigned char *a, const unsigned char *b)
192 {
193 return memcmp(a, b, len);
194 }
195
196 int gid_oid_ncmp(unsigned int len, git_oid *a, git_oid *b)
197 {
198 return git_oid_ncmp_raw(len, a->id, b->id);
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 = realloc(self->nodes, new_size * sizeof(trie_node));
217 if (self->nodes == NULL)
218 return GIT_ENOMEM;
219
220 if (new_size > self->size) {
221 memset(&self->nodes[self->size], 0x0, (new_size - self->size) * sizeof(trie_node));
222 }
223
224 self->size = new_size;
225 return GIT_SUCCESS;
226 }
227
228 static trie_node *push_leaf(git_oid_shorten *os, node_index idx, int push_at, const char *oid)
229 {
230 trie_node *node, *leaf;
231 node_index idx_leaf;
232
233 if (os->node_count >= os->size) {
234 if (resize_trie(os, os->size * 2) < GIT_SUCCESS)
235 return NULL;
236 }
237
238 idx_leaf = (node_index)os->node_count++;
239
240 if (os->node_count == SHRT_MAX)
241 os->full = 1;
242
243 node = &os->nodes[idx];
244 node->children[push_at] = -idx_leaf;
245
246 leaf = &os->nodes[idx_leaf];
247 leaf->tail = oid;
248
249 return node;
250 }
251
252 git_oid_shorten *git_oid_shorten_new(size_t min_length)
253 {
254 git_oid_shorten *os;
255
256 os = git__malloc(sizeof(git_oid_shorten));
257 if (os == NULL)
258 return NULL;
259
260 memset(os, 0x0, sizeof(git_oid_shorten));
261
262 if (resize_trie(os, 16) < GIT_SUCCESS) {
263 free(os);
264 return NULL;
265 }
266
267 os->node_count = 1;
268 os->min_length = min_length;
269
270 return os;
271 }
272
273 void git_oid_shorten_free(git_oid_shorten *os)
274 {
275 free(os->nodes);
276 free(os);
277 }
278
279
280 /*
281 * What wizardry is this?
282 *
283 * This is just a memory-optimized trie: basically a very fancy
284 * 16-ary tree, which is used to store the prefixes of the OID
285 * strings.
286 *
287 * Read more: http://en.wikipedia.org/wiki/Trie
288 *
289 * Magic that happens in this method:
290 *
291 * - Each node in the trie is an union, so it can work both as
292 * a normal node, or as a leaf.
293 *
294 * - Each normal node points to 16 children (one for each possible
295 * character in the oid). This is *not* stored in an array of
296 * pointers, because in a 64-bit arch this would be sucking
297 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
298 * insane. What we do is store Node Indexes, and use these indexes
299 * to look up each node in the om->index array. These indexes are
300 * signed shorts, so this limits the amount of unique OIDs that
301 * fit in the structure to about 20000 (assuming a more or less uniform
302 * distribution).
303 *
304 * - All the nodes in om->index array are stored contiguously in
305 * memory, and each of them is 32 bytes, so we fit 2x nodes per
306 * cache line. Convenient for speed.
307 *
308 * - To differentiate the leafs from the normal nodes, we store all
309 * the indexes towards a leaf as a negative index (indexes to normal
310 * nodes are positives). When we find that one of the children for
311 * a node has a negative value, that means it's going to be a leaf.
312 * This reduces the amount of indexes we have by two, but also reduces
313 * the size of each node by 1-4 bytes (the amount we would need to
314 * add a `is_leaf` field): this is good because it allows the nodes
315 * to fit cleanly in cache lines.
316 *
317 * - Once we reach an empty children, instead of continuing to insert
318 * new nodes for each remaining character of the OID, we store a pointer
319 * to the tail in the leaf; if the leaf is reached again, we turn it
320 * into a normal node and use the tail to create a new leaf.
321 *
322 * This is a pretty good balance between performance and memory usage.
323 */
324 int git_oid_shorten_add(git_oid_shorten *os, const char *text_oid)
325 {
326 int i, is_leaf;
327 node_index idx;
328
329 if (os->full)
330 return GIT_ENOMEM;
331
332 idx = 0;
333 is_leaf = 0;
334
335 for (i = 0; i < GIT_OID_HEXSZ; ++i) {
336 int c = from_hex[(int)text_oid[i]];
337 trie_node *node;
338
339 if (c == -1)
340 return git__throw(GIT_ENOTOID, "Failed to shorten OID. Invalid hex value");
341
342 node = &os->nodes[idx];
343
344 if (is_leaf) {
345 const char *tail;
346
347 tail = node->tail;
348 node->tail = NULL;
349
350 node = push_leaf(os, idx, from_hex[(int)tail[0]], &tail[1]);
351 if (node == NULL)
352 return GIT_ENOMEM;
353 }
354
355 if (node->children[c] == 0) {
356 if (push_leaf(os, idx, c, &text_oid[i + 1]) == NULL)
357 return GIT_ENOMEM;
358 break;
359 }
360
361 idx = node->children[c];
362 is_leaf = 0;
363
364 if (idx < 0) {
365 node->children[c] = idx = -idx;
366 is_leaf = 1;
367 }
368 }
369
370 if (++i > os->min_length)
371 os->min_length = i;
372
373 return os->min_length;
374 }
375