@@ -92,9 +92,33 @@ static const std::string JSON_ASYNC_TYPE_KEY_NAME("type");
9292 std::setfill (' \t ' ) << std::setw((iword_idx)) << ""
9393// Class to handle communications via gdb remote protocol.
9494
95- // Prototypes
95+ // If `ch` is a meta character as per the binary packet convention in the
96+ // gdb-remote protocol, quote it and write it into `stream`, otherwise write it
97+ // as is.
98+ static void binary_encode_char(std::ostringstream &stream, char ch) {
99+ if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
100+ stream.put (' }' );
101+ stream.put (ch ^ 0x20 );
102+ } else {
103+ stream.put (ch);
104+ }
105+ }
106+
107+ // Equivalent to calling binary_encode_char for every element of `data`.
108+ static void binary_encode_data_vector (std::ostringstream &stream,
109+ std::vector<uint8_t > data) {
110+ for (auto ch : data)
111+ binary_encode_char (stream, ch);
112+ }
96113
97- static std::string binary_encode_string(const std::string &s);
114+ // Quote any meta characters in a std::string as per the binary
115+ // packet convention in the gdb-remote protocol.
116+ static std::string binary_encode_string (const std::string &s) {
117+ std::ostringstream stream;
118+ for (char ch : s)
119+ binary_encode_char (stream, ch);
120+ return stream.str ();
121+ }
98122
99123// Decode a single hex character and return the hex value as a number or
100124// -1 if "ch" is not a hex character.
@@ -1303,26 +1327,6 @@ std::vector<uint8_t> decode_binary_data(const char *str, size_t len) {
13031327 return bytes;
13041328}
13051329
1306- // Quote any meta characters in a std::string as per the binary
1307- // packet convention in the gdb-remote protocol.
1308-
1309- static std::string binary_encode_string (const std::string &s) {
1310- std::string output;
1311- const size_t s_size = s.size ();
1312- const char *s_chars = s.c_str ();
1313-
1314- for (size_t i = 0 ; i < s_size; i++) {
1315- unsigned char ch = *(s_chars + i);
1316- if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
1317- output.push_back (' }' ); // 0x7d
1318- output.push_back (ch ^ 0x20 );
1319- } else {
1320- output.push_back (ch);
1321- }
1322- }
1323- return output;
1324- }
1325-
13261330// If the value side of a key-value pair in JSON is a string,
13271331// and that string has a " character in it, the " character must
13281332// 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