gdb/remote.c: Eliminate target_buf/target_buf_size hack
[deliverable/binutils-gdb.git] / gdb / common / rsp-low.c
CommitLineData
9c3d6531
TT
1/* Low-level RSP routines for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 1988-2017 Free Software Foundation, Inc.
9c3d6531
TT
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
727605ca 20#include "common-defs.h"
9c3d6531
TT
21#include "rsp-low.h"
22
9d2d0b8b 23/* See rsp-low.h. */
9c3d6531
TT
24
25int
26fromhex (int a)
27{
28 if (a >= '0' && a <= '9')
29 return a - '0';
30 else if (a >= 'a' && a <= 'f')
31 return a - 'a' + 10;
32 else if (a >= 'A' && a <= 'F')
33 return a - 'A' + 10;
34 else
35 error (_("Reply contains invalid hex digit %d"), a);
36}
37
9d2d0b8b
TT
38/* See rsp-low.h. */
39
9c3d6531
TT
40int
41tohex (int nib)
42{
43 if (nib < 10)
44 return '0' + nib;
45 else
46 return 'a' + nib - 10;
47}
48
49/* Encode 64 bits in 16 chars of hex. */
50
51static const char hexchars[] = "0123456789abcdef";
52
53static int
54ishex (int ch, int *val)
55{
56 if ((ch >= 'a') && (ch <= 'f'))
57 {
58 *val = ch - 'a' + 10;
59 return 1;
60 }
61 if ((ch >= 'A') && (ch <= 'F'))
62 {
63 *val = ch - 'A' + 10;
64 return 1;
65 }
66 if ((ch >= '0') && (ch <= '9'))
67 {
68 *val = ch - '0';
69 return 1;
70 }
71 return 0;
72}
73
9d2d0b8b
TT
74/* See rsp-low.h. */
75
9c3d6531
TT
76char *
77pack_nibble (char *buf, int nibble)
78{
79 *buf++ = hexchars[(nibble & 0x0f)];
80 return buf;
81}
82
9d2d0b8b
TT
83/* See rsp-low.h. */
84
9c3d6531
TT
85char *
86pack_hex_byte (char *pkt, int byte)
87{
88 *pkt++ = hexchars[(byte >> 4) & 0xf];
89 *pkt++ = hexchars[(byte & 0xf)];
90 return pkt;
91}
92
9d2d0b8b
TT
93/* See rsp-low.h. */
94
9c3d6531
TT
95char *
96unpack_varlen_hex (char *buff, /* packet to parse */
97 ULONGEST *result)
98{
99 int nibble;
100 ULONGEST retval = 0;
101
102 while (ishex (*buff, &nibble))
103 {
104 buff++;
105 retval = retval << 4;
106 retval |= nibble & 0x0f;
107 }
108 *result = retval;
109 return buff;
110}
111
9d2d0b8b
TT
112/* See rsp-low.h. */
113
9c3d6531
TT
114int
115hex2bin (const char *hex, gdb_byte *bin, int count)
116{
117 int i;
118
119 for (i = 0; i < count; i++)
120 {
121 if (hex[0] == 0 || hex[1] == 0)
122 {
123 /* Hex string is short, or of uneven length.
124 Return the count that has been converted so far. */
125 return i;
126 }
127 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
128 hex += 2;
129 }
130 return i;
131}
132
9d2d0b8b
TT
133/* See rsp-low.h. */
134
0a2dde4a
SDJ
135std::string
136hex2str (const char *hex)
137{
138 std::string ret;
139 size_t len = strlen (hex);
140
141 ret.reserve (len / 2);
142 for (size_t i = 0; i < len; ++i)
143 {
144 if (hex[0] == '\0' || hex[1] == '\0')
145 {
146 /* Hex string is short, or of uneven length. Return what we
147 have so far. */
148 return ret;
149 }
150 ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
151 hex += 2;
152 }
153
154 return ret;
155}
156
157/* See rsp-low.h. */
158
9c3d6531
TT
159int
160bin2hex (const gdb_byte *bin, char *hex, int count)
161{
162 int i;
163
9c3d6531
TT
164 for (i = 0; i < count; i++)
165 {
166 *hex++ = tohex ((*bin >> 4) & 0xf);
167 *hex++ = tohex (*bin++ & 0xf);
168 }
169 *hex = 0;
170 return i;
171}
172
0a2dde4a
SDJ
173/* See rsp-low.h. */
174
175std::string
176bin2hex (const gdb_byte *bin, int count)
177{
178 std::string ret;
179
180 ret.reserve (count * 2);
181 for (int i = 0; i < count; ++i)
182 {
183 ret += tohex ((*bin >> 4) & 0xf);
184 ret += tohex (*bin++ & 0xf);
185 }
186
187 return ret;
188}
189
124e13d9
SM
190/* Return whether byte B needs escaping when sent as part of binary data. */
191
192static int
193needs_escaping (gdb_byte b)
194{
195 return b == '$' || b == '#' || b == '}' || b == '*';
196}
197
9d2d0b8b
TT
198/* See rsp-low.h. */
199
9c3d6531 200int
124e13d9
SM
201remote_escape_output (const gdb_byte *buffer, int len_units, int unit_size,
202 gdb_byte *out_buf, int *out_len_units,
203 int out_maxlen_bytes)
9c3d6531 204{
124e13d9
SM
205 int input_unit_index, output_byte_index = 0, byte_index_in_unit;
206 int number_escape_bytes_needed;
207
208 /* Try to copy integral addressable memory units until
209 (1) we run out of space or
210 (2) we copied all of them. */
211 for (input_unit_index = 0;
212 input_unit_index < len_units;
213 input_unit_index++)
9c3d6531 214 {
124e13d9
SM
215 /* Find out how many escape bytes we need for this unit. */
216 number_escape_bytes_needed = 0;
217 for (byte_index_in_unit = 0;
218 byte_index_in_unit < unit_size;
219 byte_index_in_unit++)
9c3d6531 220 {
124e13d9
SM
221 int idx = input_unit_index * unit_size + byte_index_in_unit;
222 gdb_byte b = buffer[idx];
223 if (needs_escaping (b))
224 number_escape_bytes_needed++;
9c3d6531 225 }
124e13d9
SM
226
227 /* Check if we have room to fit this escaped unit. */
228 if (output_byte_index + unit_size + number_escape_bytes_needed >
229 out_maxlen_bytes)
230 break;
231
232 /* Copy the unit byte per byte, adding escapes. */
233 for (byte_index_in_unit = 0;
234 byte_index_in_unit < unit_size;
235 byte_index_in_unit++)
9c3d6531 236 {
124e13d9
SM
237 int idx = input_unit_index * unit_size + byte_index_in_unit;
238 gdb_byte b = buffer[idx];
239 if (needs_escaping (b))
240 {
241 out_buf[output_byte_index++] = '}';
242 out_buf[output_byte_index++] = b ^ 0x20;
243 }
244 else
245 out_buf[output_byte_index++] = b;
9c3d6531
TT
246 }
247 }
248
124e13d9
SM
249 *out_len_units = input_unit_index;
250 return output_byte_index;
9c3d6531
TT
251}
252
9d2d0b8b
TT
253/* See rsp-low.h. */
254
9c3d6531
TT
255int
256remote_unescape_input (const gdb_byte *buffer, int len,
257 gdb_byte *out_buf, int out_maxlen)
258{
259 int input_index, output_index;
260 int escaped;
261
262 output_index = 0;
263 escaped = 0;
264 for (input_index = 0; input_index < len; input_index++)
265 {
266 gdb_byte b = buffer[input_index];
267
268 if (output_index + 1 > out_maxlen)
269 error (_("Received too much data from the target."));
270
271 if (escaped)
272 {
273 out_buf[output_index++] = b ^ 0x20;
274 escaped = 0;
275 }
276 else if (b == '}')
277 escaped = 1;
278 else
279 out_buf[output_index++] = b;
280 }
281
282 if (escaped)
283 error (_("Unmatched escape character in target response."));
284
285 return output_index;
286}
287
This page took 0.557802 seconds and 4 git commands to generate.