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
This commit is contained in:
commit
bde1370010
7
cache.h
7
cache.h
@ -1340,6 +1340,13 @@ extern int set_disambiguate_hint_config(const char *var, const char *value);
|
|||||||
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
|
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
|
||||||
extern int get_oid_hex(const char *hex, struct object_id *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,
|
* 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
|
* and writes the NUL-terminated output to the buffer `out`, which must be at
|
||||||
|
12
hex.c
12
hex.c
@ -35,6 +35,18 @@ const signed char hexval_table[256] = {
|
|||||||
-1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
|
-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 get_sha1_hex(const char *hex, unsigned char *sha1)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
10
http-push.c
10
http-push.c
@ -1007,20 +1007,18 @@ static void remote_ls(const char *path, int flags,
|
|||||||
void (*userFunc)(struct remote_ls_ctx *ls),
|
void (*userFunc)(struct remote_ls_ctx *ls),
|
||||||
void *userData);
|
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)
|
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)
|
if (strlen(path) != GIT_SHA1_HEXSZ + 1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
memcpy(hex, path, 2);
|
if (hex_to_bytes(oid->hash, path, 1))
|
||||||
|
return -1;
|
||||||
path += 2;
|
path += 2;
|
||||||
path++; /* skip '/' */
|
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)
|
static void process_ls_object(struct remote_ls_ctx *ls)
|
||||||
|
17
notes.c
17
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)
|
static int non_note_cmp(const struct non_note *a, const struct non_note *b)
|
||||||
{
|
{
|
||||||
return strcmp(a->path, b->path);
|
return strcmp(a->path, b->path);
|
||||||
|
24
sha1_file.c
24
sha1_file.c
@ -1881,6 +1881,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
int r = 0;
|
int r = 0;
|
||||||
|
struct object_id oid;
|
||||||
|
|
||||||
if (subdir_nr > 0xff)
|
if (subdir_nr > 0xff)
|
||||||
BUG("invalid loose object subdirectory: %x", subdir_nr);
|
BUG("invalid loose object subdirectory: %x", subdir_nr);
|
||||||
@ -1898,6 +1899,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oid.hash[0] = subdir_nr;
|
||||||
|
|
||||||
while ((de = readdir(dir))) {
|
while ((de = readdir(dir))) {
|
||||||
if (is_dot_or_dotdot(de->d_name))
|
if (is_dot_or_dotdot(de->d_name))
|
||||||
continue;
|
continue;
|
||||||
@ -1905,20 +1908,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
|
|||||||
strbuf_setlen(path, baselen);
|
strbuf_setlen(path, baselen);
|
||||||
strbuf_addf(path, "/%s", de->d_name);
|
strbuf_addf(path, "/%s", de->d_name);
|
||||||
|
|
||||||
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) {
|
if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
|
||||||
char hex[GIT_MAX_HEXSZ+1];
|
!hex_to_bytes(oid.hash + 1, de->d_name,
|
||||||
struct object_id oid;
|
GIT_SHA1_RAWSZ - 1)) {
|
||||||
|
if (obj_cb) {
|
||||||
xsnprintf(hex, sizeof(hex), "%02x%s",
|
r = obj_cb(&oid, path->buf, data);
|
||||||
subdir_nr, de->d_name);
|
if (r)
|
||||||
if (!get_oid_hex(hex, &oid)) {
|
break;
|
||||||
if (obj_cb) {
|
|
||||||
r = obj_cb(&oid, path->buf, data);
|
|
||||||
if (r)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cruft_cb) {
|
if (cruft_cb) {
|
||||||
|
Loading…
Reference in New Issue
Block a user