📄 lzmaalone.cpp
字号:
CMyComPtr<ISequentialOutStream> outStream;
COutFileStream *outStreamSpec = NULL;
if (stdOutMode)
{
outStream = new CStdOutFileStream;
MY_SET_BINARY_MODE(stdout);
}
else
{
if (paramIndex >= nonSwitchStrings.Size())
IncorrectCommand();
const UString &outputName = nonSwitchStrings[paramIndex++];
outStreamSpec = new COutFileStream;
outStream = outStreamSpec;
if (!outStreamSpec->Create(GetSystemString(outputName), true))
{
fprintf(stderr, "\nError: can not open output file %s\n",
(const char *)GetOemString(outputName));
return 1;
}
}
if (parser[NKey::kFilter86].ThereIs)
{
// -f86 switch is for x86 filtered mode: BCJ + LZMA.
if (parser[NKey::kEOS].ThereIs || stdInMode)
throw "Can not use stdin in this mode";
UInt64 fileSize;
inStreamSpec->File.GetLength(fileSize);
if (fileSize > 0xF0000000)
throw "File is too big";
UInt32 inSize = (UInt32)fileSize;
Byte *inBuffer = 0;
if (inSize != 0)
{
inBuffer = (Byte *)MyAlloc((size_t)inSize);
if (inBuffer == 0)
throw kCantAllocate;
}
UInt32 processedSize;
if (ReadStream(inStream, inBuffer, (UInt32)inSize, &processedSize) != S_OK)
throw "Can not read";
if ((UInt32)inSize != processedSize)
throw "Read size error";
Byte *outBuffer = 0;
size_t outSizeProcessed;
if (encodeMode)
{
// we allocate 105% of original size for output buffer
size_t outSize = (size_t)fileSize / 20 * 21 + (1 << 16);
if (outSize != 0)
{
outBuffer = (Byte *)MyAlloc((size_t)outSize);
if (outBuffer == 0)
throw kCantAllocate;
}
if (!dictionaryIsDefined)
dictionary = 1 << 23;
int res = LzmaRamEncode(inBuffer, inSize, outBuffer, outSize, &outSizeProcessed,
dictionary, parser[NKey::kFilter86].PostCharIndex == 0 ? SZ_FILTER_YES : SZ_FILTER_AUTO);
if (res != 0)
{
fprintf(stderr, "\nEncoder error = %d\n", (int)res);
return 1;
}
}
else
{
size_t outSize;
if (LzmaRamGetUncompressedSize(inBuffer, inSize, &outSize) != 0)
throw "data error";
if (outSize != 0)
{
outBuffer = (Byte *)MyAlloc(outSize);
if (outBuffer == 0)
throw kCantAllocate;
}
int res = LzmaRamDecompress(inBuffer, inSize, outBuffer, outSize, &outSizeProcessed, malloc, free);
if (res != 0)
throw "LzmaDecoder error";
}
if (WriteStream(outStream, outBuffer, (UInt32)outSizeProcessed, &processedSize) != S_OK)
throw kWriteError;
MyFree(outBuffer);
MyFree(inBuffer);
return 0;
}
UInt64 fileSize;
if (encodeMode)
{
NCompress::NLZMA::CEncoder *encoderSpec = new NCompress::NLZMA::CEncoder;
CMyComPtr<ICompressCoder> encoder = encoderSpec;
if (!dictionaryIsDefined)
dictionary = 1 << 23;
UInt32 posStateBits = 2;
UInt32 litContextBits = 3; // for normal files
// UInt32 litContextBits = 0; // for 32-bit data
UInt32 litPosBits = 0;
// UInt32 litPosBits = 2; // for 32-bit data
UInt32 algorithm = 1;
UInt32 numFastBytes = 128;
UInt32 matchFinderCycles = 16 + numFastBytes / 2;
bool matchFinderCyclesDefined = false;
bool eos = parser[NKey::kEOS].ThereIs || stdInMode;
if(parser[NKey::kMode].ThereIs)
if (!GetNumber(parser[NKey::kMode].PostStrings[0], algorithm))
IncorrectCommand();
if(parser[NKey::kFastBytes].ThereIs)
if (!GetNumber(parser[NKey::kFastBytes].PostStrings[0], numFastBytes))
IncorrectCommand();
matchFinderCyclesDefined = parser[NKey::kMatchFinderCycles].ThereIs;
if (matchFinderCyclesDefined)
if (!GetNumber(parser[NKey::kMatchFinderCycles].PostStrings[0], matchFinderCycles))
IncorrectCommand();
if(parser[NKey::kLitContext].ThereIs)
if (!GetNumber(parser[NKey::kLitContext].PostStrings[0], litContextBits))
IncorrectCommand();
if(parser[NKey::kLitPos].ThereIs)
if (!GetNumber(parser[NKey::kLitPos].PostStrings[0], litPosBits))
IncorrectCommand();
if(parser[NKey::kPosBits].ThereIs)
if (!GetNumber(parser[NKey::kPosBits].PostStrings[0], posStateBits))
IncorrectCommand();
PROPID propIDs[] =
{
NCoderPropID::kDictionarySize,
NCoderPropID::kPosStateBits,
NCoderPropID::kLitContextBits,
NCoderPropID::kLitPosBits,
NCoderPropID::kAlgorithm,
NCoderPropID::kNumFastBytes,
NCoderPropID::kMatchFinder,
NCoderPropID::kEndMarker,
NCoderPropID::kNumThreads,
NCoderPropID::kMatchFinderCycles,
};
const int kNumPropsMax = sizeof(propIDs) / sizeof(propIDs[0]);
PROPVARIANT properties[kNumPropsMax];
for (int p = 0; p < 6; p++)
properties[p].vt = VT_UI4;
properties[0].ulVal = (UInt32)dictionary;
properties[1].ulVal = (UInt32)posStateBits;
properties[2].ulVal = (UInt32)litContextBits;
properties[3].ulVal = (UInt32)litPosBits;
properties[4].ulVal = (UInt32)algorithm;
properties[5].ulVal = (UInt32)numFastBytes;
properties[6].vt = VT_BSTR;
properties[6].bstrVal = (BSTR)(const wchar_t *)mf;
properties[7].vt = VT_BOOL;
properties[7].boolVal = eos ? VARIANT_TRUE : VARIANT_FALSE;
properties[8].vt = VT_UI4;
properties[8].ulVal = (UInt32)numThreads;
// it must be last in property list
properties[9].vt = VT_UI4;
properties[9].ulVal = (UInt32)matchFinderCycles;
int numProps = kNumPropsMax;
if (!matchFinderCyclesDefined)
numProps--;
if (encoderSpec->SetCoderProperties(propIDs, properties, numProps) != S_OK)
IncorrectCommand();
encoderSpec->WriteCoderProperties(outStream);
if (eos || stdInMode)
fileSize = (UInt64)(Int64)-1;
else
inStreamSpec->File.GetLength(fileSize);
for (int i = 0; i < 8; i++)
{
Byte b = Byte(fileSize >> (8 * i));
if (outStream->Write(&b, 1, 0) != S_OK)
{
fprintf(stderr, kWriteError);
return 1;
}
}
HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
if (result == E_OUTOFMEMORY)
{
fprintf(stderr, "\nError: Can not allocate memory\n");
return 1;
}
else if (result != S_OK)
{
fprintf(stderr, "\nEncoder error = %X\n", (unsigned int)result);
return 1;
}
}
else
{
NCompress::NLZMA::CDecoder *decoderSpec = new NCompress::NLZMA::CDecoder;
CMyComPtr<ICompressCoder> decoder = decoderSpec;
const UInt32 kPropertiesSize = 5;
Byte properties[kPropertiesSize];
UInt32 processedSize;
if (ReadStream(inStream, properties, kPropertiesSize, &processedSize) != S_OK)
{
fprintf(stderr, kReadError);
return 1;
}
if (processedSize != kPropertiesSize)
{
fprintf(stderr, kReadError);
return 1;
}
if (decoderSpec->SetDecoderProperties2(properties, kPropertiesSize) != S_OK)
{
fprintf(stderr, "SetDecoderProperties error");
return 1;
}
fileSize = 0;
for (int i = 0; i < 8; i++)
{
Byte b;
if (inStream->Read(&b, 1, &processedSize) != S_OK)
{
fprintf(stderr, kReadError);
return 1;
}
if (processedSize != 1)
{
fprintf(stderr, kReadError);
return 1;
}
fileSize |= ((UInt64)b) << (8 * i);
}
if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)
{
fprintf(stderr, "Decoder error");
return 1;
}
}
if (outStreamSpec != NULL)
{
if (outStreamSpec->Close() != S_OK)
{
fprintf(stderr, "File closing error");
return 1;
}
}
return 0;
}
int main(int n, const char *args[])
{
try { return main2(n, args); }
catch(const char *s)
{
fprintf(stderr, "\nError: %s\n", s);
return 1;
}
catch(...)
{
fprintf(stderr, "\nError\n");
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -