Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsCacheMetaData.h" |
michael@0 | 8 | #include "nsICacheEntryDescriptor.h" |
michael@0 | 9 | |
michael@0 | 10 | const char * |
michael@0 | 11 | nsCacheMetaData::GetElement(const char * key) |
michael@0 | 12 | { |
michael@0 | 13 | const char * data = mBuffer; |
michael@0 | 14 | const char * limit = mBuffer + mMetaSize; |
michael@0 | 15 | |
michael@0 | 16 | while (data < limit) { |
michael@0 | 17 | // Point to the value part |
michael@0 | 18 | const char * value = data + strlen(data) + 1; |
michael@0 | 19 | NS_ABORT_IF_FALSE(value < limit, "Cache Metadata corrupted"); |
michael@0 | 20 | if (strcmp(data, key) == 0) |
michael@0 | 21 | return value; |
michael@0 | 22 | |
michael@0 | 23 | // Skip value part |
michael@0 | 24 | data = value + strlen(value) + 1; |
michael@0 | 25 | } |
michael@0 | 26 | NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted"); |
michael@0 | 27 | return nullptr; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | nsresult |
michael@0 | 32 | nsCacheMetaData::SetElement(const char * key, |
michael@0 | 33 | const char * value) |
michael@0 | 34 | { |
michael@0 | 35 | const uint32_t keySize = strlen(key) + 1; |
michael@0 | 36 | char * pos = (char *)GetElement(key); |
michael@0 | 37 | |
michael@0 | 38 | if (!value) { |
michael@0 | 39 | // No value means remove the key/value pair completely, if existing |
michael@0 | 40 | if (pos) { |
michael@0 | 41 | uint32_t oldValueSize = strlen(pos) + 1; |
michael@0 | 42 | uint32_t offset = pos - mBuffer; |
michael@0 | 43 | uint32_t remainder = mMetaSize - (offset + oldValueSize); |
michael@0 | 44 | |
michael@0 | 45 | memmove(pos - keySize, pos + oldValueSize, remainder); |
michael@0 | 46 | mMetaSize -= keySize + oldValueSize; |
michael@0 | 47 | } |
michael@0 | 48 | return NS_OK; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | const uint32_t valueSize = strlen(value) + 1; |
michael@0 | 52 | uint32_t newSize = mMetaSize + valueSize; |
michael@0 | 53 | if (pos) { |
michael@0 | 54 | const uint32_t oldValueSize = strlen(pos) + 1; |
michael@0 | 55 | const uint32_t offset = pos - mBuffer; |
michael@0 | 56 | const uint32_t remainder = mMetaSize - (offset + oldValueSize); |
michael@0 | 57 | |
michael@0 | 58 | // Update the value in place |
michael@0 | 59 | newSize -= oldValueSize; |
michael@0 | 60 | nsresult rv = EnsureBuffer(newSize); |
michael@0 | 61 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 62 | |
michael@0 | 63 | // Move the remainder to the right place |
michael@0 | 64 | pos = mBuffer + offset; |
michael@0 | 65 | memmove(pos + valueSize, pos + oldValueSize, remainder); |
michael@0 | 66 | } else { |
michael@0 | 67 | // allocate new meta data element |
michael@0 | 68 | newSize += keySize; |
michael@0 | 69 | nsresult rv = EnsureBuffer(newSize); |
michael@0 | 70 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 71 | |
michael@0 | 72 | // Add after last element |
michael@0 | 73 | pos = mBuffer + mMetaSize; |
michael@0 | 74 | memcpy(pos, key, keySize); |
michael@0 | 75 | pos += keySize; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Update value |
michael@0 | 79 | memcpy(pos, value, valueSize); |
michael@0 | 80 | mMetaSize = newSize; |
michael@0 | 81 | |
michael@0 | 82 | return NS_OK; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | nsresult |
michael@0 | 86 | nsCacheMetaData::FlattenMetaData(char * buffer, uint32_t bufSize) |
michael@0 | 87 | { |
michael@0 | 88 | if (mMetaSize > bufSize) { |
michael@0 | 89 | NS_ERROR("buffer size too small for meta data."); |
michael@0 | 90 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | memcpy(buffer, mBuffer, mMetaSize); |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | nsresult |
michael@0 | 98 | nsCacheMetaData::UnflattenMetaData(const char * data, uint32_t size) |
michael@0 | 99 | { |
michael@0 | 100 | if (data && size) { |
michael@0 | 101 | // Check if the metadata ends with a zero byte. |
michael@0 | 102 | if (data[size-1] != '\0') { |
michael@0 | 103 | NS_ERROR("Cache MetaData is not null terminated"); |
michael@0 | 104 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 105 | } |
michael@0 | 106 | // Check that there are an even number of zero bytes |
michael@0 | 107 | // to match the pattern { key \0 value \0 } |
michael@0 | 108 | bool odd = false; |
michael@0 | 109 | for (uint32_t i = 0; i < size; i++) { |
michael@0 | 110 | if (data[i] == '\0') |
michael@0 | 111 | odd = !odd; |
michael@0 | 112 | } |
michael@0 | 113 | if (odd) { |
michael@0 | 114 | NS_ERROR("Cache MetaData is malformed"); |
michael@0 | 115 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | nsresult rv = EnsureBuffer(size); |
michael@0 | 119 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 120 | |
michael@0 | 121 | memcpy(mBuffer, data, size); |
michael@0 | 122 | mMetaSize = size; |
michael@0 | 123 | } |
michael@0 | 124 | return NS_OK; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | nsresult |
michael@0 | 128 | nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor) |
michael@0 | 129 | { |
michael@0 | 130 | const char * data = mBuffer; |
michael@0 | 131 | const char * limit = mBuffer + mMetaSize; |
michael@0 | 132 | |
michael@0 | 133 | while (data < limit) { |
michael@0 | 134 | const char * key = data; |
michael@0 | 135 | // Skip key part |
michael@0 | 136 | data += strlen(data) + 1; |
michael@0 | 137 | NS_ABORT_IF_FALSE(data < limit, "Metadata corrupted"); |
michael@0 | 138 | bool keepGoing; |
michael@0 | 139 | nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing); |
michael@0 | 140 | if (NS_FAILED(rv) || !keepGoing) |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | |
michael@0 | 143 | // Skip value part |
michael@0 | 144 | data += strlen(data) + 1; |
michael@0 | 145 | } |
michael@0 | 146 | NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted"); |
michael@0 | 147 | return NS_OK; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | nsresult |
michael@0 | 151 | nsCacheMetaData::EnsureBuffer(uint32_t bufSize) |
michael@0 | 152 | { |
michael@0 | 153 | if (mBufferSize < bufSize) { |
michael@0 | 154 | char * buf = (char *)moz_realloc(mBuffer, bufSize); |
michael@0 | 155 | if (!buf) { |
michael@0 | 156 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 157 | } |
michael@0 | 158 | mBuffer = buf; |
michael@0 | 159 | mBufferSize = bufSize; |
michael@0 | 160 | } |
michael@0 | 161 | return NS_OK; |
michael@0 | 162 | } |