Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
nsFastLoadFile.cpp File Reference
#include <string.h>
#include "prtypes.h"
#include "nscore.h"
#include "nsDebug.h"
#include "nsEnumeratorUtils.h"
#include "nsMemory.h"
#include "nsXPIDLString.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIComponentManager.h"
#include "nsIFile.h"
#include "nsILocalFile.h"
#include "nsISeekableStream.h"
#include "nsISerializable.h"
#include "nsIStreamBufferAccess.h"
#include "nsBinaryStream.h"
#include "nsFastLoadFile.h"
#include "nsInt64.h"

Go to the source code of this file.

Classes

struct  nsStringMapEntry
struct  nsDocumentMapEntry
struct  nsDocumentMapReadEntry
struct  nsObjectMapEntry
struct  nsURIMapReadEntry
struct  nsDocumentMapWriteEntry
struct  nsURIMapWriteEntry
struct  nsDependencyMapEntry
struct  nsSharpObjectMapEntry

Defines

#define METER(x)   /* nothing */
#define TRACE_MUX(args)   /* nothing */
#define FOLD_ONES_COMPLEMENT_CARRY(X)   ((X) = ((X) & 0xffff) + ((X) >> 16))
#define ONES_COMPLEMENT_ACCUMULATE(X, Y)
#define FLETCHER_ACCUMULATE(A, B, U)
#define MFL_CHECKSUM_BUFSIZE   8192
 XXX tuneme.
#define MFL_SINGLE_REF_PSEUDO_TAG   PR_BIT(MFL_OBJECT_TAG_BITS)

Functions

 NS_AccumulateFastLoadChecksum (PRUint32 *aChecksum, const PRUint8 *aBuffer, PRUint32 aLength, PRBool aLastBuffer)
 Compute Fletcher's 16-bit checksum over aLength bytes starting at aBuffer, with the initial accumulators seeded from *aChecksum, and final checksum returned in *aChecksum.
 NS_AddFastLoadChecksums (PRUint32 sum1, PRUint32 sum2, PRUint32 sum2ByteCount)
 NS_IMPL_ISUPPORTS_INHERITED5 (nsFastLoadFileReader, nsBinaryInputStream, nsIObjectInputStream, nsIFastLoadFileControl, nsIFastLoadReadControl, nsISeekableStream, nsIFastLoadFileReader) nsresult nsFastLoadFileReader
 strmap_ClearEntry (PLDHashTable *aTable, PLDHashEntryHdr *aHdr)
 objmap_ClearEntry (PLDHashTable *aTable, PLDHashEntryHdr *aHdr)
NS_COM nsresult NS_NewFastLoadFileReader (nsIObjectInputStream **aResult, nsIInputStream *aSrcStream)
 NS_IMPL_ISUPPORTS_INHERITED4 (nsFastLoadFileWriter, nsBinaryOutputStream, nsIObjectOutputStream, nsIFastLoadFileControl, nsIFastLoadWriteControl, nsISeekableStream) struct nsIDMapEntry
 idmap_GetKey (PLDHashTable *aTable, PLDHashEntryHdr *aHdr)
 idmap_HashKey (PLDHashTable *aTable, const void *aKey)
 idmap_MatchEntry (PLDHashTable *aTable, const PLDHashEntryHdr *aHdr, const void *aKey)
NS_COM nsresult NS_NewFastLoadFileWriter (nsIObjectOutputStream **aResult, nsIOutputStream *aDestStream, nsIFastLoadFileIO *aFileIO)
 NS_IMPL_ISUPPORTS_INHERITED1 (nsFastLoadFileUpdater, nsFastLoadFileWriter, nsIFastLoadFileIO) NS_IMETHODIMP nsFastLoadFileUpdater
NS_COM nsresult NS_NewFastLoadFileUpdater (nsIObjectOutputStream **aResult, nsIOutputStream *aOutputStream, nsIObjectInputStream *aReaderAsStream)

Variables

static const char magic [] = MFL_FILE_MAGIC
static const PLDHashTableOps strmap_DHashTableOps
static const PLDHashTableOps objmap_DHashTableOps
static const PLDHashTableOps idmap_DHashTableOps

Define Documentation

#define FLETCHER_ACCUMULATE (   A,
  B,
  U 
)
Value:
ONES_COMPLEMENT_ACCUMULATE(A, U);     \
                                        ONES_COMPLEMENT_ACCUMULATE(B, A)

Definition at line 103 of file nsFastLoadFile.cpp.

#define FOLD_ONES_COMPLEMENT_CARRY (   X)    ((X) = ((X) & 0xffff) + ((X) >> 16))

Definition at line 100 of file nsFastLoadFile.cpp.

#define METER (   x)    /* nothing */

Definition at line 68 of file nsFastLoadFile.cpp.

XXX tuneme.

Definition at line 638 of file nsFastLoadFile.cpp.

Definition at line 2081 of file nsFastLoadFile.cpp.

Value:
(X) += (Y); if ((X) & 0x80000000)     \
                                        FOLD_ONES_COMPLEMENT_CARRY(X)

Definition at line 101 of file nsFastLoadFile.cpp.

#define TRACE_MUX (   args)    /* nothing */

Definition at line 94 of file nsFastLoadFile.cpp.


Function Documentation

idmap_GetKey ( PLDHashTable aTable,
PLDHashEntryHdr aHdr 
)

Definition at line 1254 of file nsFastLoadFile.cpp.

{
    nsIDMapEntry* entry = NS_STATIC_CAST(nsIDMapEntry*, aHdr);

    return &entry->mSlowID;
}
idmap_HashKey ( PLDHashTable aTable,
const void aKey 
)

Definition at line 1262 of file nsFastLoadFile.cpp.

{
    const nsID *idp = NS_REINTERPRET_CAST(const nsID*, aKey);

    return idp->m0;
}
idmap_MatchEntry ( PLDHashTable aTable,
const PLDHashEntryHdr aHdr,
const void aKey 
)

Definition at line 1270 of file nsFastLoadFile.cpp.

{
    const nsIDMapEntry* entry = NS_STATIC_CAST(const nsIDMapEntry*, aHdr);
    const nsID *idp = NS_REINTERPRET_CAST(const nsID*, aKey);

    return memcmp(&entry->mSlowID, idp, sizeof(nsID)) == 0;
}

Here is the call graph for this function:

NS_AccumulateFastLoadChecksum ( PRUint32 aChecksum,
const PRUint8 aBuffer,
PRUint32  aLength,
PRBool  aLastBuffer 
)

Compute Fletcher's 16-bit checksum over aLength bytes starting at aBuffer, with the initial accumulators seeded from *aChecksum, and final checksum returned in *aChecksum.

The return value is the number of unchecked bytes, which may be non-zero if aBuffer is misaligned or aLength is odd. Callers should copy any remaining bytes to the front of the next buffer.

If aLastBuffer is false, do not check any bytes remaining due to misaligned aBuffer or odd aLength, instead returning the remaining byte count. But if aLastBuffer is true, treat aBuffer as the last buffer in the file and check every byte, returning 0. Here's a read-loop checksumming sketch:

char buf[BUFSIZE]; PRUint32 len, rem = 0; PRUint32 checksum = 0;

while (NS_SUCCEEDED(rv = Read(buf + rem, sizeof buf - rem, &len)) && len) { len += rem; rem = NS_AccumulateFastLoadChecksum(&checksum, NS_REINTERPRET_CAST(PRUint8*, buf), len, PR_FALSE); if (rem) memcpy(buf, buf + len - rem, rem); }

if (rem) { NS_AccumulateFastLoadChecksum(&checksum, NS_REINTERPRET_CAST(PRUint8*, buf), rem, PR_TRUE); }

After this, if NS_SUCCEEDED(rv), checksum contains a valid FastLoad sum.

Definition at line 107 of file nsFastLoadFile.cpp.

{
    PRUint32 C = *aChecksum;
    PRUint32 A = C & 0xffff;
    PRUint32 B = C >> 16;

    PRUint16 U = 0;
    if (aLength >= 4) {
        PRBool odd = PRWord(aBuffer) & 1;
        switch (PRWord(aBuffer) & 3) {
          case 3:
            U = (aBuffer[0] << 8) | aBuffer[1];
            FLETCHER_ACCUMULATE(A, B, U);
            U = aBuffer[2];
            aBuffer += 3;
            aLength -= 3;
            break;

          case 2:
            U = (aBuffer[0] << 8) | aBuffer[1];
            FLETCHER_ACCUMULATE(A, B, U);
            U = 0;
            aBuffer += 2;
            aLength -= 2;
            break;

          case 1:
            U = *aBuffer++;
            aLength--;
            break;
        }

        PRUint32 W;
        if (odd) {
            while (aLength > 3) {
                W = *NS_REINTERPRET_CAST(const PRUint32*, aBuffer);
                U <<= 8;
#ifdef IS_BIG_ENDIAN
                U |= W >> 24;
                FLETCHER_ACCUMULATE(A, B, U);
                U = PRUint16(W >> 8);
                FLETCHER_ACCUMULATE(A, B, U);
                U = W & 0xff;
#else
                U |= W & 0xff;
                FLETCHER_ACCUMULATE(A, B, U);
                U = PRUint16(W >> 8);
                U = NS_SWAP16(U);
                FLETCHER_ACCUMULATE(A, B, U);
                U = W >> 24;
#endif
                aBuffer += 4;
                aLength -= 4;
            }
            aBuffer--;      // we're odd, we didn't checksum the last byte
            aLength++;
        } else {
            while (aLength > 3) {
                W = *NS_REINTERPRET_CAST(const PRUint32*, aBuffer);
#ifdef IS_BIG_ENDIAN
                U = W >> 16;
                FLETCHER_ACCUMULATE(A, B, U);
                U = PRUint16(W);
                FLETCHER_ACCUMULATE(A, B, U);
#else
                U = NS_SWAP16(W);
                FLETCHER_ACCUMULATE(A, B, U);
                U = W >> 16;
                U = NS_SWAP16(W);
                FLETCHER_ACCUMULATE(A, B, U);
#endif
                aBuffer += 4;
                aLength -= 4;
            }
        }
    }

    if (aLastBuffer) {
        NS_ASSERTION(aLength <= 4, "aLength botch");
        switch (aLength) {
          case 4:
            U = (aBuffer[0] << 8) | aBuffer[1];
            FLETCHER_ACCUMULATE(A, B, U);
            U = (aBuffer[2] << 8) | aBuffer[3];
            FLETCHER_ACCUMULATE(A, B, U);
            break;

          case 3:
            U = (aBuffer[0] << 8) | aBuffer[1];
            FLETCHER_ACCUMULATE(A, B, U);
            U = aBuffer[2];
            FLETCHER_ACCUMULATE(A, B, U);
            break;

          case 2:
            U = (aBuffer[0] << 8) | aBuffer[1];
            FLETCHER_ACCUMULATE(A, B, U);
            break;

          case 1:
            U = aBuffer[0];
            FLETCHER_ACCUMULATE(A, B, U);
            break;
        }

        aLength = 0;
    }

    while (A >> 16)
        FOLD_ONES_COMPLEMENT_CARRY(A);
    while (B >> 16)
        FOLD_ONES_COMPLEMENT_CARRY(B);

    *aChecksum = (B << 16) | A;
    return aLength;
}

Here is the caller graph for this function:

NS_AddFastLoadChecksums ( PRUint32  sum1,
PRUint32  sum2,
PRUint32  sum2ByteCount 
)

Definition at line 228 of file nsFastLoadFile.cpp.

{
    PRUint32 A1 = sum1 & 0xffff;
    PRUint32 B1 = sum1 >> 16;

    PRUint32 A2 = sum2 & 0xffff;
    PRUint32 B2 = sum2 >> 16;

    PRUint32 A = A1 + A2;
    while (A >> 16)
        FOLD_ONES_COMPLEMENT_CARRY(A);

    PRUint32 B = B2;
    for (PRUint32 n = (sum2ByteCount + 1) / 2; n != 0; n--)
        ONES_COMPLEMENT_ACCUMULATE(B, B1);
    while (B >> 16)
        FOLD_ONES_COMPLEMENT_CARRY(B);

    return (B << 16) | A;
}

Definition at line 2334 of file nsFastLoadFile.cpp.

{
    *aResult = mInputStream;
    NS_IF_ADDREF(*aResult);
    return NS_OK;
}

Definition at line 1239 of file nsFastLoadFile.cpp.

                    : public PLDHashEntryHdr {
    NSFastLoadID    mFastID;            // 1 + nsFastLoadFooter::mIDMap index
    nsID            mSlowID;            // key, used by PLDHashTableOps below
};

Definition at line 261 of file nsFastLoadFile.cpp.

{
    nsresult rv;
    PRUint32 bytesRead;

    rv = Read(NS_REINTERPRET_CAST(char*, aHeader), sizeof *aHeader, &bytesRead);
    if (NS_FAILED(rv))
        return rv;

    if (bytesRead != sizeof *aHeader ||
        memcmp(aHeader->mMagic, magic, MFL_FILE_MAGIC_SIZE)) {
        return NS_ERROR_UNEXPECTED;
    }

    aHeader->mChecksum     = NS_SWAP32(aHeader->mChecksum);
    aHeader->mVersion      = NS_SWAP32(aHeader->mVersion);
    aHeader->mFooterOffset = NS_SWAP32(aHeader->mFooterOffset);
    aHeader->mFileSize     = NS_SWAP32(aHeader->mFileSize);

    return NS_OK;
}

Here is the call graph for this function:

Definition at line 1218 of file nsFastLoadFile.cpp.

{
    nsFastLoadFileReader* reader = new nsFastLoadFileReader(aSrcStream);
    if (!reader)
        return NS_ERROR_OUT_OF_MEMORY;

    // Stabilize reader's refcnt.
    nsCOMPtr<nsIObjectInputStream> stream(reader);

    nsresult rv = reader->Open();
    if (NS_FAILED(rv))
        return rv;

    *aResult = stream;
    NS_ADDREF(*aResult);
    return NS_OK;
}

Here is the call graph for this function:

NS_COM nsresult NS_NewFastLoadFileUpdater ( nsIObjectOutputStream **  aResult,
nsIOutputStream aOutputStream,
nsIObjectInputStream aReaderAsStream 
)

Definition at line 2569 of file nsFastLoadFile.cpp.

{
    // Make sure that aReaderAsStream is an nsFastLoadFileReader.
    nsCOMPtr<nsIFastLoadFileReader> reader(do_QueryInterface(aReaderAsStream));
    if (!reader)
        return NS_ERROR_UNEXPECTED;

    nsFastLoadFileUpdater* updater = new nsFastLoadFileUpdater(aOutputStream);
    if (!updater)
        return NS_ERROR_OUT_OF_MEMORY;

    // Stabilize updater's refcnt.
    nsCOMPtr<nsIObjectOutputStream> stream(updater);

    nsresult rv = updater->Open(NS_STATIC_CAST(nsFastLoadFileReader*,
                                               aReaderAsStream));
    if (NS_FAILED(rv))
        return rv;

    *aResult = stream;
    NS_ADDREF(*aResult);
    return NS_OK;
}

Here is the call graph for this function:

Definition at line 2311 of file nsFastLoadFile.cpp.

{
    nsFastLoadFileWriter* writer =
        new nsFastLoadFileWriter(aDestStream, aFileIO);
    if (!writer)
        return NS_ERROR_OUT_OF_MEMORY;

    // Stabilize writer's refcnt.
    nsCOMPtr<nsIObjectOutputStream> stream(writer);

    nsresult rv = writer->Open();
    if (NS_FAILED(rv))
        return rv;

    *aResult = stream;
    NS_ADDREF(*aResult);
    return NS_OK;
}

Here is the call graph for this function:

objmap_ClearEntry ( PLDHashTable aTable,
PLDHashEntryHdr aHdr 
)

Definition at line 366 of file nsFastLoadFile.cpp.

{
    nsObjectMapEntry* entry = NS_STATIC_CAST(nsObjectMapEntry*, aHdr);

    // Ignore tagged object ids stored as object pointer keys (the updater
    // code does this).
    if ((NS_PTR_TO_INT32(entry->mObject) & MFL_OBJECT_DEF_TAG) == 0)
        NS_IF_RELEASE(entry->mObject);
    PL_DHashClearEntryStub(aTable, aHdr);
}

Here is the call graph for this function:

strmap_ClearEntry ( PLDHashTable aTable,
PLDHashEntryHdr aHdr 
)

Definition at line 331 of file nsFastLoadFile.cpp.

{
    nsStringMapEntry* entry = NS_STATIC_CAST(nsStringMapEntry*, aHdr);

    if (entry->mString)
        nsMemory::Free((void*) entry->mString);
    NS_IF_RELEASE(entry->mURI);
    PL_DHashClearEntryStub(aTable, aHdr);
}

Here is the call graph for this function:


Variable Documentation

const char magic[] = MFL_FILE_MAGIC [static]

Definition at line 253 of file nsFastLoadFile.cpp.