This source file includes following definitions.
- QueryKnownToValidate
- SetKnownToValidate
- Serialize
- Reset
- Deserialize
- DeserializeImpl
#include "components/nacl/browser/nacl_validation_cache.h"
#include "base/pickle.h"
#include "base/rand_util.h"
namespace nacl {
const size_t kValidationCacheCacheSize = 200;
const size_t kValidationCacheKeySize = 64;
const size_t kValidationCacheEntrySize = 32;
const char kValidationCacheBeginMagic[] = "NaCl";
const char kValidationCacheEndMagic[] = "Done";
NaClValidationCache::NaClValidationCache()
: validation_cache_(kValidationCacheCacheSize) {
Reset();
}
NaClValidationCache::~NaClValidationCache() {
}
bool NaClValidationCache::QueryKnownToValidate(const std::string& signature,
bool reorder) {
if (signature.length() == kValidationCacheEntrySize) {
ValidationCacheType::iterator iter;
if (reorder) {
iter = validation_cache_.Get(signature);
} else {
iter = validation_cache_.Peek(signature);
}
if (iter != validation_cache_.end()) {
return iter->second;
}
}
return false;
}
void NaClValidationCache::SetKnownToValidate(const std::string& signature) {
if (signature.length() == kValidationCacheEntrySize) {
validation_cache_.Put(signature, true);
}
}
void NaClValidationCache::Serialize(Pickle* pickle) const {
pickle->WriteString(kValidationCacheBeginMagic);
pickle->WriteString(validation_cache_key_);
pickle->WriteInt(validation_cache_.size());
ValidationCacheType::const_reverse_iterator iter;
for (iter = validation_cache_.rbegin();
iter != validation_cache_.rend();
++iter) {
pickle->WriteString(iter->first);
}
pickle->WriteString(kValidationCacheEndMagic);
}
void NaClValidationCache::Reset() {
validation_cache_key_ = base::RandBytesAsString(kValidationCacheKeySize);
validation_cache_.Clear();
}
bool NaClValidationCache::Deserialize(const Pickle* pickle) {
bool success = DeserializeImpl(pickle);
if (!success) {
Reset();
}
return success;
}
bool NaClValidationCache::DeserializeImpl(const Pickle* pickle) {
PickleIterator iter(*pickle);
std::string buffer;
int count;
if (!iter.ReadString(&buffer))
return false;
if (0 != buffer.compare(kValidationCacheBeginMagic))
return false;
if (!iter.ReadString(&buffer))
return false;
if (buffer.size() != kValidationCacheKeySize)
return false;
validation_cache_key_ = buffer;
validation_cache_.Clear();
if (!iter.ReadInt(&count))
return false;
for (int i = 0; i < count; ++i) {
if (!iter.ReadString(&buffer))
return false;
if (buffer.size() != kValidationCacheEntrySize)
return false;
validation_cache_.Put(buffer, true);
}
if (!iter.ReadString(&buffer))
return false;
if (0 != buffer.compare(kValidationCacheEndMagic))
return false;
return true;
}
}