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/>. */
30 /* Convert hex digit A to a number. */
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
);
51 return 'a' + nib
- 10;
54 /* Encode 64 bits in 16 chars of hex. */
56 static const char hexchars
[] = "0123456789abcdef";
59 ishex (int ch
, int *val
)
61 if ((ch
>= 'a') && (ch
<= 'f'))
66 if ((ch
>= 'A') && (ch
<= 'F'))
71 if ((ch
>= '0') && (ch
<= '9'))
80 pack_nibble (char *buf
, int nibble
)
82 *buf
++ = hexchars
[(nibble
& 0x0f)];
87 pack_hex_byte (char *pkt
, int byte
)
89 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
90 *pkt
++ = hexchars
[(byte
& 0xf)];
95 unpack_varlen_hex (char *buff
, /* packet to parse */
101 while (ishex (*buff
, &nibble
))
104 retval
= retval
<< 4;
105 retval
|= nibble
& 0x0f;
112 hex2bin (const char *hex
, gdb_byte
*bin
, int count
)
116 for (i
= 0; i
< count
; i
++)
118 if (hex
[0] == 0 || hex
[1] == 0)
120 /* Hex string is short, or of uneven length.
121 Return the count that has been converted so far. */
124 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
131 unhexify (char *bin
, const char *hex
, int count
)
135 for (i
= 0; i
< count
; i
++)
137 if (hex
[0] == 0 || hex
[1] == 0)
139 /* Hex string is short, or of uneven length.
140 Return the count that has been converted so far. */
143 *bin
++ = fromhex (hex
[0]) * 16 + fromhex (hex
[1]);
150 convert_ascii_to_int (const char *from
, unsigned char *to
, int n
)
155 nib1
= fromhex (*from
++);
156 nib2
= fromhex (*from
++);
157 *to
++ = (((nib1
& 0x0f) << 4) & 0xf0) | (nib2
& 0x0f);
162 bin2hex (const gdb_byte
*bin
, char *hex
, int count
)
166 for (i
= 0; i
< count
; i
++)
168 *hex
++ = tohex ((*bin
>> 4) & 0xf);
169 *hex
++ = tohex (*bin
++ & 0xf);
176 hexify (char *hex
, const char *bin
, int count
)
180 /* May use a length, or a nul-terminated string as input. */
182 count
= strlen (bin
);
184 for (i
= 0; i
< count
; i
++)
186 *hex
++ = tohex ((*bin
>> 4) & 0xf);
187 *hex
++ = tohex (*bin
++ & 0xf);
194 convert_int_to_ascii (const unsigned char *from
, char *to
, int n
)
201 nib
= ((ch
& 0xf0) >> 4) & 0x0f;
210 remote_escape_output (const gdb_byte
*buffer
, int len
,
211 gdb_byte
*out_buf
, int *out_len
,
214 int input_index
, output_index
;
217 for (input_index
= 0; input_index
< len
; input_index
++)
219 gdb_byte b
= buffer
[input_index
];
221 if (b
== '$' || b
== '#' || b
== '}' || b
== '*')
223 /* These must be escaped. */
224 if (output_index
+ 2 > out_maxlen
)
226 out_buf
[output_index
++] = '}';
227 out_buf
[output_index
++] = b
^ 0x20;
231 if (output_index
+ 1 > out_maxlen
)
233 out_buf
[output_index
++] = b
;
237 *out_len
= input_index
;
242 remote_unescape_input (const gdb_byte
*buffer
, int len
,
243 gdb_byte
*out_buf
, int out_maxlen
)
245 int input_index
, output_index
;
250 for (input_index
= 0; input_index
< len
; input_index
++)
252 gdb_byte b
= buffer
[input_index
];
254 if (output_index
+ 1 > out_maxlen
)
255 error (_("Received too much data from the target."));
259 out_buf
[output_index
++] = b
^ 0x20;
265 out_buf
[output_index
++] = b
;
269 error (_("Unmatched escape character in target response."));
This page took 0.045047 seconds and 4 git commands to generate.