]>
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"
15 static char to_hex
[] = "0123456789abcdef";
17 static int oid_error_invalid(const char *msg
)
19 giterr_set(GITERR_INVALID
, "Unable to parse OID - %s", msg
);
23 int git_oid_fromstrn(git_oid
*out
, const char *str
, size_t length
)
31 return oid_error_invalid("too short");
33 if (length
> GIT_OID_HEXSZ
)
34 return oid_error_invalid("too long");
36 memset(out
->id
, 0, GIT_OID_RAWSZ
);
38 for (p
= 0; p
< length
; p
++) {
39 v
= git__fromhex(str
[p
]);
41 return oid_error_invalid("contains invalid characters");
43 out
->id
[p
/ 2] |= (unsigned char)(v
<< (p
% 2 ? 0 : 4));
49 int git_oid_fromstrp(git_oid
*out
, const char *str
)
51 return git_oid_fromstrn(out
, str
, strlen(str
));
54 int git_oid_fromstr(git_oid
*out
, const char *str
)
56 return git_oid_fromstrn(out
, str
, GIT_OID_HEXSZ
);
59 GIT_INLINE(char) *fmt_one(char *str
, unsigned int val
)
61 *str
++ = to_hex
[val
>> 4];
62 *str
++ = to_hex
[val
& 0xf];
66 void git_oid_nfmt(char *str
, size_t n
, const git_oid
*oid
)
74 if (n
> GIT_OID_HEXSZ
) {
75 memset(&str
[GIT_OID_HEXSZ
], 0, n
- GIT_OID_HEXSZ
);
81 for (i
= 0; i
< max_i
; i
++)
82 str
= fmt_one(str
, oid
->id
[i
]);
85 *str
++ = to_hex
[oid
->id
[i
] >> 4];
88 void git_oid_fmt(char *str
, const git_oid
*oid
)
90 git_oid_nfmt(str
, GIT_OID_HEXSZ
, oid
);
93 void git_oid_pathfmt(char *str
, const git_oid
*oid
)
97 str
= fmt_one(str
, oid
->id
[0]);
99 for (i
= 1; i
< sizeof(oid
->id
); i
++)
100 str
= fmt_one(str
, oid
->id
[i
]);
103 char *git_oid_tostr_s(const git_oid
*oid
)
105 char *str
= GIT_GLOBAL
->oid_fmt
;
106 git_oid_nfmt(str
, GIT_OID_HEXSZ
+ 1, oid
);
110 char *git_oid_allocfmt(const git_oid
*oid
)
112 char *str
= git__malloc(GIT_OID_HEXSZ
+ 1);
115 git_oid_nfmt(str
, GIT_OID_HEXSZ
+ 1, oid
);
119 char *git_oid_tostr(char *out
, size_t n
, const git_oid
*oid
)
124 if (n
> GIT_OID_HEXSZ
+ 1)
125 n
= GIT_OID_HEXSZ
+ 1;
127 git_oid_nfmt(out
, n
- 1, oid
); /* allow room for terminating NUL */
134 git_oid
*oid
, const char **buffer_out
,
135 const char *buffer_end
, const char *header
)
137 const size_t sha_len
= GIT_OID_HEXSZ
;
138 const size_t header_len
= strlen(header
);
140 const char *buffer
= *buffer_out
;
142 if (buffer
+ (header_len
+ sha_len
+ 1) > buffer_end
)
145 if (memcmp(buffer
, header
, header_len
) != 0)
148 if (buffer
[header_len
+ sha_len
] != '\n')
151 if (git_oid_fromstr(oid
, buffer
+ header_len
) < 0)
154 *buffer_out
= buffer
+ (header_len
+ sha_len
+ 1);
159 void git_oid__writebuf(git_buf
*buf
, const char *header
, const git_oid
*oid
)
161 char hex_oid
[GIT_OID_HEXSZ
];
163 git_oid_fmt(hex_oid
, oid
);
164 git_buf_puts(buf
, header
);
165 git_buf_put(buf
, hex_oid
, GIT_OID_HEXSZ
);
166 git_buf_putc(buf
, '\n');
169 void git_oid_fromraw(git_oid
*out
, const unsigned char *raw
)
171 memcpy(out
->id
, raw
, sizeof(out
->id
));
174 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
176 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
179 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
181 return git_oid__cmp(a
, b
);
184 int git_oid_equal(const git_oid
*a
, const git_oid
*b
)
186 return (git_oid__cmp(a
, b
) == 0);
189 int git_oid_ncmp(const git_oid
*oid_a
, const git_oid
*oid_b
, size_t len
)
191 const unsigned char *a
= oid_a
->id
;
192 const unsigned char *b
= oid_b
->id
;
194 if (len
> GIT_OID_HEXSZ
)
206 if ((*a
^ *b
) & 0xf0)
212 int git_oid_strcmp(const git_oid
*oid_a
, const char *str
)
214 const unsigned char *a
;
215 unsigned char strval
;
218 for (a
= oid_a
->id
; *str
&& (a
- oid_a
->id
) < GIT_OID_RAWSZ
; ++a
) {
219 if ((hexval
= git__fromhex(*str
++)) < 0)
221 strval
= (unsigned char)(hexval
<< 4);
223 if ((hexval
= git__fromhex(*str
++)) < 0)
228 return (*a
- strval
);
234 int git_oid_streq(const git_oid
*oid_a
, const char *str
)
236 return git_oid_strcmp(oid_a
, str
) == 0 ? 0 : -1;
239 int git_oid_iszero(const git_oid
*oid_a
)
241 const unsigned char *a
= oid_a
->id
;
243 for (i
= 0; i
< GIT_OID_RAWSZ
; ++i
, ++a
)
249 typedef short node_index
;
253 node_index children
[16];
256 struct git_oid_shorten
{
258 size_t node_count
, size
;
259 int min_length
, full
;
262 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
264 self
->nodes
= git__reallocarray(self
->nodes
, new_size
, sizeof(trie_node
));
265 GITERR_CHECK_ALLOC(self
->nodes
);
267 if (new_size
> self
->size
) {
268 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
271 self
->size
= new_size
;
275 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
277 trie_node
*node
, *leaf
;
280 if (os
->node_count
>= os
->size
) {
281 if (resize_trie(os
, os
->size
* 2) < 0)
285 idx_leaf
= (node_index
)os
->node_count
++;
287 if (os
->node_count
== SHRT_MAX
) {
292 node
= &os
->nodes
[idx
];
293 node
->children
[push_at
] = -idx_leaf
;
295 leaf
= &os
->nodes
[idx_leaf
];
301 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
305 assert((size_t)((int)min_length
) == min_length
);
307 os
= git__calloc(1, sizeof(git_oid_shorten
));
311 if (resize_trie(os
, 16) < 0) {
317 os
->min_length
= (int)min_length
;
322 void git_oid_shorten_free(git_oid_shorten
*os
)
327 git__free(os
->nodes
);
333 * What wizardry is this?
335 * This is just a memory-optimized trie: basically a very fancy
336 * 16-ary tree, which is used to store the prefixes of the OID
339 * Read more: http://en.wikipedia.org/wiki/Trie
341 * Magic that happens in this method:
343 * - Each node in the trie is an union, so it can work both as
344 * a normal node, or as a leaf.
346 * - Each normal node points to 16 children (one for each possible
347 * character in the oid). This is *not* stored in an array of
348 * pointers, because in a 64-bit arch this would be sucking
349 * 16*sizeof(void*) = 128 bytes of memory per node, which is
350 * insane. What we do is store Node Indexes, and use these indexes
351 * to look up each node in the om->index array. These indexes are
352 * signed shorts, so this limits the amount of unique OIDs that
353 * fit in the structure to about 20000 (assuming a more or less uniform
356 * - All the nodes in om->index array are stored contiguously in
357 * memory, and each of them is 32 bytes, so we fit 2x nodes per
358 * cache line. Convenient for speed.
360 * - To differentiate the leafs from the normal nodes, we store all
361 * the indexes towards a leaf as a negative index (indexes to normal
362 * nodes are positives). When we find that one of the children for
363 * a node has a negative value, that means it's going to be a leaf.
364 * This reduces the amount of indexes we have by two, but also reduces
365 * the size of each node by 1-4 bytes (the amount we would need to
366 * add a `is_leaf` field): this is good because it allows the nodes
367 * to fit cleanly in cache lines.
369 * - Once we reach an empty children, instead of continuing to insert
370 * new nodes for each remaining character of the OID, we store a pointer
371 * to the tail in the leaf; if the leaf is reached again, we turn it
372 * into a normal node and use the tail to create a new leaf.
374 * This is a pretty good balance between performance and memory usage.
376 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
383 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
387 if (text_oid
== NULL
)
388 return os
->min_length
;
393 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
394 int c
= git__fromhex(text_oid
[i
]);
398 giterr_set(GITERR_INVALID
, "Unable to shorten OID - invalid hex value");
402 node
= &os
->nodes
[idx
];
410 node
= push_leaf(os
, idx
, git__fromhex(tail
[0]), &tail
[1]);
413 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
418 if (node
->children
[c
] == 0) {
419 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
) {
421 giterr_set(GITERR_INVALID
, "Unable to shorten OID - OID set full");
427 idx
= node
->children
[c
];
431 node
->children
[c
] = idx
= -idx
;
436 if (++i
> os
->min_length
)
439 return os
->min_length
;