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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 31, 2017 at 14:46 UTC 0ec218656a02ea48e173262f7b80513feeb7f263
3 files changed +19 -17
cache.h
+7
@@ -1317,6 +1317,13 @@ extern int set_disambiguate_hint_config(const char *var, const char *value);
1317 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
1318 extern int get_oid_hex(const char *hex, struct object_id *sha1);
1319
1320 +/*
1321 + * Read `len` pairs of hexadecimal digits from `hex` and write the
1322 + * values to `binary` as `len` bytes. Return 0 on success, or -1 if
1323 + * the input does not consist of hex digits).
1324 + */
1325 +extern int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
1326 +
1327 /*
1328 * Convert a binary sha1 to its hex equivalent. The `_r` variant is reentrant,
1329 * and writes the NUL-terminated output to the buffer `out`, which must be at
hex.c
+12
@@ -35,6 +35,18 @@ const signed char hexval_table[256] = {
35 -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
36 };
37
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 +
50 int get_sha1_hex(const char *hex, unsigned char *sha1)
51 {
52 int i;
notes.c
-17
@@ -334,23 +334,6 @@ static void note_tree_free(struct int_node *tree)
334 }
335 }
336
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 -
337 static int non_note_cmp(const struct non_note *a, const struct non_note *b)
338 {
339 return strcmp(a->path, b->path);