bool LZMADecodeFile ( const char *fromFile, const char *toFile, CProgressInfo7Zip *progress ) { CMyComPtr<ISequentialInStream> inStream; CInFileStream *inStreamSpec = new CInFileStream; inStream = inStreamSpec; if ( !inStreamSpec->Open ( GetSystemString(fromFile) ) ) return false; CMyComPtr<ISequentialOutStream> outStream; COutFileStream *outStreamSpec = new COutFileStream; outStream = outStreamSpec; if ( !outStreamSpec->Create ( GetSystemString(toFile), true ) ) return false; 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 ) return false; if ( processedSize != kPropertiesSize ) return false; if ( decoderSpec->SetDecoderProperties2(properties, kPropertiesSize) != S_OK ) return false; UInt64 fileSize = 0; for (int i = 0; i < 8; i++) { Byte b; if ( inStream->Read(&b, 1, &processedSize) != S_OK ) return false; if ( processedSize != 1 ) return false; fileSize |= ((UInt64)b) << (8 * i); } if ( progress ) { progress->Init(); progress->ApprovedStart = 1 << 21; progress->SetMax ( fileSize ); } if ( decoder->Code(inStream, outStream, 0, &fileSize, progress) != S_OK ) // decoder error return false; return true; }
void decode(NCompress::NLzma::CDecoder *decoderSpec, CMyComPtr<ISequentialInStream> inStream, CMyComPtr<ISequentialOutStream> outStream) { const UInt32 kPropertiesSize = 5; Byte properties[kPropertiesSize]; UInt32 processedSize; UInt64 fileSize = 0; if (inStream->Read(properties, kPropertiesSize, &processedSize) != S_OK) throw Exception("Read error"); if (processedSize != kPropertiesSize) throw Exception("Read error"); if (decoderSpec->SetDecoderProperties2(properties, kPropertiesSize) != S_OK) throw Exception("SetDecoderProperties() error"); for (int i = 0; i < 8; i++) { Byte b; if (inStream->Read(&b, sizeof(b), &processedSize) != S_OK) throw Exception("Read error"); if (processedSize != 1) throw Exception("Read error"); fileSize |= ((UInt64)b) << (8 * i); } if (decoderSpec->Code(inStream, outStream, 0, &fileSize, 0) != S_OK) throw Exception("Decoder error"); }
unsigned char *LZMADecodeData ( unsigned char *fromData, long fromSize, long &toSize, CProgressInfo7Zip *progress ) { CMyComPtr<ISequentialInStream> inStream; CInDataStream *inStreamSpec = new CInDataStream; inStream = inStreamSpec; inStreamSpec->LoadData ( fromData, fromSize ); 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 ) return NULL; if ( processedSize != kPropertiesSize ) return NULL; if ( decoderSpec->SetDecoderProperties2(properties, kPropertiesSize) != S_OK ) return NULL; UInt64 fileSize = 0; for (int i = 0; i < 8; i++) { Byte b; if ( inStream->Read(&b, 1, &processedSize) != S_OK ) return NULL; if ( processedSize != 1 ) return NULL; fileSize |= ((UInt64)b) << (8 * i); } CMyComPtr<ISequentialOutStream> outStream; COutDataStream *outStreamSpec = new COutDataStream; outStream = outStreamSpec; outStreamSpec->Create ( fileSize ); // CProgressInfo *progressInfoSpec = new CProgressInfo; // CMyComPtr<ICompressProgressInfo> progressInfo = progress; if ( progress ) { progress->Init(); progress->ApprovedStart = 1 << 21; progress->SetMax ( fileSize ); } if ( decoder->Code(inStream, outStream, 0, &fileSize, progress) != S_OK ) // decoder error return NULL; toSize = outStreamSpec->GetCurrentSize (); return outStreamSpec->GetData (); }
static bool ReadStream(CMyComPtr<IInStream> & inStream, Int64 offset, UINT32 seekOrigin, CByteBuffer & signature) { UInt64 savedPosition = 0; UInt64 newPosition = 0; #if MY_VER_MAJOR >= 15 UInt32 readCount = signature.Size(); #else UInt32 readCount = signature.GetCapacity(); #endif unsigned char * buf = signature; if (S_OK != inStream->Seek(0, FILE_CURRENT, &savedPosition)) return false; if (S_OK != inStream->Seek(offset, seekOrigin, &newPosition)) { inStream->Seek(savedPosition, FILE_BEGIN, &newPosition); //restore pos return false; } while (readCount > 0) { UInt32 processedCount = 0; if (S_OK != inStream->Read(buf, readCount, &processedCount)) { inStream->Seek(savedPosition, FILE_BEGIN, &newPosition); //restore pos return false; } if (processedCount == 0) break; readCount -= processedCount; buf += processedCount; } inStream->Seek(savedPosition, FILE_BEGIN, &newPosition); //restore pos return readCount == 0; }
int main2(int n, const char *args[]) { fprintf(stderr, "\nLZMA 4.27 Copyright (c) 1999-2005 Igor Pavlov 2005-08-07\n"); if (n == 1) { PrintHelp(); return 0; } if (sizeof(Byte) != 1 || sizeof(UInt32) < 4 || sizeof(UInt64) < 4) { fprintf(stderr, "Unsupported base types. Edit Common/Types.h and recompile"); return 1; } UStringVector commandStrings; WriteArgumentsToStringList(n, args, commandStrings); CParser parser(kNumSwitches); try { parser.ParseStrings(kSwitchForms, commandStrings); } catch(...) { IncorrectCommand(); } if(parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs) { PrintHelp(); return 0; } const UStringVector &nonSwitchStrings = parser.NonSwitchStrings; int paramIndex = 0; if (paramIndex >= nonSwitchStrings.Size()) IncorrectCommand(); const UString &command = nonSwitchStrings[paramIndex++]; bool dictionaryIsDefined = false; UInt32 dictionary = 1 << 21; if(parser[NKey::kDictionary].ThereIs) { UInt32 dicLog; if (!GetNumber(parser[NKey::kDictionary].PostStrings[0], dicLog)) IncorrectCommand(); dictionary = 1 << dicLog; dictionaryIsDefined = true; } UString mf = L"BT4"; if (parser[NKey::kMatchFinder].ThereIs) mf = parser[NKey::kMatchFinder].PostStrings[0]; if (command.CompareNoCase(L"b") == 0) { const UInt32 kNumDefaultItereations = 10; UInt32 numIterations = kNumDefaultItereations; { if (paramIndex < nonSwitchStrings.Size()) if (!GetNumber(nonSwitchStrings[paramIndex++], numIterations)) numIterations = kNumDefaultItereations; } return LzmaBenchmark(stderr, numIterations, dictionary, mf.CompareNoCase(L"BT4") == 0); } bool encodeMode = false; if (command.CompareNoCase(L"e") == 0) encodeMode = true; else if (command.CompareNoCase(L"d") == 0) encodeMode = false; else IncorrectCommand(); bool stdInMode = parser[NKey::kStdIn].ThereIs; bool stdOutMode = parser[NKey::kStdOut].ThereIs; CMyComPtr<ISequentialInStream> inStream; CInFileStream *inStreamSpec = 0; if (stdInMode) { inStream = new CStdInFileStream; MY_SET_BINARY_MODE(stdin); } else { if (paramIndex >= nonSwitchStrings.Size()) IncorrectCommand(); const UString &inputName = nonSwitchStrings[paramIndex++]; inStreamSpec = new CInFileStream; inStream = inStreamSpec; if (!inStreamSpec->Open(GetSystemString(inputName))) { fprintf(stderr, "\nError: can not open input file %s\n", (const char *)GetOemString(inputName)); return 1; } } CMyComPtr<ISequentialOutStream> outStream; if (stdOutMode) { outStream = new CStdOutFileStream; MY_SET_BINARY_MODE(stdout); } else { if (paramIndex >= nonSwitchStrings.Size()) IncorrectCommand(); const UString &outputName = nonSwitchStrings[paramIndex++]; COutFileStream *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 (inStream->Read(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, 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 (outStream->Write(outBuffer, (UInt32)outSizeProcessed, &processedSize) != S_OK) throw "Can not write"; 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 = 2; UInt32 numFastBytes = 128; 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(); 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 }; const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]); /* NWindows::NCOM::CPropVariant properties[kNumProps]; properties[0] = UInt32(dictionary); properties[1] = UInt32(posStateBits); properties[2] = UInt32(litContextBits); properties[3] = UInt32(litPosBits); properties[4] = UInt32(algorithm); properties[5] = UInt32(numFastBytes); properties[6] = mf; properties[7] = eos; */ PROPVARIANT properties[kNumProps]; 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; if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != 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, sizeof(b), 0) != S_OK) { fprintf(stderr, "Write error"); 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 {