]>
git.proxmox.com Git - libgit2.git/blob - src/oid.c
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
28 #include "repository.h"
32 static signed char from_hex
[] = {
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 00 */
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10 */
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20 */
36 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 30 */
37 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 40 */
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 50 */
39 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 60 */
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 70 */
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 90 */
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a0 */
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* b0 */
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* c0 */
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* d0 */
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* e0 */
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* f0 */
50 static char to_hex
[] = "0123456789abcdef";
52 int git_oid_mkstr(git_oid
*out
, const char *str
)
55 for (p
= 0; p
< sizeof(out
->id
); p
++, str
+= 2) {
56 int v
= (from_hex
[(unsigned char)str
[0]] << 4)
57 | from_hex
[(unsigned char)str
[1]];
59 return git__throw(GIT_ENOTOID
, "Failed to generate sha1. Given string is not a valid sha1 hash");
60 out
->id
[p
] = (unsigned char)v
;
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__parse_oid(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_mkstr(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 int git__write_oid(git_odb_stream
*stream
, const char *header
, const git_oid
*oid
)
150 git_oid_fmt(hex_oid
+ 1, oid
);
155 stream
->write(stream
, header
, strlen(header
));
156 stream
->write(stream
, hex_oid
, 42);
160 void git_oid_mkraw(git_oid
*out
, const unsigned char *raw
)
162 memcpy(out
->id
, raw
, sizeof(out
->id
));
165 void git_oid_cpy(git_oid
*out
, const git_oid
*src
)
167 memcpy(out
->id
, src
->id
, sizeof(out
->id
));
170 int git_oid_cmp(const git_oid
*a
, const git_oid
*b
)
172 return memcmp(a
->id
, b
->id
, sizeof(a
->id
));
176 typedef short node_index
;
180 node_index children
[16];
183 struct git_oid_shorten
{
185 size_t node_count
, size
;
186 int min_length
, full
;
189 static int resize_trie(git_oid_shorten
*self
, size_t new_size
)
191 self
->nodes
= realloc(self
->nodes
, new_size
* sizeof(trie_node
));
192 if (self
->nodes
== NULL
)
195 if (new_size
> self
->size
) {
196 memset(&self
->nodes
[self
->size
], 0x0, (new_size
- self
->size
) * sizeof(trie_node
));
199 self
->size
= new_size
;
203 static trie_node
*push_leaf(git_oid_shorten
*os
, node_index idx
, int push_at
, const char *oid
)
205 trie_node
*node
, *leaf
;
208 if (os
->node_count
>= os
->size
) {
209 if (resize_trie(os
, os
->size
* 2) < GIT_SUCCESS
)
213 idx_leaf
= (node_index
)os
->node_count
++;
215 if (os
->node_count
== SHRT_MAX
)
218 node
= &os
->nodes
[idx
];
219 node
->children
[push_at
] = -idx_leaf
;
221 leaf
= &os
->nodes
[idx_leaf
];
227 git_oid_shorten
*git_oid_shorten_new(size_t min_length
)
231 os
= git__malloc(sizeof(git_oid_shorten
));
235 memset(os
, 0x0, sizeof(git_oid_shorten
));
237 if (resize_trie(os
, 16) < GIT_SUCCESS
) {
243 os
->min_length
= min_length
;
248 void git_oid_shorten_free(git_oid_shorten
*os
)
256 * What wizardry is this?
258 * This is just a memory-optimized trie: basically a very fancy
259 * 16-ary tree, which is used to store the prefixes of the OID
262 * Read more: http://en.wikipedia.org/wiki/Trie
264 * Magic that happens in this method:
266 * - Each node in the trie is an union, so it can work both as
267 * a normal node, or as a leaf.
269 * - Each normal node points to 16 children (one for each possible
270 * character in the oid). This is *not* stored in an array of
271 * pointers, because in a 64-bit arch this would be sucking
272 * 16*sizeof(void*) = 128 bytes of memory per node, which is fucking
273 * insane. What we do is store Node Indexes, and use these indexes
274 * to look up each node in the om->index array. These indexes are
275 * signed shorts, so this limits the amount of unique OIDs that
276 * fit in the structure to about 20000 (assuming a more or less uniform
279 * - All the nodes in om->index array are stored contiguously in
280 * memory, and each of them is 32 bytes, so we fit 2x nodes per
281 * cache line. Convenient for speed.
283 * - To differentiate the leafs from the normal nodes, we store all
284 * the indexes towards a leaf as a negative index (indexes to normal
285 * nodes are positives). When we find that one of the children for
286 * a node has a negative value, that means it's going to be a leaf.
287 * This reduces the amount of indexes we have by two, but also reduces
288 * the size of each node by 1-4 bytes (the amount we would need to
289 * add a `is_leaf` field): this is good because it allows the nodes
290 * to fit cleanly in cache lines.
292 * - Once we reach an empty children, instead of continuing to insert
293 * new nodes for each remaining character of the OID, we store a pointer
294 * to the tail in the leaf; if the leaf is reached again, we turn it
295 * into a normal node and use the tail to create a new leaf.
297 * This is a pretty good balance between performance and memory usage.
299 int git_oid_shorten_add(git_oid_shorten
*os
, const char *text_oid
)
310 for (i
= 0; i
< GIT_OID_HEXSZ
; ++i
) {
311 int c
= from_hex
[(int)text_oid
[i
]];
315 return git__throw(GIT_ENOTOID
, "Failed to shorten OID. Invalid hex value");
317 node
= &os
->nodes
[idx
];
325 node
= push_leaf(os
, idx
, from_hex
[(int)tail
[0]], &tail
[1]);
330 if (node
->children
[c
] == 0) {
331 if (push_leaf(os
, idx
, c
, &text_oid
[i
+ 1]) == NULL
)
336 idx
= node
->children
[c
];
340 node
->children
[c
] = idx
= -idx
;
345 if (++i
> os
->min_length
)
348 return os
->min_length
;