2001-02-23 16:18:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
#include "nsCacheMetaData.h"
|
2001-04-04 07:30:45 +04:00
|
|
|
#include "nsICacheEntryDescriptor.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-03-25 18:20:10 +03:00
|
|
|
const char *
|
|
|
|
nsCacheMetaData::GetElement(const char * key)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2009-09-16 19:56:09 +04:00
|
|
|
const char * data = mBuffer;
|
|
|
|
const char * limit = mBuffer + mMetaSize;
|
|
|
|
|
|
|
|
while (data < limit) {
|
|
|
|
// Point to the value part
|
|
|
|
const char * value = data + strlen(data) + 1;
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(value < limit, "Cache Metadata corrupted");
|
2009-09-16 19:56:09 +04:00
|
|
|
if (strcmp(data, key) == 0)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
// Skip value part
|
|
|
|
data = value + strlen(value) + 1;
|
2001-03-02 04:51:41 +03:00
|
|
|
}
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(data == limit, "Metadata corrupted");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2003-03-25 18:20:10 +03:00
|
|
|
nsCacheMetaData::SetElement(const char * key,
|
|
|
|
const char * value)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t keySize = strlen(key) + 1;
|
2009-09-16 19:56:09 +04:00
|
|
|
char * pos = (char *)GetElement(key);
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
// No value means remove the key/value pair completely, if existing
|
|
|
|
if (pos) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldValueSize = strlen(pos) + 1;
|
|
|
|
uint32_t offset = pos - mBuffer;
|
|
|
|
uint32_t remainder = mMetaSize - (offset + oldValueSize);
|
2009-09-16 19:56:09 +04:00
|
|
|
|
|
|
|
memmove(pos - keySize, pos + oldValueSize, remainder);
|
|
|
|
mMetaSize -= keySize + oldValueSize;
|
2001-03-02 04:51:41 +03:00
|
|
|
}
|
2009-09-16 19:56:09 +04:00
|
|
|
return NS_OK;
|
2001-03-02 04:51:41 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t valueSize = strlen(value) + 1;
|
|
|
|
uint32_t newSize = mMetaSize + valueSize;
|
2009-09-16 19:56:09 +04:00
|
|
|
if (pos) {
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t oldValueSize = strlen(pos) + 1;
|
|
|
|
const uint32_t offset = pos - mBuffer;
|
|
|
|
const uint32_t remainder = mMetaSize - (offset + oldValueSize);
|
2009-09-16 19:56:09 +04:00
|
|
|
|
|
|
|
// Update the value in place
|
|
|
|
newSize -= oldValueSize;
|
|
|
|
nsresult rv = EnsureBuffer(newSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Move the remainder to the right place
|
|
|
|
pos = mBuffer + offset;
|
|
|
|
memmove(pos + valueSize, pos + oldValueSize, remainder);
|
|
|
|
} else {
|
|
|
|
// allocate new meta data element
|
|
|
|
newSize += keySize;
|
|
|
|
nsresult rv = EnsureBuffer(newSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Add after last element
|
|
|
|
pos = mBuffer + mMetaSize;
|
|
|
|
memcpy(pos, key, keySize);
|
|
|
|
pos += keySize;
|
2001-03-02 04:51:41 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
// Update value
|
|
|
|
memcpy(pos, value, valueSize);
|
|
|
|
mMetaSize = newSize;
|
|
|
|
|
2003-03-25 18:20:10 +03:00
|
|
|
return NS_OK;
|
2001-03-13 18:43:17 +03:00
|
|
|
}
|
|
|
|
|
2001-02-28 10:13:32 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheMetaData::FlattenMetaData(char * buffer, uint32_t bufSize)
|
2001-02-28 10:13:32 +03:00
|
|
|
{
|
2003-09-11 20:57:40 +04:00
|
|
|
if (mMetaSize > bufSize) {
|
|
|
|
NS_ERROR("buffer size too small for meta data.");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2003-03-25 18:20:10 +03:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
memcpy(buffer, mBuffer, mMetaSize);
|
2001-03-02 07:09:01 +03:00
|
|
|
return NS_OK;
|
2001-02-28 10:13:32 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-02 07:09:01 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheMetaData::UnflattenMetaData(const char * data, uint32_t size)
|
2001-03-02 07:09:01 +03:00
|
|
|
{
|
2009-09-16 19:56:09 +04:00
|
|
|
if (data && size) {
|
2011-08-23 20:45:10 +04:00
|
|
|
// Check if the metadata ends with a zero byte.
|
|
|
|
if (data[size-1] != '\0') {
|
|
|
|
NS_ERROR("Cache MetaData is not null terminated");
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
// Check that there are an even number of zero bytes
|
|
|
|
// to match the pattern { key \0 value \0 }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool odd = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2017-07-06 15:00:35 +03:00
|
|
|
if (data[i] == '\0')
|
2011-08-23 20:45:10 +04:00
|
|
|
odd = !odd;
|
|
|
|
}
|
|
|
|
if (odd) {
|
|
|
|
NS_ERROR("Cache MetaData is malformed");
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
nsresult rv = EnsureBuffer(size);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-09-11 20:57:40 +04:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
memcpy(mBuffer, data, size);
|
|
|
|
mMetaSize = size;
|
2001-03-02 07:09:01 +03:00
|
|
|
}
|
2003-09-11 20:57:40 +04:00
|
|
|
return NS_OK;
|
2001-03-02 07:09:01 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-04-04 07:30:45 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor)
|
|
|
|
{
|
2009-09-16 19:56:09 +04:00
|
|
|
const char * data = mBuffer;
|
|
|
|
const char * limit = mBuffer + mMetaSize;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
while (data < limit) {
|
|
|
|
const char * key = data;
|
|
|
|
// Skip key part
|
|
|
|
data += strlen(data) + 1;
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(data < limit, "Metadata corrupted");
|
2011-09-29 10:19:26 +04:00
|
|
|
bool keepGoing;
|
2009-09-16 19:56:09 +04:00
|
|
|
nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing);
|
2003-03-25 18:20:10 +03:00
|
|
|
if (NS_FAILED(rv) || !keepGoing)
|
2012-01-28 19:13:43 +04:00
|
|
|
return NS_OK;
|
2001-02-28 10:13:32 +03:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
// Skip value part
|
|
|
|
data += strlen(data) + 1;
|
2001-06-14 03:37:41 +04:00
|
|
|
}
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(data == limit, "Metadata corrupted");
|
2003-03-25 18:20:10 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-06-14 03:37:41 +04:00
|
|
|
|
2009-09-16 19:56:09 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCacheMetaData::EnsureBuffer(uint32_t bufSize)
|
2001-02-28 10:13:32 +03:00
|
|
|
{
|
2009-09-16 19:56:09 +04:00
|
|
|
if (mBufferSize < bufSize) {
|
2015-02-19 07:51:06 +03:00
|
|
|
char * buf = (char *)realloc(mBuffer, bufSize);
|
2009-09-16 19:56:09 +04:00
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mBuffer = buf;
|
|
|
|
mBufferSize = bufSize;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|