1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsCacheMetaData.cpp, released
17 : * February 22, 2001.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Netscape Communications Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2001
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan <gordon@netscape.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsCacheMetaData.h"
42 : #include "nsICacheEntryDescriptor.h"
43 : #include "prmem.h"
44 :
45 : const char *
46 8494 : nsCacheMetaData::GetElement(const char * key)
47 : {
48 8494 : const char * data = mBuffer;
49 8494 : const char * limit = mBuffer + mMetaSize;
50 :
51 30544 : while (data < limit) {
52 : // Point to the value part
53 17289 : const char * value = data + strlen(data) + 1;
54 17289 : NS_ABORT_IF_FALSE(value < limit, "Cache Metadata corrupted");
55 17289 : if (strcmp(data, key) == 0)
56 3733 : return value;
57 :
58 : // Skip value part
59 13556 : data = value + strlen(value) + 1;
60 : }
61 4761 : NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
62 4761 : return nsnull;
63 : }
64 :
65 :
66 : nsresult
67 4848 : nsCacheMetaData::SetElement(const char * key,
68 : const char * value)
69 : {
70 4848 : const PRUint32 keySize = strlen(key) + 1;
71 4848 : char * pos = (char *)GetElement(key);
72 :
73 4848 : if (!value) {
74 : // No value means remove the key/value pair completely, if existing
75 909 : if (pos) {
76 904 : PRUint32 oldValueSize = strlen(pos) + 1;
77 904 : PRUint32 offset = pos - mBuffer;
78 904 : PRUint32 remainder = mMetaSize - (offset + oldValueSize);
79 :
80 904 : memmove(pos - keySize, pos + oldValueSize, remainder);
81 904 : mMetaSize -= keySize + oldValueSize;
82 : }
83 909 : return NS_OK;
84 : }
85 :
86 3939 : const PRUint32 valueSize = strlen(value) + 1;
87 3939 : PRUint32 newSize = mMetaSize + valueSize;
88 3939 : if (pos) {
89 749 : const PRUint32 oldValueSize = strlen(pos) + 1;
90 749 : const PRUint32 offset = pos - mBuffer;
91 749 : const PRUint32 remainder = mMetaSize - (offset + oldValueSize);
92 :
93 : // Update the value in place
94 749 : newSize -= oldValueSize;
95 749 : nsresult rv = EnsureBuffer(newSize);
96 749 : NS_ENSURE_SUCCESS(rv, rv);
97 :
98 : // Move the remainder to the right place
99 749 : pos = mBuffer + offset;
100 749 : memmove(pos + valueSize, pos + oldValueSize, remainder);
101 : } else {
102 : // allocate new meta data element
103 3190 : newSize += keySize;
104 3190 : nsresult rv = EnsureBuffer(newSize);
105 3190 : NS_ENSURE_SUCCESS(rv, rv);
106 :
107 : // Add after last element
108 3190 : pos = mBuffer + mMetaSize;
109 3190 : memcpy(pos, key, keySize);
110 3190 : pos += keySize;
111 : }
112 :
113 : // Update value
114 3939 : memcpy(pos, value, valueSize);
115 3939 : mMetaSize = newSize;
116 :
117 3939 : return NS_OK;
118 : }
119 :
120 : nsresult
121 691 : nsCacheMetaData::FlattenMetaData(char * buffer, PRUint32 bufSize)
122 : {
123 691 : if (mMetaSize > bufSize) {
124 0 : NS_ERROR("buffer size too small for meta data.");
125 0 : return NS_ERROR_OUT_OF_MEMORY;
126 : }
127 :
128 691 : memcpy(buffer, mBuffer, mMetaSize);
129 691 : return NS_OK;
130 : }
131 :
132 : nsresult
133 285 : nsCacheMetaData::UnflattenMetaData(const char * data, PRUint32 size)
134 : {
135 285 : if (data && size) {
136 : // Check if the metadata ends with a zero byte.
137 274 : if (data[size-1] != '\0') {
138 0 : NS_ERROR("Cache MetaData is not null terminated");
139 0 : return NS_ERROR_ILLEGAL_VALUE;
140 : }
141 : // Check that there are an even number of zero bytes
142 : // to match the pattern { key \0 value \0 }
143 274 : bool odd = false;
144 58669 : for (PRUint32 i = 0; i < size; i++) {
145 58395 : if (data[i] == '\0')
146 1352 : odd = !odd;
147 : }
148 274 : if (odd) {
149 0 : NS_ERROR("Cache MetaData is malformed");
150 0 : return NS_ERROR_ILLEGAL_VALUE;
151 : }
152 :
153 274 : nsresult rv = EnsureBuffer(size);
154 274 : NS_ENSURE_SUCCESS(rv, rv);
155 :
156 274 : memcpy(mBuffer, data, size);
157 274 : mMetaSize = size;
158 : }
159 285 : return NS_OK;
160 : }
161 :
162 : nsresult
163 0 : nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor)
164 : {
165 0 : const char * data = mBuffer;
166 0 : const char * limit = mBuffer + mMetaSize;
167 :
168 0 : while (data < limit) {
169 0 : const char * key = data;
170 : // Skip key part
171 0 : data += strlen(data) + 1;
172 0 : NS_ABORT_IF_FALSE(data < limit, "Metadata corrupted");
173 : bool keepGoing;
174 0 : nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing);
175 0 : if (NS_FAILED(rv) || !keepGoing)
176 0 : return NS_OK;
177 :
178 : // Skip value part
179 0 : data += strlen(data) + 1;
180 : }
181 0 : NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
182 0 : return NS_OK;
183 : }
184 :
185 : nsresult
186 4213 : nsCacheMetaData::EnsureBuffer(PRUint32 bufSize)
187 : {
188 4213 : if (mBufferSize < bufSize) {
189 2862 : char * buf = (char *)PR_REALLOC(mBuffer, bufSize);
190 2862 : if (!buf) {
191 0 : return NS_ERROR_OUT_OF_MEMORY;
192 : }
193 2862 : mBuffer = buf;
194 2862 : mBufferSize = bufSize;
195 : }
196 4213 : return NS_OK;
197 : }
|