don't let bin2hex call strlen
[deliverable/binutils-gdb.git] / gdb / common / rsp-low.c
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
32 int
33 fromhex (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
45 int
46 tohex (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
56 static const char hexchars[] = "0123456789abcdef";
57
58 static int
59 ishex (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
79 char *
80 pack_nibble (char *buf, int nibble)
81 {
82 *buf++ = hexchars[(nibble & 0x0f)];
83 return buf;
84 }
85
86 char *
87 pack_hex_byte (char *pkt, int byte)
88 {
89 *pkt++ = hexchars[(byte >> 4) & 0xf];
90 *pkt++ = hexchars[(byte & 0xf)];
91 return pkt;
92 }
93
94 char *
95 unpack_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
111 int
112 hex2bin (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
130 int
131 unhexify (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
149 void
150 convert_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
161 int
162 bin2hex (const gdb_byte *bin, char *hex, int count)
163 {
164 int i;
165
166 for (i = 0; i < count; i++)
167 {
168 *hex++ = tohex ((*bin >> 4) & 0xf);
169 *hex++ = tohex (*bin++ & 0xf);
170 }
171 *hex = 0;
172 return i;
173 }
174
175 int
176 hexify (char *hex, const char *bin, int count)
177 {
178 int i;
179
180 /* May use a length, or a nul-terminated string as input. */
181 if (count == 0)
182 count = strlen (bin);
183
184 for (i = 0; i < count; i++)
185 {
186 *hex++ = tohex ((*bin >> 4) & 0xf);
187 *hex++ = tohex (*bin++ & 0xf);
188 }
189 *hex = 0;
190 return i;
191 }
192
193 void
194 convert_int_to_ascii (const unsigned char *from, char *to, int n)
195 {
196 int nib;
197 int ch;
198 while (n--)
199 {
200 ch = *from++;
201 nib = ((ch & 0xf0) >> 4) & 0x0f;
202 *to++ = tohex (nib);
203 nib = ch & 0x0f;
204 *to++ = tohex (nib);
205 }
206 *to++ = 0;
207 }
208
209 int
210 remote_escape_output (const gdb_byte *buffer, int len,
211 gdb_byte *out_buf, int *out_len,
212 int out_maxlen)
213 {
214 int input_index, output_index;
215
216 output_index = 0;
217 for (input_index = 0; input_index < len; input_index++)
218 {
219 gdb_byte b = buffer[input_index];
220
221 if (b == '$' || b == '#' || b == '}' || b == '*')
222 {
223 /* These must be escaped. */
224 if (output_index + 2 > out_maxlen)
225 break;
226 out_buf[output_index++] = '}';
227 out_buf[output_index++] = b ^ 0x20;
228 }
229 else
230 {
231 if (output_index + 1 > out_maxlen)
232 break;
233 out_buf[output_index++] = b;
234 }
235 }
236
237 *out_len = input_index;
238 return output_index;
239 }
240
241 int
242 remote_unescape_input (const gdb_byte *buffer, int len,
243 gdb_byte *out_buf, int out_maxlen)
244 {
245 int input_index, output_index;
246 int escaped;
247
248 output_index = 0;
249 escaped = 0;
250 for (input_index = 0; input_index < len; input_index++)
251 {
252 gdb_byte b = buffer[input_index];
253
254 if (output_index + 1 > out_maxlen)
255 error (_("Received too much data from the target."));
256
257 if (escaped)
258 {
259 out_buf[output_index++] = b ^ 0x20;
260 escaped = 0;
261 }
262 else if (b == '}')
263 escaped = 1;
264 else
265 out_buf[output_index++] = b;
266 }
267
268 if (escaped)
269 error (_("Unmatched escape character in target response."));
270
271 return output_index;
272 }
273
This page took 0.045047 seconds and 4 git commands to generate.