Skip to content

Commit bde1370

Browse files
committed
Merge branch 'rs/hex-to-bytes-cleanup'
Code cleanup. * rs/hex-to-bytes-cleanup: sha1_file: use hex_to_bytes() http-push: use hex_to_bytes() notes: move hex_to_bytes() to hex.c and export it
2 parents b169d18 + 62a24c8 commit bde1370

File tree

5 files changed

+34
-36
lines changed

5 files changed

+34
-36
lines changed

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,13 @@ extern int set_disambiguate_hint_config(const char *var, const char *value);
13401340
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
13411341
extern int get_oid_hex(const char *hex, struct object_id *sha1);
13421342

1343+
/*
1344+
* Read `len` pairs of hexadecimal digits from `hex` and write the
1345+
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
1346+
* the input does not consist of hex digits).
1347+
*/
1348+
extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
1349+
13431350
/*
13441351
* Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
13451352
* and writes the NUL-terminated output to the buffer `out`, which must be at

hex.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ const signed char hexval_table[256] = {
3535
-1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
3636
};
3737

38+
int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
39+
{
40+
for (; len; len--, hex += 2) {
41+
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
42+
43+
if (val & ~0xff)
44+
return -1;
45+
*binary++ = val;
46+
}
47+
return 0;
48+
}
49+
3850
int get_sha1_hex(const char *hex, unsigned char *sha1)
3951
{
4052
int i;

http-push.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,20 +1007,18 @@ static void remote_ls(const char *path, int flags,
10071007
void (*userFunc)(struct remote_ls_ctx *ls),
10081008
void *userData);
10091009

1010-
/* extract hex from sharded "xx/x{40}" filename */
1010+
/* extract hex from sharded "xx/x{38}" filename */
10111011
static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
10121012
{
1013-
char hex[GIT_MAX_HEXSZ];
1014-
10151013
if (strlen(path) != GIT_SHA1_HEXSZ + 1)
10161014
return -1;
10171015

1018-
memcpy(hex, path, 2);
1016+
if (hex_to_bytes(oid->hash, path, 1))
1017+
return -1;
10191018
path += 2;
10201019
path++; /* skip '/' */
1021-
memcpy(hex + 2, path, GIT_SHA1_HEXSZ - 2);
10221020

1023-
return get_oid_hex(hex, oid);
1021+
return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
10241022
}
10251023

10261024
static void process_ls_object(struct remote_ls_ctx *ls)

notes.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,6 @@ static void note_tree_free(struct int_node *tree)
334334
}
335335
}
336336

337-
/*
338-
* Read `len` pairs of hexadecimal digits from `hex` and write the
339-
* values to `binary` as `len` bytes. Return 0 on success, or -1 if
340-
* the input does not consist of hex digits).
341-
*/
342-
static int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
343-
{
344-
for (; len; len--, hex += 2) {
345-
unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
346-
347-
if (val & ~0xff)
348-
return -1;
349-
*binary++ = val;
350-
}
351-
return 0;
352-
}
353-
354337
static int non_note_cmp(const struct non_note *a, const struct non_note *b)
355338
{
356339
return strcmp(a->path, b->path);

sha1_file.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
18811881
DIR *dir;
18821882
struct dirent *de;
18831883
int r = 0;
1884+
struct object_id oid;
18841885

18851886
if (subdir_nr > 0xff)
18861887
BUG("invalid loose object subdirectory: %x", subdir_nr);
@@ -1898,27 +1899,24 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
18981899
return r;
18991900
}
19001901

1902+
oid.hash[0] = subdir_nr;
1903+
19011904
while ((de = readdir(dir))) {
19021905
if (is_dot_or_dotdot(de->d_name))
19031906
continue;
19041907

19051908
strbuf_setlen(path, baselen);
19061909
strbuf_addf(path, "/%s", de->d_name);
19071910

1908-
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) {
1909-
char hex[GIT_MAX_HEXSZ+1];
1910-
struct object_id oid;
1911-
1912-
xsnprintf(hex, sizeof(hex), "%02x%s",
1913-
subdir_nr, de->d_name);
1914-
if (!get_oid_hex(hex, &oid)) {
1915-
if (obj_cb) {
1916-
r = obj_cb(&oid, path->buf, data);
1917-
if (r)
1918-
break;
1919-
}
1920-
continue;
1911+
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
1912+
!hex_to_bytes(oid.hash + 1, de->d_name,
1913+
GIT_SHA1_RAWSZ - 1)) {
1914+
if (obj_cb) {
1915+
r = obj_cb(&oid, path->buf, data);
1916+
if (r)
1917+
break;
19211918
}
1919+
continue;
19221920
}
19231921

19241922
if (cruft_cb) {

0 commit comments

Comments
 (0)