2009-03-31 13:57:36 +02:00
|
|
|
/*
|
|
|
|
* minimal lzma implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Eric Biederman
|
|
|
|
* Copyright (C) 2005 Joel Yliluoma
|
|
|
|
* Copyright (C) 2007 coresystems GmbH
|
|
|
|
* (Adapted by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
|
|
|
|
* Copyright (C) 2007 Patrick Georgi <patrick@georgi-clan.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "C/Common/MyInitGuid.h"
|
|
|
|
#include "C/7zip/Compress/LZMA/LZMAEncoder.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
const std::vector<unsigned char> LZMACompress
|
|
|
|
(const std::vector<unsigned char>& buf);
|
|
|
|
|
|
|
|
const std::vector<unsigned char> LZMADeCompress
|
|
|
|
(const std::vector<unsigned char>& buf);
|
|
|
|
|
|
|
|
static inline uint16_t R16(const void* p)
|
|
|
|
{
|
|
|
|
const unsigned char* data = (const unsigned char*)p;
|
|
|
|
return (data[0] << 0) | (data[1] << 8);
|
|
|
|
}
|
|
|
|
static inline uint32_t R32(const void* p)
|
|
|
|
{
|
|
|
|
const unsigned char* data = (const unsigned char*)p;
|
|
|
|
return R16(data) | (R16(data+2) << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define L (uint64_t)
|
|
|
|
|
|
|
|
static inline uint64_t R64(const void* p)
|
|
|
|
{
|
|
|
|
const unsigned char* data = (const unsigned char*)p;
|
|
|
|
return (L R32(data)) | ((L R32(data+4)) << 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef L
|
|
|
|
|
|
|
|
static UInt32 SelectDictionarySizeFor(unsigned datasize)
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
return datasize;
|
|
|
|
#else
|
|
|
|
#ifdef __GNUC__
|
|
|
|
/* gnu c can optimize this switch statement into a fast binary
|
|
|
|
* search, but it cannot do so for the list of the if statements.
|
|
|
|
*/
|
|
|
|
switch(datasize)
|
|
|
|
{
|
|
|
|
case 0 ... 512 : return 512;
|
|
|
|
case 513 ... 1024: return 2048;
|
|
|
|
case 1025 ... 4096: return 8192;
|
|
|
|
case 4097 ... 16384: return 32768;
|
|
|
|
case 16385 ... 65536: return 528288;
|
|
|
|
case 65537 ... 528288: return 1048576*4;
|
|
|
|
case 528289 ... 786432: return 1048576*16;
|
|
|
|
default: return 1048576*32;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(datasize <= 512) return 512;
|
|
|
|
if(datasize <= 1024) return 1024;
|
|
|
|
if(datasize <= 4096) return 4096;
|
2010-04-27 08:56:47 +02:00
|
|
|
if(datasize <= 16384) return 32768;
|
2009-03-31 13:57:36 +02:00
|
|
|
if(datasize <= 65536) return 528288;
|
|
|
|
if(datasize <= 528288) return 1048576*4;
|
2010-10-11 00:05:02 +02:00
|
|
|
if(datasize <= 786432) return 1048576*16;
|
2009-03-31 13:57:36 +02:00
|
|
|
return 32*1048576;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CInStreamRam: public ISequentialInStream, public CMyUnknownImp
|
|
|
|
{
|
|
|
|
const std::vector<unsigned char>& input;
|
|
|
|
size_t Pos;
|
|
|
|
public:
|
|
|
|
MY_UNKNOWN_IMP
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
CInStreamRam(const std::vector<unsigned char>& buf) : input(buf), Pos(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~CInStreamRam() {}
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
|
|
|
|
};
|
|
|
|
|
|
|
|
STDMETHODIMP CInStreamRam::Read(void *data, UInt32 size, UInt32 *processedSize)
|
|
|
|
{
|
|
|
|
UInt32 remain = input.size() - Pos;
|
|
|
|
if (size > remain) size = remain;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
std::memcpy(data, &input[Pos], size);
|
|
|
|
Pos += size;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
if(processedSize != NULL) *processedSize = size;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class COutStreamRam: public ISequentialOutStream, public CMyUnknownImp
|
|
|
|
{
|
|
|
|
std::vector<Byte> result;
|
|
|
|
size_t Pos;
|
|
|
|
public:
|
|
|
|
MY_UNKNOWN_IMP
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
COutStreamRam(): result(), Pos(0) { }
|
|
|
|
virtual ~COutStreamRam() { }
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
void Reserve(unsigned n) { result.reserve(n); }
|
|
|
|
const std::vector<Byte>& Get() const { return result; }
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
HRESULT WriteByte(Byte b)
|
|
|
|
{
|
|
|
|
if(Pos >= result.size()) result.resize(Pos+1);
|
|
|
|
result[Pos++] = b;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
|
|
|
|
};
|
|
|
|
|
|
|
|
STDMETHODIMP COutStreamRam::Write(const void *data, UInt32 size, UInt32 *processedSize)
|
|
|
|
{
|
|
|
|
if(Pos+size > result.size()) result.resize(Pos+size);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
std::memcpy(&result[Pos], data, size);
|
|
|
|
if(processedSize != NULL) *processedSize = size;
|
|
|
|
Pos += size;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<unsigned char> LZMACompress(const std::vector<unsigned char>& buf)
|
|
|
|
{
|
|
|
|
if(buf.empty()) return buf;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
const UInt32 dictionarysize = SelectDictionarySizeFor(buf.size());
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
NCompress::NLZMA::CEncoder *encoderSpec = new NCompress::NLZMA::CEncoder;
|
|
|
|
CMyComPtr<ICompressCoder> encoder = encoderSpec;
|
2010-04-27 08:56:47 +02:00
|
|
|
const PROPID propIDs[] =
|
2009-03-31 13:57:36 +02:00
|
|
|
{
|
|
|
|
NCoderPropID::kAlgorithm,
|
2010-04-27 08:56:47 +02:00
|
|
|
NCoderPropID::kDictionarySize,
|
2009-03-31 13:57:36 +02:00
|
|
|
NCoderPropID::kNumFastBytes,
|
|
|
|
};
|
|
|
|
const unsigned kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);
|
|
|
|
PROPVARIANT properties[kNumProps];
|
|
|
|
properties[0].vt = VT_UI4; properties[0].ulVal = (UInt32)2;
|
|
|
|
properties[1].vt = VT_UI4; properties[1].ulVal = (UInt32)dictionarysize;
|
|
|
|
properties[2].vt = VT_UI4; properties[2].ulVal = (UInt32)64;
|
|
|
|
|
|
|
|
if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)
|
|
|
|
{
|
|
|
|
Error:
|
|
|
|
return std::vector<unsigned char> ();
|
|
|
|
}
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
COutStreamRam *const outStreamSpec = new COutStreamRam;
|
|
|
|
CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
|
|
|
|
CInStreamRam *const inStreamSpec = new CInStreamRam(buf);
|
|
|
|
CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
outStreamSpec->Reserve(buf.size());
|
|
|
|
|
|
|
|
if (encoderSpec->WriteCoderProperties(outStream) != S_OK) goto Error;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
for (unsigned i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
UInt64 t = (UInt64)buf.size();
|
|
|
|
outStreamSpec->WriteByte((Byte)((t) >> (8 * i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT lzmaResult = encoder->Code(inStream, outStream, 0, 0, 0);
|
|
|
|
if (lzmaResult != S_OK) goto Error;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
return outStreamSpec->Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef RC_NORMALIZE
|
|
|
|
|
|
|
|
#include "C/7zip/Decompress/LzmaDecode.h"
|
|
|
|
#include "C/7zip/Decompress/LzmaDecode.c"
|
|
|
|
|
|
|
|
const std::vector<unsigned char> LZMADeCompress
|
|
|
|
(const std::vector<unsigned char>& buf)
|
|
|
|
{
|
|
|
|
if(buf.size() <= 5+8) return std::vector<unsigned char> ();
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
uint_least64_t out_sizemax = R64(&buf[5]);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
std::vector<unsigned char> result(out_sizemax);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
CLzmaDecoderState state;
|
|
|
|
LzmaDecodeProperties(&state.Properties, &buf[0], LZMA_PROPERTIES_SIZE);
|
|
|
|
state.Probs = new CProb[LzmaGetNumProbs(&state.Properties)];
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
SizeT in_done;
|
|
|
|
SizeT out_done;
|
|
|
|
LzmaDecode(&state, &buf[13], buf.size()-13, &in_done,
|
|
|
|
&result[0], result.size(), &out_done);
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
delete[] state.Probs;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
result.resize(out_done);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef COMPACT
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
FILE *f, *infile, *outfile;
|
|
|
|
int c;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2009-03-31 13:57:36 +02:00
|
|
|
if (argc != 4) {
|
|
|
|
std::fprintf(stderr, "'lzma e file1 file2' encodes file1 into file2.\n"
|
|
|
|
"'lzma d file2 file1' decodes file2 into file1.\n");
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (argc == 4) {
|
|
|
|
if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
|
|
|
|
|| (s = argv[2], (infile = fopen(s, "rb")) == NULL)
|
|
|
|
|| (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
|
|
|
|
std::fprintf(stderr, "??? %s\n", s);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat fs;
|
|
|
|
int si;
|
|
|
|
if (fstat(fileno(infile), &fs)) {
|
|
|
|
std::perror(strerror(errno));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
si=fs.st_size;
|
|
|
|
|
|
|
|
char *Buf=(char *)malloc(si);
|
|
|
|
fread(Buf,si, 1, infile);
|
|
|
|
|
|
|
|
std::vector<unsigned char> result;
|
2010-04-27 08:56:47 +02:00
|
|
|
if (toupper(*argv[1]) == 'E')
|
2009-03-31 13:57:36 +02:00
|
|
|
result = LZMACompress(std::vector<unsigned char>(Buf,Buf+si));
|
2010-04-27 08:56:47 +02:00
|
|
|
else
|
2009-03-31 13:57:36 +02:00
|
|
|
result = LZMADeCompress(std::vector<unsigned char>(Buf,Buf+si));
|
|
|
|
|
|
|
|
fwrite(&result[0], result.size(), 1, outfile);
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress a buffer with lzma
|
|
|
|
* Don't copy the result back if it is too large.
|
|
|
|
* @param in a pointer to the buffer
|
|
|
|
* @param in_len the length in bytes
|
|
|
|
* @param out a pointer to a buffer of at least size in_len
|
2010-04-27 08:56:47 +02:00
|
|
|
* @param out_len a pointer to the compressed length of in
|
2009-03-31 13:57:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
void do_lzma_compress(char *in, int in_len, char *out, int *out_len) {
|
|
|
|
std::vector<unsigned char> result;
|
|
|
|
result = LZMACompress(std::vector<unsigned char>(in, in + in_len));
|
|
|
|
*out_len = result.size();
|
|
|
|
if (*out_len < in_len)
|
|
|
|
std::memcpy(out, &result[0], *out_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len) {
|
|
|
|
std::vector<unsigned char> result;
|
|
|
|
result = LZMADeCompress(std::vector<unsigned char>(src, src + src_len));
|
2009-05-26 14:22:10 +02:00
|
|
|
if (result.size() <= (SizeT)dst_len)
|
2009-03-31 13:57:36 +02:00
|
|
|
std::memcpy(dst, &result[0], result.size());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Not copying %d bytes to %d-byte buffer!\n",
|
2009-05-26 14:22:10 +02:00
|
|
|
(unsigned int)result.size(), dst_len);
|
2009-03-31 13:57:36 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|