summaryrefslogtreecommitdiffstats
path: root/source/core
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-10-23 13:25:58 -0400
committerGitHub <noreply@github.com>2019-10-23 13:25:58 -0400
commit73dcec672a2be0082945b71fc7848d9be0233f56 (patch)
treeb628912a14463457c45d1f442be993dafe2b68be /source/core
parent85f858c6d1b91fd2ed89844aae7535a48e51a4c8 (diff)
Version information on repro binary format. (#1090)
* * Added semantic versioning and hashing test on repro data. * Added RiffSemanticVersion type * Fix linux build warning.
Diffstat (limited to 'source/core')
-rw-r--r--source/core/slang-riff.cpp45
-rw-r--r--source/core/slang-riff.h58
2 files changed, 87 insertions, 16 deletions
diff --git a/source/core/slang-riff.cpp b/source/core/slang-riff.cpp
index ce8b7129b..0b0519b48 100644
--- a/source/core/slang-riff.cpp
+++ b/source/core/slang-riff.cpp
@@ -42,20 +42,28 @@ namespace Slang
}
-/* static */SlangResult RiffUtil::writeData(uint32_t riffType, const void* data, size_t size, Stream* out)
+/* static */SlangResult RiffUtil::writeData(const RiffChunk* header, size_t headerSize, const void* payload, size_t payloadSize, Stream* out)
{
- SLANG_ASSERT(uint64_t(size) <= uint64_t(0xfffffffff));
+ SLANG_ASSERT(uint64_t(payloadSize) <= uint64_t(0xfffffffff));
+ SLANG_ASSERT(headerSize >= sizeof(RiffChunk));
+ SLANG_ASSERT((headerSize & 3) == 0);
// TODO(JS): Could handle endianness here
+
RiffChunk chunk;
- chunk.m_type = riffType;
- chunk.m_size = uint32_t(size);
+ chunk.m_type = header->m_type;
+ chunk.m_size = uint32_t(headerSize - sizeof(RiffChunk) + payloadSize);
try
{
- out->write(&chunk, sizeof(chunk));
- out->write(data, size);
- size_t remaining = size & 3;
+ // The chunk
+ out->write(&chunk, sizeof(RiffChunk));
+ // The rest of the header
+ out->write(header + 1, headerSize - sizeof(RiffChunk));
+
+ out->write(payload, payloadSize);
+ size_t remaining = payloadSize & 3;
+
if (remaining)
{
uint8_t end[4] = { 0, 0, 0, 0};
@@ -70,19 +78,30 @@ namespace Slang
return SLANG_OK;
}
-
-/* static */SlangResult RiffUtil::readData(Stream* stream, RiffChunk& outChunk, List<uint8_t>& data)
+/* static */SlangResult RiffUtil::readData(Stream* stream, RiffChunk* outHeader, size_t headerSize, List<uint8_t>& data)
{
- SLANG_RETURN_ON_FAIL(readChunk(stream, outChunk));
+ RiffChunk chunk;
+ SLANG_RETURN_ON_FAIL(readChunk(stream, chunk));
+ if (chunk.m_size < headerSize)
+ {
+ return SLANG_FAIL;
+ }
- data.setCount(outChunk.m_size);
+ *outHeader = chunk;
try
{
- stream->read(data.getBuffer(), outChunk.m_size);
+ // Read the header
+ stream->read(outHeader + 1, headerSize - sizeof(RiffChunk));
+
+ const size_t payloadSize = chunk.m_size - (headerSize - sizeof(RiffChunk));
+
+ data.setCount(payloadSize);
+
+ stream->read(data.getBuffer(), payloadSize);
// Skip to the alignment
- uint32_t remaining = outChunk.m_size & 3;
+ uint32_t remaining = payloadSize & 3;
if (remaining)
{
stream->seek(SeekOrigin::Current, 4 - remaining);
diff --git a/source/core/slang-riff.h b/source/core/slang-riff.h
index f6854310b..f681d6d68 100644
--- a/source/core/slang-riff.h
+++ b/source/core/slang-riff.h
@@ -17,6 +17,59 @@ struct RiffFourCC
static const uint32_t kRiff = SLANG_FOUR_CC('R', 'I', 'F', 'F');
};
+// Follows semantic version rules
+// https://semver.org/
+//
+// major.minor.patch
+// Patch versions indicate a change.
+// Minor means a change that is backwards compatible with previous minor versions. A step in minor and/or major zeros patch.
+// Major means a non compatible change. A step in major, zeros minor and patch.
+struct RiffSemanticVersion
+{
+ typedef RiffSemanticVersion ThisType;
+ typedef uint32_t RawType;
+
+ /// ==
+ bool operator==(const ThisType& rhs) const { return m_raw == rhs.m_raw; }
+ bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
+
+ /// A patch change indices a different version but does not change the compatibility of the format
+ int getPatch() const { return m_raw & 0xff; }
+ /// A minor change implies a format change that is backwards compatible
+ int getMinor() const { return (m_raw >> 8) & 0xff; }
+ /// A major change is binary incompatible by default
+ int getMajor() const { return (m_raw >> 16); }
+
+ static RawType makeRaw(int major, int minor, int patch)
+ {
+ SLANG_ASSERT((major | minor | patch) >= 0);
+ SLANG_ASSERT(major < 0x10000 && minor < 0x100 && patch < 0x100);
+ return (RawType(major) << 16) | (RawType(minor) << 8) | RawType(patch);
+ }
+
+ static RiffSemanticVersion makeFromRaw(RawType raw)
+ {
+ ThisType version;
+ version.m_raw = raw;
+ return version;
+ }
+
+ static RiffSemanticVersion make(int major, int minor, int patch) { return makeFromRaw(makeRaw(major, minor, patch)); }
+
+ /// True if the read version is compatible with the current version, based on semantic rules.
+ static bool areCompatible(const ThisType& currentVersion, const ThisType& readVersion)
+ {
+ const RawType currentRaw = currentVersion.m_raw;
+ const RawType readRaw = readVersion.m_raw;
+
+ // Must have same major version.
+ // For minor version, the read version must be less than or equal.
+ return ((currentRaw & 0xffff0000) == (readRaw & 0xffff0000)) && ((currentRaw & 0xff00) >= (readRaw & 0xff00));
+ }
+
+ RawType m_raw;
+};
+
struct RiffChunk
{
uint32_t m_type; ///< The FourCC code that identifies this chunk
@@ -31,9 +84,8 @@ struct RiffUtil
static SlangResult readChunk(Stream* stream, RiffChunk& outChunk);
-
- static SlangResult writeData(uint32_t riffType, const void* data, size_t size, Stream* out);
- static SlangResult readData(Stream* stream, RiffChunk& outChunk, List<uint8_t>& data);
+ static SlangResult writeData(const RiffChunk* header, size_t headerSize, const void* payload, size_t payloadSize, Stream* out);
+ static SlangResult readData(Stream* stream, RiffChunk* outHeader, size_t headerSize, List<uint8_t>& data);
};
}