Bug 695843 part 4 - Allow nsZipCursor to always use the given buffer. r=tglek
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 08 Dec 2011 11:03:36 +0100
changeset 82246 692b6b2207b47648b1b4690c2c7134e3c7590632
parent 82245 3032fd2bd96a5afe71c8e73b627c6bc97465bc9d
child 82247 616b0239a9fbb3297610f433fff79c9351b97aeb
push id21587
push userbmo@edmorley.co.uk
push dateThu, 08 Dec 2011 15:13:43 +0000
treeherdermozilla-central@98db2311a44c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstglek
bugs695843
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 695843 part 4 - Allow nsZipCursor to always use the given buffer. r=tglek
modules/libjar/nsZipArchive.cpp
modules/libjar/nsZipArchive.h
--- a/modules/libjar/nsZipArchive.cpp
+++ b/modules/libjar/nsZipArchive.cpp
@@ -1033,30 +1033,37 @@ nsZipCursor::nsZipCursor(nsZipItem *item
 
 nsZipCursor::~nsZipCursor()
 {
   if (mItem->Compression() == DEFLATED) {
     inflateEnd(&mZs);
   }
 }
 
-PRUint8* nsZipCursor::Read(PRUint32 *aBytesRead) {
+PRUint8* nsZipCursor::ReadOrCopy(PRUint32 *aBytesRead, bool aCopy) {
   int zerr;
   PRUint8 *buf = nsnull;
   bool verifyCRC = true;
 
   if (!mZs.next_in)
     return nsnull;
 MOZ_WIN_MEM_TRY_BEGIN
   switch (mItem->Compression()) {
   case STORED:
-    *aBytesRead = mZs.avail_in;
-    buf = mZs.next_in;
-    mZs.next_in += mZs.avail_in;
-    mZs.avail_in = 0;
+    if (!aCopy) {
+      *aBytesRead = mZs.avail_in;
+      buf = mZs.next_in;
+      mZs.next_in += mZs.avail_in;
+      mZs.avail_in = 0;
+    } else {
+      *aBytesRead = mZs.avail_in > mBufSize ? mBufSize : mZs.avail_in;
+      memcpy(mBuf, mZs.next_in, *aBytesRead);
+      mZs.avail_in -= *aBytesRead;
+      mZs.next_in += *aBytesRead;
+    }
     break;
   case DEFLATED:
     buf = mBuf;
     mZs.next_out = buf;
     mZs.avail_out = mBufSize;
     
     zerr = inflate(&mZs, Z_PARTIAL_FLUSH);
     if (zerr != Z_OK && zerr != Z_STREAM_END)
--- a/modules/libjar/nsZipArchive.h
+++ b/modules/libjar/nsZipArchive.h
@@ -305,19 +305,34 @@ public:
 
   /**
    * Performs reads. In the compressed case it uses aBuf(passed in constructor), for stored files
    * it returns a zero-copy buffer.
    *
    * @param   aBytesRead  Outparam for number of bytes read.
    * @return  data read or NULL if item is corrupted.
    */
-  PRUint8* Read(PRUint32 *aBytesRead);
+  PRUint8* Read(PRUint32 *aBytesRead) {
+    return ReadOrCopy(aBytesRead, false);
+  }
+
+  /**
+   * Performs a copy. It always uses aBuf(passed in constructor).
+   *
+   * @param   aBytesRead  Outparam for number of bytes read.
+   * @return  data read or NULL if item is corrupted.
+   */
+  PRUint8* Copy(PRUint32 *aBytesRead) {
+    return ReadOrCopy(aBytesRead, true);
+  }
 
 private:
+  /* Actual implementation for both Read and Copy above */
+  PRUint8* ReadOrCopy(PRUint32 *aBytesRead, bool aCopy);
+
   nsZipItem *mItem; 
   PRUint8  *mBuf; 
   PRUint32  mBufSize; 
   z_stream  mZs;
   PRUint32 mCRC;
   bool mDoCRC;
 };