Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 1 | #ifndef HEX_H |
| 2 | #define HEX_H |
| 3 | |
Patrick Steinhardt | 8a676bd | 2024-06-14 08:50:32 +0200 | [diff] [blame] | 4 | #include "hash.h" |
Calvin Wan | d88e810 | 2023-09-29 14:20:48 -0700 | [diff] [blame] | 5 | #include "hex-ll.h" |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 6 | |
| 7 | /* |
Junio C Hamano | 08e5fb1 | 2023-07-24 16:11:03 -0700 | [diff] [blame] | 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. |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 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 | */ |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 16 | int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop); |
| 17 | |
| 18 | /* |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 19 | * Convert a binary hash in "unsigned char []" or an object name in |
| 20 | * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant, |
| 21 | * and writes the NUL-terminated output to the buffer `out`, which must be at |
| 22 | * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for |
| 23 | * convenience. |
| 24 | * |
| 25 | * The non-`_r` variant returns a static buffer, but uses a ring of 4 |
| 26 | * buffers, making it safe to make multiple calls for a single statement, like: |
| 27 | * |
| 28 | * printf("%s -> %s", hash_to_hex(one), hash_to_hex(two)); |
| 29 | * printf("%s -> %s", oid_to_hex(one), oid_to_hex(two)); |
| 30 | */ |
| 31 | char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *); |
| 32 | char *oid_to_hex_r(char *out, const struct object_id *oid); |
| 33 | char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */ |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 34 | char *oid_to_hex(const struct object_id *oid); /* same static buffer */ |
| 35 | |
| 36 | /* |
| 37 | * Parse a 40-character hexadecimal object ID starting from hex, updating the |
| 38 | * pointer specified by end when parsing stops. The resulting object ID is |
| 39 | * stored in oid. Returns 0 on success. Parsing will stop on the first NUL or |
| 40 | * other invalid character. end is only updated on success; otherwise, it is |
| 41 | * unmodified. |
| 42 | */ |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 43 | int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end, |
| 44 | const struct git_hash_algo *algo); |
| 45 | |
Elijah Newren | b73ecb4 | 2023-02-24 00:09:26 +0000 | [diff] [blame] | 46 | /* |
| 47 | * These functions work like get_oid_hex and parse_oid_hex, but they will parse |
| 48 | * a hex value for any algorithm. The algorithm is detected based on the length |
| 49 | * and the algorithm in use is returned. If this is not a hex object ID in any |
| 50 | * algorithm, returns GIT_HASH_UNKNOWN. |
| 51 | */ |
| 52 | int get_oid_hex_any(const char *hex, struct object_id *oid); |
| 53 | int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end); |
| 54 | |
Patrick Steinhardt | dc89b7d | 2024-06-14 08:51:14 +0200 | [diff] [blame] | 55 | #ifdef USE_THE_REPOSITORY_VARIABLE |
| 56 | |
| 57 | /* Like get_oid_hex_algop, but for `the_hash_algo`. */ |
| 58 | int get_hash_hex(const char *hex, unsigned char *hash); |
| 59 | int get_oid_hex(const char *hex, struct object_id *oid); |
| 60 | |
| 61 | /* Like parse_oid_hex_algop, but uses `the_hash_algo`. */ |
| 62 | int parse_oid_hex(const char *hex, struct object_id *oid, const char **end); |
| 63 | |
| 64 | /* |
| 65 | * Same as `hash_to_hex_algop()`, but uses `the_hash_algo`. |
| 66 | */ |
| 67 | char *hash_to_hex(const unsigned char *hash); |
| 68 | |
| 69 | #endif /* USE_THE_REPOSITORY_VARIABLE */ |
| 70 | #endif /* HEX_H */ |