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 NACL_SDK_BUILD_TOOLS_DEBUG_SERVER_COMMON_DEBUG_BLOB_H_ | |
5 #define NACL_SDK_BUILD_TOOLS_DEBUG_SERVER_COMMON_DEBUG_BLOB_H_ | |
6 | |
7 #include <deque> | |
8 #include <string> | |
9 | |
10 typedef unsigned char byte; | |
11 | |
12 // I'm not sure where to get these definitions, | |
13 // so I defined it here - for now. | |
14 typedef unsigned long long u_int32_t; | |
15 typedef unsigned long long u_int64_t; | |
16 | |
17 /// Class for working with raw binary data. | |
18 /// Blob == Binary Large Object, an acronim from database world. | |
19 /// Used mostly in Remote Serial Protocol (RSP) code. | |
20 /// | |
21 /// It's a wrapper of std::deque<unsigned char>. | |
22 /// Another way to look at it as a string that can have zero | |
23 /// elements. Note that RSP supports binary packets. | |
24 /// Why not use std::string instead? | |
25 /// I pop characters from the front sometimes, std::deque is more efficient | |
26 /// that std::vector (that's how std::string is implemented). | |
27 namespace debug { | |
28 class Blob { | |
29 public: | |
30 Blob(); | |
31 | |
32 /// Copy constructor. | |
33 /// @param other object to be copied | |
34 Blob(const Blob& other); | |
35 | |
36 /// Initializes object with data from |buff|. | |
37 /// @param[in] buff pointer to the data to be copied into Blob. | |
38 /// @param[in] buff_sz number of bytes to copy. | |
39 /// Data in |buff| can be modified right after constructor call. | |
40 Blob(const void* buff, size_t buff_sz); | |
41 | |
42 /// Initializes object with data from |buff|. | |
43 /// @param[in] buff pointer to zero-terminated data to be copied into Blob. | |
44 /// Data in |buff| can be modified right after constructor call. | |
45 Blob(const char* buff); | |
46 | |
47 /// Initializes object with data from |buff|. | |
48 /// @param[in] str data to be copied into Blob. | |
49 Blob(const std::string& str); | |
50 | |
51 virtual ~Blob(); | |
52 Blob& operator = (const Blob& other); | |
53 bool operator == (const Blob& other) const; | |
54 | |
55 /// @return number of bytes in the Blob. | |
56 size_t size() const { return value_.size(); } | |
57 | |
58 /// @return a byte at |position|. | |
59 byte operator[] (size_t position) const; | |
60 | |
61 /// @return a byte at |position|. | |
62 byte GetAt(size_t position) const; | |
63 | |
64 /// @return a byte at zero position. | |
65 byte Front() const; | |
66 | |
67 /// @return last byte. | |
68 byte Back() const; | |
69 | |
70 /// Removes first byte. | |
71 /// @return removed byte. | |
72 byte PopFront(); | |
73 | |
74 /// Removes last byte. | |
75 /// @return removed byte. | |
76 byte PopBack(); | |
77 | |
78 /// Inserts byte at beginning. | |
79 void PushFront(byte c); | |
80 | |
81 /// Appends byte. | |
82 void PushBack(byte c); | |
83 | |
84 /// Appends Blob. | |
85 /// @param[in] other Blob to append. | |
86 void Append(const Blob& other); | |
87 | |
88 /// Removes all bytes. | |
89 void Clear(); | |
90 | |
91 /// Writes formatted data to Blob. | |
92 /// @param[in] string that contains the text to be written to the Blob. | |
93 void Format(const char* fmt, ...); | |
94 | |
95 /// @return std::string with elements equal to elements of the Blob. | |
96 /// Note: Blob shall not have zero elements. | |
97 /// example: {0x43, 0x34} -> "C4" | |
98 std::string ToString() const; | |
99 | |
100 /// @return std::string with hex representation of the Blob. | |
101 /// example: {0x43, 0x3a} -> "433a" | |
102 std::string ToHexString(bool remove_leading_zeroes=true) const; | |
103 | |
104 void* ToCBuffer() const; | |
105 | |
106 /// @return number of bytes copied into the |buff|. | |
107 size_t Peek(size_t offset, void* buff, size_t buff_sz) const; | |
108 | |
109 /// Converts string with hex representation to raw data. | |
110 bool LoadFromHexString(const std::string& hex_str); | |
111 | |
112 /// Reverses the order of the bytes. | |
113 void Reverse(); | |
114 | |
115 /// @return true if elements are the same, and number of them is the same. | |
116 /// @param blob Blob to compare with. | |
117 /// @param to_length number of bytes to compare. If |to_length| is | |
118 /// equal to -1, all bytes of |blob| are compared. | |
119 bool Compare(const Blob& blob, size_t to_length = -1) const; | |
120 | |
121 /// @return true, if |prefix| bytes equal bytes in the begining of this Blob. | |
122 bool IsPrefix(const Blob& prefix) const; | |
123 | |
124 /// Removes bytes from the front of this blob, until first byte of this Blob | |
125 /// is contained in the |chars|. | |
126 /// @return Blob with bytes removed from this Blob. | |
127 Blob PopBlobFromFrontUnilChars(const char* chars); | |
128 | |
129 /// Removes bytes from the front of this blob, until first byte of this Blob | |
130 /// is not contained in the |chars|. | |
131 /// @return Blob with bytes removed from this Blob. | |
132 void PopMatchingCharsFromFront(const char* chars); | |
133 | |
134 /// Removes byte from the front of this blob, converts it to integer | |
135 /// assumes hex test representation is in the blob. | |
136 /// example: {0x | |
137 /// @return | |
138 unsigned int PopInt8FromFront(); | |
139 | |
140 /// Removes bytes from the front of this blob, converts it to 32 bit integer | |
141 /// @return | |
142 u_int32_t PopInt32FromFront(); | |
143 | |
144 u_int64_t PopInt64FromFront(); | |
145 void PopSpacesFromBothEnds(); | |
146 | |
147 unsigned int ToInt() const; | |
148 void Split(const char* delimiters, std::deque<Blob>* tokens) const; | |
149 | |
150 static bool HexCharToInt(byte c, unsigned int* result); | |
151 static char GetHexDigit(unsigned int value, int digit_position); | |
152 | |
153 protected: | |
154 std::deque<byte> value_; | |
155 }; | |
156 | |
157 class BlobUniTest { | |
158 public: | |
159 BlobUniTest(); | |
160 int Run(std::string* error); // returns 0 if success, error code if failed. | |
161 | |
162 }; | |
163 | |
164 } // namespace debug | |
165 #endif // NACL_SDK_BUILD_TOOLS_DEBUG_SERVER_COMMON_DEBUG_BLOB_H_ | |
OLD | NEW |