1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-21 20:56:09 +02:00

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:
Junio C Hamano 2017-11-09 14:31:27 +09:00
commit bde1370010
5 changed files with 34 additions and 36 deletions

View File

@ -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_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

12
hex.c
View File

@ -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;

View File

@ -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)

17
notes.c
View File

@ -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);

View File

@ -1881,6 +1881,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);
@ -1898,6 +1899,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;
@ -1905,20 +1908,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) {