ArgumentCoders.cpp [plain text]
#include "config.h"
#include "ArgumentCoders.h"
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
namespace CoreIPC {
void ArgumentCoder<AtomicString>::encode(ArgumentEncoder* encoder, const AtomicString& atomicString)
{
encoder->encode(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->encodeUInt32(std::numeric_limits<uint32_t>::max());
return;
}
uint32_t length = string.length();
encoder->encode(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->encodeUInt32(std::numeric_limits<uint32_t>::max());
return;
}
uint32_t length = string.length();
encoder->encode(length);
encoder->encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters()), length * sizeof(UChar), __alignof(UChar));
}
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;
}
if (!decoder->bufferIsLargeEnoughToContain<UChar>(length)) {
decoder->markInvalid();
return false;
}
UChar* buffer;
String string = String::createUninitialized(length, buffer);
if (!decoder->decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length * sizeof(UChar), __alignof(UChar)))
return false;
result = string;
return true;
}
}