int SFTWorker::doDecompression(const char* buff, uint32 buffSize, bool endFile) { if (!m_pBzs) { gcException e(ERR_BZ2, 0, "Bzip2 handle was nullptr"); return reportError(BZ_STREAM_END, e); } m_pBzs->write(buff, buffSize, endFile); try { m_pBzs->doWork(); } catch (gcException &e) { return reportError(BZ_STREAM_END, e); } size_t outBuffSize = m_pBzs->getReadSize(); if (outBuffSize == 0) return m_pBzs->getLastStatus(); AutoDelete<char> outBuff(new char[outBuffSize]); m_pBzs->read(outBuff, outBuffSize); int32 res = doWrite(outBuff, outBuffSize); if (res == BZ_OK) return m_pBzs->getLastStatus(); return res; }
/** * Reads and merges sfd (wfd) files together. */ int sfdmerger2( int argc, char* argv[] ) { // COH to get BT and handle arguments CommandlineOptionHandler coh( argc, argv, 2 ); coh.setSummary( "Merges any number of sfd files together " "into one. But the result must form a square." ); coh.setTailHelp( "outSFDfile inSFDfile+" ); int wfdVersion = 0; // Only version 0 supported by all sfd readers 20081121 bool testRun = false; char* name; coh.addOption( "-w", "--wfd-version", CommandlineOptionHandler::integerVal, 1, &wfdVersion, "0", "Set the sfd version to save, default 0. Only version 0 " "supported by any reader but this."); coh.addOption( "-t", "--test", CommandlineOptionHandler::presentVal, 1, &testRun, "F", "If to only test to load the sfd files not make any output, " "default false" ); coh.addOption( "-n", "--name", CommandlineOptionHandler::stringVal, 1, &name, "\0", "The name of the merged sfd, like 22_6_3 for a 3x3 tile " "merge of x 66-68 and y 18-20."); if ( !coh.parse() ) { coh.printHelp( cout ); return 1; } MC2String outFile( coh.getTail( 0 ) ); SFDFile outSfd( name, false/*debug, overwritten by load*/ ); uint32 totalBuffSize = 0; int res = 0; // For all the input files for ( int i = 1/*0 is out file name*/; i < coh.getTailLength() && res == 0 ; ++i ) { // Read and merge MC2String inFile( coh.getTail( i ) ); mc2dbg << "Attempting to read " << inFile << endl; vector<byte> buff; int fres = File::readFile( inFile.c_str(), buff ); if ( fres < 0 ) { res = 2; } else { mc2dbg << "Attempting to load " << inFile << endl; auto_ptr<BitBuffer> bitBuff( new BitBuffer( &buff.front(), buff.size() ) ); SFDFile sfd( inFile.c_str(), false/*debug, overwritten by load*/ ); sfd.load( *bitBuff ); // Size calculation! (Add all input file sizes and use) totalBuffSize += buff.size(); if ( false && !testRun ) { auto_ptr<BitBuffer> outBuff( new BitBuffer( buff.size() ) ); sfd.save( *outBuff, wfdVersion ); MC2String saveFile( inFile + ".test" ); int fres = File::writeFile( saveFile.c_str(), outBuff->getBufferAddress(), outBuff->getCurrentOffset() ); if ( fres < 0 ) { mc2dbg << "Failed to save saved copy of sfd " << fres << endl; res = 2; } else { mc2dbg << "Saved copy of sfd " << saveFile << " size " << fres << " bytes" << endl; } } // End if to save each input as .test if ( i == 1 ) { // Set first as out outSfd.assign( sfd ); // Set the name outSfd.setName( name ); } else { if ( !outSfd.merge( sfd ) ) { mc2dbg << "Failed to merge " << inFile << " into all buffers" << endl; res = false; } } } } // End for all input files if ( res == 0 && !testRun ) { auto_ptr<BitBuffer> outBuff( new BitBuffer( totalBuffSize * (wfdVersion > 0 ? 2 : 1 ) ) ); outSfd.save( *outBuff, wfdVersion ); int fres = File::writeFile( outFile.c_str(), outBuff->getBufferAddress(), outBuff->getCurrentOffset() ); if ( fres < 0 ) { mc2dbg << "Failed to save merged sfd " << fres << endl; res = 2; } else { mc2dbg << "Saved merged sfd " << outFile << " size " << fres << " bytes" << endl; } } coh.deleteTheStrings(); return res; }