]>
git.proxmox.com Git - libgit2.git/blob - src/oid.c
2 * Copyright (C) 2009-2011 the libgit2 contributors
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.
10 #include "repository.h"
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 */
32 static char to_hex
[] = "0123456789abcdef";
34 int git_oid_fromstrn(git_oid
*out
, const char *str
, size_t length
)
40 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is too short");
42 if (length
> GIT_OID_HEXSZ
)
43 length
= GIT_OID_HEXSZ
;
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]];
50 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is not a valid sha1 hash");
52 out
->id
[p
/ 2] = (unsigned char)v
;
56 v
= (from_hex
[(unsigned char)str
[p
+ 0]] << 4);
58 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is not a valid sha1 hash");
60 out
->id
[p
/ 2] = (unsigned char)v
;
64 memset(out
->id
+ p
/ 2, 0, (GIT_OID_HEXSZ
- p
) / 2);
69 int git_oid_fromstr(git_oid
*out
, const char *str
)
71 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
74 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
76 *str
++ = to_hex
[val
>> 4];
77 *str
++ = to_hex
[val
& 0xf];
81 void git_oid_fmt(char *str
, const git_oid
*oid
)
85 for (i
= 0; i
< sizeof(oid
->id
); i
++)
86 str
= fmt_one(str
, oid
->id
[i
]);
89 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
93 str
= fmt_one(str
, oid
->id
[0]);
95 for (i
= 1; i
< sizeof(oid
->id
); i
++)
96 str
= fmt_one(str
, oid
->id
[i
]);
99 char *git_oid_allocfmt(const git_oid
*oid
)
101 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
104 git_oid_fmt(str
, oid
);
105 str
[GIT_OID_HEXSZ
] = '\0';
109 char *git_oid_to_string(char *out
, size_t n
, const git_oid
*oid
)
111 char str
[GIT_OID_HEXSZ
];
113 if (!out
|| n
== 0 || !oid
)
116 n
--; /* allow room for terminating NUL */
119 git_oid_fmt(str
, oid
);
120 if (n
> GIT_OID_HEXSZ
)
130 int git_oid__parse(git_oid
*oid
, const char **buffer_out
,
131 const char *buffer_end
, const char *header
)
133 const size_t sha_len
= GIT_OID_HEXSZ
;
134 const size_t header_len
= strlen(header
);
136 const char *buffer
= *buffer_out
;
138 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
139 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer too small");
141 if (memcmp(buffer
, header
, header_len
) != 0)
142 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer and header do not match");
144 if (buffer
[header_len
+ sha_len
] != '\n')
145 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer not terminated correctly");
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");
150 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
155 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
157 char hex_oid
[GIT_OID_HEXSZ
];
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');
165 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
167 memcpy(out
->id
, raw
, sizeof(out
->id
));
170 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
172 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
175 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
177 return memcmp(a
->id
, b
->id
, sizeof(a
->id
));
180 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, unsigned int len
)
182 const unsigned char *a
= oid_a
->id
;
183 const unsigned char *b
= oid_b
->id
;
194 if ((*a
^ *b
) & 0xf0)
200 typedef short node_index
;
204 node_index children
[16];
207 struct git_oid_shorten
{
209 size_t node_count
, size
;
210 int min_length
, full
;
213 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
215 self
->nodes
= realloc(self
->nodes
, new_size
* sizeof(trie_node
));
216 if (self
->nodes
== NULL
)
219 if (new_size
> self
->size
) {
220 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
223 self
->size
= new_size
;
227 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
229 trie_node
*node
, *leaf
;
232 if (os
->node_count
>= os
->size
) {
233 if (resize_trie(os
, os
->size
* 2) < GIT_SUCCESS
)
237 idx_leaf
= (node_index
)os
->node_count
++;
239 if (os
->node_count
== SHRT_MAX
)
242 node
= &os
->nodes
[idx
];
243 node
->children
[push_at
] = -idx_leaf
;
245 leaf
= &os
->nodes
[idx_leaf
];
251 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
255 os
= git__malloc(sizeof(git_oid_shorten
));
259 memset(os
, 0x0, sizeof(git_oid_shorten
));
261 if (resize_trie(os
, 16) < GIT_SUCCESS
) {
267 os
->min_length
= min_length
;
272 void git_oid_shorten_free(git_oid_shorten
*os
)
280 * What wizardry is this?
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
286 * Read more: http://en.wikipedia.org/wiki/Trie
288 * Magic that happens in this method:
290 * - Each node in the trie is an union, so it can work both as
291 * a normal node, or as a leaf.
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
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.
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.
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.
321 * This is a pretty good balance between performance and memory usage.
323 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
331 if (text_oid
== NULL
)
332 return os
->min_length
;
337 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
338 int c
= from_hex
[(int)text_oid
[i
]];
342 return git__throw(GIT_ENOTOID
, "Failed to shorten OID. Invalid hex value");
344 node
= &os
->nodes
[idx
];
352 node
= push_leaf(os
, idx
, from_hex
[(int)tail
[0]], &tail
[1]);
357 if (node
->children
[c
] == 0) {
358 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
)
363 idx
= node
->children
[c
];
367 node
->children
[c
] = idx
= -idx
;
372 if (++i
> os
->min_length
)
375 return os
->min_length
;