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