1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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* nsCacheMetaData::GetElement(const char* key
) {
11 const char* data
= mBuffer
;
12 const char* limit
= mBuffer
+ mMetaSize
;
14 while (data
< limit
) {
15 // Point to the value part
16 const char* value
= data
+ strlen(data
) + 1;
17 MOZ_ASSERT(value
< limit
, "Cache Metadata corrupted");
18 if (strcmp(data
, key
) == 0) return value
;
21 data
= value
+ strlen(value
) + 1;
23 MOZ_ASSERT(data
== limit
, "Metadata corrupted");
27 nsresult
nsCacheMetaData::SetElement(const char* key
, const char* value
) {
28 const uint32_t keySize
= strlen(key
) + 1;
29 char* pos
= (char*)GetElement(key
);
32 // No value means remove the key/value pair completely, if existing
34 uint32_t oldValueSize
= strlen(pos
) + 1;
35 uint32_t offset
= pos
- mBuffer
;
36 uint32_t remainder
= mMetaSize
- (offset
+ oldValueSize
);
38 memmove(pos
- keySize
, pos
+ oldValueSize
, remainder
);
39 mMetaSize
-= keySize
+ oldValueSize
;
44 const uint32_t valueSize
= strlen(value
) + 1;
45 uint32_t newSize
= mMetaSize
+ valueSize
;
47 const uint32_t oldValueSize
= strlen(pos
) + 1;
48 const uint32_t offset
= pos
- mBuffer
;
49 const uint32_t remainder
= mMetaSize
- (offset
+ oldValueSize
);
51 // Update the value in place
52 newSize
-= oldValueSize
;
53 nsresult rv
= EnsureBuffer(newSize
);
54 NS_ENSURE_SUCCESS(rv
, rv
);
56 // Move the remainder to the right place
57 pos
= mBuffer
+ offset
;
58 memmove(pos
+ valueSize
, pos
+ oldValueSize
, remainder
);
60 // allocate new meta data element
62 nsresult rv
= EnsureBuffer(newSize
);
63 NS_ENSURE_SUCCESS(rv
, rv
);
65 // Add after last element
66 pos
= mBuffer
+ mMetaSize
;
67 memcpy(pos
, key
, keySize
);
72 memcpy(pos
, value
, valueSize
);
78 nsresult
nsCacheMetaData::FlattenMetaData(char* buffer
, uint32_t bufSize
) {
79 if (mMetaSize
> bufSize
) {
80 NS_ERROR("buffer size too small for meta data.");
81 return NS_ERROR_OUT_OF_MEMORY
;
85 MOZ_ASSERT(mMetaSize
== 0);
86 MOZ_ASSERT(bufSize
== 0);
90 memcpy(buffer
, mBuffer
, mMetaSize
);
94 nsresult
nsCacheMetaData::UnflattenMetaData(const char* data
, uint32_t size
) {
96 // Check if the metadata ends with a zero byte.
97 if (data
[size
- 1] != '\0') {
98 NS_ERROR("Cache MetaData is not null terminated");
99 return NS_ERROR_ILLEGAL_VALUE
;
101 // Check that there are an even number of zero bytes
102 // to match the pattern { key \0 value \0 }
104 for (uint32_t i
= 0; i
< size
; i
++) {
105 if (data
[i
] == '\0') odd
= !odd
;
108 NS_ERROR("Cache MetaData is malformed");
109 return NS_ERROR_ILLEGAL_VALUE
;
112 nsresult rv
= EnsureBuffer(size
);
113 NS_ENSURE_SUCCESS(rv
, rv
);
115 memcpy(mBuffer
, data
, size
);
121 nsresult
nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor
* visitor
) {
122 const char* data
= mBuffer
;
123 const char* limit
= mBuffer
+ mMetaSize
;
125 while (data
< limit
) {
126 const char* key
= data
;
128 data
+= strlen(data
) + 1;
129 MOZ_ASSERT(data
< limit
, "Metadata corrupted");
131 nsresult rv
= visitor
->VisitMetaDataElement(key
, data
, &keepGoing
);
132 if (NS_FAILED(rv
) || !keepGoing
) return NS_OK
;
135 data
+= strlen(data
) + 1;
137 MOZ_ASSERT(data
== limit
, "Metadata corrupted");
141 nsresult
nsCacheMetaData::EnsureBuffer(uint32_t bufSize
) {
142 if (mBufferSize
< bufSize
) {
143 char* buf
= (char*)realloc(mBuffer
, bufSize
);
145 return NS_ERROR_OUT_OF_MEMORY
;
148 mBufferSize
= bufSize
;