michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCacheMetaData.h" michael@0: #include "nsICacheEntryDescriptor.h" michael@0: michael@0: const char * michael@0: nsCacheMetaData::GetElement(const char * key) michael@0: { michael@0: const char * data = mBuffer; michael@0: const char * limit = mBuffer + mMetaSize; michael@0: michael@0: while (data < limit) { michael@0: // Point to the value part michael@0: const char * value = data + strlen(data) + 1; michael@0: NS_ABORT_IF_FALSE(value < limit, "Cache Metadata corrupted"); michael@0: if (strcmp(data, key) == 0) michael@0: return value; michael@0: michael@0: // Skip value part michael@0: data = value + strlen(value) + 1; michael@0: } michael@0: NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted"); michael@0: return nullptr; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: nsCacheMetaData::SetElement(const char * key, michael@0: const char * value) michael@0: { michael@0: const uint32_t keySize = strlen(key) + 1; michael@0: char * pos = (char *)GetElement(key); michael@0: michael@0: if (!value) { michael@0: // No value means remove the key/value pair completely, if existing michael@0: if (pos) { michael@0: uint32_t oldValueSize = strlen(pos) + 1; michael@0: uint32_t offset = pos - mBuffer; michael@0: uint32_t remainder = mMetaSize - (offset + oldValueSize); michael@0: michael@0: memmove(pos - keySize, pos + oldValueSize, remainder); michael@0: mMetaSize -= keySize + oldValueSize; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: const uint32_t valueSize = strlen(value) + 1; michael@0: uint32_t newSize = mMetaSize + valueSize; michael@0: if (pos) { michael@0: const uint32_t oldValueSize = strlen(pos) + 1; michael@0: const uint32_t offset = pos - mBuffer; michael@0: const uint32_t remainder = mMetaSize - (offset + oldValueSize); michael@0: michael@0: // Update the value in place michael@0: newSize -= oldValueSize; michael@0: nsresult rv = EnsureBuffer(newSize); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Move the remainder to the right place michael@0: pos = mBuffer + offset; michael@0: memmove(pos + valueSize, pos + oldValueSize, remainder); michael@0: } else { michael@0: // allocate new meta data element michael@0: newSize += keySize; michael@0: nsresult rv = EnsureBuffer(newSize); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Add after last element michael@0: pos = mBuffer + mMetaSize; michael@0: memcpy(pos, key, keySize); michael@0: pos += keySize; michael@0: } michael@0: michael@0: // Update value michael@0: memcpy(pos, value, valueSize); michael@0: mMetaSize = newSize; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsCacheMetaData::FlattenMetaData(char * buffer, uint32_t bufSize) michael@0: { michael@0: if (mMetaSize > bufSize) { michael@0: NS_ERROR("buffer size too small for meta data."); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: memcpy(buffer, mBuffer, mMetaSize); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsCacheMetaData::UnflattenMetaData(const char * data, uint32_t size) michael@0: { michael@0: if (data && size) { michael@0: // Check if the metadata ends with a zero byte. michael@0: if (data[size-1] != '\0') { michael@0: NS_ERROR("Cache MetaData is not null terminated"); michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: // Check that there are an even number of zero bytes michael@0: // to match the pattern { key \0 value \0 } michael@0: bool odd = false; michael@0: for (uint32_t i = 0; i < size; i++) { michael@0: if (data[i] == '\0') michael@0: odd = !odd; michael@0: } michael@0: if (odd) { michael@0: NS_ERROR("Cache MetaData is malformed"); michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: michael@0: nsresult rv = EnsureBuffer(size); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: memcpy(mBuffer, data, size); michael@0: mMetaSize = size; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor) michael@0: { michael@0: const char * data = mBuffer; michael@0: const char * limit = mBuffer + mMetaSize; michael@0: michael@0: while (data < limit) { michael@0: const char * key = data; michael@0: // Skip key part michael@0: data += strlen(data) + 1; michael@0: NS_ABORT_IF_FALSE(data < limit, "Metadata corrupted"); michael@0: bool keepGoing; michael@0: nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing); michael@0: if (NS_FAILED(rv) || !keepGoing) michael@0: return NS_OK; michael@0: michael@0: // Skip value part michael@0: data += strlen(data) + 1; michael@0: } michael@0: NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsCacheMetaData::EnsureBuffer(uint32_t bufSize) michael@0: { michael@0: if (mBufferSize < bufSize) { michael@0: char * buf = (char *)moz_realloc(mBuffer, bufSize); michael@0: if (!buf) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: mBuffer = buf; michael@0: mBufferSize = bufSize; michael@0: } michael@0: return NS_OK; michael@0: }