From 19422a5329c036e67841764ef9245b061dd76fa3 Mon Sep 17 00:00:00 2001 From: Shian-Yow Wu Date: Fri, 16 May 2014 13:34:43 +0800 Subject: [PATCH] Bug 945152 - Part 3-1: New libjar APIs. r=aklotz --- modules/libjar/nsIJARChannel.idl | 11 +++++++-- modules/libjar/nsJARChannel.cpp | 7 ++++++ modules/libjar/nsZipArchive.cpp | 25 ++++++++++++++++----- modules/libjar/nsZipArchive.h | 7 ++++++ netwerk/protocol/app/AppProtocolHandler.cpp | 5 +++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/modules/libjar/nsIJARChannel.idl b/modules/libjar/nsIJARChannel.idl index 6da0f0b3b0c..e0fcc833d3e 100644 --- a/modules/libjar/nsIJARChannel.idl +++ b/modules/libjar/nsIJARChannel.idl @@ -5,8 +5,10 @@ #include "nsIChannel.idl" -[scriptable, builtinclass, uuid(6e6cc56d-51eb-4299-a795-dcfd1229ab3d)] -interface nsIJARChannel : nsIChannel +interface nsIFile; + +[scriptable, builtinclass, uuid(063e9698-ec67-4fe2-aa19-d21505beaa61)] +interface nsIJARChannel : nsIChannel { /** * Returns TRUE if the JAR file is not safe (if the content type reported @@ -20,4 +22,9 @@ interface nsIJARChannel : nsIChannel * Forces the uri to be a app:// uri. */ void setAppURI(in nsIURI uri); + + /** + * Returns the JAR file. + */ + readonly attribute nsIFile jarFile; }; diff --git a/modules/libjar/nsJARChannel.cpp b/modules/libjar/nsJARChannel.cpp index d1909737816..4c38f3bc3e4 100644 --- a/modules/libjar/nsJARChannel.cpp +++ b/modules/libjar/nsJARChannel.cpp @@ -825,6 +825,13 @@ nsJARChannel::SetAppURI(nsIURI *aURI) { return NS_OK; } +NS_IMETHODIMP +nsJARChannel::GetJarFile(nsIFile **aFile) +{ + NS_IF_ADDREF(*aFile = mJarFile); + return NS_OK; +} + //----------------------------------------------------------------------------- // nsIDownloadObserver //----------------------------------------------------------------------------- diff --git a/modules/libjar/nsZipArchive.cpp b/modules/libjar/nsZipArchive.cpp index cb3e5d01530..28fcfb3cb2d 100644 --- a/modules/libjar/nsZipArchive.cpp +++ b/modules/libjar/nsZipArchive.cpp @@ -763,9 +763,9 @@ nsZipHandle* nsZipArchive::GetFD() } //--------------------------------------------- -// nsZipArchive::GetData +// nsZipArchive::GetDataOffset //--------------------------------------------- -const uint8_t* nsZipArchive::GetData(nsZipItem* aItem) +uint32_t nsZipArchive::GetDataOffset(nsZipItem* aItem) { PR_ASSERT (aItem); MOZ_WIN_MEM_TRY_BEGIN @@ -775,12 +775,12 @@ MOZ_WIN_MEM_TRY_BEGIN const uint8_t* data = mFd->mFileData; uint32_t offset = aItem->LocalOffset(); if (offset + ZIPLOCAL_SIZE > len) - return nullptr; + return 0; // -- check signature before using the structure, in case the zip file is corrupt ZipLocal* Local = (ZipLocal*)(data + offset); if ((xtolong(Local->signature) != LOCALSIG)) - return nullptr; + return 0; //-- NOTE: extralen is different in central header and local header //-- for archives created using the Unix "zip" utility. To set @@ -789,11 +789,24 @@ MOZ_WIN_MEM_TRY_BEGIN xtoint(Local->filename_len) + xtoint(Local->extrafield_len); + return offset; +MOZ_WIN_MEM_TRY_CATCH(return 0) +} + +//--------------------------------------------- +// nsZipArchive::GetData +//--------------------------------------------- +const uint8_t* nsZipArchive::GetData(nsZipItem* aItem) +{ + PR_ASSERT (aItem); +MOZ_WIN_MEM_TRY_BEGIN + uint32_t offset = GetDataOffset(aItem); + // -- check if there is enough source data in the file - if (offset + aItem->Size() > len) + if (!offset || offset + aItem->Size() > mFd->mLen) return nullptr; - return data + offset; + return mFd->mFileData + offset; MOZ_WIN_MEM_TRY_CATCH(return nullptr) } diff --git a/modules/libjar/nsZipArchive.h b/modules/libjar/nsZipArchive.h index bb2de4373fc..8e110169652 100644 --- a/modules/libjar/nsZipArchive.h +++ b/modules/libjar/nsZipArchive.h @@ -178,6 +178,13 @@ public: */ nsZipHandle* GetFD(); + /** + * Gets the data offset. + * @param aItem Pointer to nsZipItem + * returns 0 on failure. + */ + uint32_t GetDataOffset(nsZipItem* aItem); + /** * Get pointer to the data of the item. * @param aItem Pointer to nsZipItem diff --git a/netwerk/protocol/app/AppProtocolHandler.cpp b/netwerk/protocol/app/AppProtocolHandler.cpp index 2fe9344f013..c88e533ab30 100644 --- a/netwerk/protocol/app/AppProtocolHandler.cpp +++ b/netwerk/protocol/app/AppProtocolHandler.cpp @@ -118,6 +118,11 @@ NS_IMETHODIMP DummyChannel::SetAppURI(nsIURI *aURI) return NS_ERROR_NOT_IMPLEMENTED; } +NS_IMETHODIMP DummyChannel::GetJarFile(nsIFile* *aFile) +{ + return NS_ERROR_NOT_IMPLEMENTED; +} + NS_IMETHODIMP DummyChannel::Run() { nsresult rv = mListener->OnStartRequest(this, mListenerContext);