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"
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)
24 data
= value
+ strlen(value
) + 1;
26 NS_ABORT_IF_FALSE(data
== limit
, "Metadata corrupted");
32 nsCacheMetaData::SetElement(const char * key
,
35 const uint32_t keySize
= strlen(key
) + 1;
36 char * pos
= (char *)GetElement(key
);
39 // No value means remove the key/value pair completely, if existing
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
;
51 const uint32_t valueSize
= strlen(value
) + 1;
52 uint32_t newSize
= mMetaSize
+ valueSize
;
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
);
67 // allocate new meta data element
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
);
79 memcpy(pos
, value
, valueSize
);
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
);
98 nsCacheMetaData::UnflattenMetaData(const char * data
, uint32_t 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 }
109 for (uint32_t i
= 0; i
< size
; i
++) {
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
);
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
;
136 data
+= strlen(data
) + 1;
137 NS_ABORT_IF_FALSE(data
< limit
, "Metadata corrupted");
139 nsresult rv
= visitor
->VisitMetaDataElement(key
, data
, &keepGoing
);
140 if (NS_FAILED(rv
) || !keepGoing
)
144 data
+= strlen(data
) + 1;
146 NS_ABORT_IF_FALSE(data
== limit
, "Metadata corrupted");
151 nsCacheMetaData::EnsureBuffer(uint32_t bufSize
)
153 if (mBufferSize
< bufSize
) {
154 char * buf
= (char *)moz_realloc(mBuffer
, bufSize
);
156 return NS_ERROR_OUT_OF_MEMORY
;
159 mBufferSize
= bufSize
;