/* Low-level RSP routines for GDB, the GNU debugger.
- Copyright (C) 1988-2014 Free Software Foundation, Inc.
+ Copyright (C) 1988-2016 Free Software Foundation, Inc.
This file is part of GDB.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
-#ifdef GDBSERVER
-#include "server.h"
-#else
-#include "defs.h"
-#endif
-
-#include <string.h>
-
+#include "common-defs.h"
#include "rsp-low.h"
-/* Convert hex digit A to a number. */
+/* See rsp-low.h. */
int
fromhex (int a)
error (_("Reply contains invalid hex digit %d"), a);
}
+/* See rsp-low.h. */
+
int
tohex (int nib)
{
return 0;
}
+/* See rsp-low.h. */
+
char *
pack_nibble (char *buf, int nibble)
{
return buf;
}
+/* See rsp-low.h. */
+
char *
pack_hex_byte (char *pkt, int byte)
{
return pkt;
}
+/* See rsp-low.h. */
+
char *
unpack_varlen_hex (char *buff, /* packet to parse */
ULONGEST *result)
return buff;
}
+/* See rsp-low.h. */
+
int
hex2bin (const char *hex, gdb_byte *bin, int count)
{
return i;
}
-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
-convert_ascii_to_int (const char *from, unsigned char *to, int n)
-{
- int nib1, nib2;
- while (n--)
- {
- nib1 = fromhex (*from++);
- nib2 = fromhex (*from++);
- *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
- }
-}
+/* See rsp-low.h. */
int
bin2hex (const gdb_byte *bin, char *hex, int count)
return i;
}
-int
-hexify (char *hex, const char *bin, int count)
-{
- int i;
-
- for (i = 0; i < count; i++)
- {
- *hex++ = tohex ((*bin >> 4) & 0xf);
- *hex++ = tohex (*bin++ & 0xf);
- }
- *hex = 0;
- return i;
-}
+/* Return whether byte B needs escaping when sent as part of binary data. */
-void
-convert_int_to_ascii (const unsigned char *from, char *to, int n)
+static int
+needs_escaping (gdb_byte b)
{
- int nib;
- int ch;
- while (n--)
- {
- ch = *from++;
- nib = ((ch & 0xf0) >> 4) & 0x0f;
- *to++ = tohex (nib);
- nib = ch & 0x0f;
- *to++ = tohex (nib);
- }
- *to++ = 0;
+ return b == '$' || b == '#' || b == '}' || b == '*';
}
+/* See rsp-low.h. */
+
int
-remote_escape_output (const gdb_byte *buffer, int len,
- gdb_byte *out_buf, int *out_len,
- int out_maxlen)
+remote_escape_output (const gdb_byte *buffer, int len_units, int unit_size,
+ gdb_byte *out_buf, int *out_len_units,
+ int out_maxlen_bytes)
{
- int input_index, output_index;
-
- output_index = 0;
- for (input_index = 0; input_index < len; input_index++)
+ int input_unit_index, output_byte_index = 0, byte_index_in_unit;
+ int number_escape_bytes_needed;
+
+ /* Try to copy integral addressable memory units until
+ (1) we run out of space or
+ (2) we copied all of them. */
+ for (input_unit_index = 0;
+ input_unit_index < len_units;
+ input_unit_index++)
{
- gdb_byte b = buffer[input_index];
-
- if (b == '$' || b == '#' || b == '}' || b == '*')
+ /* Find out how many escape bytes we need for this unit. */
+ number_escape_bytes_needed = 0;
+ for (byte_index_in_unit = 0;
+ byte_index_in_unit < unit_size;
+ byte_index_in_unit++)
{
- /* These must be escaped. */
- if (output_index + 2 > out_maxlen)
- break;
- out_buf[output_index++] = '}';
- out_buf[output_index++] = b ^ 0x20;
+ int idx = input_unit_index * unit_size + byte_index_in_unit;
+ gdb_byte b = buffer[idx];
+ if (needs_escaping (b))
+ number_escape_bytes_needed++;
}
- else
+
+ /* Check if we have room to fit this escaped unit. */
+ if (output_byte_index + unit_size + number_escape_bytes_needed >
+ out_maxlen_bytes)
+ break;
+
+ /* Copy the unit byte per byte, adding escapes. */
+ for (byte_index_in_unit = 0;
+ byte_index_in_unit < unit_size;
+ byte_index_in_unit++)
{
- if (output_index + 1 > out_maxlen)
- break;
- out_buf[output_index++] = b;
+ int idx = input_unit_index * unit_size + byte_index_in_unit;
+ gdb_byte b = buffer[idx];
+ if (needs_escaping (b))
+ {
+ out_buf[output_byte_index++] = '}';
+ out_buf[output_byte_index++] = b ^ 0x20;
+ }
+ else
+ out_buf[output_byte_index++] = b;
}
}
- *out_len = input_index;
- return output_index;
+ *out_len_units = input_unit_index;
+ return output_byte_index;
}
+/* See rsp-low.h. */
+
int
remote_unescape_input (const gdb_byte *buffer, int len,
gdb_byte *out_buf, int out_maxlen)