@@ -113,6 +113,8 @@ class JSONGenerator {
113
113
114
114
virtual void Dump (std::ostream &s) const = 0;
115
115
116
+ virtual void DumpBinaryEscaped (std::ostream &s) const = 0;
117
+
116
118
private:
117
119
Type m_type;
118
120
};
@@ -136,6 +138,17 @@ class JSONGenerator {
136
138
s << " ]" ;
137
139
}
138
140
141
+ void DumpBinaryEscaped (std::ostream &s) const override {
142
+ s << " [" ;
143
+ const size_t arrsize = m_items.size ();
144
+ for (size_t i = 0 ; i < arrsize; ++i) {
145
+ m_items[i]->DumpBinaryEscaped (s);
146
+ if (i + 1 < arrsize)
147
+ s << " ," ;
148
+ }
149
+ s << " ]" ;
150
+ }
151
+
139
152
protected:
140
153
typedef std::vector<ObjectSP> collection;
141
154
collection m_items;
@@ -151,6 +164,8 @@ class JSONGenerator {
151
164
152
165
void Dump (std::ostream &s) const override { s << m_value; }
153
166
167
+ void DumpBinaryEscaped (std::ostream &s) const override { Dump (s); }
168
+
154
169
protected:
155
170
uint64_t m_value;
156
171
};
@@ -165,6 +180,8 @@ class JSONGenerator {
165
180
166
181
void Dump (std::ostream &s) const override { s << m_value; }
167
182
183
+ void DumpBinaryEscaped (std::ostream &s) const override { Dump (s); }
184
+
168
185
protected:
169
186
double m_value;
170
187
};
@@ -184,6 +201,8 @@ class JSONGenerator {
184
201
s << " false" ;
185
202
}
186
203
204
+ void DumpBinaryEscaped (std::ostream &s) const override { Dump (s); }
205
+
187
206
protected:
188
207
bool m_value;
189
208
};
@@ -199,15 +218,33 @@ class JSONGenerator {
199
218
void SetValue (const std::string &string) { m_value = string; }
200
219
201
220
void Dump (std::ostream &s) const override {
202
- std::string quoted;
221
+ s << ' "' ;
222
+ const size_t strsize = m_value.size ();
223
+ for (size_t i = 0 ; i < strsize; ++i) {
224
+ char ch = m_value[i];
225
+ if (ch == ' "' )
226
+ s << ' \\ ' ;
227
+ s << ch;
228
+ }
229
+ s << ' "' ;
230
+ }
231
+
232
+ void DumpBinaryEscaped (std::ostream &s) const override {
233
+ s << ' "' ;
203
234
const size_t strsize = m_value.size ();
204
235
for (size_t i = 0 ; i < strsize; ++i) {
205
236
char ch = m_value[i];
206
237
if (ch == ' "' )
207
- quoted.push_back (' \\ ' );
208
- quoted.push_back (ch);
238
+ s << ' \\ ' ;
239
+ // gdb remote serial protocol binary escaping
240
+ if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
241
+ s << ' }' ; // 0x7d next character is escaped
242
+ s << static_cast <char >(ch ^ 0x20 );
243
+ } else {
244
+ s << ch;
245
+ }
209
246
}
210
- s << ' "' << quoted. c_str () << ' " ' ;
247
+ s << ' "' ;
211
248
}
212
249
213
250
protected:
@@ -269,7 +306,43 @@ class JSONGenerator {
269
306
s << " }" ;
270
307
}
271
308
309
+ void DumpBinaryEscaped (std::ostream &s) const override {
310
+ bool have_printed_one_elem = false ;
311
+ s << " {" ;
312
+ for (collection::const_iterator iter = m_dict.begin ();
313
+ iter != m_dict.end (); ++iter) {
314
+ if (!have_printed_one_elem) {
315
+ have_printed_one_elem = true ;
316
+ } else {
317
+ s << " ," ;
318
+ }
319
+ s << " \" " << binary_encode_string (iter->first ) << " \" :" ;
320
+ iter->second ->DumpBinaryEscaped (s);
321
+ }
322
+ // '}' must be escaped for the gdb remote serial
323
+ // protocol.
324
+ s << " }" ;
325
+ s << static_cast <char >(' }' ^ 0x20 );
326
+ }
327
+
272
328
protected:
329
+ std::string binary_encode_string (const std::string &s) const {
330
+ std::string output;
331
+ const size_t s_size = s.size ();
332
+ const char *s_chars = s.c_str ();
333
+
334
+ for (size_t i = 0 ; i < s_size; i++) {
335
+ unsigned char ch = *(s_chars + i);
336
+ if (ch == ' #' || ch == ' $' || ch == ' }' || ch == ' *' ) {
337
+ output.push_back (' }' ); // 0x7d
338
+ output.push_back (ch ^ 0x20 );
339
+ } else {
340
+ output.push_back (ch);
341
+ }
342
+ }
343
+ return output;
344
+ }
345
+
273
346
// Keep the dictionary as a vector so the dictionary doesn't reorder itself
274
347
// when you dump it
275
348
// We aren't accessing keys by name, so this won't affect performance
@@ -288,6 +361,8 @@ class JSONGenerator {
288
361
289
362
void Dump (std::ostream &s) const override { s << " null" ; }
290
363
364
+ void DumpBinaryEscaped (std::ostream &s) const override { Dump (s); }
365
+
291
366
protected:
292
367
};
293
368
@@ -304,6 +379,8 @@ class JSONGenerator {
304
379
305
380
void Dump (std::ostream &s) const override ;
306
381
382
+ void DumpBinaryEscaped (std::ostream &s) const override ;
383
+
307
384
private:
308
385
void *m_object;
309
386
};
0 commit comments