Commit ef1e5da1 by Vicent Martí

Merge pull request #438 from jdavid/development

Make git_oid_fromstrn support hex strings of odd length
parents cd19ca95 0e058e78
...@@ -34,15 +34,16 @@ static char to_hex[] = "0123456789abcdef"; ...@@ -34,15 +34,16 @@ static char to_hex[] = "0123456789abcdef";
int git_oid_fromstrn(git_oid *out, const char *str, size_t length) int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
{ {
size_t p; size_t p;
int v;
if (length < 4)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is too short");
if (length > GIT_OID_HEXSZ) if (length > GIT_OID_HEXSZ)
length = GIT_OID_HEXSZ; length = GIT_OID_HEXSZ;
if (length % 2) for (p = 0; p < length - 1; p += 2) {
length--; v = (from_hex[(unsigned char)str[p + 0]] << 4)
for (p = 0; p < length; p += 2) {
int v = (from_hex[(unsigned char)str[p + 0]] << 4)
| from_hex[(unsigned char)str[p + 1]]; | from_hex[(unsigned char)str[p + 1]];
if (v < 0) if (v < 0)
...@@ -51,8 +52,16 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length) ...@@ -51,8 +52,16 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
out->id[p / 2] = (unsigned char)v; out->id[p / 2] = (unsigned char)v;
} }
for (; p < GIT_OID_HEXSZ; p += 2) if (length % 2) {
out->id[p / 2] = 0x0; v = (from_hex[(unsigned char)str[p + 0]] << 4);
if (v < 0)
return git__throw(GIT_ENOTOID, "Failed to generate sha1. Given string is not a valid sha1 hash");
out->id[p / 2] = (unsigned char)v;
p += 2;
}
memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
return GIT_SUCCESS; return GIT_SUCCESS;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment