2012-02-22 11:12:15 +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/. */
|
|
|
|
|
|
|
|
#include <algorithm>
|
2013-03-06 10:30:07 +04:00
|
|
|
#include <map>
|
2012-02-22 11:12:15 +04:00
|
|
|
#include <sys/stat.h>
|
2013-04-11 11:37:44 +04:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2012-02-22 11:12:15 +04:00
|
|
|
#include <cstring>
|
2013-03-06 10:29:51 +04:00
|
|
|
#include <cstdlib>
|
2012-02-22 11:12:15 +04:00
|
|
|
#include <zlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "mozilla/Assertions.h"
|
2012-07-30 22:17:53 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2013-12-30 23:34:54 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2012-02-22 11:12:15 +04:00
|
|
|
#include "SeekableZStream.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
|
2013-07-10 09:12:35 +04:00
|
|
|
Logging Logging::Singleton;
|
|
|
|
|
2013-03-13 18:28:57 +04:00
|
|
|
const char *filterName[] = {
|
|
|
|
"none",
|
|
|
|
"thumb",
|
|
|
|
"arm",
|
|
|
|
"x86",
|
|
|
|
"auto"
|
|
|
|
};
|
|
|
|
|
2013-03-06 20:01:56 +04:00
|
|
|
/* Maximum supported size for chunkSize */
|
|
|
|
static const size_t maxChunkSize =
|
2013-11-11 23:15:46 +04:00
|
|
|
1 << (8 * std::min(sizeof(((SeekableZStreamHeader *)nullptr)->chunkSize),
|
|
|
|
sizeof(((SeekableZStreamHeader *)nullptr)->lastChunkSize)) - 1);
|
2013-03-06 20:01:56 +04:00
|
|
|
|
2013-03-06 10:29:33 +04:00
|
|
|
class Buffer: public MappedPtr
|
|
|
|
{
|
|
|
|
public:
|
2013-06-27 04:35:49 +04:00
|
|
|
virtual ~Buffer() { }
|
|
|
|
|
2013-03-06 10:29:33 +04:00
|
|
|
virtual bool Resize(size_t size)
|
|
|
|
{
|
2013-11-11 23:15:46 +04:00
|
|
|
MemoryRange buf = mmap(nullptr, size, PROT_READ | PROT_WRITE,
|
2013-06-27 04:35:49 +04:00
|
|
|
MAP_PRIVATE | MAP_ANON, -1, 0);
|
2013-03-06 10:29:33 +04:00
|
|
|
if (buf == MAP_FAILED)
|
|
|
|
return false;
|
|
|
|
if (*this != MAP_FAILED)
|
|
|
|
memcpy(buf, *this, std::min(size, GetLength()));
|
2013-06-27 04:35:49 +04:00
|
|
|
Assign(buf);
|
2013-03-06 10:29:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-13 18:28:57 +04:00
|
|
|
|
|
|
|
bool Fill(Buffer &other)
|
|
|
|
{
|
|
|
|
size_t size = other.GetLength();
|
|
|
|
if (!size || !Resize(size))
|
|
|
|
return false;
|
|
|
|
memcpy(static_cast<void *>(*this), static_cast<void *>(other), size);
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-06 10:29:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileBuffer: public Buffer
|
2013-03-06 10:29:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool Init(const char *name, bool writable_ = false)
|
|
|
|
{
|
|
|
|
fd = open(name, writable_ ? O_RDWR | O_CREAT | O_TRUNC : O_RDONLY, 0666);
|
|
|
|
if (fd == -1)
|
|
|
|
return false;
|
|
|
|
writable = writable_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:29:33 +04:00
|
|
|
virtual bool Resize(size_t size)
|
2013-03-06 10:29:13 +04:00
|
|
|
{
|
|
|
|
if (writable) {
|
|
|
|
if (ftruncate(fd, size) == -1)
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-11 23:15:46 +04:00
|
|
|
Assign(MemoryRange::mmap(nullptr, size,
|
|
|
|
PROT_READ | (writable ? PROT_WRITE : 0),
|
2013-06-27 04:35:49 +04:00
|
|
|
writable ? MAP_SHARED : MAP_PRIVATE, fd, 0));
|
2013-03-06 10:29:13 +04:00
|
|
|
return this != MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getFd()
|
|
|
|
{
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoCloseFD fd;
|
|
|
|
bool writable;
|
|
|
|
};
|
|
|
|
|
2013-03-13 18:28:57 +04:00
|
|
|
class FilteredBuffer: public Buffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Filter(Buffer &other, SeekableZStream::FilterId filter, size_t chunkSize)
|
|
|
|
{
|
|
|
|
SeekableZStream::ZStreamFilter filterCB =
|
|
|
|
SeekableZStream::GetFilter(filter);
|
|
|
|
MOZ_ASSERT(filterCB);
|
|
|
|
Fill(other);
|
|
|
|
size_t size = other.GetLength();
|
|
|
|
Bytef *data = reinterpret_cast<Bytef *>(static_cast<void *>(*this));
|
|
|
|
size_t avail = 0;
|
|
|
|
/* Filter needs to be applied in chunks. */
|
|
|
|
while (size) {
|
|
|
|
avail = std::min(size, chunkSize);
|
|
|
|
filterCB(data - static_cast<unsigned char *>(static_cast<void *>(*this)),
|
|
|
|
SeekableZStream::FILTER, data, avail);
|
|
|
|
size -= avail;
|
|
|
|
data += avail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-06 10:30:07 +04:00
|
|
|
template <typename T>
|
|
|
|
class Dictionary: public Buffer
|
|
|
|
{
|
|
|
|
typedef T piece;
|
|
|
|
typedef std::pair<piece, int> stat_pair;
|
|
|
|
|
|
|
|
static bool stat_cmp(stat_pair a, stat_pair b)
|
|
|
|
{
|
|
|
|
return a.second < b.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
Dictionary(Buffer &inBuf, size_t size)
|
|
|
|
{
|
2013-03-13 18:28:57 +04:00
|
|
|
if (!size || !Resize(size))
|
2013-03-06 10:30:07 +04:00
|
|
|
return;
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Creating dictionary");
|
2013-03-06 10:30:07 +04:00
|
|
|
piece *origBufPieces = reinterpret_cast<piece *>(
|
|
|
|
static_cast<void *>(inBuf));
|
|
|
|
std::map<piece, int> stats;
|
2013-06-27 04:35:49 +04:00
|
|
|
for (unsigned int i = 0; i < inBuf.GetLength() / sizeof(piece); i++) {
|
2013-03-06 10:30:07 +04:00
|
|
|
stats[origBufPieces[i]]++;
|
|
|
|
}
|
|
|
|
std::vector<stat_pair> statsVec(stats.begin(), stats.end());
|
|
|
|
std::sort(statsVec.begin(), statsVec.end(), stat_cmp);
|
|
|
|
|
|
|
|
piece *dictPieces = reinterpret_cast<piece *>(
|
|
|
|
static_cast<void *>(*this));
|
|
|
|
typename std::vector<stat_pair>::reverse_iterator it = statsVec.rbegin();
|
2013-03-13 18:28:57 +04:00
|
|
|
for (int i = size / sizeof(piece); i > 0 && it < statsVec.rend();
|
|
|
|
i--, ++it) {
|
2013-03-06 10:30:07 +04:00
|
|
|
dictPieces[i - 1] = it->first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
class SzipAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual int run(const char *name, Buffer &origBuf,
|
|
|
|
const char *outName, Buffer &outBuf) = 0;
|
|
|
|
|
|
|
|
virtual ~SzipAction() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SzipDecompress: public SzipAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int run(const char *name, Buffer &origBuf,
|
|
|
|
const char *outName, Buffer &outBuf);
|
|
|
|
};
|
|
|
|
|
2013-03-06 10:29:59 +04:00
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
class SzipCompress: public SzipAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int run(const char *name, Buffer &origBuf,
|
|
|
|
const char *outName, Buffer &outBuf);
|
|
|
|
|
2013-03-06 10:30:07 +04:00
|
|
|
SzipCompress(size_t aChunkSize, SeekableZStream::FilterId aFilter,
|
|
|
|
size_t aDictSize)
|
2013-03-06 10:29:51 +04:00
|
|
|
: chunkSize(aChunkSize ? aChunkSize : 16384)
|
2013-03-13 18:28:57 +04:00
|
|
|
, filter(aFilter)
|
|
|
|
, dictSize(aDictSize)
|
2013-03-06 10:29:51 +04:00
|
|
|
{}
|
|
|
|
|
2013-03-06 10:30:07 +04:00
|
|
|
const static signed char winSizeLog = 15;
|
|
|
|
const static size_t winSize = 1 << winSizeLog;
|
|
|
|
|
2013-03-06 10:29:59 +04:00
|
|
|
const static SeekableZStream::FilterId DEFAULT_FILTER =
|
|
|
|
#if defined(TARGET_THUMB)
|
|
|
|
SeekableZStream::BCJ_THUMB;
|
|
|
|
#elif defined(TARGET_ARM)
|
|
|
|
SeekableZStream::BCJ_ARM;
|
2013-03-08 12:32:56 +04:00
|
|
|
#elif defined(TARGET_X86)
|
|
|
|
SeekableZStream::BCJ_X86;
|
2013-03-06 10:29:59 +04:00
|
|
|
#else
|
|
|
|
SeekableZStream::NONE;
|
|
|
|
#endif
|
|
|
|
|
2013-03-13 18:28:57 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
int do_compress(Buffer &origBuf, Buffer &outBuf, const unsigned char *aDict,
|
|
|
|
size_t aDictSize, SeekableZStream::FilterId aFilter);
|
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
size_t chunkSize;
|
2013-03-06 10:29:59 +04:00
|
|
|
SeekableZStream::FilterId filter;
|
2013-03-06 10:30:07 +04:00
|
|
|
size_t dictSize;
|
2013-03-06 10:29:51 +04:00
|
|
|
};
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
/* Decompress a seekable compressed stream */
|
2013-03-06 10:29:51 +04:00
|
|
|
int SzipDecompress::run(const char *name, Buffer &origBuf,
|
|
|
|
const char *outName, Buffer &outBuf)
|
2012-02-22 11:12:15 +04:00
|
|
|
{
|
2013-03-06 10:29:22 +04:00
|
|
|
size_t origSize = origBuf.GetLength();
|
|
|
|
if (origSize < sizeof(SeekableZStreamHeader)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("%s is not compressed", name);
|
2013-04-11 11:37:44 +04:00
|
|
|
return 0;
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
SeekableZStream zstream;
|
|
|
|
if (!zstream.Init(origBuf, origSize))
|
2013-04-11 11:37:44 +04:00
|
|
|
return 0;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
size_t size = zstream.GetUncompressedSize();
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
/* Give enough room for the uncompressed data */
|
|
|
|
if (!outBuf.Resize(size)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Error resizing %s: %s", outName, strerror(errno));
|
2012-02-22 11:12:15 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
if (!zstream.Decompress(outBuf, 0, size))
|
2012-02-22 11:12:15 +04:00
|
|
|
return 1;
|
|
|
|
|
2013-03-06 10:29:22 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate a seekable compressed stream. */
|
2013-03-06 10:29:51 +04:00
|
|
|
int SzipCompress::run(const char *name, Buffer &origBuf,
|
|
|
|
const char *outName, Buffer &outBuf)
|
2013-03-06 10:29:22 +04:00
|
|
|
{
|
|
|
|
size_t origSize = origBuf.GetLength();
|
|
|
|
if (origSize == 0) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Won't compress %s: it's empty", name);
|
2012-02-22 11:12:15 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-04-11 11:37:44 +04:00
|
|
|
if (SeekableZStreamHeader::validate(origBuf)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
WARN("Skipping %s: it's already a szip", name);
|
2013-04-11 11:37:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2013-03-13 18:28:57 +04:00
|
|
|
bool compressed = false;
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("Size = %" PRIuSize, origSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-13 18:28:57 +04:00
|
|
|
/* Allocate a buffer the size of the uncompressed data: we don't want
|
|
|
|
* a compressed file larger than that anyways. */
|
|
|
|
if (!outBuf.Resize(origSize)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't allocate output buffer: %s", strerror(errno));
|
2013-03-13 18:28:57 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the most appropriate filter */
|
|
|
|
SeekableZStream::FilterId firstFilter, lastFilter;
|
|
|
|
bool scanFilters;
|
|
|
|
if (filter == SeekableZStream::FILTER_MAX) {
|
|
|
|
firstFilter = SeekableZStream::NONE;
|
|
|
|
lastFilter = SeekableZStream::FILTER_MAX;
|
|
|
|
scanFilters = true;
|
|
|
|
} else {
|
|
|
|
firstFilter = lastFilter = filter;
|
|
|
|
++lastFilter;
|
|
|
|
scanFilters = false;
|
|
|
|
}
|
|
|
|
|
2016-02-26 21:50:59 +03:00
|
|
|
mozilla::UniquePtr<Buffer> filteredBuf;
|
2013-03-13 18:28:57 +04:00
|
|
|
Buffer *origData;
|
|
|
|
for (SeekableZStream::FilterId f = firstFilter; f < lastFilter; ++f) {
|
2016-02-26 21:50:59 +03:00
|
|
|
mozilla::UniquePtr<FilteredBuffer> filteredTmp;
|
2013-03-13 18:28:57 +04:00
|
|
|
Buffer tmpBuf;
|
|
|
|
if (f != SeekableZStream::NONE) {
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Applying filter \"%s\"", filterName[f]);
|
2016-02-26 21:50:59 +03:00
|
|
|
filteredTmp = mozilla::MakeUnique<FilteredBuffer>();
|
2013-03-13 18:28:57 +04:00
|
|
|
filteredTmp->Filter(origBuf, f, chunkSize);
|
2016-02-26 21:50:59 +03:00
|
|
|
origData = filteredTmp.get();
|
2013-03-13 18:28:57 +04:00
|
|
|
} else {
|
|
|
|
origData = &origBuf;
|
|
|
|
}
|
|
|
|
if (dictSize && !scanFilters) {
|
2016-02-26 21:50:59 +03:00
|
|
|
filteredBuf = mozilla::Move(filteredTmp);
|
2013-03-13 18:28:57 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Compressing with no dictionary");
|
2013-11-11 23:15:46 +04:00
|
|
|
if (do_compress(*origData, tmpBuf, nullptr, 0, f) == 0) {
|
2013-03-13 18:28:57 +04:00
|
|
|
if (tmpBuf.GetLength() < outBuf.GetLength()) {
|
|
|
|
outBuf.Fill(tmpBuf);
|
|
|
|
compressed = true;
|
|
|
|
filter = f;
|
2016-02-26 21:50:59 +03:00
|
|
|
filteredBuf = mozilla::Move(filteredTmp);
|
2013-03-13 18:28:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 21:50:59 +03:00
|
|
|
origData = filteredBuf ? filteredBuf.get() : &origBuf;
|
2013-03-13 18:28:57 +04:00
|
|
|
|
|
|
|
if (dictSize) {
|
|
|
|
Dictionary<uint64_t> dict(*origData, dictSize ? SzipCompress::winSize : 0);
|
|
|
|
|
|
|
|
/* Find the most appropriate dictionary size */
|
|
|
|
size_t firstDictSize, lastDictSize;
|
|
|
|
if (dictSize == (size_t) -1) {
|
|
|
|
/* If we scanned for filters, we effectively already tried dictSize=0 */
|
|
|
|
firstDictSize = scanFilters ? 4096 : 0;
|
|
|
|
lastDictSize = SzipCompress::winSize;
|
|
|
|
} else {
|
|
|
|
firstDictSize = lastDictSize = dictSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer tmpBuf;
|
|
|
|
for (size_t d = firstDictSize; d <= lastDictSize; d += 4096) {
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Compressing with dictionary of size %" PRIuSize, d);
|
2013-03-13 18:28:57 +04:00
|
|
|
if (do_compress(*origData, tmpBuf, static_cast<unsigned char *>(dict)
|
|
|
|
+ SzipCompress::winSize - d, d, filter))
|
|
|
|
continue;
|
|
|
|
if (!compressed || tmpBuf.GetLength() < outBuf.GetLength()) {
|
|
|
|
outBuf.Fill(tmpBuf);
|
|
|
|
compressed = true;
|
|
|
|
dictSize = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!compressed) {
|
|
|
|
outBuf.Fill(origBuf);
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("Not compressed");
|
2013-03-13 18:28:57 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dictSize == (size_t) -1)
|
|
|
|
dictSize = 0;
|
|
|
|
|
2013-06-27 04:35:49 +04:00
|
|
|
DEBUG_LOG("Used filter \"%s\" and dictionary size of %" PRIuSize,
|
|
|
|
filterName[filter], dictSize);
|
|
|
|
LOG("Compressed size is %" PRIuSize, outBuf.GetLength());
|
2013-03-13 18:28:57 +04:00
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
Buffer tmpBuf;
|
|
|
|
SzipDecompress decompress;
|
|
|
|
if (decompress.run("buffer", outBuf, "buffer", tmpBuf))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size_t size = tmpBuf.GetLength();
|
|
|
|
if (size != origSize) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Compression error: %" PRIuSize " != %" PRIuSize, size, origSize);
|
2013-03-13 18:28:57 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (memcmp(static_cast<void *>(origBuf), static_cast<void *>(tmpBuf), size)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Compression error: content mismatch");
|
2013-03-13 18:28:57 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SzipCompress::do_compress(Buffer &origBuf, Buffer &outBuf,
|
|
|
|
const unsigned char *aDict, size_t aDictSize,
|
|
|
|
SeekableZStream::FilterId aFilter)
|
|
|
|
{
|
|
|
|
size_t origSize = origBuf.GetLength();
|
|
|
|
MOZ_ASSERT(origSize != 0);
|
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
/* Expected total number of chunks */
|
2013-03-06 10:29:51 +04:00
|
|
|
size_t nChunks = ((origSize + chunkSize - 1) / chunkSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:30:07 +04:00
|
|
|
/* The first chunk is going to be stored after the header, the dictionary
|
|
|
|
* and the offset table */
|
2013-03-13 18:28:57 +04:00
|
|
|
size_t offset = sizeof(SeekableZStreamHeader) + aDictSize
|
2013-03-06 10:30:07 +04:00
|
|
|
+ nChunks * sizeof(uint32_t);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-13 18:28:57 +04:00
|
|
|
if (offset >= origSize)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Allocate a buffer the size of the uncompressed data: we don't want
|
|
|
|
* a compressed file larger than that anyways. */
|
2013-03-06 10:29:13 +04:00
|
|
|
if (!outBuf.Resize(origSize)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't allocate output buffer: %s", strerror(errno));
|
2012-02-22 11:12:15 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:29:13 +04:00
|
|
|
SeekableZStreamHeader *header = new (outBuf) SeekableZStreamHeader;
|
2013-03-06 10:30:07 +04:00
|
|
|
unsigned char *dictionary = static_cast<unsigned char *>(
|
|
|
|
outBuf + sizeof(SeekableZStreamHeader));
|
|
|
|
le_uint32 *entry =
|
2013-03-13 18:28:57 +04:00
|
|
|
reinterpret_cast<le_uint32 *>(dictionary + aDictSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Initialize header */
|
2013-03-06 10:29:51 +04:00
|
|
|
header->chunkSize = chunkSize;
|
2013-03-13 18:28:57 +04:00
|
|
|
header->dictSize = aDictSize;
|
2012-02-22 11:12:15 +04:00
|
|
|
header->totalSize = offset;
|
2013-03-13 18:28:57 +04:00
|
|
|
header->windowBits = -SzipCompress::winSizeLog; // Raw stream,
|
|
|
|
// window size of 32k.
|
|
|
|
header->filter = aFilter;
|
|
|
|
if (aDictSize)
|
|
|
|
memcpy(dictionary, aDict, aDictSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Initialize zlib structure */
|
|
|
|
z_stream zStream;
|
|
|
|
memset(&zStream, 0, sizeof(zStream));
|
2013-03-06 10:29:13 +04:00
|
|
|
zStream.avail_out = origSize - offset;
|
|
|
|
zStream.next_out = static_cast<Bytef*>(outBuf) + offset;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
size_t avail = 0;
|
2013-03-06 10:29:13 +04:00
|
|
|
size_t size = origSize;
|
2013-03-13 18:28:57 +04:00
|
|
|
unsigned char *data = reinterpret_cast<unsigned char *>(
|
|
|
|
static_cast<void *>(origBuf));
|
2013-03-06 10:29:13 +04:00
|
|
|
while (size) {
|
2013-03-06 10:29:51 +04:00
|
|
|
avail = std::min(size, chunkSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Compress chunk */
|
2013-03-06 10:29:41 +04:00
|
|
|
int ret = deflateInit2(&zStream, 9, Z_DEFLATED, header->windowBits,
|
|
|
|
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
2013-03-13 18:28:57 +04:00
|
|
|
if (aDictSize)
|
|
|
|
deflateSetDictionary(&zStream, dictionary, aDictSize);
|
2012-02-22 11:12:15 +04:00
|
|
|
MOZ_ASSERT(ret == Z_OK);
|
|
|
|
zStream.avail_in = avail;
|
2013-03-06 10:30:07 +04:00
|
|
|
zStream.next_in = data;
|
2012-02-22 11:12:15 +04:00
|
|
|
ret = deflate(&zStream, Z_FINISH);
|
2015-12-31 04:08:13 +03:00
|
|
|
/* Under normal conditions, deflate returns Z_STREAM_END. If there is not
|
|
|
|
* enough room to compress, deflate returns Z_OK and avail_out is 0. We
|
|
|
|
* still want to deflateEnd in that case, so fall through. It will bail
|
|
|
|
* on the avail_out test that follows. */
|
|
|
|
MOZ_ASSERT(ret == Z_STREAM_END || ret == Z_OK);
|
2012-02-22 11:12:15 +04:00
|
|
|
ret = deflateEnd(&zStream);
|
|
|
|
MOZ_ASSERT(ret == Z_OK);
|
2013-03-13 18:28:57 +04:00
|
|
|
if (zStream.avail_out <= 0)
|
|
|
|
return 1;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
2013-03-06 10:29:13 +04:00
|
|
|
size_t len = origSize - offset - zStream.avail_out;
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
/* Adjust headers */
|
|
|
|
header->totalSize += len;
|
|
|
|
*entry++ = offset;
|
|
|
|
header->nChunks++;
|
|
|
|
|
|
|
|
/* Prepare for next iteration */
|
2013-03-06 10:29:13 +04:00
|
|
|
size -= avail;
|
2013-03-06 10:30:07 +04:00
|
|
|
data += avail;
|
2012-02-22 11:12:15 +04:00
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
header->lastChunkSize = avail;
|
2013-03-13 18:28:57 +04:00
|
|
|
MOZ_ASSERT(header->totalSize == offset);
|
2013-06-27 04:35:49 +04:00
|
|
|
MOZ_ASSERT(header->nChunks == nChunks);
|
2013-03-13 18:28:57 +04:00
|
|
|
|
2013-03-06 10:29:13 +04:00
|
|
|
if (!outBuf.Resize(offset)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Error truncating output: %s", strerror(errno));
|
2013-03-06 10:29:13 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2012-02-22 11:12:15 +04:00
|
|
|
|
|
|
|
return 0;
|
2013-03-13 18:28:57 +04:00
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
bool GetSize(const char *str, size_t *out)
|
2013-03-06 10:29:22 +04:00
|
|
|
{
|
2013-03-06 10:29:51 +04:00
|
|
|
char *end;
|
|
|
|
MOZ_ASSERT(out);
|
|
|
|
errno = 0;
|
|
|
|
*out = strtol(str, &end, 10);
|
|
|
|
return (!errno && !*end);
|
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2013-12-30 23:34:54 +04:00
|
|
|
mozilla::UniquePtr<SzipAction> action;
|
2013-03-06 10:29:51 +04:00
|
|
|
char **firstArg;
|
|
|
|
bool compress = true;
|
|
|
|
size_t chunkSize = 0;
|
2013-03-13 18:28:57 +04:00
|
|
|
SeekableZStream::FilterId filter = SzipCompress::DEFAULT_FILTER;
|
|
|
|
size_t dictSize = (size_t) 0;
|
2013-03-06 10:29:51 +04:00
|
|
|
|
2013-07-10 09:12:35 +04:00
|
|
|
Logging::Init();
|
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
for (firstArg = &argv[1]; argc > 2; argc--, firstArg++) {
|
2013-03-06 10:29:51 +04:00
|
|
|
if (!firstArg[0] || firstArg[0][0] != '-')
|
|
|
|
break;
|
|
|
|
if (strcmp(firstArg[0], "-d") == 0) {
|
|
|
|
compress = false;
|
|
|
|
} else if (strcmp(firstArg[0], "-c") == 0) {
|
|
|
|
firstArg++;
|
|
|
|
argc--;
|
|
|
|
if (!firstArg[0])
|
|
|
|
break;
|
|
|
|
if (!GetSize(firstArg[0], &chunkSize) || !chunkSize ||
|
2013-03-06 20:01:56 +04:00
|
|
|
(chunkSize % 4096) || (chunkSize > maxChunkSize)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Invalid chunk size");
|
2013-03-06 10:29:51 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-06 10:29:59 +04:00
|
|
|
} else if (strcmp(firstArg[0], "-f") == 0) {
|
|
|
|
firstArg++;
|
|
|
|
argc--;
|
|
|
|
if (!firstArg[0])
|
|
|
|
break;
|
2013-03-13 18:28:57 +04:00
|
|
|
bool matched = false;
|
2013-06-27 04:35:49 +04:00
|
|
|
for (unsigned int i = 0; i < sizeof(filterName) / sizeof(char *); ++i) {
|
2013-03-13 18:28:57 +04:00
|
|
|
if (strcmp(firstArg[0], filterName[i]) == 0) {
|
|
|
|
filter = static_cast<SeekableZStream::FilterId>(i);
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matched) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Invalid filter");
|
2013-03-06 10:29:59 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-06 10:30:07 +04:00
|
|
|
} else if (strcmp(firstArg[0], "-D") == 0) {
|
|
|
|
firstArg++;
|
|
|
|
argc--;
|
|
|
|
if (!firstArg[0])
|
|
|
|
break;
|
2013-03-13 18:28:57 +04:00
|
|
|
if (strcmp(firstArg[0], "auto") == 0) {
|
|
|
|
dictSize = -1;
|
|
|
|
} else if (!GetSize(firstArg[0], &dictSize) || (dictSize >= 1 << 16)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Invalid dictionary size");
|
2013-03-06 10:30:07 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-06 10:29:51 +04:00
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
if (argc != 2 || !firstArg[0]) {
|
2013-06-27 04:35:49 +04:00
|
|
|
LOG("usage: %s [-d] [-c CHUNKSIZE] [-f FILTER] [-D DICTSIZE] file",
|
2013-04-11 11:37:44 +04:00
|
|
|
argv[0]);
|
2013-03-06 10:29:22 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:29:51 +04:00
|
|
|
if (compress) {
|
2013-12-30 23:34:54 +04:00
|
|
|
action.reset(new SzipCompress(chunkSize, filter, dictSize));
|
2013-03-06 10:29:51 +04:00
|
|
|
} else {
|
|
|
|
if (chunkSize) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("-c is incompatible with -d");
|
2013-03-06 10:29:51 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-04-11 11:37:44 +04:00
|
|
|
if (dictSize) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("-D is incompatible with -d");
|
2013-03-06 10:30:07 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-12-30 23:34:54 +04:00
|
|
|
action.reset(new SzipDecompress());
|
2013-03-06 10:29:51 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
std::stringstream tmpOutStream;
|
|
|
|
tmpOutStream << firstArg[0] << ".sz." << getpid();
|
|
|
|
std::string tmpOut(tmpOutStream.str());
|
|
|
|
int ret;
|
2013-03-06 10:29:22 +04:00
|
|
|
struct stat st;
|
2013-04-11 11:37:44 +04:00
|
|
|
{
|
|
|
|
FileBuffer origBuf;
|
|
|
|
if (!origBuf.Init(firstArg[0])) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't open %s: %s", firstArg[0], strerror(errno));
|
2013-04-11 11:37:44 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
ret = fstat(origBuf.getFd(), &st);
|
|
|
|
if (ret == -1) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't stat %s: %s", firstArg[0], strerror(errno));
|
2013-04-11 11:37:44 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
size_t origSize = st.st_size;
|
2013-03-06 10:29:22 +04:00
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
/* Mmap the original file */
|
|
|
|
if (!origBuf.Resize(origSize)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't mmap %s: %s", firstArg[0], strerror(errno));
|
2013-04-11 11:37:44 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the compressed file */
|
|
|
|
FileBuffer outBuf;
|
|
|
|
if (!outBuf.Init(tmpOut.c_str(), true)) {
|
2014-06-13 03:45:58 +04:00
|
|
|
ERROR("Couldn't open %s: %s", tmpOut.c_str(), strerror(errno));
|
2013-04-11 11:37:44 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = action->run(firstArg[0], origBuf, tmpOut.c_str(), outBuf);
|
|
|
|
if ((ret == 0) && (fstat(outBuf.getFd(), &st) == -1)) {
|
|
|
|
st.st_size = 0;
|
|
|
|
}
|
2013-03-06 10:29:22 +04:00
|
|
|
}
|
|
|
|
|
2013-04-11 11:37:44 +04:00
|
|
|
if ((ret == 0) && st.st_size) {
|
|
|
|
rename(tmpOut.c_str(), firstArg[0]);
|
|
|
|
} else {
|
|
|
|
unlink(tmpOut.c_str());
|
|
|
|
}
|
2013-03-06 10:29:51 +04:00
|
|
|
return ret;
|
2013-03-06 10:29:22 +04:00
|
|
|
}
|