Include string.h in common-defs.h
[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 "rsp-low.h"
27
28 /* See rsp-low.h. */
29
30 int
31 fromhex (int a)
32 {
33 if (a >= '0' && a <= '9')
34 return a - '0';
35 else if (a >= 'a' && a <= 'f')
36 return a - 'a' + 10;
37 else if (a >= 'A' && a <= 'F')
38 return a - 'A' + 10;
39 else
40 error (_("Reply contains invalid hex digit %d"), a);
41 }
42
43 /* See rsp-low.h. */
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 /* See rsp-low.h. */
80
81 char *
82 pack_nibble (char *buf, int nibble)
83 {
84 *buf++ = hexchars[(nibble & 0x0f)];
85 return buf;
86 }
87
88 /* See rsp-low.h. */
89
90 char *
91 pack_hex_byte (char *pkt, int byte)
92 {
93 *pkt++ = hexchars[(byte >> 4) & 0xf];
94 *pkt++ = hexchars[(byte & 0xf)];
95 return pkt;
96 }
97
98 /* See rsp-low.h. */
99
100 char *
101 unpack_varlen_hex (char *buff, /* packet to parse */
102 ULONGEST *result)
103 {
104 int nibble;
105 ULONGEST retval = 0;
106
107 while (ishex (*buff, &nibble))
108 {
109 buff++;
110 retval = retval << 4;
111 retval |= nibble & 0x0f;
112 }
113 *result = retval;
114 return buff;
115 }
116
117 /* See rsp-low.h. */
118
119 int
120 hex2bin (const char *hex, gdb_byte *bin, int count)
121 {
122 int i;
123
124 for (i = 0; i < count; i++)
125 {
126 if (hex[0] == 0 || hex[1] == 0)
127 {
128 /* Hex string is short, or of uneven length.
129 Return the count that has been converted so far. */
130 return i;
131 }
132 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
133 hex += 2;
134 }
135 return i;
136 }
137
138 /* See rsp-low.h. */
139
140 int
141 bin2hex (const gdb_byte *bin, char *hex, int count)
142 {
143 int i;
144
145 for (i = 0; i < count; i++)
146 {
147 *hex++ = tohex ((*bin >> 4) & 0xf);
148 *hex++ = tohex (*bin++ & 0xf);
149 }
150 *hex = 0;
151 return i;
152 }
153
154 /* See rsp-low.h. */
155
156 int
157 remote_escape_output (const gdb_byte *buffer, int len,
158 gdb_byte *out_buf, int *out_len,
159 int out_maxlen)
160 {
161 int input_index, output_index;
162
163 output_index = 0;
164 for (input_index = 0; input_index < len; input_index++)
165 {
166 gdb_byte b = buffer[input_index];
167
168 if (b == '$' || b == '#' || b == '}' || b == '*')
169 {
170 /* These must be escaped. */
171 if (output_index + 2 > out_maxlen)
172 break;
173 out_buf[output_index++] = '}';
174 out_buf[output_index++] = b ^ 0x20;
175 }
176 else
177 {
178 if (output_index + 1 > out_maxlen)
179 break;
180 out_buf[output_index++] = b;
181 }
182 }
183
184 *out_len = input_index;
185 return output_index;
186 }
187
188 /* See rsp-low.h. */
189
190 int
191 remote_unescape_input (const gdb_byte *buffer, int len,
192 gdb_byte *out_buf, int out_maxlen)
193 {
194 int input_index, output_index;
195 int escaped;
196
197 output_index = 0;
198 escaped = 0;
199 for (input_index = 0; input_index < len; input_index++)
200 {
201 gdb_byte b = buffer[input_index];
202
203 if (output_index + 1 > out_maxlen)
204 error (_("Received too much data from the target."));
205
206 if (escaped)
207 {
208 out_buf[output_index++] = b ^ 0x20;
209 escaped = 0;
210 }
211 else if (b == '}')
212 escaped = 1;
213 else
214 out_buf[output_index++] = b;
215 }
216
217 if (escaped)
218 error (_("Unmatched escape character in target response."));
219
220 return output_index;
221 }
222
This page took 0.047209 seconds and 5 git commands to generate.