in plugins/experimental/wasm/lib/src/pairs_util.cc [40:102]
bool PairsUtil::marshalPairs(const Pairs &pairs, char *buffer, size_t size) {
if (buffer == nullptr) {
return false;
}
char *pos = buffer;
const char *end = buffer + size;
// Write number of pairs.
uint32_t num_pairs =
htowasm(pairs.size(), contextOrEffectiveContext() != nullptr
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
: false);
if (pos + sizeof(uint32_t) > end) {
return false;
}
::memcpy(pos, &num_pairs, sizeof(uint32_t));
pos += sizeof(uint32_t);
for (const auto &p : pairs) {
// Write name length.
uint32_t name_len =
htowasm(p.first.size(), contextOrEffectiveContext() != nullptr
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
: false);
if (pos + sizeof(uint32_t) > end) {
return false;
}
::memcpy(pos, &name_len, sizeof(uint32_t));
pos += sizeof(uint32_t);
// Write value length.
uint32_t value_len =
htowasm(p.second.size(), contextOrEffectiveContext() != nullptr
? contextOrEffectiveContext()->wasmVm()->usesWasmByteOrder()
: false);
if (pos + sizeof(uint32_t) > end) {
return false;
}
::memcpy(pos, &value_len, sizeof(uint32_t));
pos += sizeof(uint32_t);
}
for (const auto &p : pairs) {
// Write name.
if (pos + p.first.size() + 1 > end) {
return false;
}
::memcpy(pos, p.first.data(), p.first.size());
pos += p.first.size();
*pos++ = '\0'; // NULL-terminated string.
// Write value.
if (pos + p.second.size() + 1 > end) {
return false;
}
::memcpy(pos, p.second.data(), p.second.size());
pos += p.second.size();
*pos++ = '\0'; // NULL-terminated string.
}
return pos == end;
}