Bug 853360 - Implement the coneGain part of the AudioPannerNode. r=roc
[gecko.git] / netwerk / cache / nsCacheMetaData.cpp
blobd7dc37a32e7ed6d8fbb30d56f7282ce56b774793
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsCacheMetaData.h"
8 #include "nsICacheEntryDescriptor.h"
10 const char *
11 nsCacheMetaData::GetElement(const char * key)
13 const char * data = mBuffer;
14 const char * limit = mBuffer + mMetaSize;
16 while (data < limit) {
17 // Point to the value part
18 const char * value = data + strlen(data) + 1;
19 NS_ABORT_IF_FALSE(value < limit, "Cache Metadata corrupted");
20 if (strcmp(data, key) == 0)
21 return value;
23 // Skip value part
24 data = value + strlen(value) + 1;
26 NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
27 return nullptr;
31 nsresult
32 nsCacheMetaData::SetElement(const char * key,
33 const char * value)
35 const uint32_t keySize = strlen(key) + 1;
36 char * pos = (char *)GetElement(key);
38 if (!value) {
39 // No value means remove the key/value pair completely, if existing
40 if (pos) {
41 uint32_t oldValueSize = strlen(pos) + 1;
42 uint32_t offset = pos - mBuffer;
43 uint32_t remainder = mMetaSize - (offset + oldValueSize);
45 memmove(pos - keySize, pos + oldValueSize, remainder);
46 mMetaSize -= keySize + oldValueSize;
48 return NS_OK;
51 const uint32_t valueSize = strlen(value) + 1;
52 uint32_t newSize = mMetaSize + valueSize;
53 if (pos) {
54 const uint32_t oldValueSize = strlen(pos) + 1;
55 const uint32_t offset = pos - mBuffer;
56 const uint32_t remainder = mMetaSize - (offset + oldValueSize);
58 // Update the value in place
59 newSize -= oldValueSize;
60 nsresult rv = EnsureBuffer(newSize);
61 NS_ENSURE_SUCCESS(rv, rv);
63 // Move the remainder to the right place
64 pos = mBuffer + offset;
65 memmove(pos + valueSize, pos + oldValueSize, remainder);
66 } else {
67 // allocate new meta data element
68 newSize += keySize;
69 nsresult rv = EnsureBuffer(newSize);
70 NS_ENSURE_SUCCESS(rv, rv);
72 // Add after last element
73 pos = mBuffer + mMetaSize;
74 memcpy(pos, key, keySize);
75 pos += keySize;
78 // Update value
79 memcpy(pos, value, valueSize);
80 mMetaSize = newSize;
82 return NS_OK;
85 nsresult
86 nsCacheMetaData::FlattenMetaData(char * buffer, uint32_t bufSize)
88 if (mMetaSize > bufSize) {
89 NS_ERROR("buffer size too small for meta data.");
90 return NS_ERROR_OUT_OF_MEMORY;
93 memcpy(buffer, mBuffer, mMetaSize);
94 return NS_OK;
97 nsresult
98 nsCacheMetaData::UnflattenMetaData(const char * data, uint32_t size)
100 if (data && size) {
101 // Check if the metadata ends with a zero byte.
102 if (data[size-1] != '\0') {
103 NS_ERROR("Cache MetaData is not null terminated");
104 return NS_ERROR_ILLEGAL_VALUE;
106 // Check that there are an even number of zero bytes
107 // to match the pattern { key \0 value \0 }
108 bool odd = false;
109 for (uint32_t i = 0; i < size; i++) {
110 if (data[i] == '\0')
111 odd = !odd;
113 if (odd) {
114 NS_ERROR("Cache MetaData is malformed");
115 return NS_ERROR_ILLEGAL_VALUE;
118 nsresult rv = EnsureBuffer(size);
119 NS_ENSURE_SUCCESS(rv, rv);
121 memcpy(mBuffer, data, size);
122 mMetaSize = size;
124 return NS_OK;
127 nsresult
128 nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor)
130 const char * data = mBuffer;
131 const char * limit = mBuffer + mMetaSize;
133 while (data < limit) {
134 const char * key = data;
135 // Skip key part
136 data += strlen(data) + 1;
137 NS_ABORT_IF_FALSE(data < limit, "Metadata corrupted");
138 bool keepGoing;
139 nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing);
140 if (NS_FAILED(rv) || !keepGoing)
141 return NS_OK;
143 // Skip value part
144 data += strlen(data) + 1;
146 NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
147 return NS_OK;
150 nsresult
151 nsCacheMetaData::EnsureBuffer(uint32_t bufSize)
153 if (mBufferSize < bufSize) {
154 char * buf = (char *)moz_realloc(mBuffer, bufSize);
155 if (!buf) {
156 return NS_ERROR_OUT_OF_MEMORY;
158 mBuffer = buf;
159 mBufferSize = bufSize;
161 return NS_OK;