1 /* Low-level RSP routines for GDB, the GNU debugger.
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 if (a >= '0' && a <= '9')
37 else if (a >= 'a' && a <= 'f')
39 else if (a >= 'A' && a <= 'F')
42 error (_("Reply contains invalid hex digit %d"), a);
53 return 'a' + nib - 10;
56 /* Encode 64 bits in 16 chars of hex. */
58 static const char hexchars[] = "0123456789abcdef";
61 ishex (int ch, int *val)
63 if ((ch >= 'a') && (ch <= 'f'))
68 if ((ch >= 'A') && (ch <= 'F'))
73 if ((ch >= '0') && (ch <= '9'))
84 pack_nibble (char *buf, int nibble)
86 *buf++ = hexchars[(nibble & 0x0f)];
93 pack_hex_byte (char *pkt, int byte)
95 *pkt++ = hexchars[(byte >> 4) & 0xf];
96 *pkt++ = hexchars[(byte & 0xf)];
103 unpack_varlen_hex (char *buff, /* packet to parse */
109 while (ishex (*buff, &nibble))
112 retval = retval << 4;
113 retval |= nibble & 0x0f;
122 hex2bin (const char *hex, gdb_byte *bin, int count)
126 for (i = 0; i < count; i++)
128 if (hex[0] == 0 || hex[1] == 0)
130 /* Hex string is short, or of uneven length.
131 Return the count that has been converted so far. */
134 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
143 bin2hex (const gdb_byte *bin, char *hex, int count)
147 for (i = 0; i < count; i++)
149 *hex++ = tohex ((*bin >> 4) & 0xf);
150 *hex++ = tohex (*bin++ & 0xf);
159 remote_escape_output (const gdb_byte *buffer, int len,
160 gdb_byte *out_buf, int *out_len,
163 int input_index, output_index;
166 for (input_index = 0; input_index < len; input_index++)
168 gdb_byte b = buffer[input_index];
170 if (b == '$' || b == '#' || b == '}' || b == '*')
172 /* These must be escaped. */
173 if (output_index + 2 > out_maxlen)
175 out_buf[output_index++] = '}';
176 out_buf[output_index++] = b ^ 0x20;
180 if (output_index + 1 > out_maxlen)
182 out_buf[output_index++] = b;
186 *out_len = input_index;
193 remote_unescape_input (const gdb_byte *buffer, int len,
194 gdb_byte *out_buf, int out_maxlen)
196 int input_index, output_index;
201 for (input_index = 0; input_index < len; input_index++)
203 gdb_byte b = buffer[input_index];
205 if (output_index + 1 > out_maxlen)
206 error (_("Received too much data from the target."));
210 out_buf[output_index++] = b ^ 0x20;
216 out_buf[output_index++] = b;
220 error (_("Unmatched escape character in target response."));