| 1 | /* | ||
| 2 | * Copyright (C) the libgit2 contributors. All rights reserved. | ||
| 3 | * | ||
| 4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | ||
| 5 | * a Linking Exception. For full terms see the included COPYING file. | ||
| 6 | */ | ||
| 7 | #ifndef INCLUDE_oid_h__ | ||
| 8 | #define INCLUDE_oid_h__ | ||
| 9 | |||
| 10 | #include "common.h" | ||
| 11 | |||
| 12 | #include "git2/oid.h" | ||
| 13 | |||
| 14 | extern const git_oid git_oid__empty_blob_sha1; | ||
| 15 | extern const git_oid git_oid__empty_tree_sha1; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * Format a git_oid into a newly allocated c-string. | ||
| 19 | * | ||
| 20 | * The c-string is owned by the caller and needs to be manually freed. | ||
| 21 | * | ||
| 22 | * @param id the oid structure to format | ||
| 23 | * @return the c-string; NULL if memory is exhausted. Caller must | ||
| 24 | * deallocate the string with git__free(). | ||
| 25 | */ | ||
| 26 | char *git_oid_allocfmt(const git_oid *id); | ||
| 27 | |||
| 28 | GIT_INLINE(int) git_oid_raw_ncmp( | ||
| 29 | const unsigned char *sha1, | ||
| 30 | const unsigned char *sha2, | ||
| 31 | size_t len) | ||
| 32 | { | ||
| 33 | if (len > GIT_OID_HEXSZ) | ||
| 34 | len = GIT_OID_HEXSZ; | ||
| 35 | |||
| 36 | while (len > 1) { | ||
| 37 | if (*sha1 != *sha2) | ||
| 38 | return 1; | ||
| 39 | sha1++; | ||
| 40 | sha2++; | ||
| 41 | len -= 2; | ||
| 42 | }; | ||
| 43 | |||
| 44 | if (len) | ||
| 45 | if ((*sha1 ^ *sha2) & 0xf0) | ||
| 46 | return 1; | ||
| 47 | |||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | 11 | GIT_INLINE(int) git_oid_raw_cmp( | |
| 52 | const unsigned char *sha1, | ||
| 53 | const unsigned char *sha2) | ||
| 54 | { | ||
| 55 | 11 | return memcmp(sha1, sha2, GIT_OID_RAWSZ); | |
| 56 | } | ||
| 57 | |||
| 58 | GIT_INLINE(int) git_oid_raw_cpy( | ||
| 59 | unsigned char *dst, | ||
| 60 | const unsigned char *src) | ||
| 61 | { | ||
| 62 | memcpy(dst, src, GIT_OID_RAWSZ); | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | /* | ||
| 67 | * Compare two oid structures. | ||
| 68 | * | ||
| 69 | * @param a first oid structure. | ||
| 70 | * @param b second oid structure. | ||
| 71 | * @return <0, 0, >0 if a < b, a == b, a > b. | ||
| 72 | */ | ||
| 73 | 11 | GIT_INLINE(int) git_oid__cmp(const git_oid *a, const git_oid *b) | |
| 74 | { | ||
| 75 | 11 | return git_oid_raw_cmp(a->id, b->id); | |
| 76 | } | ||
| 77 | |||
| 78 | GIT_INLINE(void) git_oid__cpy_prefix( | ||
| 79 | git_oid *out, const git_oid *id, size_t len) | ||
| 80 | { | ||
| 81 | memcpy(&out->id, id->id, (len + 1) / 2); | ||
| 82 | |||
| 83 | if (len & 1) | ||
| 84 | out->id[len / 2] &= 0xF0; | ||
| 85 | } | ||
| 86 | |||
| 87 | GIT_INLINE(bool) git_oid__is_hexstr(const char *str) | ||
| 88 | { | ||
| 89 | size_t i; | ||
| 90 | |||
| 91 | for (i = 0; str[i] != '\0'; i++) { | ||
| 92 | if (git__fromhex(str[i]) < 0) | ||
| 93 | return false; | ||
| 94 | } | ||
| 95 | |||
| 96 | return (i == GIT_OID_HEXSZ); | ||
| 97 | } | ||
| 98 | |||
| 99 | #endif |