Update libjar module.
This commit is contained in:
parent
14766a6c38
commit
bfa9b4e82e
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "nsJARInputStream.h"
|
#include "nsJARInputStream.h"
|
||||||
#include "zipstruct.h" // defines ZIP compression codes
|
#include "zipstruct.h" // defines ZIP compression codes
|
||||||
|
#include "brotli/decode.h"
|
||||||
#include "nsZipArchive.h"
|
#include "nsZipArchive.h"
|
||||||
|
|
||||||
#include "nsEscape.h"
|
#include "nsEscape.h"
|
||||||
|
@ -51,6 +52,13 @@ nsJARInputStream::InitFile(nsJAR *aJar, nsZipItem *item)
|
||||||
mOutCrc = crc32(0L, Z_NULL, 0);
|
mOutCrc = crc32(0L, Z_NULL, 0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case MOZ_JAR_BROTLI:
|
||||||
|
mBrotliState = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
||||||
|
mMode = MODE_BROTLI;
|
||||||
|
mInCrc = item->CRC32();
|
||||||
|
mOutCrc = crc32(0L, Z_NULL, 0);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return NS_ERROR_NOT_IMPLEMENTED;
|
return NS_ERROR_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
@ -166,6 +174,7 @@ nsJARInputStream::Available(uint64_t *_retval)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MODE_INFLATE:
|
case MODE_INFLATE:
|
||||||
|
case MODE_BROTLI:
|
||||||
case MODE_COPY:
|
case MODE_COPY:
|
||||||
*_retval = mOutSize - mZs.total_out;
|
*_retval = mOutSize - mZs.total_out;
|
||||||
break;
|
break;
|
||||||
|
@ -195,6 +204,7 @@ MOZ_WIN_MEM_TRY_BEGIN
|
||||||
return ReadDirectory(aBuffer, aCount, aBytesRead);
|
return ReadDirectory(aBuffer, aCount, aBytesRead);
|
||||||
|
|
||||||
case MODE_INFLATE:
|
case MODE_INFLATE:
|
||||||
|
case MODE_BROTLI:
|
||||||
if (mZs.total_out < mOutSize) {
|
if (mZs.total_out < mOutSize) {
|
||||||
rv = ContinueInflate(aBuffer, aCount, aBytesRead);
|
rv = ContinueInflate(aBuffer, aCount, aBytesRead);
|
||||||
}
|
}
|
||||||
|
@ -246,6 +256,9 @@ nsJARInputStream::Close()
|
||||||
if (mMode == MODE_INFLATE) {
|
if (mMode == MODE_INFLATE) {
|
||||||
inflateEnd(&mZs);
|
inflateEnd(&mZs);
|
||||||
}
|
}
|
||||||
|
if (mMode == MODE_BROTLI) {
|
||||||
|
BrotliDecoderDestroyInstance(mBrotliState);
|
||||||
|
}
|
||||||
mMode = MODE_CLOSED;
|
mMode = MODE_CLOSED;
|
||||||
mFd = nullptr;
|
mFd = nullptr;
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
|
@ -255,6 +268,8 @@ nsresult
|
||||||
nsJARInputStream::ContinueInflate(char* aBuffer, uint32_t aCount,
|
nsJARInputStream::ContinueInflate(char* aBuffer, uint32_t aCount,
|
||||||
uint32_t* aBytesRead)
|
uint32_t* aBytesRead)
|
||||||
{
|
{
|
||||||
|
bool finished = false;
|
||||||
|
|
||||||
// No need to check the args, ::Read did that, but assert them at least
|
// No need to check the args, ::Read did that, but assert them at least
|
||||||
NS_ASSERTION(aBuffer,"aBuffer parameter must not be null");
|
NS_ASSERTION(aBuffer,"aBuffer parameter must not be null");
|
||||||
NS_ASSERTION(aBytesRead,"aBytesRead parameter must not be null");
|
NS_ASSERTION(aBytesRead,"aBytesRead parameter must not be null");
|
||||||
|
@ -266,11 +281,35 @@ nsJARInputStream::ContinueInflate(char* aBuffer, uint32_t aCount,
|
||||||
mZs.avail_out = std::min(aCount, (mOutSize-oldTotalOut));
|
mZs.avail_out = std::min(aCount, (mOutSize-oldTotalOut));
|
||||||
mZs.next_out = (unsigned char*)aBuffer;
|
mZs.next_out = (unsigned char*)aBuffer;
|
||||||
|
|
||||||
// now inflate
|
if (mMode == MODE_INFLATE) {
|
||||||
int zerr = inflate(&mZs, Z_SYNC_FLUSH);
|
// now inflate
|
||||||
if ((zerr != Z_OK) && (zerr != Z_STREAM_END)) {
|
int zerr = inflate(&mZs, Z_SYNC_FLUSH);
|
||||||
nsZipArchive::sFileCorruptedReason = "nsJARInputStream: error while inflating";
|
if ((zerr != Z_OK) && (zerr != Z_STREAM_END)) {
|
||||||
return NS_ERROR_FILE_CORRUPTED;
|
nsZipArchive::sFileCorruptedReason = "nsJARInputStream: error while inflating";
|
||||||
|
return NS_ERROR_FILE_CORRUPTED;
|
||||||
|
}
|
||||||
|
finished = (zerr == Z_STREAM_END);
|
||||||
|
} else {
|
||||||
|
MOZ_ASSERT(mMode == MODE_BROTLI);
|
||||||
|
/* The brotli library wants size_t, but z_stream only contains
|
||||||
|
* unsigned int for avail_* and unsigned long for total_*.
|
||||||
|
* So use temporary stack values. */
|
||||||
|
size_t avail_in = mZs.avail_in;
|
||||||
|
size_t avail_out = mZs.avail_out;
|
||||||
|
size_t total_out = mZs.total_out;
|
||||||
|
BrotliDecoderResult result = BrotliDecoderDecompressStream(
|
||||||
|
mBrotliState,
|
||||||
|
&avail_in, const_cast<const unsigned char**>(&mZs.next_in),
|
||||||
|
&avail_out, &mZs.next_out, &total_out);
|
||||||
|
/* We don't need to update avail_out, it's not used outside this
|
||||||
|
* function. */
|
||||||
|
mZs.total_out = total_out;
|
||||||
|
mZs.avail_in = avail_in;
|
||||||
|
if (result == BROTLI_DECODER_RESULT_ERROR) {
|
||||||
|
nsZipArchive::sFileCorruptedReason = "nsJARInputStream: brotli decompression error";
|
||||||
|
return NS_ERROR_FILE_CORRUPTED;
|
||||||
|
}
|
||||||
|
finished = (result == BROTLI_DECODER_RESULT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
*aBytesRead = (mZs.total_out - oldTotalOut);
|
*aBytesRead = (mZs.total_out - oldTotalOut);
|
||||||
|
@ -280,8 +319,10 @@ nsJARInputStream::ContinueInflate(char* aBuffer, uint32_t aCount,
|
||||||
|
|
||||||
// be aggressive about ending the inflation
|
// be aggressive about ending the inflation
|
||||||
// for some reason we don't always get Z_STREAM_END
|
// for some reason we don't always get Z_STREAM_END
|
||||||
if (zerr == Z_STREAM_END || mZs.total_out == mOutSize) {
|
if (finished || mZs.total_out == mOutSize) {
|
||||||
inflateEnd(&mZs);
|
if (mMode == MODE_INFLATE) {
|
||||||
|
inflateEnd(&mZs);
|
||||||
|
}
|
||||||
|
|
||||||
// stop returning valid data as soon as we know we have a bad CRC
|
// stop returning valid data as soon as we know we have a bad CRC
|
||||||
if (mOutCrc != mInCrc) {
|
if (mOutCrc != mInCrc) {
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include "nsTArray.h"
|
#include "nsTArray.h"
|
||||||
#include "mozilla/Attributes.h"
|
#include "mozilla/Attributes.h"
|
||||||
|
|
||||||
|
struct BrotliDecoderStateStruct;
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------
|
/*-------------------------------------------------------------------------
|
||||||
* Class nsJARInputStream declaration. This class defines the type of the
|
* Class nsJARInputStream declaration. This class defines the type of the
|
||||||
* object returned by calls to nsJAR::GetInputStream(filename) for the
|
* object returned by calls to nsJAR::GetInputStream(filename) for the
|
||||||
|
@ -20,9 +22,15 @@
|
||||||
class nsJARInputStream final : public nsIInputStream
|
class nsJARInputStream final : public nsIInputStream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
nsJARInputStream() :
|
nsJARInputStream()
|
||||||
mOutSize(0), mInCrc(0), mOutCrc(0), mNameLen(0),
|
: mOutSize(0)
|
||||||
mCurPos(0), mArrPos(0), mMode(MODE_NOTINITED)
|
, mInCrc(0)
|
||||||
|
, mOutCrc(0)
|
||||||
|
, mBrotliState(nullptr)
|
||||||
|
, mNameLen(0)
|
||||||
|
, mCurPos(0)
|
||||||
|
, mArrPos(0)
|
||||||
|
, mMode(MODE_NOTINITED)
|
||||||
{
|
{
|
||||||
memset(&mZs, 0, sizeof(z_stream));
|
memset(&mZs, 0, sizeof(z_stream));
|
||||||
}
|
}
|
||||||
|
@ -45,6 +53,7 @@ class nsJARInputStream final : public nsIInputStream
|
||||||
uint32_t mInCrc; // CRC as provided by the zipentry
|
uint32_t mInCrc; // CRC as provided by the zipentry
|
||||||
uint32_t mOutCrc; // CRC as calculated by me
|
uint32_t mOutCrc; // CRC as calculated by me
|
||||||
z_stream mZs; // zip data structure
|
z_stream mZs; // zip data structure
|
||||||
|
BrotliDecoderStateStruct* mBrotliState; // Brotli decoder state
|
||||||
|
|
||||||
/* For directory reading */
|
/* For directory reading */
|
||||||
RefPtr<nsJAR> mJar; // string reference to zipreader
|
RefPtr<nsJAR> mJar; // string reference to zipreader
|
||||||
|
@ -59,6 +68,7 @@ class nsJARInputStream final : public nsIInputStream
|
||||||
MODE_CLOSED,
|
MODE_CLOSED,
|
||||||
MODE_DIRECTORY,
|
MODE_DIRECTORY,
|
||||||
MODE_INFLATE,
|
MODE_INFLATE,
|
||||||
|
MODE_BROTLI,
|
||||||
MODE_COPY
|
MODE_COPY
|
||||||
} JISMode;
|
} JISMode;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This module implements a simple archive extractor for the PKZIP format.
|
* This module implements a simple archive extractor.
|
||||||
*
|
*
|
||||||
* The underlying nsZipArchive is NOT thread-safe. Do not pass references
|
* The underlying nsZipArchive is NOT thread-safe. Do not pass references
|
||||||
* or pointers to it across thread boundaries.
|
* or pointers to it across thread boundaries.
|
||||||
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
#define READTYPE int32_t
|
#define READTYPE int32_t
|
||||||
#include "zlib.h"
|
#include "zlib.h"
|
||||||
|
#include "brotli/decode.h"
|
||||||
#include "nsISupportsUtils.h"
|
#include "nsISupportsUtils.h"
|
||||||
#include "prio.h"
|
#include "prio.h"
|
||||||
#include "plstr.h"
|
#include "plstr.h"
|
||||||
|
@ -1186,6 +1187,7 @@ nsZipCursor::nsZipCursor(nsZipItem *item, nsZipArchive *aZip, uint8_t* aBuf,
|
||||||
: mItem(item)
|
: mItem(item)
|
||||||
, mBuf(aBuf)
|
, mBuf(aBuf)
|
||||||
, mBufSize(aBufSize)
|
, mBufSize(aBufSize)
|
||||||
|
, mBrotliState(nullptr)
|
||||||
, mCRC(0)
|
, mCRC(0)
|
||||||
, mDoCRC(doCRC)
|
, mDoCRC(doCRC)
|
||||||
{
|
{
|
||||||
|
@ -1200,6 +1202,10 @@ nsZipCursor::nsZipCursor(nsZipItem *item, nsZipArchive *aZip, uint8_t* aBuf,
|
||||||
|
|
||||||
mZs.avail_in = item->Size();
|
mZs.avail_in = item->Size();
|
||||||
mZs.next_in = (Bytef*)aZip->GetData(item);
|
mZs.next_in = (Bytef*)aZip->GetData(item);
|
||||||
|
|
||||||
|
if (mItem->Compression() == MOZ_JAR_BROTLI) {
|
||||||
|
mBrotliState = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
if (doCRC)
|
if (doCRC)
|
||||||
mCRC = crc32(0L, Z_NULL, 0);
|
mCRC = crc32(0L, Z_NULL, 0);
|
||||||
|
@ -1210,6 +1216,9 @@ nsZipCursor::~nsZipCursor()
|
||||||
if (mItem->Compression() == DEFLATED) {
|
if (mItem->Compression() == DEFLATED) {
|
||||||
inflateEnd(&mZs);
|
inflateEnd(&mZs);
|
||||||
}
|
}
|
||||||
|
if (mItem->Compression() == MOZ_JAR_BROTLI) {
|
||||||
|
BrotliDecoderDestroyInstance(mBrotliState);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* nsZipCursor::ReadOrCopy(uint32_t *aBytesRead, bool aCopy) {
|
uint8_t* nsZipCursor::ReadOrCopy(uint32_t *aBytesRead, bool aCopy) {
|
||||||
|
@ -1246,6 +1255,29 @@ MOZ_WIN_MEM_TRY_BEGIN
|
||||||
*aBytesRead = mZs.next_out - buf;
|
*aBytesRead = mZs.next_out - buf;
|
||||||
verifyCRC = (zerr == Z_STREAM_END);
|
verifyCRC = (zerr == Z_STREAM_END);
|
||||||
break;
|
break;
|
||||||
|
case MOZ_JAR_BROTLI: {
|
||||||
|
buf = mBuf;
|
||||||
|
mZs.next_out = buf;
|
||||||
|
/* The brotli library wants size_t, but z_stream only contains
|
||||||
|
* unsigned int for avail_*. So use temporary stack values. */
|
||||||
|
size_t avail_out = mBufSize;
|
||||||
|
size_t avail_in = mZs.avail_in;
|
||||||
|
BrotliDecoderResult result = BrotliDecoderDecompressStream(
|
||||||
|
mBrotliState,
|
||||||
|
&avail_in, const_cast<const unsigned char**>(&mZs.next_in),
|
||||||
|
&avail_out, &mZs.next_out, nullptr);
|
||||||
|
/* We don't need to update avail_out, it's not used outside this
|
||||||
|
* function. */
|
||||||
|
mZs.avail_in = avail_in;
|
||||||
|
|
||||||
|
if (result == BROTLI_DECODER_RESULT_ERROR) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
*aBytesRead = mZs.next_out - buf;
|
||||||
|
verifyCRC = (result == BROTLI_DECODER_RESULT_SUCCESS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -1272,7 +1304,9 @@ nsZipItemPtr_base::nsZipItemPtr_base(nsZipArchive *aZip,
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uint32_t size = 0;
|
uint32_t size = 0;
|
||||||
if (item->Compression() == DEFLATED) {
|
bool compressed = (item->Compression() == DEFLATED) ||
|
||||||
|
(item->Compression() == MOZ_JAR_BROTLI);
|
||||||
|
if (compressed) {
|
||||||
size = item->RealSize();
|
size = item->RealSize();
|
||||||
mAutoBuf = MakeUniqueFallible<uint8_t[]>(size);
|
mAutoBuf = MakeUniqueFallible<uint8_t[]>(size);
|
||||||
if (!mAutoBuf) {
|
if (!mAutoBuf) {
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
class nsZipFind;
|
class nsZipFind;
|
||||||
struct PRFileDesc;
|
struct PRFileDesc;
|
||||||
|
struct BrotliDecoderStateStruct;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This file defines some of the basic structures used by libjar to
|
* This file defines some of the basic structures used by libjar to
|
||||||
|
@ -314,6 +315,7 @@ private:
|
||||||
uint8_t *mBuf;
|
uint8_t *mBuf;
|
||||||
uint32_t mBufSize;
|
uint32_t mBufSize;
|
||||||
z_stream mZs;
|
z_stream mZs;
|
||||||
|
BrotliDecoderStateStruct* mBrotliState;
|
||||||
uint32_t mCRC;
|
uint32_t mCRC;
|
||||||
bool mDoCRC;
|
bool mDoCRC;
|
||||||
};
|
};
|
||||||
|
|
|
@ -102,6 +102,7 @@ typedef struct ZipEnd_
|
||||||
#define TOKENIZED 7
|
#define TOKENIZED 7
|
||||||
#define DEFLATED 8
|
#define DEFLATED 8
|
||||||
#define UNSUPPORTED 0xFF
|
#define UNSUPPORTED 0xFF
|
||||||
|
/* non-standard extension */
|
||||||
|
#define MOZ_JAR_BROTLI 0x81
|
||||||
|
|
||||||
#endif /* _zipstruct_h */
|
#endif /* _zipstruct_h */
|
||||||
|
|
Loading…
Reference in New Issue