]>
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
)
38 if (length
> GIT_OID_HEXSZ
)
39 length
= GIT_OID_HEXSZ
;
44 for (p
= 0; p
< length
; p
+= 2) {
45 int v
= (from_hex
[(unsigned char)str
[p
+ 0]] << 4)
46 | from_hex
[(unsigned char)str
[p
+ 1]];
49 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is not a valid sha1 hash");
51 out
->id
[p
/ 2] = (unsigned char)v
;
54 for (; p
< GIT_OID_HEXSZ
; p
+= 2)
60 int git_oid_fromstr(git_oid
*out
, const char *str
)
62 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
65 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
67 *str
++ = to_hex
[val
>> 4];
68 *str
++ = to_hex
[val
& 0xf];
72 void git_oid_fmt(char *str
, const git_oid
*oid
)
76 for (i
= 0; i
< sizeof(oid
->id
); i
++)
77 str
= fmt_one(str
, oid
->id
[i
]);
80 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
84 str
= fmt_one(str
, oid
->id
[0]);
86 for (i
= 1; i
< sizeof(oid
->id
); i
++)
87 str
= fmt_one(str
, oid
->id
[i
]);
90 char *git_oid_allocfmt(const git_oid
*oid
)
92 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
95 git_oid_fmt(str
, oid
);
96 str
[GIT_OID_HEXSZ
] = '\0';
100 char *git_oid_to_string(char *out
, size_t n
, const git_oid
*oid
)
102 char str
[GIT_OID_HEXSZ
];
104 if (!out
|| n
== 0 || !oid
)
107 n
--; /* allow room for terminating NUL */
110 git_oid_fmt(str
, oid
);
111 if (n
> GIT_OID_HEXSZ
)
121 int git_oid__parse(git_oid
*oid
, const char **buffer_out
,
122 const char *buffer_end
, const char *header
)
124 const size_t sha_len
= GIT_OID_HEXSZ
;
125 const size_t header_len
= strlen(header
);
127 const char *buffer
= *buffer_out
;
129 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
130 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer too small");
132 if (memcmp(buffer
, header
, header_len
) != 0)
133 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer and header do not match");
135 if (buffer
[header_len
+ sha_len
] != '\n')
136 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Buffer not terminated correctly");
138 if (git_oid_fromstr(oid
, buffer
+ header_len
) < GIT_SUCCESS
)
139 return git__throw(GIT_EOBJCORRUPTED
, "Failed to parse OID. Failed to generate sha1");
141 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
146 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
148 char hex_oid
[GIT_OID_HEXSZ
];
150 git_oid_fmt(hex_oid
, oid
);
151 git_buf_puts(buf
, header
);
152 git_buf_put(buf
, hex_oid
, GIT_OID_HEXSZ
);
153 git_buf_putc(buf
, '\n');
156 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
158 memcpy(out
->id
, raw
, sizeof(out
->id
));
161 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
163 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
166 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
168 return memcmp(a
->id
, b
->id
, sizeof(a
->id
));
171 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, unsigned int len
)
173 const unsigned char *a
= oid_a
->id
;
174 const unsigned char *b
= oid_b
->id
;
185 if ((*a
^ *b
) & 0xf0)
191 typedef short node_index
;
195 node_index children
[16];
198 struct git_oid_shorten
{
200 size_t node_count
, size
;
201 int min_length
, full
;
204 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
206 self
->nodes
= realloc(self
->nodes
, new_size
* sizeof(trie_node
));
207 if (self
->nodes
== NULL
)
210 if (new_size
> self
->size
) {
211 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
214 self
->size
= new_size
;
218 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
220 trie_node
*node
, *leaf
;
223 if (os
->node_count
>= os
->size
) {
224 if (resize_trie(os
, os
->size
* 2) < GIT_SUCCESS
)
228 idx_leaf
= (node_index
)os
->node_count
++;
230 if (os
->node_count
== SHRT_MAX
)
233 node
= &os
->nodes
[idx
];
234 node
->children
[push_at
] = -idx_leaf
;
236 leaf
= &os
->nodes
[idx_leaf
];
242 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
246 os
= git__malloc(sizeof(git_oid_shorten
));
250 memset(os
, 0x0, sizeof(git_oid_shorten
));
252 if (resize_trie(os
, 16) < GIT_SUCCESS
) {
258 os
->min_length
= min_length
;
263 void git_oid_shorten_free(git_oid_shorten
*os
)
271 * What wizardry is this?
273 * This is just a memory-optimized trie: basically a very fancy
274 * 16-ary tree, which is used to store the prefixes of the OID
277 * Read more: http://en.wikipedia.org/wiki/Trie
279 * Magic that happens in this method:
281 * - Each node in the trie is an union, so it can work both as
282 * a normal node, or as a leaf.
284 * - Each normal node points to 16 children (one for each possible
285 * character in the oid). This is *not* stored in an array of
286 * pointers, because in a 64-bit arch this would be sucking
287 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
288 * insane. What we do is store Node Indexes, and use these indexes
289 * to look up each node in the om->index array. These indexes are
290 * signed shorts, so this limits the amount of unique OIDs that
291 * fit in the structure to about 20000 (assuming a more or less uniform
294 * - All the nodes in om->index array are stored contiguously in
295 * memory, and each of them is 32 bytes, so we fit 2x nodes per
296 * cache line. Convenient for speed.
298 * - To differentiate the leafs from the normal nodes, we store all
299 * the indexes towards a leaf as a negative index (indexes to normal
300 * nodes are positives). When we find that one of the children for
301 * a node has a negative value, that means it's going to be a leaf.
302 * This reduces the amount of indexes we have by two, but also reduces
303 * the size of each node by 1-4 bytes (the amount we would need to
304 * add a `is_leaf` field): this is good because it allows the nodes
305 * to fit cleanly in cache lines.
307 * - Once we reach an empty children, instead of continuing to insert
308 * new nodes for each remaining character of the OID, we store a pointer
309 * to the tail in the leaf; if the leaf is reached again, we turn it
310 * into a normal node and use the tail to create a new leaf.
312 * This is a pretty good balance between performance and memory usage.
314 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
322 if (text_oid
== NULL
)
323 return os
->min_length
;
328 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
329 int c
= from_hex
[(int)text_oid
[i
]];
333 return git__throw(GIT_ENOTOID
, "Failed to shorten OID. Invalid hex value");
335 node
= &os
->nodes
[idx
];
343 node
= push_leaf(os
, idx
, from_hex
[(int)tail
[0]], &tail
[1]);
348 if (node
->children
[c
] == 0) {
349 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
)
354 idx
= node
->children
[c
];
358 node
->children
[c
] = idx
= -idx
;
363 if (++i
> os
->min_length
)
366 return os
->min_length
;