]> git.ipfire.org Git - thirdparty/git.git/blame - hex.h
reftable/block: reuse uncompressed blocks
[thirdparty/git.git] / hex.h
CommitLineData
b73ecb48
EN
1#ifndef HEX_H
2#define HEX_H
3
d1cbe1e6 4#include "hash-ll.h"
d88e8106 5#include "hex-ll.h"
b73ecb48
EN
6
7/*
08e5fb12
JH
8 * Try to read a hash (specified by the_hash_algo) in hexadecimal
9 * format from the 40 (or whatever length the hash algorithm uses)
10 * characters starting at hex. Write the 20-byte (or the length of
11 * the hash) result to hash in binary form.
b73ecb48
EN
12 * Return 0 on success. Reading stops if a NUL is encountered in the
13 * input, so it is safe to pass this function an arbitrary
14 * null-terminated string.
15 */
08e5fb12
JH
16int get_hash_hex(const char *hex, unsigned char *hash);
17int get_oid_hex(const char *hex, struct object_id *oid);
b73ecb48
EN
18
19/* Like get_oid_hex, but for an arbitrary hash algorithm. */
20int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
21
b73ecb48
EN
22/*
23 * Convert a binary hash in "unsigned char []" or an object name in
24 * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant,
25 * and writes the NUL-terminated output to the buffer `out`, which must be at
26 * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
27 * convenience.
28 *
29 * The non-`_r` variant returns a static buffer, but uses a ring of 4
30 * buffers, making it safe to make multiple calls for a single statement, like:
31 *
32 * printf("%s -> %s", hash_to_hex(one), hash_to_hex(two));
33 * printf("%s -> %s", oid_to_hex(one), oid_to_hex(two));
34 */
35char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
36char *oid_to_hex_r(char *out, const struct object_id *oid);
37char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
38char *hash_to_hex(const unsigned char *hash); /* same static buffer */
39char *oid_to_hex(const struct object_id *oid); /* same static buffer */
40
41/*
42 * Parse a 40-character hexadecimal object ID starting from hex, updating the
43 * pointer specified by end when parsing stops. The resulting object ID is
44 * stored in oid. Returns 0 on success. Parsing will stop on the first NUL or
45 * other invalid character. end is only updated on success; otherwise, it is
46 * unmodified.
47 */
48int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
49
50/* Like parse_oid_hex, but for an arbitrary hash algorithm. */
51int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
52 const struct git_hash_algo *algo);
53
54
55/*
56 * These functions work like get_oid_hex and parse_oid_hex, but they will parse
57 * a hex value for any algorithm. The algorithm is detected based on the length
58 * and the algorithm in use is returned. If this is not a hex object ID in any
59 * algorithm, returns GIT_HASH_UNKNOWN.
60 */
61int get_oid_hex_any(const char *hex, struct object_id *oid);
62int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
63
64#endif