]>
Commit | Line | Data |
---|---|---|
b73ecb48 EN |
1 | #ifndef HEX_H |
2 | #define HEX_H | |
3 | ||
d1cbe1e6 | 4 | #include "hash-ll.h" |
b73ecb48 EN |
5 | |
6 | extern const signed char hexval_table[256]; | |
7 | static inline unsigned int hexval(unsigned char c) | |
8 | { | |
9 | return hexval_table[c]; | |
10 | } | |
11 | ||
12 | /* | |
13 | * Convert two consecutive hexadecimal digits into a char. Return a | |
14 | * negative value on error. Don't run over the end of short strings. | |
15 | */ | |
16 | static inline int hex2chr(const char *s) | |
17 | { | |
18 | unsigned int val = hexval(s[0]); | |
19 | return (val & ~0xf) ? val : (val << 4) | hexval(s[1]); | |
20 | } | |
21 | ||
22 | /* | |
23 | * Try to read a SHA1 in hexadecimal format from the 40 characters | |
24 | * starting at hex. Write the 20-byte result to sha1 in binary form. | |
25 | * Return 0 on success. Reading stops if a NUL is encountered in the | |
26 | * input, so it is safe to pass this function an arbitrary | |
27 | * null-terminated string. | |
28 | */ | |
29 | int get_sha1_hex(const char *hex, unsigned char *sha1); | |
30 | int get_oid_hex(const char *hex, struct object_id *sha1); | |
31 | ||
32 | /* Like get_oid_hex, but for an arbitrary hash algorithm. */ | |
33 | int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop); | |
34 | ||
35 | /* | |
36 | * Read `len` pairs of hexadecimal digits from `hex` and write the | |
37 | * values to `binary` as `len` bytes. Return 0 on success, or -1 if | |
38 | * the input does not consist of hex digits). | |
39 | */ | |
40 | int hex_to_bytes(unsigned char *binary, const char *hex, size_t len); | |
41 | ||
42 | /* | |
43 | * Convert a binary hash in "unsigned char []" or an object name in | |
44 | * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant, | |
45 | * and writes the NUL-terminated output to the buffer `out`, which must be at | |
46 | * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for | |
47 | * convenience. | |
48 | * | |
49 | * The non-`_r` variant returns a static buffer, but uses a ring of 4 | |
50 | * buffers, making it safe to make multiple calls for a single statement, like: | |
51 | * | |
52 | * printf("%s -> %s", hash_to_hex(one), hash_to_hex(two)); | |
53 | * printf("%s -> %s", oid_to_hex(one), oid_to_hex(two)); | |
54 | */ | |
55 | char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *); | |
56 | char *oid_to_hex_r(char *out, const struct object_id *oid); | |
57 | char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */ | |
58 | char *hash_to_hex(const unsigned char *hash); /* same static buffer */ | |
59 | char *oid_to_hex(const struct object_id *oid); /* same static buffer */ | |
60 | ||
61 | /* | |
62 | * Parse a 40-character hexadecimal object ID starting from hex, updating the | |
63 | * pointer specified by end when parsing stops. The resulting object ID is | |
64 | * stored in oid. Returns 0 on success. Parsing will stop on the first NUL or | |
65 | * other invalid character. end is only updated on success; otherwise, it is | |
66 | * unmodified. | |
67 | */ | |
68 | int parse_oid_hex(const char *hex, struct object_id *oid, const char **end); | |
69 | ||
70 | /* Like parse_oid_hex, but for an arbitrary hash algorithm. */ | |
71 | int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end, | |
72 | const struct git_hash_algo *algo); | |
73 | ||
74 | ||
75 | /* | |
76 | * These functions work like get_oid_hex and parse_oid_hex, but they will parse | |
77 | * a hex value for any algorithm. The algorithm is detected based on the length | |
78 | * and the algorithm in use is returned. If this is not a hex object ID in any | |
79 | * algorithm, returns GIT_HASH_UNKNOWN. | |
80 | */ | |
81 | int get_oid_hex_any(const char *hex, struct object_id *oid); | |
82 | int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end); | |
83 | ||
84 | #endif |