]> git.proxmox.com Git - libgit2.git/blobdiff - src/oid.c
push: remove own copy of callbacks
[libgit2.git] / src / oid.c
index f74c43fe278d63002f7d0ceb04362484a35c1316..9fe2ebb6522e19a0af083c609aba330ca1e162b9 100644 (file)
--- a/src/oid.c
+++ b/src/oid.c
@@ -8,6 +8,7 @@
 #include "common.h"
 #include "git2/oid.h"
 #include "repository.h"
+#include "global.h"
 #include <string.h>
 #include <limits.h>
 
@@ -24,30 +25,24 @@ int git_oid_fromstrn(git_oid *out, const char *str, size_t length)
        size_t p;
        int v;
 
-       if (length > GIT_OID_HEXSZ)
-               return oid_error_invalid("too long");
+       assert(out && str);
 
-       for (p = 0; p < length - 1; p += 2) {
-               v = (git__fromhex(str[p + 0]) << 4)
-                               | git__fromhex(str[p + 1]);
+       if (!length)
+               return oid_error_invalid("too short");
 
-               if (v < 0)
-                       return oid_error_invalid("contains invalid characters");
+       if (length > GIT_OID_HEXSZ)
+               return oid_error_invalid("too long");
 
-               out->id[p / 2] = (unsigned char)v;
-       }
+       memset(out->id, 0, GIT_OID_RAWSZ);
 
-       if (length % 2) {
-               v = (git__fromhex(str[p + 0]) << 4);
+       for (p = 0; p < length; p++) {
+               v = git__fromhex(str[p]);
                if (v < 0)
                        return oid_error_invalid("contains invalid characters");
 
-               out->id[p / 2] = (unsigned char)v;
-               p += 2;
+               out->id[p / 2] |= (unsigned char)(v << (p % 2 ? 0 : 4));
        }
 
-       memset(out->id + p / 2, 0, (GIT_OID_HEXSZ - p) / 2);
-
        return 0;
 }
 
@@ -105,6 +100,13 @@ void git_oid_pathfmt(char *str, const git_oid *oid)
                str = fmt_one(str, oid->id[i]);
 }
 
+char *git_oid_tostr_s(const git_oid *oid)
+{
+       char *str = GIT_GLOBAL->oid_fmt;
+       git_oid_nfmt(str, GIT_OID_HEXSZ + 1, oid);
+       return str;
+}
+
 char *git_oid_allocfmt(const git_oid *oid)
 {
        char *str = git__malloc(GIT_OID_HEXSZ + 1);
@@ -209,7 +211,7 @@ int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
 
 int git_oid_strcmp(const git_oid *oid_a, const char *str)
 {
-       const unsigned char *a = oid_a->id;
+       const unsigned char *a;
        unsigned char strval;
        int hexval;
 
@@ -259,7 +261,7 @@ struct git_oid_shorten {
 
 static int resize_trie(git_oid_shorten *self, size_t new_size)
 {
-       self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node));
+       self->nodes = git__reallocarray(self->nodes, new_size, sizeof(trie_node));
        GITERR_CHECK_ALLOC(self->nodes);
 
        if (new_size > self->size) {