diff options
Diffstat (limited to 'src/oid.c')
-rw-r--r-- | src/oid.c | 28 |
1 files changed, 23 insertions, 5 deletions
@@ -49,19 +49,37 @@ static signed char from_hex[] = { }; static char to_hex[] = "0123456789abcdef"; -int git_oid_fromstr(git_oid *out, const char *str) +int git_oid_fromstrn(git_oid *out, const char *str, size_t length) { size_t p; - for (p = 0; p < sizeof(out->id); p++, str += 2) { - int v = (from_hex[(unsigned char)str[0]] << 4) - | from_hex[(unsigned char)str[1]]; + + if (length > GIT_OID_HEXSZ) + length = GIT_OID_HEXSZ; + + if (length % 2) + length--; + + for (p = 0; p < length; p += 2) { + int v = (from_hex[(unsigned char)str[p + 0]] << 4) + | from_hex[(unsigned char)str[p + 1]]; + if (v < 0) return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash"); - out->id[p] = (unsigned char)v; + + out->id[p / 2] = (unsigned char)v; } + + for (; p < GIT_OID_HEXSZ; p += 2) + out->id[p / 2] = 0x0; + return GIT_SUCCESS; } +int git_oid_fromstr(git_oid *out, const char *str) +{ + return git_oid_fromstrn(out, str, GIT_OID_HEXSZ); +} + GIT_INLINE(char) *fmt_one(char *str, unsigned int val) { *str++ = to_hex[val >> 4]; |