OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #ifndef DEBUGGER_BASE_DEBUG_BLOB_H_ | |
5 #define DEBUGGER_BASE_DEBUG_BLOB_H_ | |
6 | |
7 #include <deque> | |
8 #include <string> | |
9 | |
10 #ifdef _WIN32 | |
11 // I'm not sure from where to get these definitions, | |
12 // so I defined them here - for now. | |
13 typedef unsigned char uint8_t; | |
14 typedef unsigned int uint32_t; // NOLINT | |
15 typedef unsigned long long uint64_t; // NOLINT | |
16 #else | |
17 #include <stdint.h> | |
18 #endif | |
19 | |
20 /// Class for working with raw binary data. | |
21 /// Blob == Binary Large Object, an acronym from database world. | |
22 /// | |
23 /// It's a wrapper of std::deque<unsigned char>. | |
24 /// Another way to look at it as a string that can have zero elements. | |
25 /// Why not use std::string instead? | |
26 /// I pop characters from the front sometimes, std::deque is more efficient | |
27 /// that std::string (aka std::string). | |
28 namespace debug { | |
29 class Blob { | |
30 public: | |
31 Blob(); | |
32 | |
33 /// Copy constructor. | |
34 /// @param other object to be copied | |
35 Blob(const Blob& other); | |
36 | |
37 /// Initializes object with data from |buff|. | |
38 /// @param[in] buff pointer to the data to be copied into Blob. | |
39 /// @param[in] buff_sz number of bytes to copy. | |
40 /// Data in |buff| can be modified right after constructor call. | |
41 Blob(const void* buff, size_t buff_sz); | |
42 | |
43 virtual ~Blob(); | |
44 Blob& operator = (const Blob& other); | |
45 bool operator == (const Blob& other) const; | |
46 | |
47 /// @return number of bytes in the Blob. | |
48 size_t size() const { return value_.size(); } | |
49 | |
50 /// @param position offset of the byte to be returned | |
51 /// @return a byte at |position|. | |
52 uint8_t operator[] (size_t position) const; | |
53 uint8_t& operator[] (size_t position); | |
54 | |
55 /// @param position offset of the byte to be returned | |
56 /// @return a byte at |position|. | |
57 uint8_t GetAt(size_t position) const; | |
58 | |
59 /// @return a byte at zero position. | |
60 uint8_t Front() const; | |
61 | |
62 /// @return last byte. | |
63 uint8_t Back() const; | |
64 | |
65 /// Removes first byte. | |
66 /// @return removed byte. | |
67 uint8_t PopFront(); | |
68 | |
69 /// Removes last byte. | |
70 /// @return removed byte. | |
71 uint8_t PopBack(); | |
72 | |
73 /// Inserts byte at beginning. | |
74 void PushFront(uint8_t c); | |
75 | |
76 /// Appends byte. | |
77 /// @param c byte to append | |
78 void PushBack(uint8_t c); | |
79 | |
80 /// Appends Blob. | |
81 /// @param[in] other Blob to append. | |
82 void Append(const Blob& other); | |
83 | |
84 /// Removes all byte. Drops size to zero. | |
85 void Clear(); | |
86 | |
87 /// Copies data from blob to |buff|. | |
88 /// @param[in] offset offset of data in the blob | |
89 /// @param[out] buff buffer where data is to be copied. | |
90 /// @param[in] buff_sz size of |buff|. | |
91 /// @return number of bytes copied into the |buff|. | |
92 size_t Peek(size_t offset, void* buff, size_t buff_sz) const; | |
93 | |
94 /// @return std::string with elements equal to elements of the Blob. | |
95 /// Note: Blob shall not have zero elements. | |
96 /// example: {0x43, 0x34} -> "C4" | |
97 std::string ToString() const; | |
98 | |
99 /// @return std::string with hex representation of the Blob. | |
100 /// example: {0x03, 0x3a} -> "033a" | |
101 std::string ToHexString() const; | |
102 | |
103 /// @return std::string with hex representation of the Blob. | |
104 /// example: {0x03, 0x3a} -> "33a" | |
105 std::string ToHexStringNoLeadingZeroes() const; | |
106 | |
107 /// Converts string with hex representation to raw data. | |
108 /// example: "c46a" -> {0xc4, 0x6a} | |
109 /// @param hex_str with hex-represented data | |
110 bool FromHexString(const std::string& hex_str); | |
111 | |
112 /// Initializes object with data from |str|. | |
113 /// @param[in] str data to be copied into Blob. | |
114 Blob& FromString(const std::string& str); | |
115 | |
116 /// Reverses the order of the byte. | |
117 void Reverse(); | |
118 | |
119 /// Splits blob into tokens. | |
120 /// @param[in] delimiters blob with bytes that act as token separators | |
121 /// @param[out] tokens resulting tokens. | |
122 void Split(const Blob& delimiters, std::deque<Blob>* tokens) const; | |
123 | |
124 /// Removes bytes from the front of this blob, until first byte of this Blob | |
125 /// is contained in the |bytes|. | |
126 /// @param bytes[in] blob with terminating bytes | |
127 /// @return Blob with bytes removed from this Blob. | |
128 debug::Blob PopBlobFromFrontUntilBytes(const Blob& bytes); | |
129 | |
130 /// Removes bytes from the front of this blob, until first byte of this Blob | |
131 /// is not contained in the |bytes|. | |
132 /// @param bytes[in] blob with matching bytes | |
133 void PopMatchingBytesFromFront(const Blob& bytes); | |
134 | |
135 /// Converts hex letter to integer. | |
136 /// Example: '2' -> 2, 'c' -> 12 | |
137 /// @param[in] c hex letter. | |
138 /// @param[out] result integer corresponding to |c| | |
139 /// @return false if |c| is not in [0..9a..f] | |
140 static bool HexCharToInt(uint8_t c, unsigned int* result); | |
141 | |
142 /// Converts part of the integer info hex letter. | |
143 /// Example: GetHexDigit(0xa5, 0) -> '5', GetHexDigit(0xa5, 1) -> 'a' | |
144 /// @param[in] value integer to be converted | |
145 /// @param[in] digit_position position of the hex letter, 0 or 1. | |
146 /// @return hex letter | |
147 static char GetHexDigit(unsigned int value, int digit_position); | |
148 | |
149 /// @return true is blob has an element == |c|. | |
150 bool HasByte(char c) const; | |
151 | |
152 protected: | |
153 std::deque<uint8_t> value_; | |
154 }; | |
155 } // namespace debug | |
156 #endif // DEBUGGER_BASE_DEBUG_BLOB_H_ | |
157 | |
OLD | NEW |