1999-03-09 05:13:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
#ifndef nsZipArchive_h_
|
|
|
|
#define nsZipArchive_h_
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
#define ZIP_TABSIZE 256
|
2009-10-04 21:20:45 +04:00
|
|
|
#define ZIP_BUFLEN \
|
|
|
|
(4 * 1024) /* Used as output buffer when deflating items to a file */
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2003-03-14 21:59:51 +03:00
|
|
|
#include "zlib.h"
|
2009-10-17 19:54:54 +04:00
|
|
|
#include "zipstruct.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2013-07-19 06:23:44 +04:00
|
|
|
#include "nsISupportsImpl.h" // For mozilla::ThreadSafeAutoRefCnt
|
2017-04-05 21:16:43 +03:00
|
|
|
#include "mozilla/ArenaAllocator.h"
|
2010-08-18 21:34:07 +04:00
|
|
|
#include "mozilla/FileUtils.h"
|
2011-12-08 14:03:36 +04:00
|
|
|
#include "mozilla/FileLocation.h"
|
2021-03-30 11:48:26 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-11-04 00:36:32 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2003-03-14 21:59:51 +03:00
|
|
|
|
2010-11-11 23:13:48 +03:00
|
|
|
class nsZipFind;
|
2003-03-14 21:59:51 +03:00
|
|
|
struct PRFileDesc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file defines some of the basic structures used by libjar to
|
|
|
|
* read Zip files. It makes use of zlib in order to do the decompression.
|
|
|
|
*
|
2006-05-02 23:33:09 +04:00
|
|
|
* A few notes on the classes/structs:
|
2003-03-14 21:59:51 +03:00
|
|
|
* nsZipArchive represents a single Zip file, and maintains an index
|
|
|
|
* of all the items in the file.
|
|
|
|
* nsZipItem represents a single item (file) in the Zip archive.
|
|
|
|
* nsZipFind represents the metadata involved in doing a search,
|
|
|
|
* and current state of the iteration of found objects.
|
2006-05-02 23:33:09 +04:00
|
|
|
* 'MT''safe' reading from the zipfile is performed through JARInputStream,
|
|
|
|
* which maintains its own file descriptor, allowing for multiple reads
|
|
|
|
* concurrently from the same zip file.
|
2003-03-14 21:59:51 +03:00
|
|
|
*/
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/**
|
2006-05-02 23:33:09 +04:00
|
|
|
* nsZipItem -- a helper struct for nsZipArchive
|
1999-04-28 04:34:30 +04:00
|
|
|
*
|
|
|
|
* each nsZipItem represents one file in the archive and all the
|
|
|
|
* information needed to manipulate it.
|
|
|
|
*/
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipItem final {
|
2009-10-17 19:54:54 +04:00
|
|
|
public:
|
2015-10-20 14:32:47 +03:00
|
|
|
nsZipItem();
|
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
const char* Name() { return ((const char*)central) + ZIPCENTRAL_SIZE; }
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t LocalOffset();
|
|
|
|
uint32_t Size();
|
|
|
|
uint32_t RealSize();
|
|
|
|
uint32_t CRC32();
|
|
|
|
uint16_t Date();
|
|
|
|
uint16_t Time();
|
|
|
|
uint16_t Compression();
|
2009-12-16 02:01:08 +03:00
|
|
|
bool IsDirectory();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t Mode();
|
|
|
|
const uint8_t* GetExtraField(uint16_t aTag, uint16_t* aBlockSize);
|
2009-12-16 02:01:08 +03:00
|
|
|
PRTime LastModTime();
|
1999-04-28 04:34:30 +04:00
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
nsZipItem* next;
|
|
|
|
const ZipCentral* central;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t nameLength;
|
2009-10-17 19:54:54 +04:00
|
|
|
bool isSynthetic;
|
1999-04-28 04:34:30 +04:00
|
|
|
};
|
|
|
|
|
2009-08-13 00:50:12 +04:00
|
|
|
class nsZipHandle;
|
2009-10-04 21:20:45 +04:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/**
|
|
|
|
* nsZipArchive -- a class for reading the PKZIP file format.
|
|
|
|
*
|
|
|
|
*/
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipArchive final {
|
2006-05-02 23:33:09 +04:00
|
|
|
friend class nsZipFind;
|
|
|
|
|
2014-06-25 06:09:14 +04:00
|
|
|
/** destructing the object closes the archive */
|
|
|
|
~nsZipArchive();
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
public:
|
2015-11-16 11:18:45 +03:00
|
|
|
static const char* sFileCorruptedReason;
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/** constructing does not open the archive. See OpenArchive() */
|
1999-03-09 05:13:56 +03:00
|
|
|
nsZipArchive();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenArchive
|
|
|
|
*
|
|
|
|
* It's an error to call this more than once on the same nsZipArchive
|
|
|
|
* object. If we were allowed to use exceptions this would have been
|
|
|
|
* part of the constructor
|
|
|
|
*
|
2020-09-14 20:00:53 +03:00
|
|
|
* @param aZipHandle The nsZipHandle used to access the zip
|
|
|
|
* @param aFd Optional PRFileDesc for Windows readahead optimization
|
1999-03-09 05:13:56 +03:00
|
|
|
* @return status code
|
|
|
|
*/
|
2020-09-14 20:00:53 +03:00
|
|
|
nsresult OpenArchive(nsZipHandle* aZipHandle, PRFileDesc* aFd = nullptr);
|
2010-09-09 07:37:34 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenArchive
|
|
|
|
*
|
|
|
|
* Convenience function that generates nsZipHandle
|
|
|
|
*
|
2014-07-18 06:46:24 +04:00
|
|
|
* @param aFile The file used to access the zip
|
Bug 1627075 - Allow lazily initializing nsZipArchives r=froydnj
Opening our Omnijars can be expensive, and it should be deferrable until after
startup is completed, provided we have a startup cache. In a previous patch in this
stack, we implemented caching of the zip central directory for omnijars, but we
still have to open the file in order to hand the object off to various omnijar
consumers. In a later patch, we will wrap nsZipArchive access in a class which
will allow us to transparently cache nsZipArchive results. These two get us
most of the way to not needing to read from the underlying omnijar files during
startup, but there are still nontrivial pieces, like nsZipFind for instance,
which we don't want to just duplicate inside of a wrapper class, so we would
like to sort out a way in which we can use an nsZipArchive class, but not
actually back it up with the real underlying file until we really need data
from it which we can't find in a cache.
Depends on D77633
Differential Revision: https://phabricator.services.mozilla.com/D78584
2020-07-08 05:44:13 +03:00
|
|
|
* @return status code
|
|
|
|
*/
|
2020-09-14 20:00:53 +03:00
|
|
|
nsresult OpenArchive(nsIFile* aFile);
|
1999-11-12 09:13:13 +03:00
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
/**
|
|
|
|
* Test the integrity of items in this archive by running
|
|
|
|
* a CRC check after extracting each item into a memory
|
|
|
|
* buffer. If an entry name is supplied only the
|
|
|
|
* specified item is tested. Else, if null is supplied
|
|
|
|
* then all the items in the archive are tested.
|
|
|
|
*
|
|
|
|
* @return status code
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult Test(const char* aEntryName);
|
2000-12-27 10:05:55 +03:00
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
/**
|
|
|
|
* Closes an open archive.
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult CloseArchive();
|
1999-11-12 09:13:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetItem
|
2006-05-02 23:33:09 +04:00
|
|
|
* @param aEntryName Name of file in the archive
|
|
|
|
* @return pointer to nsZipItem
|
2001-02-23 03:15:04 +03:00
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsZipItem* GetItem(const char* aEntryName);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
/**
|
2006-05-02 23:33:09 +04:00
|
|
|
* ExtractFile
|
1999-10-26 23:43:26 +04:00
|
|
|
*
|
2006-05-02 23:33:09 +04:00
|
|
|
* @param zipEntry Name of file in archive to extract
|
|
|
|
* @param outFD Filedescriptor to write contents to
|
|
|
|
* @param outname Name of file to write to
|
1999-10-26 23:43:26 +04:00
|
|
|
* @return status code
|
|
|
|
*/
|
2017-12-15 14:58:51 +03:00
|
|
|
nsresult ExtractFile(nsZipItem* zipEntry, nsIFile* outFile,
|
|
|
|
PRFileDesc* outFD);
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
/**
|
|
|
|
* FindInit
|
|
|
|
*
|
|
|
|
* Initializes a search for files in the archive. FindNext() returns
|
2006-05-02 23:33:09 +04:00
|
|
|
* the actual matches. The nsZipFind must be deleted when you're done
|
1999-05-19 07:21:03 +04:00
|
|
|
*
|
|
|
|
* @param aPattern a string or RegExp pattern to search for
|
2013-10-24 00:36:26 +04:00
|
|
|
* (may be nullptr to find all files in archive)
|
2006-03-30 02:10:37 +04:00
|
|
|
* @param aFind a pointer to a pointer to a structure used
|
|
|
|
* in FindNext. In the case of an error this
|
2013-10-24 00:36:26 +04:00
|
|
|
* will be set to nullptr.
|
2006-03-30 02:10:37 +04:00
|
|
|
* @return status code
|
1999-05-19 07:21:03 +04:00
|
|
|
*/
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult FindInit(const char* aPattern, nsZipFind** aFind);
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2009-10-04 21:20:45 +04:00
|
|
|
/*
|
|
|
|
* Gets an undependent handle to the mapped file.
|
|
|
|
*/
|
|
|
|
nsZipHandle* GetFD();
|
|
|
|
|
2014-05-16 09:34:43 +04:00
|
|
|
/**
|
|
|
|
* Gets the data offset.
|
|
|
|
* @param aItem Pointer to nsZipItem
|
|
|
|
* returns 0 on failure.
|
|
|
|
*/
|
|
|
|
uint32_t GetDataOffset(nsZipItem* aItem);
|
|
|
|
|
2009-10-04 21:20:45 +04:00
|
|
|
/**
|
|
|
|
* Get pointer to the data of the item.
|
|
|
|
* @param aItem Pointer to nsZipItem
|
|
|
|
* reutrns null when zip file is corrupt.
|
1999-05-19 07:21:03 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t* GetData(nsZipItem* aItem);
|
2001-09-21 03:33:23 +04:00
|
|
|
|
2012-02-22 22:45:09 +04:00
|
|
|
bool GetComment(nsACString& aComment);
|
|
|
|
|
2011-10-25 04:50:47 +04:00
|
|
|
/**
|
|
|
|
* Gets the amount of memory taken up by the archive's mapping.
|
|
|
|
* @return the size
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t SizeOfMapping();
|
2011-10-25 04:50:47 +04:00
|
|
|
|
2011-12-08 14:03:36 +04:00
|
|
|
/*
|
|
|
|
* Refcounting
|
|
|
|
*/
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_METHOD_(MozExternalRefCountType) AddRef(void);
|
|
|
|
NS_METHOD_(MozExternalRefCountType) Release(void);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
private:
|
|
|
|
//--- private members ---
|
2013-07-19 06:23:44 +04:00
|
|
|
mozilla::ThreadSafeAutoRefCnt mRefCnt; /* ref count */
|
|
|
|
NS_DECL_OWNINGTHREAD
|
2001-12-15 09:31:49 +03:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
nsZipItem* mFiles[ZIP_TABSIZE];
|
2017-04-05 21:16:43 +03:00
|
|
|
mozilla::ArenaAllocator<1024, sizeof(void*)> mArena;
|
1999-11-03 02:46:09 +03:00
|
|
|
|
2012-02-22 22:45:09 +04:00
|
|
|
const char* mCommentPtr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t mCommentLen;
|
2012-02-22 22:45:09 +04:00
|
|
|
|
2006-09-24 19:22:36 +04:00
|
|
|
// Whether we synthesized the directory entries
|
2009-10-04 21:20:45 +04:00
|
|
|
bool mBuiltSynthetics;
|
2006-09-24 19:22:36 +04:00
|
|
|
|
2009-08-13 00:50:12 +04:00
|
|
|
// file handle
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHandle> mFd;
|
2010-08-18 21:34:07 +04:00
|
|
|
|
2013-02-19 14:02:12 +04:00
|
|
|
// file URI, for logging
|
|
|
|
nsCString mURI;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2020-01-22 11:41:39 +03:00
|
|
|
// Is true if we use zipLog to log accesses in jar/zip archives. This helper
|
|
|
|
// variable avoids grabbing zipLog's lock when not necessary.
|
|
|
|
bool mUseZipLog;
|
|
|
|
|
2021-03-30 11:48:26 +03:00
|
|
|
mozilla::Mutex mLock{"nsZipArchive"};
|
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
private:
|
|
|
|
//--- private methods ---
|
2009-10-17 19:54:54 +04:00
|
|
|
nsZipItem* CreateZipItem();
|
2013-03-21 13:28:13 +04:00
|
|
|
nsresult BuildFileList(PRFileDesc* aFd = nullptr);
|
2006-09-24 19:22:36 +04:00
|
|
|
nsresult BuildSynthetics();
|
2011-12-16 23:42:07 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
nsZipArchive& operator=(const nsZipArchive& rhs) = delete;
|
|
|
|
nsZipArchive(const nsZipArchive& rhs) = delete;
|
2009-08-13 00:50:12 +04:00
|
|
|
};
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
/**
|
|
|
|
* nsZipFind
|
|
|
|
*
|
|
|
|
* a helper class for nsZipArchive, representing a search
|
|
|
|
*/
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipFind final {
|
1999-05-19 07:21:03 +04:00
|
|
|
public:
|
2011-09-29 10:19:26 +04:00
|
|
|
nsZipFind(nsZipArchive* aZip, char* aPattern, bool regExp);
|
1999-05-19 07:21:03 +04:00
|
|
|
~nsZipFind();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult FindNext(const char** aResult, uint16_t* aNameLen);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipArchive> mArchive;
|
1999-05-19 07:21:03 +04:00
|
|
|
char* mPattern;
|
|
|
|
nsZipItem* mItem;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t mSlot;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mRegExp;
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
nsZipFind& operator=(const nsZipFind& rhs) = delete;
|
|
|
|
nsZipFind(const nsZipFind& rhs) = delete;
|
1999-05-19 07:21:03 +04:00
|
|
|
};
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2010-06-05 01:10:23 +04:00
|
|
|
/**
|
|
|
|
* nsZipCursor -- a low-level class for reading the individual items in a zip.
|
|
|
|
*/
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipCursor final {
|
2010-06-05 01:10:23 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Initializes the cursor
|
|
|
|
*
|
|
|
|
* @param aItem Item of interest
|
|
|
|
* @param aZip Archive
|
|
|
|
* @param aBuf Buffer used for decompression.
|
|
|
|
* This determines the maximum Read() size in the
|
|
|
|
* compressed case.
|
|
|
|
* @param aBufSize Buffer size
|
|
|
|
* @param doCRC When set to true Read() will check crc
|
|
|
|
*/
|
2013-10-24 00:36:26 +04:00
|
|
|
nsZipCursor(nsZipItem* aItem, nsZipArchive* aZip, uint8_t* aBuf = nullptr,
|
|
|
|
uint32_t aBufSize = 0, bool doCRC = false);
|
2010-06-05 01:10:23 +04:00
|
|
|
|
|
|
|
~nsZipCursor();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2013-10-24 00:36:26 +04:00
|
|
|
* @return data read or nullptr if item is corrupted.
|
2010-06-05 01:10:23 +04:00
|
|
|
*/
|
2011-12-08 14:03:36 +04:00
|
|
|
uint8_t* Read(uint32_t* aBytesRead) { return ReadOrCopy(aBytesRead, false); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a copy. It always uses aBuf(passed in constructor).
|
|
|
|
*
|
|
|
|
* @param aBytesRead Outparam for number of bytes read.
|
2013-10-24 00:36:26 +04:00
|
|
|
* @return data read or nullptr if item is corrupted.
|
2011-12-08 14:03:36 +04:00
|
|
|
*/
|
|
|
|
uint8_t* Copy(uint32_t* aBytesRead) { return ReadOrCopy(aBytesRead, true); }
|
2010-06-05 01:10:23 +04:00
|
|
|
|
|
|
|
private:
|
2011-12-08 14:03:36 +04:00
|
|
|
/* Actual implementation for both Read and Copy above */
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* ReadOrCopy(uint32_t* aBytesRead, bool aCopy);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2010-06-05 01:10:23 +04:00
|
|
|
nsZipItem* mItem;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* mBuf;
|
|
|
|
uint32_t mBufSize;
|
2010-06-05 01:10:23 +04:00
|
|
|
z_stream mZs;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mCRC;
|
2010-06-05 01:10:23 +04:00
|
|
|
bool mDoCRC;
|
|
|
|
};
|
|
|
|
|
2010-09-09 07:37:34 +04:00
|
|
|
/**
|
2010-06-05 01:10:23 +04:00
|
|
|
* nsZipItemPtr - a RAII convenience class for reading the individual items in a
|
|
|
|
* zip. It reads whole files and does zero-copy IO for stored files. A buffer is
|
|
|
|
* allocated for decompression. Do not use when the file may be very large.
|
|
|
|
*/
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipItemPtr_base {
|
2010-06-05 01:10:23 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Initializes the reader
|
|
|
|
*
|
|
|
|
* @param aZip Archive
|
|
|
|
* @param aEntryName Archive membername
|
|
|
|
* @param doCRC When set to true Read() will check crc
|
|
|
|
*/
|
|
|
|
nsZipItemPtr_base(nsZipArchive* aZip, const char* aEntryName, bool doCRC);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Length() const { return mReadlen; }
|
2010-06-05 01:10:23 +04:00
|
|
|
|
|
|
|
protected:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsZipHandle> mZipHandle;
|
2015-11-04 00:36:32 +03:00
|
|
|
mozilla::UniquePtr<uint8_t[]> mAutoBuf;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* mReturnBuf;
|
|
|
|
uint32_t mReadlen;
|
2010-06-05 01:10:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipItemPtr final : public nsZipItemPtr_base {
|
2016-02-18 20:07:03 +03:00
|
|
|
static_assert(sizeof(T) == sizeof(char),
|
|
|
|
"This class cannot be used with larger T without re-examining"
|
|
|
|
" a number of assumptions.");
|
|
|
|
|
2010-06-05 01:10:23 +04:00
|
|
|
public:
|
|
|
|
nsZipItemPtr(nsZipArchive* aZip, const char* aEntryName, bool doCRC = false)
|
|
|
|
: nsZipItemPtr_base(aZip, aEntryName, doCRC) {}
|
|
|
|
/**
|
2013-10-24 00:36:26 +04:00
|
|
|
* @return buffer containing the whole zip member or nullptr on error.
|
2010-06-05 01:10:23 +04:00
|
|
|
* The returned buffer is owned by nsZipItemReader.
|
|
|
|
*/
|
|
|
|
const T* Buffer() const { return (const T*)mReturnBuf; }
|
|
|
|
|
|
|
|
operator const T*() const { return Buffer(); }
|
2010-11-11 23:13:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Relinquish ownership of zip member if compressed.
|
|
|
|
* Copy member into a new buffer if uncompressed.
|
|
|
|
* @return a buffer with whole zip member. It is caller's responsibility to
|
|
|
|
* free() it.
|
|
|
|
*/
|
2016-02-18 20:26:28 +03:00
|
|
|
mozilla::UniquePtr<T[]> Forget() {
|
2010-11-11 23:13:57 +03:00
|
|
|
if (!mReturnBuf) return nullptr;
|
|
|
|
// In uncompressed mmap case, give up buffer
|
|
|
|
if (mAutoBuf.get() == mReturnBuf) {
|
2013-10-24 00:36:26 +04:00
|
|
|
mReturnBuf = nullptr;
|
2016-02-18 20:26:28 +03:00
|
|
|
return mozilla::UniquePtr<T[]>(reinterpret_cast<T*>(mAutoBuf.release()));
|
2010-11-11 23:13:57 +03:00
|
|
|
}
|
2016-02-18 20:26:28 +03:00
|
|
|
auto ret = mozilla::MakeUnique<T[]>(Length());
|
|
|
|
memcpy(ret.get(), mReturnBuf, Length());
|
2013-10-24 00:36:26 +04:00
|
|
|
mReturnBuf = nullptr;
|
2010-11-11 23:13:57 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2010-06-05 01:10:23 +04:00
|
|
|
};
|
|
|
|
|
2015-10-20 14:32:47 +03:00
|
|
|
class nsZipHandle final {
|
2010-09-09 07:37:34 +04:00
|
|
|
friend class nsZipArchive;
|
2019-10-10 17:07:06 +03:00
|
|
|
friend class nsZipFind;
|
2011-12-08 14:03:36 +04:00
|
|
|
friend class mozilla::FileLocation;
|
2019-10-10 17:07:06 +03:00
|
|
|
friend class nsJARInputStream;
|
|
|
|
#if defined(XP_UNIX) && !defined(XP_DARWIN)
|
|
|
|
friend class MmapAccessScope;
|
|
|
|
#endif
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-09-09 07:37:34 +04:00
|
|
|
public:
|
2015-07-30 11:05:57 +03:00
|
|
|
static nsresult Init(nsIFile* file, nsZipHandle** ret,
|
2013-03-21 13:28:13 +04:00
|
|
|
PRFileDesc** aFd = nullptr);
|
2010-09-09 07:37:34 +04:00
|
|
|
static nsresult Init(nsZipArchive* zip, const char* entry, nsZipHandle** ret);
|
2015-03-11 03:00:01 +03:00
|
|
|
static nsresult Init(const uint8_t* aData, uint32_t aLen, nsZipHandle** aRet);
|
2010-09-09 07:37:34 +04:00
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_METHOD_(MozExternalRefCountType) AddRef(void);
|
|
|
|
NS_METHOD_(MozExternalRefCountType) Release(void);
|
2010-09-09 07:37:34 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t SizeOfMapping();
|
2011-10-25 04:50:47 +04:00
|
|
|
|
2014-07-18 06:46:24 +04:00
|
|
|
nsresult GetNSPRFileDesc(PRFileDesc** aNSPRFileDesc);
|
|
|
|
|
2010-09-09 07:37:34 +04:00
|
|
|
protected:
|
2016-04-07 04:28:24 +03:00
|
|
|
const uint8_t* mFileData; /* pointer to zip data */
|
|
|
|
uint32_t mLen; /* length of zip data */
|
2011-12-08 14:03:36 +04:00
|
|
|
mozilla::FileLocation mFile; /* source file if any, for logging */
|
2010-09-09 07:37:34 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsZipHandle();
|
|
|
|
~nsZipHandle();
|
|
|
|
|
2016-05-04 22:57:21 +03:00
|
|
|
nsresult findDataStart();
|
2016-04-07 04:28:24 +03:00
|
|
|
|
2010-09-09 07:37:34 +04:00
|
|
|
PRFileMap* mMap; /* nspr datastructure for mmap */
|
2014-07-18 06:46:24 +04:00
|
|
|
mozilla::AutoFDClose mNSPRFileDesc;
|
2020-03-02 21:52:07 +03:00
|
|
|
mozilla::UniquePtr<nsZipItemPtr<uint8_t> > mBuf;
|
2013-07-19 06:23:44 +04:00
|
|
|
mozilla::ThreadSafeAutoRefCnt mRefCnt; /* ref count */
|
|
|
|
NS_DECL_OWNINGTHREAD
|
2016-04-07 04:28:24 +03:00
|
|
|
|
|
|
|
const uint8_t* mFileStart; /* pointer to mmaped file */
|
|
|
|
uint32_t mTotalLen; /* total length of the mmaped file */
|
|
|
|
|
|
|
|
/* Magic number for CRX type expressed in Big Endian since it is a literal */
|
|
|
|
static const uint32_t kCRXMagic = 0x34327243;
|
2010-09-09 07:37:34 +04:00
|
|
|
};
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult gZlibInit(z_stream* zs);
|
|
|
|
|
1999-06-24 07:13:10 +04:00
|
|
|
#endif /* nsZipArchive_h_ */
|