Skip to content

Commit dc36087

Browse files
[debugserver][NFC] Add helper function for escaping special characters (llvm#162297)
This code was duplicated in multiple places and a subsequent patch will need to do it again. (cherry picked from commit 7ab7554)
1 parent 0d39058 commit dc36087

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

lldb/tools/debugserver/source/RNBRemote.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,33 @@ static const std::string JSON_ASYNC_TYPE_KEY_NAME("type");
9191
std::setfill('\t') << std::setw((iword_idx)) << ""
9292
// Class to handle communications via gdb remote protocol.
9393

94-
// Prototypes
94+
// If `ch` is a meta character as per the binary packet convention in the
95+
// gdb-remote protocol, quote it and write it into `stream`, otherwise write it
96+
// as is.
97+
static void binary_encode_char(std::ostringstream &stream, char ch) {
98+
if (ch == '#' || ch == '$' || ch == '}' || ch == '*') {
99+
stream.put('}');
100+
stream.put(ch ^ 0x20);
101+
} else {
102+
stream.put(ch);
103+
}
104+
}
105+
106+
// Equivalent to calling binary_encode_char for every element of `data`.
107+
static void binary_encode_data_vector(std::ostringstream &stream,
108+
std::vector<uint8_t> data) {
109+
for (auto ch : data)
110+
binary_encode_char(stream, ch);
111+
}
95112

96-
static std::string binary_encode_string(const std::string &s);
113+
// Quote any meta characters in a std::string as per the binary
114+
// packet convention in the gdb-remote protocol.
115+
static std::string binary_encode_string(const std::string &s) {
116+
std::ostringstream stream;
117+
for (char ch : s)
118+
binary_encode_char(stream, ch);
119+
return stream.str();
120+
}
97121

98122
// Decode a single hex character and return the hex value as a number or
99123
// -1 if "ch" is not a hex character.
@@ -1298,26 +1322,6 @@ std::vector<uint8_t> decode_binary_data(const char *str, size_t len) {
12981322
return bytes;
12991323
}
13001324

1301-
// Quote any meta characters in a std::string as per the binary
1302-
// packet convention in the gdb-remote protocol.
1303-
1304-
static std::string binary_encode_string(const std::string &s) {
1305-
std::string output;
1306-
const size_t s_size = s.size();
1307-
const char *s_chars = s.c_str();
1308-
1309-
for (size_t i = 0; i < s_size; i++) {
1310-
unsigned char ch = *(s_chars + i);
1311-
if (ch == '#' || ch == '$' || ch == '}' || ch == '*') {
1312-
output.push_back('}'); // 0x7d
1313-
output.push_back(ch ^ 0x20);
1314-
} else {
1315-
output.push_back(ch);
1316-
}
1317-
}
1318-
return output;
1319-
}
1320-
13211325
// If the value side of a key-value pair in JSON is a string,
13221326
// and that string has a " character in it, the " character must
13231327
// be escaped.
@@ -3210,21 +3214,9 @@ rnb_err_t RNBRemote::HandlePacket_x(const char *p) {
32103214
return SendErrorPacket("E80");
32113215
}
32123216

3213-
std::vector<uint8_t> buf_quoted;
3214-
buf_quoted.reserve(bytes_read + 30);
3215-
for (nub_size_t i = 0; i < bytes_read; i++) {
3216-
if (buf[i] == '#' || buf[i] == '$' || buf[i] == '}' || buf[i] == '*') {
3217-
buf_quoted.push_back(0x7d);
3218-
buf_quoted.push_back(buf[i] ^ 0x20);
3219-
} else {
3220-
buf_quoted.push_back(buf[i]);
3221-
}
3222-
}
3223-
length = buf_quoted.size();
3224-
3217+
buf.resize(bytes_read);
32253218
std::ostringstream ostrm;
3226-
for (unsigned long i = 0; i < length; i++)
3227-
ostrm << buf_quoted[i];
3219+
binary_encode_data_vector(ostrm, buf);
32283220

32293221
return SendPacket(ostrm.str());
32303222
}

0 commit comments

Comments
 (0)