]>
git.proxmox.com Git - libgit2.git/blob - src/oid.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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 char to_hex
[] = "0123456789abcdef";
16 static int oid_error_invalid(const char *msg
)
18 giterr_set(GITERR_INVALID
, "Unable to parse OID - %s", msg
);
22 int git_oid_fromstrn(git_oid
*out
, const char *str
, size_t length
)
30 return oid_error_invalid("too short");
32 if (length
> GIT_OID_HEXSZ
)
33 return oid_error_invalid("too long");
35 memset(out
->id
, 0, GIT_OID_RAWSZ
);
37 for (p
= 0; p
< length
; p
++) {
38 v
= git__fromhex(str
[p
]);
40 return oid_error_invalid("contains invalid characters");
42 out
->id
[p
/ 2] |= (unsigned char)(v
<< (p
% 2 ? 0 : 4));
48 int git_oid_fromstrp(git_oid
*out
, const char *str
)
50 return git_oid_fromstrn(out
, str
, strlen(str
));
53 int git_oid_fromstr(git_oid
*out
, const char *str
)
55 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
58 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
60 *str
++ = to_hex
[val
>> 4];
61 *str
++ = to_hex
[val
& 0xf];
65 void git_oid_nfmt(char *str
, size_t n
, const git_oid
*oid
)
73 if (n
> GIT_OID_HEXSZ
) {
74 memset(&str
[GIT_OID_HEXSZ
], 0, n
- GIT_OID_HEXSZ
);
80 for (i
= 0; i
< max_i
; i
++)
81 str
= fmt_one(str
, oid
->id
[i
]);
84 *str
++ = to_hex
[oid
->id
[i
] >> 4];
87 void git_oid_fmt(char *str
, const git_oid
*oid
)
89 git_oid_nfmt(str
, GIT_OID_HEXSZ
, oid
);
92 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
96 str
= fmt_one(str
, oid
->id
[0]);
98 for (i
= 1; i
< sizeof(oid
->id
); i
++)
99 str
= fmt_one(str
, oid
->id
[i
]);
102 char *git_oid_allocfmt(const git_oid
*oid
)
104 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
107 git_oid_nfmt(str
, GIT_OID_HEXSZ
+ 1, oid
);
111 char *git_oid_tostr(char *out
, size_t n
, const git_oid
*oid
)
116 if (n
> GIT_OID_HEXSZ
+ 1)
117 n
= GIT_OID_HEXSZ
+ 1;
119 git_oid_nfmt(out
, n
- 1, oid
); /* allow room for terminating NUL */
126 git_oid
*oid
, const char **buffer_out
,
127 const char *buffer_end
, const char *header
)
129 const size_t sha_len
= GIT_OID_HEXSZ
;
130 const size_t header_len
= strlen(header
);
132 const char *buffer
= *buffer_out
;
134 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
137 if (memcmp(buffer
, header
, header_len
) != 0)
140 if (buffer
[header_len
+ sha_len
] != '\n')
143 if (git_oid_fromstr(oid
, buffer
+ header_len
) < 0)
146 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
151 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
153 char hex_oid
[GIT_OID_HEXSZ
];
155 git_oid_fmt(hex_oid
, oid
);
156 git_buf_puts(buf
, header
);
157 git_buf_put(buf
, hex_oid
, GIT_OID_HEXSZ
);
158 git_buf_putc(buf
, '\n');
161 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
163 memcpy(out
->id
, raw
, sizeof(out
->id
));
166 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
168 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
171 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
173 return git_oid__cmp(a
, b
);
176 int git_oid_equal(const git_oid
*a
, const git_oid
*b
)
178 return (git_oid__cmp(a
, b
) == 0);
181 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, size_t len
)
183 const unsigned char *a
= oid_a
->id
;
184 const unsigned char *b
= oid_b
->id
;
186 if (len
> GIT_OID_HEXSZ
)
198 if ((*a
^ *b
) & 0xf0)
204 int git_oid_strcmp(const git_oid
*oid_a
, const char *str
)
206 const unsigned char *a
= oid_a
->id
;
207 unsigned char strval
;
210 for (a
= oid_a
->id
; *str
&& (a
- oid_a
->id
) < GIT_OID_RAWSZ
; ++a
) {
211 if ((hexval
= git__fromhex(*str
++)) < 0)
213 strval
= (unsigned char)(hexval
<< 4);
215 if ((hexval
= git__fromhex(*str
++)) < 0)
220 return (*a
- strval
);
226 int git_oid_streq(const git_oid
*oid_a
, const char *str
)
228 return git_oid_strcmp(oid_a
, str
) == 0 ? 0 : -1;
231 int git_oid_iszero(const git_oid
*oid_a
)
233 const unsigned char *a
= oid_a
->id
;
235 for (i
= 0; i
< GIT_OID_RAWSZ
; ++i
, ++a
)
241 typedef short node_index
;
245 node_index children
[16];
248 struct git_oid_shorten
{
250 size_t node_count
, size
;
251 int min_length
, full
;
254 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
256 self
->nodes
= git__realloc(self
->nodes
, new_size
* sizeof(trie_node
));
257 GITERR_CHECK_ALLOC(self
->nodes
);
259 if (new_size
> self
->size
) {
260 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
263 self
->size
= new_size
;
267 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
269 trie_node
*node
, *leaf
;
272 if (os
->node_count
>= os
->size
) {
273 if (resize_trie(os
, os
->size
* 2) < 0)
277 idx_leaf
= (node_index
)os
->node_count
++;
279 if (os
->node_count
== SHRT_MAX
) {
284 node
= &os
->nodes
[idx
];
285 node
->children
[push_at
] = -idx_leaf
;
287 leaf
= &os
->nodes
[idx_leaf
];
293 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
297 assert((size_t)((int)min_length
) == min_length
);
299 os
= git__calloc(1, sizeof(git_oid_shorten
));
303 if (resize_trie(os
, 16) < 0) {
309 os
->min_length
= (int)min_length
;
314 void git_oid_shorten_free(git_oid_shorten
*os
)
319 git__free(os
->nodes
);
325 * What wizardry is this?
327 * This is just a memory-optimized trie: basically a very fancy
328 * 16-ary tree, which is used to store the prefixes of the OID
331 * Read more: http://en.wikipedia.org/wiki/Trie
333 * Magic that happens in this method:
335 * - Each node in the trie is an union, so it can work both as
336 * a normal node, or as a leaf.
338 * - Each normal node points to 16 children (one for each possible
339 * character in the oid). This is *not* stored in an array of
340 * pointers, because in a 64-bit arch this would be sucking
341 * 16*sizeof(void*) = 128 bytes of memory per node, which is
342 * insane. What we do is store Node Indexes, and use these indexes
343 * to look up each node in the om->index array. These indexes are
344 * signed shorts, so this limits the amount of unique OIDs that
345 * fit in the structure to about 20000 (assuming a more or less uniform
348 * - All the nodes in om->index array are stored contiguously in
349 * memory, and each of them is 32 bytes, so we fit 2x nodes per
350 * cache line. Convenient for speed.
352 * - To differentiate the leafs from the normal nodes, we store all
353 * the indexes towards a leaf as a negative index (indexes to normal
354 * nodes are positives). When we find that one of the children for
355 * a node has a negative value, that means it's going to be a leaf.
356 * This reduces the amount of indexes we have by two, but also reduces
357 * the size of each node by 1-4 bytes (the amount we would need to
358 * add a `is_leaf` field): this is good because it allows the nodes
359 * to fit cleanly in cache lines.
361 * - Once we reach an empty children, instead of continuing to insert
362 * new nodes for each remaining character of the OID, we store a pointer
363 * to the tail in the leaf; if the leaf is reached again, we turn it
364 * into a normal node and use the tail to create a new leaf.
366 * This is a pretty good balance between performance and memory usage.
368 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
375 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
379 if (text_oid
== NULL
)
380 return os
->min_length
;
385 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
386 int c
= git__fromhex(text_oid
[i
]);
390 giterr_set(GITERR_INVALID
, "Unable to shorten OID - invalid hex value");
394 node
= &os
->nodes
[idx
];
402 node
= push_leaf(os
, idx
, git__fromhex(tail
[0]), &tail
[1]);
405 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
410 if (node
->children
[c
] == 0) {
411 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
) {
413 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
419 idx
= node
->children
[c
];
423 node
->children
[c
] = idx
= -idx
;
428 if (++i
> os
->min_length
)
431 return os
->min_length
;