File: lib-oid.c

package info (click to toggle)
cgit 1.2.3%2Bgit20240802.70.09d24d7%2Bgit2.46.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 56,184 kB
  • sloc: ansic: 301,641; sh: 254,574; perl: 29,353; tcl: 22,151; makefile: 4,198; python: 3,594; javascript: 810; csh: 45; ruby: 43; lisp: 12
file content (52 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "test-lib.h"
#include "lib-oid.h"
#include "strbuf.h"
#include "hex.h"

static int init_hash_algo(void)
{
	static int algo = -1;

	if (algo < 0) {
		const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH");
		algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1;

		if (!check(algo != GIT_HASH_UNKNOWN))
			test_msg("BUG: invalid GIT_TEST_DEFAULT_HASH value ('%s')",
				 algo_name);
	}
	return algo;
}

static int get_oid_arbitrary_hex_algop(const char *hex, struct object_id *oid,
				       const struct git_hash_algo *algop)
{
	int ret;
	size_t sz = strlen(hex);
	struct strbuf buf = STRBUF_INIT;

	if (!check(sz <= algop->hexsz)) {
		test_msg("BUG: hex string (%s) bigger than maximum allowed (%lu)",
			 hex, (unsigned long)algop->hexsz);
		return -1;
	}

	strbuf_add(&buf, hex, sz);
	strbuf_addchars(&buf, '0', algop->hexsz - sz);

	ret = get_oid_hex_algop(buf.buf, oid, algop);
	if (!check_int(ret, ==, 0))
		test_msg("BUG: invalid hex input (%s) provided", hex);

	strbuf_release(&buf);
	return ret;
}

int get_oid_arbitrary_hex(const char *hex, struct object_id *oid)
{
	int hash_algo = init_hash_algo();

	if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN))
		return -1;
	return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]);
}