172090c21SPavel Labath //===-- StdStringExtractor.h ------------------------------------*- C++ -*-===//
272090c21SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
672090c21SPavel Labath //
772090c21SPavel Labath //===----------------------------------------------------------------------===//
872090c21SPavel Labath 
9cdc514e4SJonas Devlieghere #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_STDSTRINGEXTRACTOR_H
10cdc514e4SJonas Devlieghere #define LLDB_TOOLS_DEBUGSERVER_SOURCE_STDSTRINGEXTRACTOR_H
1172090c21SPavel Labath 
12*76e47d48SRaphael Isemann #include <cstdint>
1372090c21SPavel Labath #include <string>
1472090c21SPavel Labath 
1572090c21SPavel Labath 
1672090c21SPavel Labath // Based on StringExtractor, with the added limitation that this file should not
1772090c21SPavel Labath // take a dependency on LLVM, as it is used from debugserver.
1872090c21SPavel Labath class StdStringExtractor {
1972090c21SPavel Labath public:
2072090c21SPavel Labath   enum { BigEndian = 0, LittleEndian = 1 };
2172090c21SPavel Labath   // Constructors and Destructors
2272090c21SPavel Labath   StdStringExtractor();
2372090c21SPavel Labath   StdStringExtractor(const char *packet_cstr);
2472090c21SPavel Labath   virtual ~StdStringExtractor();
2572090c21SPavel Labath 
2672090c21SPavel Labath   // Returns true if the file position is still valid for the data
2772090c21SPavel Labath   // contained in this string extractor object.
IsGood()2872090c21SPavel Labath   bool IsGood() const { return m_index != UINT64_MAX; }
2972090c21SPavel Labath 
GetFilePos()3072090c21SPavel Labath   uint64_t GetFilePos() const { return m_index; }
3172090c21SPavel Labath 
SetFilePos(uint32_t idx)3272090c21SPavel Labath   void SetFilePos(uint32_t idx) { m_index = idx; }
3372090c21SPavel Labath 
Clear()3472090c21SPavel Labath   void Clear() {
3572090c21SPavel Labath     m_packet.clear();
3672090c21SPavel Labath     m_index = 0;
3772090c21SPavel Labath   }
3872090c21SPavel Labath 
3972090c21SPavel Labath   void SkipSpaces();
4072090c21SPavel Labath 
GetStringRef()4172090c21SPavel Labath   const std::string &GetStringRef() const { return m_packet; }
4272090c21SPavel Labath 
Empty()4372090c21SPavel Labath   bool Empty() { return m_packet.empty(); }
4472090c21SPavel Labath 
GetBytesLeft()4572090c21SPavel Labath   size_t GetBytesLeft() {
4672090c21SPavel Labath     if (m_index < m_packet.size())
4772090c21SPavel Labath       return m_packet.size() - m_index;
4872090c21SPavel Labath     return 0;
4972090c21SPavel Labath   }
5072090c21SPavel Labath 
5172090c21SPavel Labath   char GetChar(char fail_value = '\0');
5272090c21SPavel Labath 
5372090c21SPavel Labath   char PeekChar(char fail_value = '\0') {
5472090c21SPavel Labath     const char *cstr = Peek();
5572090c21SPavel Labath     if (cstr)
5672090c21SPavel Labath       return cstr[0];
5772090c21SPavel Labath     return fail_value;
5872090c21SPavel Labath   }
5972090c21SPavel Labath 
6072090c21SPavel Labath   int DecodeHexU8();
6172090c21SPavel Labath 
6272090c21SPavel Labath   uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
6372090c21SPavel Labath 
6472090c21SPavel Labath   bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
6572090c21SPavel Labath 
6672090c21SPavel Labath   bool GetNameColonValue(std::string &name, std::string &value);
6772090c21SPavel Labath 
6872090c21SPavel Labath   int32_t GetS32(int32_t fail_value, int base = 0);
6972090c21SPavel Labath 
7072090c21SPavel Labath   uint32_t GetU32(uint32_t fail_value, int base = 0);
7172090c21SPavel Labath 
7272090c21SPavel Labath   int64_t GetS64(int64_t fail_value, int base = 0);
7372090c21SPavel Labath 
7472090c21SPavel Labath   uint64_t GetU64(uint64_t fail_value, int base = 0);
7572090c21SPavel Labath 
7672090c21SPavel Labath   uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
7772090c21SPavel Labath 
7872090c21SPavel Labath   uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
7972090c21SPavel Labath 
8072090c21SPavel Labath   size_t GetHexBytes(void *dst, size_t dst_len, uint8_t fail_fill_value);
8172090c21SPavel Labath 
8272090c21SPavel Labath   size_t GetHexBytesAvail(void *dst, size_t dst_len);
8372090c21SPavel Labath 
8472090c21SPavel Labath   size_t GetHexByteString(std::string &str);
8572090c21SPavel Labath 
8672090c21SPavel Labath   size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
8772090c21SPavel Labath 
8872090c21SPavel Labath   size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
8972090c21SPavel Labath 
Peek()9072090c21SPavel Labath   const char *Peek() {
9172090c21SPavel Labath     if (m_index < m_packet.size())
9272090c21SPavel Labath       return m_packet.c_str() + m_index;
9372090c21SPavel Labath     return nullptr;
9472090c21SPavel Labath   }
9572090c21SPavel Labath 
9672090c21SPavel Labath protected:
9772090c21SPavel Labath   // For StdStringExtractor only
9872090c21SPavel Labath   std::string m_packet; // The string in which to extract data.
9972090c21SPavel Labath   uint64_t m_index;     // When extracting data from a packet, this index
10072090c21SPavel Labath                         // will march along as things get extracted. If set
10172090c21SPavel Labath                         // to UINT64_MAX the end of the packet data was
10272090c21SPavel Labath                         // reached when decoding information
10372090c21SPavel Labath };
10472090c21SPavel Labath 
105cdc514e4SJonas Devlieghere #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_STDSTRINGEXTRACTOR_H
106