1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include "affconfig.h"
#include "afflib.h"
#include "afflib_i.h"
#include "../../../Common/MyWindows.h"
#include "../../../Common/MyInitGuid.h"
#include "../../Common/FileStreams.h"
#include "../../Common/StreamUtils.h"
#include "../LZMA/LZMADecoder.h"
#include "../LZMA/LZMAEncoder.h"
#include "LzmaBench.h"
#include "LzmaRam.h"
extern "C" {
#include "LzmaRamDecode.h"
}
class CInMemoryStream: public ISequentialInStream, public CMyUnknownImp {
public:
const unsigned char *buf;
size_t buflen;
size_t ptr;
virtual ~CInMemoryStream(){}
CInMemoryStream(const unsigned char *buf_,size_t len){
buf = buf_;
buflen = len;
ptr = 0;
}
MY_UNKNOWN_IMP1(IInStream)
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize){
if(ptr+size > buflen) size = buflen - ptr; // max that can be read
memcpy(data,buf+ptr,size);
ptr += size;
if(processedSize) *processedSize = size;
return S_OK;
}
};
class COutMemoryStream: public ISequentialOutStream, public CMyUnknownImp {
public:
unsigned char *buf;
size_t buflen;
size_t ptr;
size_t *notify;
virtual ~COutMemoryStream(){}
COutMemoryStream(unsigned char *buf_,size_t len,size_t *notify_){
buf = buf_;
buflen = len;
ptr = 0;
notify = notify_;
}
MY_UNKNOWN_IMP1(IOutStream) STDMETHOD(Write)(const void *data, UInt32 size,
UInt32 *processedSize){
if(ptr+size > buflen) return E_FAIL;
memcpy(buf+ptr,data,size);
ptr += size;
if(processedSize) *processedSize = size;
if(notify) *notify = ptr;
return S_OK;
}
};
/*
* Attempt to compress. Return -1 if fail.
* (Fails if compression results in expansion.
*/
int lzma_compress(unsigned char *dest,size_t *destLen,const unsigned char *data,size_t datalen,int level)
{
PROPID propIDs[] = {
NCoderPropID::kDictionarySize,
NCoderPropID::kPosStateBits,
NCoderPropID::kLitContextBits,
NCoderPropID::kLitPosBits,
NCoderPropID::kAlgorithm,
NCoderPropID::kNumFastBytes,
NCoderPropID::kMatchFinder,
NCoderPropID::kEndMarker
};
const int nprops = sizeof(propIDs) / sizeof(propIDs[0]);
PROPVARIANT p[nprops];
p[0].vt = VT_UI4; p[0].ulVal = UInt32(1 << 24);
p[1].vt = VT_UI4; p[1].ulVal = UInt32(2); // posBits
p[2].vt = VT_UI4; p[2].ulVal = UInt32(3); // literal context bits
p[3].vt = VT_UI4; p[3].ulVal = UInt32(0); // literal pos bits
p[4].vt = VT_UI4; p[4].ulVal = UInt32(2); // compression mode
p[5].vt = VT_UI4; p[5].ulVal = UInt32(128); // fast_bytes
// old code generates warnings now
//p[6].vt = VT_BSTR; p[6].bstrVal = L"bt4"; // it's okay; we won't change it
// new code
const void *temp = L"bt4";
p[6].vt = VT_BSTR; p[6].bstrVal = (OLECHAR *)temp; // it's okay; we won't change it
p[7].vt = VT_BOOL; p[7].boolVal = VARIANT_FALSE;
NCompress::NLZMA::CEncoder *encoder = new NCompress::NLZMA::CEncoder;
if (encoder->SetCoderProperties(propIDs, p, nprops) != S_OK){
return -1; /* Couldn't set encoder properties */
}
/* Open and configure the output stream */
UInt64 fileSize = datalen;
COutMemoryStream *outStream = new COutMemoryStream(dest,*destLen,destLen);
outStream->AddRef();
encoder->WriteCoderProperties(outStream);
for (int i = 0; i < 8; i++) {
Byte b = Byte(fileSize >> (8 * i));
if (outStream->Write(&b, sizeof(b), 0) != S_OK){
outStream->Release();
return -1; /* Write error while encoding */
}
}
CInMemoryStream *inStream = new CInMemoryStream(data,datalen);
inStream->AddRef();
HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
inStream->Release();
outStream->Release();
delete(encoder);
return result;
}
int lzma_uncompress(unsigned char *buf,size_t *buflen, const unsigned char *cbuf,size_t cbuf_size)
{
CInMemoryStream *inStream = new CInMemoryStream(cbuf,cbuf_size);
inStream->AddRef();
const UInt32 kPropertiesSize = 5;
Byte properties[kPropertiesSize];
UInt32 processedSize;
UInt64 fileSize = 0;
NCompress::NLZMA::CDecoder decoderSpec;
if (inStream->Read(properties, kPropertiesSize, &processedSize) != S_OK){
inStream->Release();
return -1;
}
if (processedSize != kPropertiesSize) return -1;
if (decoderSpec.SetDecoderProperties2(properties, kPropertiesSize) != S_OK){
inStream->Release();
return -1;
}
for (int i = 0; i < 8; i++) {
Byte b;
if (inStream->Read(&b, sizeof(b), &processedSize) != S_OK) return -1;
if (processedSize != 1){
inStream->Release();
return -1;
}
fileSize |= ((UInt64)b) << (8 * i);
}
COutMemoryStream *outStream = new COutMemoryStream(buf,*buflen,buflen);
outStream->AddRef();
int r = decoderSpec.Code(inStream, outStream, 0, &fileSize, 0);
inStream->Release();
outStream->Release();
return r;
}
|