Git fork
at reftables-rust 124 lines 2.9 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "git-compat-util.h" 4#include "hash.h" 5#include "hex.h" 6 7static int get_hash_hex_algop(const char *hex, unsigned char *hash, 8 const struct git_hash_algo *algop) 9{ 10 for (size_t i = 0; i < algop->rawsz; i++) { 11 int val = hex2chr(hex); 12 if (val < 0) 13 return -1; 14 *hash++ = val; 15 hex += 2; 16 } 17 return 0; 18} 19 20int get_hash_hex(const char *hex, unsigned char *sha1) 21{ 22 return get_hash_hex_algop(hex, sha1, the_hash_algo); 23} 24 25int get_oid_hex_algop(const char *hex, struct object_id *oid, 26 const struct git_hash_algo *algop) 27{ 28 int ret = get_hash_hex_algop(hex, oid->hash, algop); 29 if (!ret) { 30 oid_set_algo(oid, algop); 31 if (algop->rawsz != GIT_MAX_RAWSZ) 32 memset(oid->hash + algop->rawsz, 0, 33 GIT_MAX_RAWSZ - algop->rawsz); 34 } 35 return ret; 36} 37 38/* 39 * NOTE: This function relies on hash algorithms being in order from shortest 40 * length to longest length. 41 */ 42int get_oid_hex_any(const char *hex, struct object_id *oid) 43{ 44 int i; 45 for (i = GIT_HASH_NALGOS - 1; i > 0; i--) { 46 if (!get_oid_hex_algop(hex, oid, &hash_algos[i])) 47 return i; 48 } 49 return GIT_HASH_UNKNOWN; 50} 51 52int get_oid_hex(const char *hex, struct object_id *oid) 53{ 54 return get_oid_hex_algop(hex, oid, the_hash_algo); 55} 56 57int parse_oid_hex_algop(const char *hex, struct object_id *oid, 58 const char **end, 59 const struct git_hash_algo *algop) 60{ 61 int ret = get_oid_hex_algop(hex, oid, algop); 62 if (!ret) 63 *end = hex + algop->hexsz; 64 return ret; 65} 66 67int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end) 68{ 69 int ret = get_oid_hex_any(hex, oid); 70 if (ret) 71 *end = hex + hash_algos[ret].hexsz; 72 return ret; 73} 74 75int parse_oid_hex(const char *hex, struct object_id *oid, const char **end) 76{ 77 return parse_oid_hex_algop(hex, oid, end, the_hash_algo); 78} 79 80char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, 81 const struct git_hash_algo *algop) 82{ 83 static const char hex[] = "0123456789abcdef"; 84 char *buf = buffer; 85 86 /* 87 * Our struct object_id has been memset to 0, so default to printing 88 * using the default hash. 89 */ 90 if (algop == &hash_algos[0]) 91 algop = the_hash_algo; 92 93 for (size_t i = 0; i < algop->rawsz; i++) { 94 unsigned int val = *hash++; 95 *buf++ = hex[val >> 4]; 96 *buf++ = hex[val & 0xf]; 97 } 98 *buf = '\0'; 99 100 return buffer; 101} 102 103char *oid_to_hex_r(char *buffer, const struct object_id *oid) 104{ 105 return hash_to_hex_algop_r(buffer, oid->hash, &hash_algos[oid->algo]); 106} 107 108char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop) 109{ 110 static int bufno; 111 static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; 112 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); 113 return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop); 114} 115 116char *hash_to_hex(const unsigned char *hash) 117{ 118 return hash_to_hex_algop(hash, the_hash_algo); 119} 120 121char *oid_to_hex(const struct object_id *oid) 122{ 123 return hash_to_hex_algop(oid->hash, &hash_algos[oid->algo]); 124}