@@ -95,9 +95,33 @@ static const std::string JSON_ASYNC_TYPE_KEY_NAME("type");
9595 std::setfill (' \t ' ) << std::setw((iword_idx)) << ""
9696// Class to handle communications via gdb remote protocol.
9797
98- // Prototypes
98+ // If `ch` is a meta character as per the binary packet convention in the
99+ // gdb-remote protocol, quote it and write it into `stream`, otherwise write it
100+ // as is.
101+ static void binary_encode_char(std::ostringstream &stream, char ch) {
102+ if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
103+ stream.put (' }' );
104+ stream.put (ch ^ 0x20 );
105+ } else {
106+ stream.put (ch);
107+ }
108+ }
109+
110+ // Equivalent to calling binary_encode_char for every element of `data`.
111+ static void binary_encode_data_vector (std::ostringstream &stream,
112+ std::vector<uint8_t > data) {
113+ for (auto ch : data)
114+ binary_encode_char (stream, ch);
115+ }
99116
100- static std::string binary_encode_string(const std::string &s);
117+ // Quote any meta characters in a std::string as per the binary
118+ // packet convention in the gdb-remote protocol.
119+ static std::string binary_encode_string (const std::string &s) {
120+ std::ostringstream stream;
121+ for (char ch : s)
122+ binary_encode_char (stream, ch);
123+ return stream.str ();
124+ }
101125
102126// Decode a single hex character and return the hex value as a number or
103127// -1 if "ch" is not a hex character.
@@ -1304,26 +1328,6 @@ std::vector<uint8_t> decode_binary_data(const char *str, size_t len) {
13041328 return bytes;
13051329}
13061330
1307- // Quote any meta characters in a std::string as per the binary
1308- // packet convention in the gdb-remote protocol.
1309-
1310- static std::string binary_encode_string (const std::string &s) {
1311- std::string output;
1312- const size_t s_size = s.size ();
1313- const char *s_chars = s.c_str ();
1314-
1315- for (size_t i = 0 ; i < s_size; i++) {
1316- unsigned char ch = *(s_chars + i);
1317- if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
1318- output.push_back (' }' ); // 0x7d
1319- output.push_back (ch ^ 0x20 );
1320- } else {
1321- output.push_back (ch);
1322- }
1323- }
1324- return output;
1325- }
1326-
13271331// If the value side of a key-value pair in JSON is a string,
13281332// and that string has a " character in it, the " character must
13291333// be escaped.
@@ -3216,21 +3220,9 @@ rnb_err_t RNBRemote::HandlePacket_x(const char *p) {
32163220 return SendErrorPacket (" E80" );
32173221 }
32183222
3219- std::vector<uint8_t > buf_quoted;
3220- buf_quoted.reserve (bytes_read + 30 );
3221- for (nub_size_t i = 0 ; i < bytes_read; i++) {
3222- if (buf[i] == ' #' || buf[i] == ' $' || buf[i] == ' }' || buf[i] == ' *' ) {
3223- buf_quoted.push_back (0x7d );
3224- buf_quoted.push_back (buf[i] ^ 0x20 );
3225- } else {
3226- buf_quoted.push_back (buf[i]);
3227- }
3228- }
3229- length = buf_quoted.size ();
3230-
3223+ buf.resize (bytes_read);
32313224 std::ostringstream ostrm;
3232- for (unsigned long i = 0 ; i < length; i++)
3233- ostrm << buf_quoted[i];
3225+ binary_encode_data_vector (ostrm, buf);
32343226
32353227 return SendPacket (ostrm.str ());
32363228}
0 commit comments