]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/common/rsp-low.c
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / common / rsp-low.c
1 /* Low-level RSP routines for GDB, the GNU debugger.
2
3 Copyright (C) 1988-2015 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 #include "common-defs.h"
21 #include "rsp-low.h"
22
23 /* See rsp-low.h. */
24
25 int
26 fromhex (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
38 /* See rsp-low.h. */
39
40 int
41 tohex (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
51 static const char hexchars[] = "0123456789abcdef";
52
53 static int
54 ishex (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
74 /* See rsp-low.h. */
75
76 char *
77 pack_nibble (char *buf, int nibble)
78 {
79 *buf++ = hexchars[(nibble & 0x0f)];
80 return buf;
81 }
82
83 /* See rsp-low.h. */
84
85 char *
86 pack_hex_byte (char *pkt, int byte)
87 {
88 *pkt++ = hexchars[(byte >> 4) & 0xf];
89 *pkt++ = hexchars[(byte & 0xf)];
90 return pkt;
91 }
92
93 /* See rsp-low.h. */
94
95 char *
96 unpack_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
112 /* See rsp-low.h. */
113
114 int
115 hex2bin (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
133 /* See rsp-low.h. */
134
135 int
136 bin2hex (const gdb_byte *bin, char *hex, int count)
137 {
138 int i;
139
140 for (i = 0; i < count; i++)
141 {
142 *hex++ = tohex ((*bin >> 4) & 0xf);
143 *hex++ = tohex (*bin++ & 0xf);
144 }
145 *hex = 0;
146 return i;
147 }
148
149 /* See rsp-low.h. */
150
151 int
152 remote_escape_output (const gdb_byte *buffer, int len,
153 gdb_byte *out_buf, int *out_len,
154 int out_maxlen)
155 {
156 int input_index, output_index;
157
158 output_index = 0;
159 for (input_index = 0; input_index < len; input_index++)
160 {
161 gdb_byte b = buffer[input_index];
162
163 if (b == '$' || b == '#' || b == '}' || b == '*')
164 {
165 /* These must be escaped. */
166 if (output_index + 2 > out_maxlen)
167 break;
168 out_buf[output_index++] = '}';
169 out_buf[output_index++] = b ^ 0x20;
170 }
171 else
172 {
173 if (output_index + 1 > out_maxlen)
174 break;
175 out_buf[output_index++] = b;
176 }
177 }
178
179 *out_len = input_index;
180 return output_index;
181 }
182
183 /* See rsp-low.h. */
184
185 int
186 remote_unescape_input (const gdb_byte *buffer, int len,
187 gdb_byte *out_buf, int out_maxlen)
188 {
189 int input_index, output_index;
190 int escaped;
191
192 output_index = 0;
193 escaped = 0;
194 for (input_index = 0; input_index < len; input_index++)
195 {
196 gdb_byte b = buffer[input_index];
197
198 if (output_index + 1 > out_maxlen)
199 error (_("Received too much data from the target."));
200
201 if (escaped)
202 {
203 out_buf[output_index++] = b ^ 0x20;
204 escaped = 0;
205 }
206 else if (b == '}')
207 escaped = 1;
208 else
209 out_buf[output_index++] = b;
210 }
211
212 if (escaped)
213 error (_("Unmatched escape character in target response."));
214
215 return output_index;
216 }
217