15ffd83dbSDimitry Andric //===-- Stream.cpp --------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/Utility/Stream.h" 100b57cec5SDimitry Andric 11*5f757f3fSDimitry Andric #include "lldb/Utility/AnsiTerminal.h" 120b57cec5SDimitry Andric #include "lldb/Utility/Endian.h" 130b57cec5SDimitry Andric #include "lldb/Utility/VASPrintf.h" 140b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 15480093f4SDimitry Andric #include "llvm/Support/Format.h" 160b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 17*5f757f3fSDimitry Andric #include "llvm/Support/Regex.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #include <string> 200b57cec5SDimitry Andric 21fe6060f1SDimitry Andric #include <cinttypes> 22fe6060f1SDimitry Andric #include <cstddef> 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric using namespace lldb; 250b57cec5SDimitry Andric using namespace lldb_private; 260b57cec5SDimitry Andric 275ffd83dbSDimitry Andric Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order, 285ffd83dbSDimitry Andric bool colors) 290b57cec5SDimitry Andric : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order), 3081ad6265SDimitry Andric m_forwarder(*this, colors) {} 310b57cec5SDimitry Andric 325ffd83dbSDimitry Andric Stream::Stream(bool colors) 33fe6060f1SDimitry Andric : m_flags(0), m_byte_order(endian::InlHostByteOrder()), 34fe6060f1SDimitry Andric m_forwarder(*this, colors) {} 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric // Destructor 37fe6060f1SDimitry Andric Stream::~Stream() = default; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric ByteOrder Stream::SetByteOrder(ByteOrder byte_order) { 400b57cec5SDimitry Andric ByteOrder old_byte_order = m_byte_order; 410b57cec5SDimitry Andric m_byte_order = byte_order; 420b57cec5SDimitry Andric return old_byte_order; 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // Put an offset "uval" out to the stream using the printf format in "format". 460b57cec5SDimitry Andric void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); } 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric // Put an SLEB128 "uval" out to the stream using the printf format in "format". 490b57cec5SDimitry Andric size_t Stream::PutSLEB128(int64_t sval) { 500b57cec5SDimitry Andric if (m_flags.Test(eBinary)) 510b57cec5SDimitry Andric return llvm::encodeSLEB128(sval, m_forwarder); 520b57cec5SDimitry Andric else 530b57cec5SDimitry Andric return Printf("0x%" PRIi64, sval); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric // Put an ULEB128 "uval" out to the stream using the printf format in "format". 570b57cec5SDimitry Andric size_t Stream::PutULEB128(uint64_t uval) { 580b57cec5SDimitry Andric if (m_flags.Test(eBinary)) 590b57cec5SDimitry Andric return llvm::encodeULEB128(uval, m_forwarder); 600b57cec5SDimitry Andric else 610b57cec5SDimitry Andric return Printf("0x%" PRIx64, uval); 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric // Print a raw NULL terminated C string to the stream. 650b57cec5SDimitry Andric size_t Stream::PutCString(llvm::StringRef str) { 660b57cec5SDimitry Andric size_t bytes_written = 0; 670b57cec5SDimitry Andric bytes_written = Write(str.data(), str.size()); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // when in binary mode, emit the NULL terminator 700b57cec5SDimitry Andric if (m_flags.Test(eBinary)) 710b57cec5SDimitry Andric bytes_written += PutChar('\0'); 720b57cec5SDimitry Andric return bytes_written; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 75*5f757f3fSDimitry Andric void Stream::PutCStringColorHighlighted(llvm::StringRef text, 76*5f757f3fSDimitry Andric llvm::StringRef pattern, 77*5f757f3fSDimitry Andric llvm::StringRef prefix, 78*5f757f3fSDimitry Andric llvm::StringRef suffix) { 79*5f757f3fSDimitry Andric // Only apply color formatting when a pattern is present and both prefix and 80*5f757f3fSDimitry Andric // suffix are specified. In the absence of these conditions, output the text 81*5f757f3fSDimitry Andric // without color formatting. 82*5f757f3fSDimitry Andric if (pattern.empty() || (prefix.empty() && suffix.empty())) { 83*5f757f3fSDimitry Andric PutCString(text); 84*5f757f3fSDimitry Andric return; 85*5f757f3fSDimitry Andric } 86*5f757f3fSDimitry Andric 87*5f757f3fSDimitry Andric llvm::Regex reg_pattern(pattern); 88*5f757f3fSDimitry Andric llvm::SmallVector<llvm::StringRef, 1> matches; 89*5f757f3fSDimitry Andric llvm::StringRef remaining = text; 90*5f757f3fSDimitry Andric std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes( 91*5f757f3fSDimitry Andric prefix.str() + "%.*s" + suffix.str()); 92*5f757f3fSDimitry Andric while (reg_pattern.match(remaining, &matches)) { 93*5f757f3fSDimitry Andric llvm::StringRef match = matches[0]; 94*5f757f3fSDimitry Andric size_t match_start_pos = match.data() - remaining.data(); 95*5f757f3fSDimitry Andric PutCString(remaining.take_front(match_start_pos)); 96*5f757f3fSDimitry Andric Printf(format_str.c_str(), match.size(), match.data()); 97*5f757f3fSDimitry Andric remaining = remaining.drop_front(match_start_pos + match.size()); 98*5f757f3fSDimitry Andric } 99*5f757f3fSDimitry Andric if (remaining.size()) 100*5f757f3fSDimitry Andric PutCString(remaining); 101*5f757f3fSDimitry Andric } 102*5f757f3fSDimitry Andric 1030b57cec5SDimitry Andric // Print a double quoted NULL terminated C string to the stream using the 1040b57cec5SDimitry Andric // printf format in "format". 1050b57cec5SDimitry Andric void Stream::QuotedCString(const char *cstr, const char *format) { 1060b57cec5SDimitry Andric Printf(format, cstr); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Put an address "addr" out to the stream with optional prefix and suffix 1100b57cec5SDimitry Andric // strings. 111480093f4SDimitry Andric void lldb_private::DumpAddress(llvm::raw_ostream &s, uint64_t addr, 112480093f4SDimitry Andric uint32_t addr_size, const char *prefix, 1130b57cec5SDimitry Andric const char *suffix) { 1140b57cec5SDimitry Andric if (prefix == nullptr) 1150b57cec5SDimitry Andric prefix = ""; 1160b57cec5SDimitry Andric if (suffix == nullptr) 1170b57cec5SDimitry Andric suffix = ""; 118480093f4SDimitry Andric s << prefix << llvm::format_hex(addr, 2 + 2 * addr_size) << suffix; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Put an address range out to the stream with optional prefix and suffix 1220b57cec5SDimitry Andric // strings. 123480093f4SDimitry Andric void lldb_private::DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr, 124480093f4SDimitry Andric uint64_t hi_addr, uint32_t addr_size, 125480093f4SDimitry Andric const char *prefix, const char *suffix) { 1260b57cec5SDimitry Andric if (prefix && prefix[0]) 127480093f4SDimitry Andric s << prefix; 128480093f4SDimitry Andric DumpAddress(s, lo_addr, addr_size, "["); 129480093f4SDimitry Andric DumpAddress(s, hi_addr, addr_size, "-", ")"); 1300b57cec5SDimitry Andric if (suffix && suffix[0]) 131480093f4SDimitry Andric s << suffix; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric size_t Stream::PutChar(char ch) { return Write(&ch, 1); } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric // Print some formatted output to the stream. 1370b57cec5SDimitry Andric size_t Stream::Printf(const char *format, ...) { 1380b57cec5SDimitry Andric va_list args; 1390b57cec5SDimitry Andric va_start(args, format); 1400b57cec5SDimitry Andric size_t result = PrintfVarArg(format, args); 1410b57cec5SDimitry Andric va_end(args); 1420b57cec5SDimitry Andric return result; 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric // Print some formatted output to the stream. 1460b57cec5SDimitry Andric size_t Stream::PrintfVarArg(const char *format, va_list args) { 1470b57cec5SDimitry Andric llvm::SmallString<1024> buf; 1480b57cec5SDimitry Andric VASprintf(buf, format, args); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // Include the NULL termination byte for binary output 1510b57cec5SDimitry Andric size_t length = buf.size(); 1520b57cec5SDimitry Andric if (m_flags.Test(eBinary)) 1530b57cec5SDimitry Andric ++length; 1540b57cec5SDimitry Andric return Write(buf.c_str(), length); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // Print and End of Line character to the stream 1580b57cec5SDimitry Andric size_t Stream::EOL() { return PutChar('\n'); } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric size_t Stream::Indent(llvm::StringRef str) { 1615ffd83dbSDimitry Andric const size_t ind_length = PutCString(std::string(m_indent_level, ' ')); 1625ffd83dbSDimitry Andric const size_t str_length = PutCString(str); 1635ffd83dbSDimitry Andric return ind_length + str_length; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // Stream a character "ch" out to this stream. 1670b57cec5SDimitry Andric Stream &Stream::operator<<(char ch) { 1680b57cec5SDimitry Andric PutChar(ch); 1690b57cec5SDimitry Andric return *this; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Stream the NULL terminated C string out to this stream. 1730b57cec5SDimitry Andric Stream &Stream::operator<<(const char *s) { 1740b57cec5SDimitry Andric Printf("%s", s); 1750b57cec5SDimitry Andric return *this; 1760b57cec5SDimitry Andric } 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric Stream &Stream::operator<<(llvm::StringRef str) { 1790b57cec5SDimitry Andric Write(str.data(), str.size()); 1800b57cec5SDimitry Andric return *this; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // Stream the pointer value out to this stream. 1840b57cec5SDimitry Andric Stream &Stream::operator<<(const void *p) { 1850b57cec5SDimitry Andric Printf("0x%.*tx", static_cast<int>(sizeof(const void *)) * 2, (ptrdiff_t)p); 1860b57cec5SDimitry Andric return *this; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // Get the current indentation level 190480093f4SDimitry Andric unsigned Stream::GetIndentLevel() const { return m_indent_level; } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric // Set the current indentation level 193480093f4SDimitry Andric void Stream::SetIndentLevel(unsigned indent_level) { 194480093f4SDimitry Andric m_indent_level = indent_level; 195480093f4SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric // Increment the current indentation level 198480093f4SDimitry Andric void Stream::IndentMore(unsigned amount) { m_indent_level += amount; } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Decrement the current indentation level 201480093f4SDimitry Andric void Stream::IndentLess(unsigned amount) { 2020b57cec5SDimitry Andric if (m_indent_level >= amount) 2030b57cec5SDimitry Andric m_indent_level -= amount; 2040b57cec5SDimitry Andric else 2050b57cec5SDimitry Andric m_indent_level = 0; 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric // Get the address size in bytes 2090b57cec5SDimitry Andric uint32_t Stream::GetAddressByteSize() const { return m_addr_size; } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // Set the address size in bytes 2120b57cec5SDimitry Andric void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // The flags get accessor 2150b57cec5SDimitry Andric Flags &Stream::GetFlags() { return m_flags; } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // The flags const get accessor 2180b57cec5SDimitry Andric const Flags &Stream::GetFlags() const { return m_flags; } 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric // The byte order get accessor 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric lldb::ByteOrder Stream::GetByteOrder() const { return m_byte_order; } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric size_t Stream::PrintfAsRawHex8(const char *format, ...) { 2250b57cec5SDimitry Andric va_list args; 2260b57cec5SDimitry Andric va_start(args, format); 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric llvm::SmallString<1024> buf; 2290b57cec5SDimitry Andric VASprintf(buf, format, args); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric ByteDelta delta(*this); 2320b57cec5SDimitry Andric for (char C : buf) 2330b57cec5SDimitry Andric _PutHex8(C, false); 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric va_end(args); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric return *delta; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric size_t Stream::PutNHex8(size_t n, uint8_t uvalue) { 2410b57cec5SDimitry Andric ByteDelta delta(*this); 2420b57cec5SDimitry Andric for (size_t i = 0; i < n; ++i) 2430b57cec5SDimitry Andric _PutHex8(uvalue, false); 2440b57cec5SDimitry Andric return *delta; 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) { 2480b57cec5SDimitry Andric if (m_flags.Test(eBinary)) { 2490b57cec5SDimitry Andric Write(&uvalue, 1); 2500b57cec5SDimitry Andric } else { 2510b57cec5SDimitry Andric if (add_prefix) 2520b57cec5SDimitry Andric PutCString("0x"); 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5', 2550b57cec5SDimitry Andric '6', '7', '8', '9', 'a', 'b', 2560b57cec5SDimitry Andric 'c', 'd', 'e', 'f'}; 2570b57cec5SDimitry Andric char nibble_chars[2]; 2580b57cec5SDimitry Andric nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf]; 2590b57cec5SDimitry Andric nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf]; 2600b57cec5SDimitry Andric Write(nibble_chars, sizeof(nibble_chars)); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric size_t Stream::PutHex8(uint8_t uvalue) { 2650b57cec5SDimitry Andric ByteDelta delta(*this); 2660b57cec5SDimitry Andric _PutHex8(uvalue, false); 2670b57cec5SDimitry Andric return *delta; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) { 2710b57cec5SDimitry Andric ByteDelta delta(*this); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 2740b57cec5SDimitry Andric byte_order = m_byte_order; 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric if (byte_order == eByteOrderLittle) { 2770b57cec5SDimitry Andric for (size_t byte = 0; byte < sizeof(uvalue); ++byte) 2780b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 2790b57cec5SDimitry Andric } else { 2800b57cec5SDimitry Andric for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) 2810b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric return *delta; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) { 2870b57cec5SDimitry Andric ByteDelta delta(*this); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 2900b57cec5SDimitry Andric byte_order = m_byte_order; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric if (byte_order == eByteOrderLittle) { 2930b57cec5SDimitry Andric for (size_t byte = 0; byte < sizeof(uvalue); ++byte) 2940b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 2950b57cec5SDimitry Andric } else { 2960b57cec5SDimitry Andric for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) 2970b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric return *delta; 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) { 3030b57cec5SDimitry Andric ByteDelta delta(*this); 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 3060b57cec5SDimitry Andric byte_order = m_byte_order; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric if (byte_order == eByteOrderLittle) { 3090b57cec5SDimitry Andric for (size_t byte = 0; byte < sizeof(uvalue); ++byte) 3100b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 3110b57cec5SDimitry Andric } else { 3120b57cec5SDimitry Andric for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) 3130b57cec5SDimitry Andric _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric return *delta; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size, 3190b57cec5SDimitry Andric lldb::ByteOrder byte_order) { 3200b57cec5SDimitry Andric switch (byte_size) { 3210b57cec5SDimitry Andric case 1: 3220b57cec5SDimitry Andric return PutHex8(static_cast<uint8_t>(uvalue)); 3230b57cec5SDimitry Andric case 2: 3240b57cec5SDimitry Andric return PutHex16(static_cast<uint16_t>(uvalue), byte_order); 3250b57cec5SDimitry Andric case 4: 3260b57cec5SDimitry Andric return PutHex32(static_cast<uint32_t>(uvalue), byte_order); 3270b57cec5SDimitry Andric case 8: 3280b57cec5SDimitry Andric return PutHex64(uvalue, byte_order); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric return 0; 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric size_t Stream::PutPointer(void *ptr) { 3340b57cec5SDimitry Andric return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(), 3350b57cec5SDimitry Andric endian::InlHostByteOrder()); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric size_t Stream::PutFloat(float f, ByteOrder byte_order) { 3390b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 3400b57cec5SDimitry Andric byte_order = m_byte_order; 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order); 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric size_t Stream::PutDouble(double d, ByteOrder byte_order) { 3460b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 3470b57cec5SDimitry Andric byte_order = m_byte_order; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order); 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) { 3530b57cec5SDimitry Andric if (byte_order == eByteOrderInvalid) 3540b57cec5SDimitry Andric byte_order = m_byte_order; 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order); 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric size_t Stream::PutRawBytes(const void *s, size_t src_len, 3600b57cec5SDimitry Andric ByteOrder src_byte_order, ByteOrder dst_byte_order) { 3610b57cec5SDimitry Andric ByteDelta delta(*this); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric if (src_byte_order == eByteOrderInvalid) 3640b57cec5SDimitry Andric src_byte_order = m_byte_order; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric if (dst_byte_order == eByteOrderInvalid) 3670b57cec5SDimitry Andric dst_byte_order = m_byte_order; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric const uint8_t *src = static_cast<const uint8_t *>(s); 3700b57cec5SDimitry Andric bool binary_was_set = m_flags.Test(eBinary); 3710b57cec5SDimitry Andric if (!binary_was_set) 3720b57cec5SDimitry Andric m_flags.Set(eBinary); 3730b57cec5SDimitry Andric if (src_byte_order == dst_byte_order) { 3740b57cec5SDimitry Andric for (size_t i = 0; i < src_len; ++i) 3750b57cec5SDimitry Andric _PutHex8(src[i], false); 3760b57cec5SDimitry Andric } else { 37781ad6265SDimitry Andric for (size_t i = src_len; i > 0; --i) 37881ad6265SDimitry Andric _PutHex8(src[i - 1], false); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric if (!binary_was_set) 3810b57cec5SDimitry Andric m_flags.Clear(eBinary); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric return *delta; 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len, 3870b57cec5SDimitry Andric ByteOrder src_byte_order, 3880b57cec5SDimitry Andric ByteOrder dst_byte_order) { 3890b57cec5SDimitry Andric ByteDelta delta(*this); 39081ad6265SDimitry Andric 3910b57cec5SDimitry Andric if (src_byte_order == eByteOrderInvalid) 3920b57cec5SDimitry Andric src_byte_order = m_byte_order; 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric if (dst_byte_order == eByteOrderInvalid) 3950b57cec5SDimitry Andric dst_byte_order = m_byte_order; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric const uint8_t *src = static_cast<const uint8_t *>(s); 3980b57cec5SDimitry Andric bool binary_is_set = m_flags.Test(eBinary); 3990b57cec5SDimitry Andric m_flags.Clear(eBinary); 4000b57cec5SDimitry Andric if (src_byte_order == dst_byte_order) { 4010b57cec5SDimitry Andric for (size_t i = 0; i < src_len; ++i) 4020b57cec5SDimitry Andric _PutHex8(src[i], false); 4030b57cec5SDimitry Andric } else { 40481ad6265SDimitry Andric for (size_t i = src_len; i > 0; --i) 40581ad6265SDimitry Andric _PutHex8(src[i - 1], false); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric if (binary_is_set) 4080b57cec5SDimitry Andric m_flags.Set(eBinary); 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric return *delta; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric size_t Stream::PutStringAsRawHex8(llvm::StringRef s) { 4140b57cec5SDimitry Andric ByteDelta delta(*this); 4150b57cec5SDimitry Andric bool binary_is_set = m_flags.Test(eBinary); 4160b57cec5SDimitry Andric m_flags.Clear(eBinary); 4170b57cec5SDimitry Andric for (char c : s) 4180b57cec5SDimitry Andric _PutHex8(c, false); 4190b57cec5SDimitry Andric if (binary_is_set) 4200b57cec5SDimitry Andric m_flags.Set(eBinary); 4210b57cec5SDimitry Andric return *delta; 4220b57cec5SDimitry Andric } 423