From 79ec0cfdb5f3461c763e0bf712cf42eb87fccb90 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 10 Dec 2019 10:02:19 -0500 Subject: DownstreamCompiler abstraction (#1149) * CPPCompiler -> DownstreamCompiler * Added DownstreamCompileResult to start abstraction such that we don't need files. * * Split out slang-blob.cpp * Made CompileResult hold a DownstreamCompileResult - for access to binary or ISlangSharedLibrary * Keep temporary files in scope. * Add a hash to the hex dump stream. * Move all file tracking into DownstreamCompiler. --- source/core/slang-hex-dump-util.cpp | 112 +++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 46 deletions(-) (limited to 'source/core/slang-hex-dump-util.cpp') diff --git a/source/core/slang-hex-dump-util.cpp b/source/core/slang-hex-dump-util.cpp index 4a3d4ce06..1583d8461 100644 --- a/source/core/slang-hex-dump-util.cpp +++ b/source/core/slang-hex-dump-util.cpp @@ -6,6 +6,7 @@ #include "slang-writer.h" #include "../../slang-com-helper.h" +#include "slang-hash.h" namespace Slang { @@ -15,12 +16,20 @@ static const UnownedStringSlice s_end = UnownedStringSlice::fromLiteral("--END-- static const char s_hex[] = "0123456789abcdef"; /* static */SlangResult HexDumpUtil::dumpWithMarkers(const List& data, int maxBytesPerLine, ISlangWriter* writer) +{ + return dumpWithMarkers(data.getBuffer(), data.getCount(), maxBytesPerLine, writer); +} + +/* static */SlangResult HexDumpUtil::dumpWithMarkers(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer) { WriterHelper helper(writer); SLANG_RETURN_ON_FAIL(helper.write(s_start.begin(), s_start.size())); - SLANG_RETURN_ON_FAIL(helper.print(" (%zu)\n", size_t(data.getCount()))); + SLANG_RETURN_ON_FAIL(helper.print(" %zu", dataCount)); - SLANG_RETURN_ON_FAIL(dump(data, maxBytesPerLine, writer)); + const int hash = GetHashCode((const char*)data, dataCount); + SLANG_RETURN_ON_FAIL(helper.print(" %d\n", hash )); + + SLANG_RETURN_ON_FAIL(dump(data, dataCount, maxBytesPerLine, writer)); SLANG_RETURN_ON_FAIL(helper.write(s_end.begin(), s_end.size())); SLANG_RETURN_ON_FAIL(helper.put("\n")); @@ -38,14 +47,19 @@ static const char s_hex[] = "0123456789abcdef"; writer->write(c, 8); } + /* static */SlangResult HexDumpUtil::dump(const List& data, int maxBytesPerLine, ISlangWriter* writer) { - int maxCharsPerLine = 2 * maxBytesPerLine + 1 + maxBytesPerLine + 1; + return dump(data.getBuffer(), data.getCount(), maxBytesPerLine, writer); +} - const uint8_t* cur = data.begin(); - const uint8_t* end = data.end(); +/* static */SlangResult HexDumpUtil::dump(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer) +{ + int maxCharsPerLine = 2 * maxBytesPerLine + 1 + maxBytesPerLine + 1; - + const uint8_t* cur = data; + const uint8_t* end = data + dataCount; + while (cur < end) { size_t count = size_t(end - cur); @@ -112,34 +126,16 @@ static int _parseHexDigit(char c) { outBytes.clear(); - bool inHex = false; - LineParser lineParser(lines); for (const auto& line : lineParser) { - if (!inHex) - { - if (line.startsWith(s_start)) - { - inHex = true; - continue; - } - } - else - { - if (line.startsWith(s_end)) - { - break; - } - } - const char* cur = line.begin(); const char* end = line.end(); while(cur + 2 <= end) { const char c = cur[0]; - if (c == ' ' || c== '\n' || c == '\r' || c == '\t') + if (c == ' ' || c == '\n' || c == '\r' || c == '\t') { // Skip to next line break; @@ -160,35 +156,59 @@ static int _parseHexDigit(char c) return SLANG_OK; } +static SlangResult _findLine(const UnownedStringSlice& find, UnownedStringSlice& ioRemaining, UnownedStringSlice& outLine) +{ + // Find the start line + UnownedStringSlice line; + while (StringUtil::extractLine(ioRemaining, line)) + { + if (line.startsWith(find)) + { + outLine = line; + return SLANG_OK; + } + } + return SLANG_FAIL; +} + +/* static */SlangResult HexDumpUtil::findStartAndEndLines(const UnownedStringSlice& lines, UnownedStringSlice& outStart, UnownedStringSlice& outEnd) +{ + UnownedStringSlice remaining(lines); + SLANG_RETURN_ON_FAIL(_findLine(s_start, remaining, outStart)); + SLANG_RETURN_ON_FAIL(_findLine(s_end, remaining, outEnd)); + return SLANG_OK; +} + /* static */SlangResult HexDumpUtil::parseWithMarkers(const UnownedStringSlice& lines, List& outBytes) { - UnownedStringSlice remaining(lines), line; + UnownedStringSlice startLine, endLine; + SLANG_RETURN_ON_FAIL(findStartAndEndLines(lines, startLine, endLine)); - while(StringUtil::extractLine(remaining, line)) + int hash; + size_t size; { - if (line.startsWith(s_start)) + // Get the size and the hash + List slices; + StringUtil::split(startLine, ' ', slices); + if (slices.getCount() != 3) { - // Extract next line - if (!StringUtil::extractLine(remaining, line)) - { - return SLANG_FAIL; - } - // It's the start line - UnownedStringSlice startLine = line; - - // Look for the ending line - do - { - if (line.startsWith(s_end)) - { - return parse(UnownedStringSlice(startLine.begin(), line.begin()), outBytes); - } - } - while ( StringUtil::extractLine(remaining, line)); + return SLANG_FAIL; } + // Extract the size + size = StringToInt(String(slices[1])); + hash = int(StringToInt(String(slices[2]))); } - return SLANG_FAIL; + SLANG_RETURN_ON_FAIL(parse(UnownedStringSlice(startLine.end(), endLine.begin()), outBytes)); + + // Calc the hash + const int readHash = GetHashCode((const char*)outBytes.begin(), outBytes.getCount()); + + if (readHash != hash || size_t(outBytes.getCount()) != size) + { + return SLANG_FAIL; + } + return SLANG_OK; } } -- cgit v1.2.3