]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/common/rsp-low.c
move some rsp bits into rsp-low.h
[thirdparty/binutils-gdb.git] / gdb / common / rsp-low.c
CommitLineData
9c3d6531
TT
1/* Low-level RSP routines for GDB, the GNU debugger.
2
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
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
20#ifdef GDBSERVER
21#include "server.h"
22#else
23#include "defs.h"
24#endif
25
26#include <string.h>
27
28#include "rsp-low.h"
29
30/* Convert hex digit A to a number. */
31
32int
33fromhex (int a)
34{
35 if (a >= '0' && a <= '9')
36 return a - '0';
37 else if (a >= 'a' && a <= 'f')
38 return a - 'a' + 10;
39 else if (a >= 'A' && a <= 'F')
40 return a - 'A' + 10;
41 else
42 error (_("Reply contains invalid hex digit %d"), a);
43}
44
45int
46tohex (int nib)
47{
48 if (nib < 10)
49 return '0' + nib;
50 else
51 return 'a' + nib - 10;
52}
53
54/* Encode 64 bits in 16 chars of hex. */
55
56static const char hexchars[] = "0123456789abcdef";
57
58static int
59ishex (int ch, int *val)
60{
61 if ((ch >= 'a') && (ch <= 'f'))
62 {
63 *val = ch - 'a' + 10;
64 return 1;
65 }
66 if ((ch >= 'A') && (ch <= 'F'))
67 {
68 *val = ch - 'A' + 10;
69 return 1;
70 }
71 if ((ch >= '0') && (ch <= '9'))
72 {
73 *val = ch - '0';
74 return 1;
75 }
76 return 0;
77}
78
79char *
80pack_nibble (char *buf, int nibble)
81{
82 *buf++ = hexchars[(nibble & 0x0f)];
83 return buf;
84}
85
86char *
87pack_hex_byte (char *pkt, int byte)
88{
89 *pkt++ = hexchars[(byte >> 4) & 0xf];
90 *pkt++ = hexchars[(byte & 0xf)];
91 return pkt;
92}
93
94char *
95unpack_varlen_hex (char *buff, /* packet to parse */
96 ULONGEST *result)
97{
98 int nibble;
99 ULONGEST retval = 0;
100
101 while (ishex (*buff, &nibble))
102 {
103 buff++;
104 retval = retval << 4;
105 retval |= nibble & 0x0f;
106 }
107 *result = retval;
108 return buff;
109}
110
111int
112hex2bin (const char *hex, gdb_byte *bin, int count)
113{
114 int i;
115
116 for (i = 0; i < count; i++)
117 {
118 if (hex[0] == 0 || hex[1] == 0)
119 {
120 /* Hex string is short, or of uneven length.
121 Return the count that has been converted so far. */
122 return i;
123 }
124 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
125 hex += 2;
126 }
127 return i;
128}
129
130int
131unhexify (char *bin, const char *hex, int count)
132{
133 int i;
134
135 for (i = 0; i < count; i++)
136 {
137 if (hex[0] == 0 || hex[1] == 0)
138 {
139 /* Hex string is short, or of uneven length.
140 Return the count that has been converted so far. */
141 return i;
142 }
143 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
144 hex += 2;
145 }
146 return i;
147}
148
149void
150convert_ascii_to_int (const char *from, unsigned char *to, int n)
151{
152 int nib1, nib2;
153 while (n--)
154 {
155 nib1 = fromhex (*from++);
156 nib2 = fromhex (*from++);
157 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
158 }
159}
160
161int
162bin2hex (const gdb_byte *bin, char *hex, int count)
163{
164 int i;
165
166 /* May use a length, or a nul-terminated string as input. */
167 if (count == 0)
168 count = strlen ((char *) bin);
169
170 for (i = 0; i < count; i++)
171 {
172 *hex++ = tohex ((*bin >> 4) & 0xf);
173 *hex++ = tohex (*bin++ & 0xf);
174 }
175 *hex = 0;
176 return i;
177}
178
179int
180hexify (char *hex, const char *bin, int count)
181{
182 int i;
183
184 /* May use a length, or a nul-terminated string as input. */
185 if (count == 0)
186 count = strlen (bin);
187
188 for (i = 0; i < count; i++)
189 {
190 *hex++ = tohex ((*bin >> 4) & 0xf);
191 *hex++ = tohex (*bin++ & 0xf);
192 }
193 *hex = 0;
194 return i;
195}
196
197void
198convert_int_to_ascii (const unsigned char *from, char *to, int n)
199{
200 int nib;
201 int ch;
202 while (n--)
203 {
204 ch = *from++;
205 nib = ((ch & 0xf0) >> 4) & 0x0f;
206 *to++ = tohex (nib);
207 nib = ch & 0x0f;
208 *to++ = tohex (nib);
209 }
210 *to++ = 0;
211}
212
213int
214remote_escape_output (const gdb_byte *buffer, int len,
215 gdb_byte *out_buf, int *out_len,
216 int out_maxlen)
217{
218 int input_index, output_index;
219
220 output_index = 0;
221 for (input_index = 0; input_index < len; input_index++)
222 {
223 gdb_byte b = buffer[input_index];
224
225 if (b == '$' || b == '#' || b == '}' || b == '*')
226 {
227 /* These must be escaped. */
228 if (output_index + 2 > out_maxlen)
229 break;
230 out_buf[output_index++] = '}';
231 out_buf[output_index++] = b ^ 0x20;
232 }
233 else
234 {
235 if (output_index + 1 > out_maxlen)
236 break;
237 out_buf[output_index++] = b;
238 }
239 }
240
241 *out_len = input_index;
242 return output_index;
243}
244
245int
246remote_unescape_input (const gdb_byte *buffer, int len,
247 gdb_byte *out_buf, int out_maxlen)
248{
249 int input_index, output_index;
250 int escaped;
251
252 output_index = 0;
253 escaped = 0;
254 for (input_index = 0; input_index < len; input_index++)
255 {
256 gdb_byte b = buffer[input_index];
257
258 if (output_index + 1 > out_maxlen)
259 error (_("Received too much data from the target."));
260
261 if (escaped)
262 {
263 out_buf[output_index++] = b ^ 0x20;
264 escaped = 0;
265 }
266 else if (b == '}')
267 escaped = 1;
268 else
269 out_buf[output_index++] = b;
270 }
271
272 if (escaped)
273 error (_("Unmatched escape character in target response."));
274
275 return output_index;
276}
277