Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Private Member Functions
nsZipReadState Class Reference

nsZipReadState More...

#include <nsZipArchive.h>

Collaboration diagram for nsZipReadState:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsZipReadState ()
 ~nsZipReadState ()
void Init (nsZipItem *aZipItem, PRFileDesc *aFd)
PRInt32 Read (char *buf, PRUint32 count, PRUint32 *actual)
 Read.
PRUint32 Available ()
 Available.

Public Attributes

PRFileDescmFd
nsZipItemmItem
PRUint32 mCurPos
unsigned char mReadBuf [ZIP_BUFLEN]
z_stream mZs
PRUint32 mCrc

Private Member Functions

PRInt32 ContinueInflate (char *aBuf, PRUint32 aCount, PRUint32 *aBytesRead)
PRInt32 ContinueCopy (char *aBuf, PRUint32 aCount, PRUint32 *aBytesRead)
nsZipReadStateoperator= (const nsZipReadState &rhs)
 nsZipReadState (const nsZipFind &rhs)

Detailed Description

nsZipReadState

a helper class for nsZipArchive, representing a read in progress

Definition at line 321 of file nsZipArchive.h.


Constructor & Destructor Documentation

Definition at line 325 of file nsZipArchive.h.

                   :
#ifndef STANDALONE
    mFd(0),
#endif
    mCurPos(0)
  { MOZ_COUNT_CTOR(nsZipReadState); }

Definition at line 331 of file nsZipArchive.h.

  {
#ifndef STANDALONE
    if (mFd)
      PR_Close(mFd);
#endif
    MOZ_COUNT_DTOR(nsZipReadState);
  }

Member Function Documentation

Available.

Returns the number of bytes left to be read from the item specified to ReadInit. ReadInit must be called first.

Parameters:
aReadthe structure returned by ReadInit
Returns:
the number of bytes still to be read

Definition at line 649 of file nsZipArchive.cpp.

{
  if (mItem->compression == DEFLATED)
    return (mItem->realsize - mZs.total_out);

  return mItem->size - mCurPos;
}

Here is the caller graph for this function:

PRInt32 nsZipReadState::ContinueCopy ( char *  aBuf,
PRUint32  aCount,
PRUint32 aBytesRead 
) [private]

Definition at line 1381 of file nsZipArchive.cpp.

{
  // we still use the fields of mZs, we just use memcpy rather than inflate

  if (mCurPos + aCount > mItem->size)
    aCount = (mItem->size - mCurPos);

  PR_ASSERT(mFd);
  PRInt32 bytesRead = PR_Read(mFd, aBuf, aCount);
  if (bytesRead < 0)
    return ZIP_ERR_DISK;

  mCurPos += bytesRead;
  if (bytesRead != aCount)
    // either file was truncated or archive lied about size
    return ZIP_ERR_CORRUPT;

  *aBytesRead = bytesRead;

  return ZIP_OK;
}

Here is the caller graph for this function:

PRInt32 nsZipReadState::ContinueInflate ( char *  aBuf,
PRUint32  aCount,
PRUint32 aBytesRead 
) [private]

Definition at line 1309 of file nsZipArchive.cpp.

{

  // just some stuff that will be helpful later
  const PRUint32 inSize = mItem->size;
  const PRUint32 outSize = mItem->realsize;

  int zerr = Z_OK;
  //-- inflate loop

  const PRUint32 oldTotalOut = mZs.total_out;

  mZs.next_out = (unsigned char*)aBuffer;
  mZs.avail_out = ((mZs.total_out + aCount) < outSize) ?
    aCount : (outSize - mZs.total_out);

  // make sure we aren't reading too much
  PR_ASSERT(mZs.avail_out <= aCount);
  
  *aBytesRead = 0;
  while (mZs.avail_out != 0 && zerr == Z_OK) {
    
    if (mZs.avail_in == 0 && mCurPos < inSize) {
      // time to fill the buffer!
      PRUint32 bytesToRead = ((mCurPos + ZIP_BUFLEN) < inSize) ?
        ZIP_BUFLEN : inSize - mCurPos;

      PR_ASSERT(mFd);
      PRInt32 bytesRead = PR_Read(mFd, mReadBuf, bytesToRead);
      if (bytesRead < 0) {
        zerr = Z_ERRNO;
        break;
      }

      mCrc = crc32(mCrc, mReadBuf, bytesRead);
      mCurPos += bytesRead;

      // now reset the state
      mZs.next_in = mReadBuf;
      mZs.avail_in = bytesRead;
    }

#if 0
    // stop returning valid data as soon as we know we have a bad CRC
    if (mCurPos >= inSize &&
        mCrc != mItem->crc32) {
      // asserting because while this rarely happens, you definitely
      // want to catch it in debug builds!
      PR_ASSERT(0);
      return ZIP_ERR_CORRUPT;
    }
#endif
    
    // now inflate
    zerr = inflate(&mZs, Z_PARTIAL_FLUSH);
  }

  if ((zerr != Z_OK) && (zerr != Z_STREAM_END))
    return ZIP_ERR_CORRUPT;
  
  *aBytesRead = (mZs.total_out - oldTotalOut);

  // be aggressive about closing the stream
  // for some reason we don't always get Z_STREAM_END
  if (zerr == Z_STREAM_END || mZs.total_out == mItem->realsize) {
    inflateEnd(&mZs);
  }

  return ZIP_OK;
}

Here is the caller graph for this function:

void nsZipReadState::Init ( nsZipItem aZipItem,
PRFileDesc aFd 
)

Definition at line 437 of file nsZipArchive.cpp.

{
    PR_ASSERT(aFd);
    mItem = aZipItem;
    mCurPos = 0;
#ifndef STANDALONE
    // take ownership of the file descriptor
    mFd = aFd;
#endif

    if (mItem->compression != STORED) {
      memset(&mZs, 0, sizeof(mZs));

#ifndef STANDALONE
      //-- ensure we have our zlib allocator for better performance
      if (!gZlibAllocator) {
        gZlibAllocator = new nsRecyclingAllocator(NBUCKETS, NS_DEFAULT_RECYCLE_TIMEOUT, "libjar");
      }

      mZs.zalloc = zlibAlloc;
      mZs.zfree = zlibFree;
      mZs.opaque = gZlibAllocator;
#endif
      int zerr = inflateInit2(&mZs, -MAX_WBITS);
      PR_ASSERT(zerr == Z_OK);
    }
    mCrc = crc32(0L, Z_NULL, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsZipReadState& nsZipReadState::operator= ( const nsZipReadState rhs) [private]
PRInt32 nsZipReadState::Read ( char *  buf,
PRUint32  count,
PRUint32 actual 
)

Read.

Read from the item specified to ReadInit. ReadInit must be called first.

Parameters:
aReadthe structure returned by ReadInit
bufbuffer to write data into.
countnumber of bytes to read
actual(out) number of bytes read
Returns:
status code

Definition at line 1273 of file nsZipArchive.cpp.

{
  if (!aBuffer)
    return ZIP_ERR_GENERAL;

  PRInt32 result;

  if (!Available()) {
    *aBytesRead = 0;
    return ZIP_OK;
  }

  switch (mItem->compression) {
  case DEFLATED:
    result = ContinueInflate(aBuffer, aCount, aBytesRead);
    break;
  case STORED:
    result = ContinueCopy(aBuffer, aCount, aBytesRead);
    break;
  default:
    result = ZIP_ERR_UNSUPPORTED;
  }

  // be agressive about closing!
  // note that sometimes, we will close mFd before we've finished
  // deflating - this is because zlib buffers the input
  if (mCurPos >= mItem->size && mFd) {
    PR_Close(mFd);
    mFd = NULL;
  }

  return result;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 377 of file nsZipArchive.h.

Definition at line 374 of file nsZipArchive.h.

Definition at line 370 of file nsZipArchive.h.

Definition at line 373 of file nsZipArchive.h.

Definition at line 375 of file nsZipArchive.h.

Definition at line 376 of file nsZipArchive.h.


The documentation for this class was generated from the following files: