[PATCH 2/8] Merge multiple hex conversions

Aleksandar Ristovski
2013-04-16T15:45:27Z

* common/common-utils.c (fromhex): Make externally linkable.
Change error message.
(tohex): Make externally linkable.
* common/common-utils.h (fromhex, tohex): New declaration.
* gdbserver/gdbreplay.c (tohex): Rename to 'fromhex'.
(logchar): Use fromhex.
* gdbserver/remote-utils.c (fromhex, unhexify): Remove.
(tohex, hexify): Remove.
(look_up_one_symbol, monitor_output): Use bin2hex instead of hexify.
* gdbserver/server.c (handle_query): Use bin2hex instead of hexify.
(handle_v_run): Ditto.
* gdbserver/server.h (unhexify, hexify): Remove declarations.
* gdbserver/tracepoint.c (cmd_qtdpsrc, cmd_qtdv): Use hex2bin
instead of unhexify.
(cmd_qtstatus): Use bin2hex instead of hexify.
(cmd_qtnotes): Use hex2bin instead of unhexify.
* monitor.c (fromhex): Remove definition.
* remote.c (tohex, fromhex): Remove fwd declarations, remove
definitions.
---
gdb/common/common-utils.c | 6 ++--
gdb/common/common-utils.h | 4 +++
gdb/gdbserver/gdbreplay.c | 6 ++--
gdb/gdbserver/remote-utils.c | 67 ++----------------------------------------
gdb/gdbserver/server.c | 5 ++--
gdb/gdbserver/server.h | 2 --
gdb/gdbserver/tracepoint.c | 16 +++++-----
gdb/monitor.c | 15 ----------
gdb/remote.c | 30 -------------------
9 files changed, 24 insertions(+), 127 deletions(-)

diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c
index 5e96692..c123ed7 100644
--- a/gdb/common/common-utils.c
+++ b/gdb/common/common-utils.c
@@ -264,7 +264,7 @@ strtoulst (const char *num, const char **trailer, int base)

/* Convert hex digit A to a number. */

-static int
+int
fromhex (int a)
{
if (a >= '0' && a <= '9')
@@ -274,7 +274,7 @@ fromhex (int a)
else if (a >= 'A' && a <= 'F')
return a - 'A' + 10;
else
- error (_("Reply contains invalid hex digit %d"), a);
+ error (_("Invalid hex digit %d"), a);
}

int
@@ -298,7 +298,7 @@ hex2bin (const char *hex, gdb_byte *bin, int count)

/* Convert number NIB to a hex digit. */

-static int
+int
tohex (int nib)
{
if (nib < 10)
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index ee7870e..2c95d34 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -74,6 +74,10 @@ char *savestring (const char *ptr, size_t len);

ULONGEST strtoulst (const char *num, const char **trailer, int base);

+extern int fromhex (int a);
+
+extern int tohex (int nib);
+
extern int hex2bin (const char *hex, gdb_byte *bin, int count);

extern int bin2hex (const gdb_byte *bin, char *hex, int count);
diff --git a/gdb/gdbserver/gdbreplay.c b/gdb/gdbserver/gdbreplay.c
index 0aa52d8..d0ff8c4 100644
--- a/gdb/gdbserver/gdbreplay.c
+++ b/gdb/gdbserver/gdbreplay.c
@@ -273,7 +273,7 @@ remote_open (char *name)
}

static int
-tohex (int ch)
+fromhex (int ch)
{
if (ch >= '0' && ch <= '9')
{
@@ -336,11 +336,11 @@ logchar (FILE *fp)
ch2 = fgetc (fp);
fputc (ch2, stdout);
fflush (stdout);
- ch = tohex (ch2) << 4;
+ ch = fromhex (ch2) << 4;
ch2 = fgetc (fp);
fputc (ch2, stdout);
fflush (stdout);
- ch |= tohex (ch2);
+ ch |= fromhex (ch2);
break;
default:
/* Treat any other char as just itself */
diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 42c6a54..6ff491e 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -416,20 +416,6 @@ remote_close (void)
reset_readchar ();
}

-/* Convert hex digit A to a number. */
-
-static int
-fromhex (int a)
-{
- if (a >= '0' && a <= '9')
- return a - '0';
- else if (a >= 'a' && a <= 'f')
- return a - 'a' + 10;
- else
- error ("Reply contains invalid hex digit");
- return 0;
-}
-
#endif

static const char hexchars[] = "0123456789abcdef";
@@ -457,25 +443,6 @@ ishex (int ch, int *val)

#ifndef IN_PROCESS_AGENT

-int
-unhexify (char *bin, const char *hex, int count)
-{
- int i;
-
- for (i = 0; i < count; i++)
- {
- if (hex[0] == 0 || hex[1] == 0)
- {
- /* Hex string is short, or of uneven length.
- Return the count that has been converted so far. */
- return i;
- }
- *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
- hex += 2;
- }
- return i;
-}
-
void
decode_address (CORE_ADDR *addrp, const char *start, int len)
{
@@ -511,37 +478,8 @@ decode_address_to_semicolon (CORE_ADDR *addrp, const char *start)

#endif

-/* Convert number NIB to a hex digit. */
-
-static int
-tohex (int nib)
-{
- if (nib < 10)
- return '0' + nib;
- else
- return 'a' + nib - 10;
-}
-
#ifndef IN_PROCESS_AGENT

-int
-hexify (char *hex, const char *bin, int count)
-{
- int i;
-
- /* May use a length, or a nul-terminated string as input. */
- if (count == 0)
- count = strlen (bin);
-
- for (i = 0; i < count; i++)
- {
- *hex++ = tohex ((*bin >> 4) & 0xf);
- *hex++ = tohex (*bin++ & 0xf);
- }
- *hex = 0;
- return i;
-}
-
/* Convert BUFFER, binary data at least LEN bytes long, into escaped
binary data in OUT_BUF. Set *OUT_LEN to the length of the data
encoded in OUT_BUF, and return the number of bytes in OUT_BUF
@@ -1608,7 +1546,8 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp, int may_ask_gdb)

/* Send the request. */
strcpy (own_buf, "qSymbol:");
- hexify (own_buf + strlen ("qSymbol:"), name, strlen (name));
+ bin2hex ((const gdb_byte *) name, own_buf + strlen ("qSymbol:"),
+ strlen (name));
if (putpkt (own_buf) < 0)
return -1;

@@ -1770,7 +1709,7 @@ monitor_output (const char *msg)
char *buf = xmalloc (strlen (msg) * 2 + 2);

buf[0] = 'O';
- hexify (buf + 1, msg, 0);
+ bin2hex ((const gdb_byte *) msg, buf + 1, 0);

putpkt (buf);
free (buf);
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 6bb36d8..8b586dd 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -1920,7 +1920,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
return;
}

- if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2)
+ if ((len % 2) != 0
+ || hex2bin (own_buf + 6, (gdb_byte *) mon, len / 2) != len / 2)
{
write_enn (own_buf);
free (mon);
@@ -2214,7 +2215,7 @@ handle_v_run (char *own_buf)
{
/* FIXME: Fail request if out of memory instead of dying. */
new_argv[i] = xmalloc (1 + (next_p - p) / 2);
- unhexify (new_argv[i], p, (next_p - p) / 2);
+ hex2bin (p, (gdb_byte *) new_argv[i], (next_p - p) / 2);
new_argv[i][(next_p - p) / 2] = '\0';
}

diff --git a/gdb/gdbserver/server.h b/gdb/gdbserver/server.h
index 139cd49..5e16088 100644
--- a/gdb/gdbserver/server.h
+++ b/gdb/gdbserver/server.h
@@ -322,8 +322,6 @@ int decode_search_memory_packet (const char *buf, int packet_len,
gdb_byte *pattern,
unsigned int *pattern_lenp);

-int unhexify (char *bin, const char *hex, int count);
-int hexify (char *hex, const char *bin, int count);
int remote_escape_output (const gdb_byte *buffer, int len,
gdb_byte *out_buf, int *out_len,
int out_maxlen);
diff --git a/gdb/gdbserver/tracepoint.c b/gdb/gdbserver/tracepoint.c
index 419765b..f48e3b6 100644
--- a/gdb/gdbserver/tracepoint.c
+++ b/gdb/gdbserver/tracepoint.c
@@ -2679,7 +2679,7 @@ cmd_qtdpsrc (char *own_buf)
packet = unpack_varlen_hex (packet, &slen);
++packet; /* skip a colon */
src = xmalloc (slen + 1);
- nbytes = unhexify (src, packet, strlen (packet) / 2);
+ nbytes = hex2bin (packet, (gdb_byte *) src, strlen (packet) / 2);
src[nbytes] = '\0';

newlast = xmalloc (sizeof (struct source_string));
@@ -2721,7 +2721,7 @@ cmd_qtdv (char *own_buf)

nbytes = strlen (packet) / 2;
varname = xmalloc (nbytes + 1);
- nbytes = unhexify (varname, packet, nbytes);
+ nbytes = hex2bin (packet, (gdb_byte *) varname, nbytes);
varname[nbytes] = '\0';

tsv = create_trace_state_variable (num, 1);
@@ -3594,17 +3594,17 @@ cmd_qtstatus (char *packet)
str = (tracing_user_name ? tracing_user_name : "");
slen = strlen (str);
buf1 = (char *) alloca (slen * 2 + 1);
- hexify (buf1, str, slen);
+ bin2hex ((const gdb_byte *) str, buf1, slen);

str = (tracing_notes ? tracing_notes : "");
slen = strlen (str);
buf2 = (char *) alloca (slen * 2 + 1);
- hexify (buf2, str, slen);
+ bin2hex ((const gdb_byte *) str, buf2, slen);

str = (tracing_stop_note ? tracing_stop_note : "");
slen = strlen (str);
buf3 = (char *) alloca (slen * 2 + 1);
- hexify (buf3, str, slen);
+ bin2hex ((const gdb_byte *) str, buf3, slen);

trace_debug ("Returning trace status as %d, stop reason %s",
tracing, tracing_stop_reason);
@@ -4078,7 +4078,7 @@ cmd_qtnotes (char *own_buf)
packet = strchr (packet, ';');
nbytes = (packet - saved) / 2;
user = xmalloc (nbytes + 1);
- nbytes = unhexify (user, saved, nbytes);
+ nbytes = hex2bin (saved, (gdb_byte *) user, nbytes);
user[nbytes] = '\0';
++packet; /* skip the semicolon */
trace_debug ("User is '%s'", user);
@@ -4092,7 +4092,7 @@ cmd_qtnotes (char *own_buf)
packet = strchr (packet, ';');
nbytes = (packet - saved) / 2;
notes = xmalloc (nbytes + 1);
- nbytes = unhexify (notes, saved, nbytes);
+ nbytes = hex2bin (saved, (gdb_byte *) notes, nbytes);
notes[nbytes] = '\0';
++packet; /* skip the semicolon */
trace_debug ("Notes is '%s'", notes);
@@ -4106,7 +4106,7 @@ cmd_qtnotes (char *own_buf)
packet = strchr (packet, ';');
nbytes = (packet - saved) / 2;
stopnote = xmalloc (nbytes + 1);
- nbytes = unhexify (stopnote, saved, nbytes);
+ nbytes = hex2bin (saved, (gdb_byte *) stopnote, nbytes);
stopnote[nbytes] = '\0';
++packet; /* skip the semicolon */
trace_debug ("tstop note is '%s'", stopnote);
diff --git a/gdb/monitor.c b/gdb/monitor.c
index 0337075..c3c623b 100644
--- a/gdb/monitor.c
+++ b/gdb/monitor.c
@@ -226,21 +226,6 @@ monitor_error (char *function, char *message,
message, safe_string);
}

-/* Convert hex digit A to a number. */
-
-static int
-fromhex (int a)
-{
- if (a >= '0' && a <= '9')
- return a - '0';
- else if (a >= 'a' && a <= 'f')
- return a - 'a' + 10;
- else if (a >= 'A' && a <= 'F')
- return a - 'A' + 10;
- else
- error (_("Invalid hex digit %d"), a);
-}
-
/* monitor_vsprintf - similar to vsprintf but handles 64-bit addresses

This function exists to get around the problem that many host platforms
diff --git a/gdb/remote.c b/gdb/remote.c
index 8f74b55..64a79e3 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -126,8 +126,6 @@ static void remote_serial_write (const char *str, int len);

static void remote_kill (struct target_ops *ops);

-static int tohex (int nib);
-
static int remote_can_async_p (void);

static int remote_is_async_p (void);
@@ -184,8 +182,6 @@ static void remote_find_new_threads (void);

static void record_currthread (ptid_t currthread);

-static int fromhex (int a);
-
static int putpkt_binary (char *buf, int cnt);

static void check_binary_download (CORE_ADDR addr);
@@ -4554,32 +4550,6 @@ extended_remote_attach (struct target_ops *ops, char *args, int from_tty)
extended_remote_attach_1 (ops, args, from_tty);
}

-/* Convert hex digit A to a number. */
-
-static int
-fromhex (int a)
-{
- if (a >= '0' && a <= '9')
- return a - '0';
- else if (a >= 'a' && a <= 'f')
- return a - 'a' + 10;
- else if (a >= 'A' && a <= 'F')
- return a - 'A' + 10;
- else
- error (_("Reply contains invalid hex digit %d"), a);
-}
-
-/* Convert number NIB to a hex digit. */
-
-static int
-tohex (int nib)
-{
- if (nib < 10)
- return '0' + nib;
- else
- return 'a' + nib - 10;
-}
-
\f
/* Check for the availability of vCont. This function should also check
the response. */
--
1.7.10.4