#include "config.h"
#include "KeyedDecoder.h"
#include <wtf/cf/TypeCasts.h>
#include <wtf/text/WTFString.h>
namespace WebKit {
KeyedDecoder::KeyedDecoder(const uint8_t* data, size_t size)
{
auto cfData = adoptCF(CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, data, size, kCFAllocatorNull));
if (auto rootDictionary = adoptCF(dynamic_cf_cast<CFDictionaryRef>(CFPropertyListCreateWithData(kCFAllocatorDefault, cfData.get(), kCFPropertyListImmutable, nullptr, nullptr))))
m_rootDictionary = WTF::move(rootDictionary);
else
m_rootDictionary = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, nullptr, nullptr, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
m_dictionaryStack.append(m_rootDictionary.get());
}
KeyedDecoder::~KeyedDecoder()
{
ASSERT(m_dictionaryStack.size() == 1);
ASSERT(m_dictionaryStack.last() == m_rootDictionary);
ASSERT(m_arrayStack.isEmpty());
ASSERT(m_arrayIndexStack.isEmpty());
}
bool KeyedDecoder::decodeBytes(const String& key, const uint8_t*& bytes, size_t& size)
{
auto data = dynamic_cf_cast<CFDataRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!data)
return false;
bytes = CFDataGetBytePtr(data);
size = CFDataGetLength(data);
return true;
}
bool KeyedDecoder::decodeBool(const String& key, bool& result)
{
auto boolean = dynamic_cf_cast<CFBooleanRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!boolean)
return false;
result = CFBooleanGetValue(boolean);
return true;
}
bool KeyedDecoder::decodeUInt32(const String& key, uint32_t& result)
{
return decodeInt32(key, reinterpret_cast<int32_t&>(result));
}
bool KeyedDecoder::decodeInt32(const String& key, int32_t& result)
{
auto number = dynamic_cf_cast<CFNumberRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!number)
return false;
return CFNumberGetValue(number, kCFNumberSInt32Type, &result);
}
bool KeyedDecoder::decodeInt64(const String& key, int64_t& result)
{
auto number = dynamic_cf_cast<CFNumberRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!number)
return false;
return CFNumberGetValue(number, kCFNumberSInt64Type, &result);
}
bool KeyedDecoder::decodeFloat(const String& key, float& result)
{
auto number = dynamic_cf_cast<CFNumberRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!number)
return false;
return CFNumberGetValue(number, kCFNumberFloatType, &result);
}
bool KeyedDecoder::decodeDouble(const String& key, double& result)
{
auto number = dynamic_cf_cast<CFNumberRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!number)
return false;
return CFNumberGetValue(number, kCFNumberDoubleType, &result);
}
bool KeyedDecoder::decodeString(const String& key, String& result)
{
auto string = dynamic_cf_cast<CFStringRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!string)
return false;
result = string;
return true;
}
bool KeyedDecoder::beginObject(const String& key)
{
auto dictionary = dynamic_cf_cast<CFDictionaryRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!dictionary)
return false;
m_dictionaryStack.append(dictionary);
return true;
}
void KeyedDecoder::endObject()
{
m_dictionaryStack.removeLast();
}
bool KeyedDecoder::beginArray(const String& key)
{
auto array = dynamic_cf_cast<CFArrayRef>(CFDictionaryGetValue(m_dictionaryStack.last(), key.createCFString().get()));
if (!array)
return false;
for (CFIndex i = 0; i < CFArrayGetCount(array); ++i) {
CFTypeRef object = CFArrayGetValueAtIndex(array, i);
if (CFGetTypeID(object) != CFDictionaryGetTypeID())
return false;
}
m_arrayStack.append(array);
m_arrayIndexStack.append(0);
return true;
}
bool KeyedDecoder::beginArrayElement()
{
if (m_arrayIndexStack.last() >= CFArrayGetCount(m_arrayStack.last()))
return false;
auto dictionary = checked_cf_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(m_arrayStack.last(), m_arrayIndexStack.last()++));
m_dictionaryStack.append(dictionary);
return true;
}
void KeyedDecoder::endArrayElement()
{
m_dictionaryStack.removeLast();
}
void KeyedDecoder::endArray()
{
m_arrayStack.removeLast();
m_arrayIndexStack.removeLast();
}
}