Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 1 | //===-- StdStringExtractor.h ------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef utility_StdStringExtractor_h_ |
| 10 | #define utility_StdStringExtractor_h_ |
| 11 | |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 12 | #include <stdint.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include <string> |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 14 | |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 15 | |
| 16 | // Based on StringExtractor, with the added limitation that this file should not |
| 17 | // take a dependency on LLVM, as it is used from debugserver. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | class StdStringExtractor { |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 19 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | enum { BigEndian = 0, LittleEndian = 1 }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | // Constructors and Destructors |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | StdStringExtractor(); |
| 23 | StdStringExtractor(const char *packet_cstr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | virtual ~StdStringExtractor(); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | // Returns true if the file position is still valid for the data |
| 27 | // contained in this string extractor object. |
| 28 | bool IsGood() const { return m_index != UINT64_MAX; } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | uint64_t GetFilePos() const { return m_index; } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | void SetFilePos(uint32_t idx) { m_index = idx; } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | void Clear() { |
| 35 | m_packet.clear(); |
| 36 | m_index = 0; |
| 37 | } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | void SkipSpaces(); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | std::string &GetStringRef() { return m_packet; } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | const std::string &GetStringRef() const { return m_packet; } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | bool Empty() { return m_packet.empty(); } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | size_t GetBytesLeft() { |
| 48 | if (m_index < m_packet.size()) |
| 49 | return m_packet.size() - m_index; |
| 50 | return 0; |
| 51 | } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | char GetChar(char fail_value = '\0'); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | char PeekChar(char fail_value = '\0') { |
| 56 | const char *cstr = Peek(); |
| 57 | if (cstr) |
| 58 | return cstr[0]; |
| 59 | return fail_value; |
| 60 | } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 61 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | int DecodeHexU8(); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | bool GetNameColonValue(std::string &name, std::string &value); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | int32_t GetS32(int32_t fail_value, int base = 0); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | uint32_t GetU32(uint32_t fail_value, int base = 0); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | int64_t GetS64(int64_t fail_value, int base = 0); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | uint64_t GetU64(uint64_t fail_value, int base = 0); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | size_t GetHexBytes(void *dst, size_t dst_len, uint8_t fail_fill_value); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | size_t GetHexBytesAvail(void *dst, size_t dst_len); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian, |
| 87 | uint64_t fail_value); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | size_t GetHexByteString(std::string &str); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length); |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | size_t GetHexByteStringTerminatedBy(std::string &str, char terminator); |
| 94 | |
| 95 | const char *Peek() { |
| 96 | if (m_index < m_packet.size()) |
| 97 | return m_packet.c_str() + m_index; |
| 98 | return nullptr; |
| 99 | } |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 100 | |
| 101 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | // For StdStringExtractor only |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | std::string m_packet; // The string in which to extract data. |
| 104 | uint64_t m_index; // When extracting data from a packet, this index |
| 105 | // will march along as things get extracted. If set |
| 106 | // to UINT64_MAX the end of the packet data was |
| 107 | // reached when decoding information |
Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | #endif // utility_StringExtractor_h_ |