]>
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
)
39 if (length
> GIT_OID_HEXSZ
)
40 length
= GIT_OID_HEXSZ
;
42 for (p
= 0; p
< length
- 1; p
+= 2) {
43 v
= (from_hex
[(unsigned char)str
[p
+ 0]] << 4)
44 | from_hex
[(unsigned char)str
[p
+ 1]];
47 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is not a valid sha1 hash");
49 out
->id
[p
/ 2] = (unsigned char)v
;
53 v
= (from_hex
[(unsigned char)str
[p
+ 0]] << 4);
54 out
->id
[p
/ 2] = (unsigned char)v
;
58 memset(out
->id
+ p
/ 2, 0, (GIT_OID_HEXSZ
- p
) / 2);
63 int git_oid_fromstr(git_oid
*out
, const char *str
)
65 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
68 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
70 *str
++ = to_hex
[val
>> 4];
71 *str
++ = to_hex
[val
& 0xf];
75 void git_oid_fmt(char *str
, const git_oid
*oid
)
79 for (i
= 0; i
< sizeof(oid
->id
); i
++)
80 str
= fmt_one(str
, oid
->id
[i
]);
83 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
87 str
= fmt_one(str
, oid
->id
[0]);
89 for (i
= 1; i
< sizeof(oid
->id
); i
++)
90 str
= fmt_one(str
, oid
->id
[i
]);
93 char *git_oid_allocfmt(const git_oid
*oid
)
95 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
98 git_oid_fmt(str
, oid
);
99 str
[GIT_OID_HEXSZ
] = '\0';
103 char *git_oid_to_string(char *out
, size_t n
, const git_oid
*oid
)
105 char str
[GIT_OID_HEXSZ
];
107 if (!out
|| n
== 0 || !oid
)
110 n
--; /* allow room for terminating NUL */
113 git_oid_fmt(str
, oid
);
114 if (n
> GIT_OID_HEXSZ
)
124 int git_oid__parse(git_oid
*oid
, const char **buffer_out
,
125 const char *buffer_end
, const char *header
)
127 const size_t sha_len
= GIT_OID_HEXSZ
;
128 const size_t header_len
= strlen(header
);
130 const char *buffer
= *buffer_out
;
132 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
133 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer too small");
135 if (memcmp(buffer
, header
, header_len
) != 0)
136 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer and header do not match");
138 if (buffer
[header_len
+ sha_len
] != '\n')
139 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer not terminated correctly");
141 if (git_oid_fromstr(oid
, buffer
+ header_len
) < GIT_SUCCESS
)
142 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Failed to generate sha1");
144 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
149 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
151 char hex_oid
[GIT_OID_HEXSZ
];
153 git_oid_fmt(hex_oid
, oid
);
154 git_buf_puts(buf
, header
);
155 git_buf_put(buf
, hex_oid
, GIT_OID_HEXSZ
);
156 git_buf_putc(buf
, '\n');
159 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
161 memcpy(out
->id
, raw
, sizeof(out
->id
));
164 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
166 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
169 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
171 return memcmp(a
->id
, b
->id
, sizeof(a
->id
));
174 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, unsigned int len
)
176 const unsigned char *a
= oid_a
->id
;
177 const unsigned char *b
= oid_b
->id
;
188 if ((*a
^ *b
) & 0xf0)
194 typedef short node_index
;
198 node_index children
[16];
201 struct git_oid_shorten
{
203 size_t node_count
, size
;
204 int min_length
, full
;
207 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
209 self
->nodes
= realloc(self
->nodes
, new_size
* sizeof(trie_node
));
210 if (self
->nodes
== NULL
)
213 if (new_size
> self
->size
) {
214 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
217 self
->size
= new_size
;
221 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
223 trie_node
*node
, *leaf
;
226 if (os
->node_count
>= os
->size
) {
227 if (resize_trie(os
, os
->size
* 2) < GIT_SUCCESS
)
231 idx_leaf
= (node_index
)os
->node_count
++;
233 if (os
->node_count
== SHRT_MAX
)
236 node
= &os
->nodes
[idx
];
237 node
->children
[push_at
] = -idx_leaf
;
239 leaf
= &os
->nodes
[idx_leaf
];
245 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
249 os
= git__malloc(sizeof(git_oid_shorten
));
253 memset(os
, 0x0, sizeof(git_oid_shorten
));
255 if (resize_trie(os
, 16) < GIT_SUCCESS
) {
261 os
->min_length
= min_length
;
266 void git_oid_shorten_free(git_oid_shorten
*os
)
274 * What wizardry is this?
276 * This is just a memory-optimized trie: basically a very fancy
277 * 16-ary tree, which is used to store the prefixes of the OID
280 * Read more: http://en.wikipedia.org/wiki/Trie
282 * Magic that happens in this method:
284 * - Each node in the trie is an union, so it can work both as
285 * a normal node, or as a leaf.
287 * - Each normal node points to 16 children (one for each possible
288 * character in the oid). This is *not* stored in an array of
289 * pointers, because in a 64-bit arch this would be sucking
290 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
291 * insane. What we do is store Node Indexes, and use these indexes
292 * to look up each node in the om->index array. These indexes are
293 * signed shorts, so this limits the amount of unique OIDs that
294 * fit in the structure to about 20000 (assuming a more or less uniform
297 * - All the nodes in om->index array are stored contiguously in
298 * memory, and each of them is 32 bytes, so we fit 2x nodes per
299 * cache line. Convenient for speed.
301 * - To differentiate the leafs from the normal nodes, we store all
302 * the indexes towards a leaf as a negative index (indexes to normal
303 * nodes are positives). When we find that one of the children for
304 * a node has a negative value, that means it's going to be a leaf.
305 * This reduces the amount of indexes we have by two, but also reduces
306 * the size of each node by 1-4 bytes (the amount we would need to
307 * add a `is_leaf` field): this is good because it allows the nodes
308 * to fit cleanly in cache lines.
310 * - Once we reach an empty children, instead of continuing to insert
311 * new nodes for each remaining character of the OID, we store a pointer
312 * to the tail in the leaf; if the leaf is reached again, we turn it
313 * into a normal node and use the tail to create a new leaf.
315 * This is a pretty good balance between performance and memory usage.
317 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
325 if (text_oid
== NULL
)
326 return os
->min_length
;
331 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
332 int c
= from_hex
[(int)text_oid
[i
]];
336 return git__throw(GIT_ENOTOID
, "Failed to shorten OID. Invalid hex value");
338 node
= &os
->nodes
[idx
];
346 node
= push_leaf(os
, idx
, from_hex
[(int)tail
[0]], &tail
[1]);
351 if (node
->children
[c
] == 0) {
352 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
)
357 idx
= node
->children
[c
];
361 node
->children
[c
] = idx
= -idx
;
366 if (++i
> os
->min_length
)
369 return os
->min_length
;