From 0ec218656a02ea48e173262f7b80513feeb7f263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 31 Oct 2017 14:46:49 +0100 Subject: notes: move hex_to_bytes() to hex.c and export it Make the function for converting pairs of hexadecimal digits to binary available to other call sites. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 6440e2b..f06bfba 100644 --- a/cache.h +++ b/cache.h @@ -1318,6 +1318,13 @@ extern int get_sha1_hex(const char *hex, unsigned char *sha1); extern int get_oid_hex(const char *hex, struct object_id *sha1); /* + * Read `len` pairs of hexadecimal digits from `hex` and write the + * values to `binary` as `len` bytes. Return 0 on success, or -1 if + * the input does not consist of hex digits). + */ +extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len); + +/* * Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant, * and writes the NUL-terminated output to the buffer `out`, which must be at * least `GIT_SHA1_HEXSZ + 1` bytes, and returns a pointer to out for diff --git a/hex.c b/hex.c index 28b4411..8df2d63 100644 --- a/hex.c +++ b/hex.c @@ -35,6 +35,18 @@ const signed char hexval_table[256] = { -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */ }; +int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) +{ + for (; len; len--, hex += 2) { + unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); + + if (val & ~0xff) + return -1; + *binary++ = val; + } + return 0; +} + int get_sha1_hex(const char *hex, unsigned char *sha1) { int i; diff --git a/notes.c b/notes.c index 5c62862..04f8c86 100644 --- a/notes.c +++ b/notes.c @@ -334,23 +334,6 @@ static void note_tree_free(struct int_node *tree) } } -/* - * Read `len` pairs of hexadecimal digits from `hex` and write the - * values to `binary` as `len` bytes. Return 0 on success, or -1 if - * the input does not consist of hex digits). - */ -static int hex_to_bytes(unsigned char *binary, const char *hex, size_t len) -{ - for (; len; len--, hex += 2) { - unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]); - - if (val & ~0xff) - return -1; - *binary++ = val; - } - return 0; -} - static int non_note_cmp(const struct non_note *a, const struct non_note *b) { return strcmp(a->path, b->path); -- cgit v0.10.2-6-g49f6 From c3bdc4e7795023863756cf6176c1ab32793459a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 31 Oct 2017 14:49:56 +0100 Subject: http-push: use hex_to_bytes() The path of a loose object contains its hash value encoded into two substrings of hexadecimal digits, separated by a slash. The current code copies the pieces into a temporary buffer to get rid of the slash and then uses get_oid_hex() to decode the hash value. Avoid the copy by using hex_to_bytes() directly on the substrings. That's shorter and easier. While at it correct the length of the second substring in a comment. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/http-push.c b/http-push.c index 493ee7d..14435ab 100644 --- a/http-push.c +++ b/http-push.c @@ -1007,20 +1007,18 @@ static void remote_ls(const char *path, int flags, void (*userFunc)(struct remote_ls_ctx *ls), void *userData); -/* extract hex from sharded "xx/x{40}" filename */ +/* extract hex from sharded "xx/x{38}" filename */ static int get_oid_hex_from_objpath(const char *path, struct object_id *oid) { - char hex[GIT_MAX_HEXSZ]; - if (strlen(path) != GIT_SHA1_HEXSZ + 1) return -1; - memcpy(hex, path, 2); + if (hex_to_bytes(oid->hash, path, 1)) + return -1; path += 2; path++; /* skip '/' */ - memcpy(hex + 2, path, GIT_SHA1_HEXSZ - 2); - return get_oid_hex(hex, oid); + return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1); } static void process_ls_object(struct remote_ls_ctx *ls) -- cgit v0.10.2-6-g49f6 From 62a24c8923274b0ef941b2e7bc6efdb2fd52e6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 31 Oct 2017 14:50:06 +0100 Subject: sha1_file: use hex_to_bytes() The path of a loose object contains its hash value encoded into two substrings of 2 and 38 hexadecimal digits separated by a slash. The first part is handed to for_each_file_in_obj_subdir() in decoded form as subdir_nr. The current code builds a full hexadecimal representation of the hash in a temporary buffer, then uses get_oid_hex() to decode it. Avoid the intermediate step by taking subdir_nr as-is and using hex_to_bytes() directly on the second substring. That's shorter and easier. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/sha1_file.c b/sha1_file.c index 10c3a00..a3c32d9 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -1884,6 +1884,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr, DIR *dir; struct dirent *de; int r = 0; + struct object_id oid; if (subdir_nr > 0xff) BUG("invalid loose object subdirectory: %x", subdir_nr); @@ -1901,6 +1902,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr, return r; } + oid.hash[0] = subdir_nr; + while ((de = readdir(dir))) { if (is_dot_or_dotdot(de->d_name)) continue; @@ -1908,20 +1911,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr, strbuf_setlen(path, baselen); strbuf_addf(path, "/%s", de->d_name); - if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) { - char hex[GIT_MAX_HEXSZ+1]; - struct object_id oid; - - xsnprintf(hex, sizeof(hex), "%02x%s", - subdir_nr, de->d_name); - if (!get_oid_hex(hex, &oid)) { - if (obj_cb) { - r = obj_cb(&oid, path->buf, data); - if (r) - break; - } - continue; + if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 && + !hex_to_bytes(oid.hash + 1, de->d_name, + GIT_SHA1_RAWSZ - 1)) { + if (obj_cb) { + r = obj_cb(&oid, path->buf, data); + if (r) + break; } + continue; } if (cruft_cb) { -- cgit v0.10.2-6-g49f6