LCOV - app.info - netwerk/cache/nsCacheMetaData.cpp

LCOV - code coverage report
| Current view: | directory - netwerk/cache - nsCacheMetaData.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 86 | 66 | 76.7 %
| Date: | 2012-04-21 | Functions: | 6 | 5 | 83.3 %

           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            8497 : nsCacheMetaData::GetElement(const char * key)
          47                 : {
          48            8497 :     const char * data = mBuffer;
          49            8497 :     const char * limit = mBuffer + mMetaSize;
          50                 :
          51           30566 :     while (data < limit) {
          52                 :         // Point to the value part
          53           17305 :         const char * value = data + strlen(data) + 1;
          54           17305 :         NS_ABORT_IF_FALSE(value < limit, "Cache Metadata corrupted");
          55           17305 :         if (strcmp(data, key) == 0)
          56            3733 :             return value;
          57                 :
          58                 :         // Skip value part
          59           13572 :         data = value + strlen(value) + 1;
          60                 :     }
          61            4764 :     NS_ABORT_IF_FALSE(data == limit, "Metadata corrupted");
          62            4764 :     return nsnull;
          63                 : }
          64                 :
          65                 :
          66                 : nsresult
          67            4852 : nsCacheMetaData::SetElement(const char * key,
          68                 :                             const char * value)
          69                 : {
          70            4852 :     const PRUint32 keySize = strlen(key) + 1;
          71            4852 :     char * pos = (char *)GetElement(key);
          72                 :
          73            4852 :     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            3943 :     const PRUint32 valueSize = strlen(value) + 1;
          87            3943 :     PRUint32 newSize = mMetaSize + valueSize;
          88            3943 :     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            3194 :         newSize += keySize;
         104            3194 :         nsresult rv = EnsureBuffer(newSize);
         105            3194 :         NS_ENSURE_SUCCESS(rv, rv);
         106                 :
         107                 :         // Add after last element
         108            3194 :         pos = mBuffer + mMetaSize;
         109            3194 :         memcpy(pos, key, keySize);
         110            3194 :         pos += keySize;
         111                 :     }
         112                 :
         113                 :     // Update value
         114            3943 :     memcpy(pos, value, valueSize);
         115            3943 :     mMetaSize = newSize;
         116                 :
         117            3943 :     return NS_OK;
         118                 : }
         119                 :
         120                 : nsresult
         121             689 : nsCacheMetaData::FlattenMetaData(char * buffer, PRUint32 bufSize)
         122                 : {
         123             689 :     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             689 :     memcpy(buffer, mBuffer, mMetaSize);
         129             689 :     return NS_OK;
         130                 : }
         131                 :
         132                 : nsresult
         133             284 : nsCacheMetaData::UnflattenMetaData(const char * data, PRUint32 size)
         134                 : {
         135             284 :     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             284 :     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            4217 : nsCacheMetaData::EnsureBuffer(PRUint32 bufSize)
         187                 : {
         188            4217 :     if (mBufferSize < bufSize) {
         189            2866 :         char * buf = (char *)PR_REALLOC(mBuffer, bufSize);
         190            2866 :         if (!buf) {
         191               0 :             return NS_ERROR_OUT_OF_MEMORY;
         192                 :         }
         193            2866 :         mBuffer = buf;
         194            2866 :         mBufferSize = bufSize;
         195                 :     }
         196            4217 :     return NS_OK;
         197                 : }

Generated by: LCOV version 1.7