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::ArgumentEncoder& 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(ArgumentDecoder& 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(ArgumentEncoder& encoder, const AtomicString& atomicString)
{
encoder << atomicString.string();
}
bool ArgumentCoder<AtomicString>::decode(ArgumentDecoder& decoder, AtomicString& atomicString)
{
String string;
if (!decoder.decode(string))
return false;
atomicString = string;
return true;
}
void ArgumentCoder<CString>::encode(ArgumentEncoder& 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(ArgumentDecoder& 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(ArgumentEncoder& 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(ArgumentDecoder& 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(ArgumentDecoder& 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);
}
#if HAVE(DTRACE)
void ArgumentCoder<uuid_t>::encode(ArgumentEncoder& encoder, const uuid_t& uuid)
{
SimpleArgumentCoder<uuid_t>::encode(encoder, uuid);
}
bool ArgumentCoder<uuid_t>::decode(ArgumentDecoder& decoder, uuid_t& uuid)
{
return SimpleArgumentCoder<uuid_t>::decode(decoder, uuid);
}
#endif
}