nsCacheMetaData.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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/. */
  6. #include "nsCacheMetaData.h"
  7. #include "nsICacheEntryDescriptor.h"
  8. const char *
  9. nsCacheMetaData::GetElement(const char * key)
  10. {
  11. const char * data = mBuffer;
  12. const char * limit = mBuffer + mMetaSize;
  13. while (data < limit) {
  14. // Point to the value part
  15. const char * value = data + strlen(data) + 1;
  16. MOZ_ASSERT(value < limit, "Cache Metadata corrupted");
  17. if (strcmp(data, key) == 0)
  18. return value;
  19. // Skip value part
  20. data = value + strlen(value) + 1;
  21. }
  22. MOZ_ASSERT(data == limit, "Metadata corrupted");
  23. return nullptr;
  24. }
  25. nsresult
  26. nsCacheMetaData::SetElement(const char * key,
  27. const char * value)
  28. {
  29. const uint32_t keySize = strlen(key) + 1;
  30. char * pos = (char *)GetElement(key);
  31. if (!value) {
  32. // No value means remove the key/value pair completely, if existing
  33. if (pos) {
  34. uint32_t oldValueSize = strlen(pos) + 1;
  35. uint32_t offset = pos - mBuffer;
  36. uint32_t remainder = mMetaSize - (offset + oldValueSize);
  37. memmove(pos - keySize, pos + oldValueSize, remainder);
  38. mMetaSize -= keySize + oldValueSize;
  39. }
  40. return NS_OK;
  41. }
  42. const uint32_t valueSize = strlen(value) + 1;
  43. uint32_t newSize = mMetaSize + valueSize;
  44. if (pos) {
  45. const uint32_t oldValueSize = strlen(pos) + 1;
  46. const uint32_t offset = pos - mBuffer;
  47. const uint32_t remainder = mMetaSize - (offset + oldValueSize);
  48. // Update the value in place
  49. newSize -= oldValueSize;
  50. nsresult rv = EnsureBuffer(newSize);
  51. NS_ENSURE_SUCCESS(rv, rv);
  52. // Move the remainder to the right place
  53. pos = mBuffer + offset;
  54. memmove(pos + valueSize, pos + oldValueSize, remainder);
  55. } else {
  56. // allocate new meta data element
  57. newSize += keySize;
  58. nsresult rv = EnsureBuffer(newSize);
  59. NS_ENSURE_SUCCESS(rv, rv);
  60. // Add after last element
  61. pos = mBuffer + mMetaSize;
  62. memcpy(pos, key, keySize);
  63. pos += keySize;
  64. }
  65. // Update value
  66. memcpy(pos, value, valueSize);
  67. mMetaSize = newSize;
  68. return NS_OK;
  69. }
  70. nsresult
  71. nsCacheMetaData::FlattenMetaData(char * buffer, uint32_t bufSize)
  72. {
  73. if (mMetaSize > bufSize) {
  74. NS_ERROR("buffer size too small for meta data.");
  75. return NS_ERROR_OUT_OF_MEMORY;
  76. }
  77. memcpy(buffer, mBuffer, mMetaSize);
  78. return NS_OK;
  79. }
  80. nsresult
  81. nsCacheMetaData::UnflattenMetaData(const char * data, uint32_t size)
  82. {
  83. if (data && size) {
  84. // Check if the metadata ends with a zero byte.
  85. if (data[size-1] != '\0') {
  86. NS_ERROR("Cache MetaData is not null terminated");
  87. return NS_ERROR_ILLEGAL_VALUE;
  88. }
  89. // Check that there are an even number of zero bytes
  90. // to match the pattern { key \0 value \0 }
  91. bool odd = false;
  92. for (uint32_t i = 0; i < size; i++) {
  93. if (data[i] == '\0')
  94. odd = !odd;
  95. }
  96. if (odd) {
  97. NS_ERROR("Cache MetaData is malformed");
  98. return NS_ERROR_ILLEGAL_VALUE;
  99. }
  100. nsresult rv = EnsureBuffer(size);
  101. NS_ENSURE_SUCCESS(rv, rv);
  102. memcpy(mBuffer, data, size);
  103. mMetaSize = size;
  104. }
  105. return NS_OK;
  106. }
  107. nsresult
  108. nsCacheMetaData::VisitElements(nsICacheMetaDataVisitor * visitor)
  109. {
  110. const char * data = mBuffer;
  111. const char * limit = mBuffer + mMetaSize;
  112. while (data < limit) {
  113. const char * key = data;
  114. // Skip key part
  115. data += strlen(data) + 1;
  116. MOZ_ASSERT(data < limit, "Metadata corrupted");
  117. bool keepGoing;
  118. nsresult rv = visitor->VisitMetaDataElement(key, data, &keepGoing);
  119. if (NS_FAILED(rv) || !keepGoing)
  120. return NS_OK;
  121. // Skip value part
  122. data += strlen(data) + 1;
  123. }
  124. MOZ_ASSERT(data == limit, "Metadata corrupted");
  125. return NS_OK;
  126. }
  127. nsresult
  128. nsCacheMetaData::EnsureBuffer(uint32_t bufSize)
  129. {
  130. if (mBufferSize < bufSize) {
  131. char * buf = (char *)realloc(mBuffer, bufSize);
  132. if (!buf) {
  133. return NS_ERROR_OUT_OF_MEMORY;
  134. }
  135. mBuffer = buf;
  136. mBufferSize = bufSize;
  137. }
  138. return NS_OK;
  139. }