ArgumentCoders.cpp [plain text]
#include "config.h"
#include "ArgumentCoders.h"
#include "DataReference.h"
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
namespace IPC {
void ArgumentCoder<std::chrono::system_clock::time_point>::encode(IPC::Encoder& encoder, const std::chrono::system_clock::time_point& timePoint)
{
encoder << static_cast<int64_t>(timePoint.time_since_epoch().count());
}
bool ArgumentCoder<std::chrono::system_clock::time_point>::decode(Decoder& decoder, std::chrono::system_clock::time_point& result)
{
int64_t time;
if (!decoder.decode(time))
return false;
result = std::chrono::system_clock::time_point(std::chrono::system_clock::duration(static_cast<std::chrono::system_clock::rep>(time)));
return true;
}
void ArgumentCoder<AtomicString>::encode(Encoder& encoder, const AtomicString& atomicString)
{
encoder << atomicString.string();
}
bool ArgumentCoder<AtomicString>::decode(Decoder& decoder, AtomicString& atomicString)
{
String string;
if (!decoder.decode(string))
return false;
atomicString = string;
return true;
}
void ArgumentCoder<CString>::encode(Encoder& encoder, const CString& string)
{
if (string.isNull()) {
encoder << std::numeric_limits<uint32_t>::max();
return;
}
uint32_t length = string.length();
encoder << length;
encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.data()), length, 1);
}
bool ArgumentCoder<CString>::decode(Decoder& decoder, CString& result)
{
uint32_t length;
if (!decoder.decode(length))
return false;
if (length == std::numeric_limits<uint32_t>::max()) {
result = CString();
return true;
}
if (!decoder.bufferIsLargeEnoughToContain<char>(length)) {
decoder.markInvalid();
return false;
}
char* buffer;
CString string = CString::newUninitialized(length, buffer);
if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length, 1))
return false;
result = string;
return true;
}
void ArgumentCoder<String>::encode(Encoder& encoder, const String& string)
{
if (string.isNull()) {
encoder << std::numeric_limits<uint32_t>::max();
return;
}
uint32_t length = string.length();
bool is8Bit = string.is8Bit();
encoder << length << is8Bit;
if (is8Bit)
encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters8()), length * sizeof(LChar), alignof(LChar));
else
encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters16()), length * sizeof(UChar), alignof(UChar));
}
template <typename CharacterType>
static inline bool decodeStringText(Decoder& decoder, uint32_t length, String& result)
{
if (!decoder.bufferIsLargeEnoughToContain<CharacterType>(length)) {
decoder.markInvalid();
return false;
}
CharacterType* buffer;
String string = String::createUninitialized(length, buffer);
if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length * sizeof(CharacterType), alignof(CharacterType)))
return false;
result = string;
return true;
}
bool ArgumentCoder<String>::decode(Decoder& decoder, String& result)
{
uint32_t length;
if (!decoder.decode(length))
return false;
if (length == std::numeric_limits<uint32_t>::max()) {
result = String();
return true;
}
bool is8Bit;
if (!decoder.decode(is8Bit))
return false;
if (is8Bit)
return decodeStringText<LChar>(decoder, length, result);
return decodeStringText<UChar>(decoder, length, result);
}
void ArgumentCoder<SHA1::Digest>::encode(Encoder& encoder, const SHA1::Digest& digest)
{
encoder.encodeFixedLengthData(digest.data(), sizeof(digest), 1);
}
bool ArgumentCoder<SHA1::Digest>::decode(Decoder& decoder, SHA1::Digest& digest)
{
return decoder.decodeFixedLengthData(digest.data(), sizeof(digest), 1);
}
}