gdb/common/rsp-low.c - gdb
Global variables defined
Functions defined
Source code
- #include "common-defs.h"
- #include "rsp-low.h"
- 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);
- }
- int
- tohex (int nib)
- {
- if (nib < 10)
- return '0' + nib;
- else
- return 'a' + nib - 10;
- }
- static const char hexchars[] = "0123456789abcdef";
- static int
- ishex (int ch, int *val)
- {
- if ((ch >= 'a') && (ch <= 'f'))
- {
- *val = ch - 'a' + 10;
- return 1;
- }
- if ((ch >= 'A') && (ch <= 'F'))
- {
- *val = ch - 'A' + 10;
- return 1;
- }
- if ((ch >= '0') && (ch <= '9'))
- {
- *val = ch - '0';
- return 1;
- }
- return 0;
- }
- char *
- pack_nibble (char *buf, int nibble)
- {
- *buf++ = hexchars[(nibble & 0x0f)];
- return buf;
- }
- char *
- pack_hex_byte (char *pkt, int byte)
- {
- *pkt++ = hexchars[(byte >> 4) & 0xf];
- *pkt++ = hexchars[(byte & 0xf)];
- return pkt;
- }
- char *
- unpack_varlen_hex (char *buff,
- ULONGEST *result)
- {
- int nibble;
- ULONGEST retval = 0;
- while (ishex (*buff, &nibble))
- {
- buff++;
- retval = retval << 4;
- retval |= nibble & 0x0f;
- }
- *result = retval;
- return buff;
- }
- int
- hex2bin (const char *hex, gdb_byte *bin, int count)
- {
- int i;
- for (i = 0; i < count; i++)
- {
- if (hex[0] == 0 || hex[1] == 0)
- {
-
- return i;
- }
- *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
- hex += 2;
- }
- return i;
- }
- int
- bin2hex (const gdb_byte *bin, char *hex, int count)
- {
- int i;
- for (i = 0; i < count; i++)
- {
- *hex++ = tohex ((*bin >> 4) & 0xf);
- *hex++ = tohex (*bin++ & 0xf);
- }
- *hex = 0;
- return i;
- }
- int
- remote_escape_output (const gdb_byte *buffer, int len,
- gdb_byte *out_buf, int *out_len,
- int out_maxlen)
- {
- int input_index, output_index;
- output_index = 0;
- for (input_index = 0; input_index < len; input_index++)
- {
- gdb_byte b = buffer[input_index];
- if (b == '$' || b == '#' || b == '}' || b == '*')
- {
-
- if (output_index + 2 > out_maxlen)
- break;
- out_buf[output_index++] = '}';
- out_buf[output_index++] = b ^ 0x20;
- }
- else
- {
- if (output_index + 1 > out_maxlen)
- break;
- out_buf[output_index++] = b;
- }
- }
- *out_len = input_index;
- return output_index;
- }
- int
- remote_unescape_input (const gdb_byte *buffer, int len,
- gdb_byte *out_buf, int out_maxlen)
- {
- int input_index, output_index;
- int escaped;
- output_index = 0;
- escaped = 0;
- for (input_index = 0; input_index < len; input_index++)
- {
- gdb_byte b = buffer[input_index];
- if (output_index + 1 > out_maxlen)
- error (_("Received too much data from the target."));
- if (escaped)
- {
- out_buf[output_index++] = b ^ 0x20;
- escaped = 0;
- }
- else if (b == '}')
- escaped = 1;
- else
- out_buf[output_index++] = b;
- }
- if (escaped)
- error (_("Unmatched escape character in target response."));
- return output_index;
- }