NS_IMETHODIMP nsJARInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t *aBytesRead) { NS_ENSURE_ARG_POINTER(aBuffer); NS_ENSURE_ARG_POINTER(aBytesRead); *aBytesRead = 0; nsresult rv = NS_OK; MOZ_WIN_MEM_TRY_BEGIN switch (mMode) { case MODE_NOTINITED: return NS_OK; case MODE_CLOSED: return NS_BASE_STREAM_CLOSED; case MODE_DIRECTORY: return ReadDirectory(aBuffer, aCount, aBytesRead); case MODE_INFLATE: #ifdef MOZ_JAR_BROTLI case MODE_BROTLI: #endif if (mZs.total_out < mOutSize) { rv = ContinueInflate(aBuffer, aCount, aBytesRead); } // be aggressive about releasing the file! // note that sometimes, we will release mFd before we've finished // deflating - this is because zlib buffers the input if (mZs.avail_in == 0) { mFd = nullptr; } break; case MODE_COPY: if (mFd) { uint32_t count = std::min(aCount, mOutSize - uint32_t(mZs.total_out)); if (count) { memcpy(aBuffer, mZs.next_in + mZs.total_out, count); mZs.total_out += count; } *aBytesRead = count; } // be aggressive about releasing the file! // note that sometimes, we will release mFd before we've finished copying. if (mZs.total_out >= mOutSize) { mFd = nullptr; } break; } MOZ_WIN_MEM_TRY_CATCH(rv = NS_ERROR_FAILURE) return rv; }
NS_IMETHODIMP nsJARInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aBytesRead) { NS_ENSURE_ARG_POINTER(aBuffer); NS_ENSURE_ARG_POINTER(aBytesRead); *aBytesRead = 0; nsresult rv = NS_OK; if (mClosed) return rv; if (mDirectory) { rv = ReadDirectory(aBuffer, aCount, aBytesRead); } else { if (mInflate) { rv = ContinueInflate(aBuffer, aCount, aBytesRead); } else { PRInt32 bytesRead = 0; aCount = PR_MIN(aCount, mInSize - mCurPos); if (aCount) { bytesRead = PR_Read(mFd, aBuffer, aCount); if (bytesRead < 0) return NS_ERROR_FILE_CORRUPTED; mCurPos += bytesRead; if (bytesRead != aCount) { // file is truncated or was lying about size, we're done PR_Close(mFd); mFd = nsnull; return NS_ERROR_FILE_CORRUPTED; } } *aBytesRead = bytesRead; } // be aggressive about closing! // note that sometimes, we will close mFd before we've finished // deflating - this is because zlib buffers the input // So, don't free the ReadBuf/InflateStruct yet. if (mCurPos >= mInSize && mFd) { PR_Close(mFd); mFd = nsnull; } } return rv; }