DebugServer2
Loading...
Searching...
No Matches
ProtocolHelpers.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Types.h"
14#include "DebugServer2/Utils/Log.h"
15
16#include <algorithm>
17#include <sstream>
18
19namespace ds2 {
20namespace GDBRemote {
21
22template <typename T> uint8_t Checksum(T const &data) {
23 uint8_t csum = 0;
24 for (char n : data) {
25 csum += n;
26 }
27 return csum;
28}
29
30template <typename T> std::string Escape(T const &data) {
31 std::ostringstream ss;
32 auto first = data.begin();
33 auto last = data.begin();
34 static std::string const searchStr = "$#}*";
35
36 while ((last = std::find_first_of(first, data.end(), searchStr.begin(),
37 searchStr.end())) != data.end()) {
38 while (first < last) {
39 ss << *first++;
40 }
41 ss << '}' << static_cast<char>(*last - 0x20);
42 first = ++last;
43 }
44
45 while (first < data.end()) {
46 ss << *first++;
47 }
48
49 return ss.str();
50}
51
52template <typename T> std::string Unescape(T const &data) {
53 std::ostringstream ss;
54 auto first = data.begin();
55 auto last = data.begin();
56
57 while ((last = std::find(first, data.end(), '}')) != data.end()) {
58 while (first < last) {
59 ss << *first++;
60 }
61 ss << static_cast<char>(*(last + 1) + 0x20);
62 last += 2, first = last;
63 }
64
65 while (first < data.end()) {
66 ss << *first++;
67 }
68
69 return ss.str();
70}
71} // namespace GDBRemote
72} // namespace ds2