Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/dmxpp/dmxpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ class BufferStream;

namespace dmxpp {

constexpr int MAX_FORMAT = 64;
constexpr int MAX_HEADER = 40 + 2 * MAX_FORMAT; // DMX_MAX_HEADER_LENGTH in SDK

namespace Format {

constexpr std::string_view TEXT = "keyvalues2";
constexpr std::string_view BINARY = "binary";
constexpr std::string_view SRCTOOLS_UTF8_TEXT = "unicode_keyvalues2";
constexpr std::string_view SRCTOOLS_UTF8_BINARY = "unicode_binary";

} // namespace Format

Expand Down
30 changes: 23 additions & 7 deletions src/dmxpp/dmxpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ DMX::DMX(const std::byte* dmxData, std::size_t dmxSize) {

BufferStreamReadOnly stream{dmxData, dmxSize};

auto header = stream.read_string();
// The header is terminated by a newline, but can be at most MAX_HEADER long.
// For binary formats that's then followed by a null terminator.
std::string header;
while (header.length() < MAX_HEADER) {
char temp = stream.read<char>();
if (temp == '\n') {
break;
}
header += temp;
}
if (header.length() < 37) {
// Minimum possible header length - early "binary_v2" version, "keyvalues2_v1" unsupported
return;
}
char encodingTypeData[64];
char encodingTypeData[MAX_FORMAT];
int32_t encodingVersionData;
char formatTypeData[64];
char formatTypeData[MAX_FORMAT];
int32_t formatVersionData;
#ifdef _WIN32
sscanf_s(header.c_str(), "<!-- dmx encoding %63s %i format %63s %i -->\n", encodingTypeData, 64, &encodingVersionData, formatTypeData, 64, &formatVersionData);
sscanf_s(header.c_str(), "<!-- dmx encoding %63s %i format %63s %i -->\n", encodingTypeData, MAX_FORMAT, &encodingVersionData, formatTypeData, MAX_FORMAT, &formatVersionData);
#else
std::sscanf(header.c_str(), "<!-- dmx encoding %s %i format %s %i -->\n", encodingTypeData, &encodingVersionData, formatTypeData, &formatVersionData);
#endif
Expand All @@ -41,9 +50,10 @@ DMX::DMX(const std::byte* dmxData, std::size_t dmxSize) {
return;
}

if (this->encodingType == Format::BINARY) {
// Srctools formats indicate that all strings are specifically UTF-8.
if (this->encodingType == Format::BINARY || this->encodingType == Format::SRCTOOLS_UTF8_BINARY) {
this->opened = this->openBinary(stream);
} else if (this->encodingType == Format::TEXT) {
} else if (this->encodingType == Format::TEXT || this->encodingType == Format::SRCTOOLS_UTF8_TEXT) {
this->opened = this->openText(dmxData, dmxSize);
}
}
Expand Down Expand Up @@ -92,6 +102,11 @@ bool DMX::openBinary(BufferStream& stream) {
const bool elementNamesAreStoredInStringList = this->encodingVersion >= 4;
const bool stringValuesAreStoredInStringList = this->encodingVersion >= 4;

// Eat the null terminator for the header.
if (stream.read<char>() != 0) {
return false;
}

// String list
std::vector<std::string> stringList;
uint32_t stringCount;
Expand Down Expand Up @@ -140,7 +155,8 @@ bool DMX::openBinary(BufferStream& stream) {
auto size = reader.read<uint32_t>();
out.reserve(size);
for (int i = 0; i < size; i++) {
out.push_back(std::get<T>(readValue(reader, Value::arrayIDToInnerID(type_), true)));
// String arrays are always inline.
out.push_back(std::get<T>(readValue(reader, Value::arrayIDToInnerID(type_), false)));
}
return out;
};
Expand Down