oid.h 4.44 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#ifndef INCLUDE_oid_h__
#define INCLUDE_oid_h__

10 11
#include "common.h"

12
#include "git2/experimental.h"
Russell Belfer committed
13
#include "git2/oid.h"
14
#include "hash.h"
Russell Belfer committed
15

16 17 18 19 20 21 22
#ifdef GIT_EXPERIMENTAL_SHA256
# define GIT_OID_NONE { 0, { 0 } }
# define GIT_OID_INIT(type, ...) { type, __VA_ARGS__ }
#else
# define GIT_OID_NONE { { 0 } }
# define GIT_OID_INIT(type, ...) { __VA_ARGS__ }
#endif
Edward Thomson committed
23

24
extern const git_oid git_oid__empty_blob_sha1;
25 26
extern const git_oid git_oid__empty_tree_sha1;

27 28 29 30 31 32 33 34 35 36
GIT_INLINE(git_oid_t) git_oid_type(const git_oid *oid)
{
#ifdef GIT_EXPERIMENTAL_SHA256
	return oid->type;
#else
	GIT_UNUSED(oid);
	return GIT_OID_SHA1;
#endif
}

37 38 39 40 41
GIT_INLINE(size_t) git_oid_size(git_oid_t type)
{
	switch (type) {
	case GIT_OID_SHA1:
		return GIT_OID_SHA1_SIZE;
42 43

#ifdef GIT_EXPERIMENTAL_SHA256
44 45
	case GIT_OID_SHA256:
		return GIT_OID_SHA256_SIZE;
46 47
#endif

48 49 50 51 52 53 54 55 56 57
	}

	return 0;
}

GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type)
{
	switch (type) {
	case GIT_OID_SHA1:
		return GIT_OID_SHA1_HEXSIZE;
58 59

#ifdef GIT_EXPERIMENTAL_SHA256
60 61
	case GIT_OID_SHA256:
		return GIT_OID_SHA256_HEXSIZE;
62 63
#endif

64 65 66 67 68
	}

	return 0;
}

69 70 71 72 73
GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type)
{
	switch (type) {
	case GIT_OID_SHA1:
		return GIT_HASH_ALGORITHM_SHA1;
74 75

#ifdef GIT_EXPERIMENTAL_SHA256
76 77
	case GIT_OID_SHA256:
		return GIT_HASH_ALGORITHM_SHA256;
78 79
#endif

80 81 82 83 84
	}

	return 0;
}

85 86 87 88 89 90 91 92 93 94 95
/**
 * Format a git_oid into a newly allocated c-string.
 *
 * The c-string is owned by the caller and needs to be manually freed.
 *
 * @param id the oid structure to format
 * @return the c-string; NULL if memory is exhausted. Caller must
 *			deallocate the string with git__free().
 */
char *git_oid_allocfmt(const git_oid *id);

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * Format the requested nibbles of an object id.
 *
 * @param str the string to write into
 * @param oid the oid structure to format
 * @param start the starting number of nibbles
 * @param count the number of nibbles to format
 */
GIT_INLINE(void) git_oid_fmt_substr(
	char *str,
	const git_oid *oid,
	size_t start,
	size_t count)
{
	static char hex[] = "0123456789abcdef";
	size_t i, end = start + count, min = start / 2, max = end / 2;

	if (start & 1)
		*str++ = hex[oid->id[min++] & 0x0f];

	for (i = min; i < max; i++) {
		*str++ = hex[oid->id[i] >> 4];
		*str++ = hex[oid->id[i] & 0x0f];
	}

	if (end & 1)
		*str++ = hex[oid->id[i] >> 4];
}

125 126 127 128 129
GIT_INLINE(int) git_oid_raw_ncmp(
	const unsigned char *sha1,
	const unsigned char *sha2,
	size_t len)
{
Edward Thomson committed
130 131
	if (len > GIT_OID_MAX_HEXSIZE)
		len = GIT_OID_MAX_HEXSIZE;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	while (len > 1) {
		if (*sha1 != *sha2)
			return 1;
		sha1++;
		sha2++;
		len -= 2;
	};

	if (len)
		if ((*sha1 ^ *sha2) & 0xf0)
			return 1;

	return 0;
}

148 149
GIT_INLINE(int) git_oid_raw_cmp(
	const unsigned char *sha1,
Edward Thomson committed
150 151
	const unsigned char *sha2,
	size_t size)
Russell Belfer committed
152
{
Edward Thomson committed
153
	return memcmp(sha1, sha2, size);
Russell Belfer committed
154 155
}

156 157
GIT_INLINE(int) git_oid_raw_cpy(
	unsigned char *dst,
Edward Thomson committed
158 159
	const unsigned char *src,
	size_t size)
160
{
Edward Thomson committed
161
	memcpy(dst, src, size);
162 163 164
	return 0;
}

165 166 167 168 169 170 171 172 173
/*
 * Compare two oid structures.
 *
 * @param a first oid structure.
 * @param b second oid structure.
 * @return <0, 0, >0 if a < b, a == b, a > b.
 */
GIT_INLINE(int) git_oid__cmp(const git_oid *a, const git_oid *b)
{
174
#ifdef GIT_EXPERIMENTAL_SHA256
Edward Thomson committed
175 176 177 178
	if (a->type != b->type)
		return a->type - b->type;

	return git_oid_raw_cmp(a->id, b->id, git_oid_size(a->type));
179 180 181
#else
	return git_oid_raw_cmp(a->id, b->id, git_oid_size(GIT_OID_SHA1));
#endif
182 183
}

184 185 186
GIT_INLINE(void) git_oid__cpy_prefix(
	git_oid *out, const git_oid *id, size_t len)
{
187
#ifdef GIT_EXPERIMENTAL_SHA256
188
	out->type = id->type;
189 190
#endif

191 192 193 194 195 196
	memcpy(&out->id, id->id, (len + 1) / 2);

	if (len & 1)
		out->id[len / 2] &= 0xF0;
}

Edward Thomson committed
197
GIT_INLINE(bool) git_oid__is_hexstr(const char *str, git_oid_t type)
198 199 200 201 202 203 204 205
{
	size_t i;

	for (i = 0; str[i] != '\0'; i++) {
		if (git__fromhex(str[i]) < 0)
			return false;
	}

Edward Thomson committed
206 207 208 209 210 211
	return (i == git_oid_hexsize(type));
}

GIT_INLINE(void) git_oid_clear(git_oid *out, git_oid_t type)
{
	memset(out->id, 0, git_oid_size(type));
212 213

#ifdef GIT_EXPERIMENTAL_SHA256
Edward Thomson committed
214
	out->type = type;
215
#endif
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231
/* SHA256 support */

int git_oid__fromstr(git_oid *out, const char *str, git_oid_t type);

int git_oid__fromstrp(git_oid *out, const char *str, git_oid_t type);

int git_oid__fromstrn(
	git_oid *out,
	const char *str,
	size_t length,
	git_oid_t type);

int git_oid__fromraw(git_oid *out, const unsigned char *raw, git_oid_t type);

Russell Belfer committed
232
#endif