Ejemplo n.º 1
0
    /**
     *  Decompress a character buffer.
     *
     *  inbuf    - the compressed buffer.
     *  inbufsz  - size of the compressed buffer.
     *  outbufsz - suggested initial size for the output buffer,
     *             set to inbufsz if 0.
     *  outbuf   - the decompressed buffer, use free_gzip to release it.
     */
    void decode_gzip( const char *inbuf, size_t inbufsz, size_t &outbufsz,
                      char *&outbuf )
    {
        Compress compressor;
        int insize = (int) inbufsz;
        int outsize = (int) outbufsz;

        /*  Allocate the initial quantity of memory suggested. */
        outbuf = (char *) malloc( outbufsz ? outbufsz : inbufsz );
        compressor.decompress( inbuf, insize, outbuf, outsize,
                               Compress::GZIP_COMPRESS );
        outbufsz = (size_t) outsize;
    }
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
    if (argc <= 5 && argc >= 2){

        QString fileName = "";
        QString compressedFileName = "";
        QString directory = "";


        if (argc == 5) {
            if (argv[1][0] == '-' && argv[1][1] == 'c' && argv[3][0] == '-' && argv && argv[3][1] == 'o'){
                fileName = argv[2];
                compressedFileName = argv[4];
                Compress c = Compress(fileName, compressedFileName, directory);
                c.compress();
                c.~Compress();
            } else {
                manHuffman();
            }
        } else if (argc == 4){
            if (argv[2][0] == '-' && argv[2][1] == 'd'){
                fileName = argv[1];
                directory = argv[3];
                Compress c = Compress(fileName, compressedFileName, directory);
                c.uncompress();
                c.~Compress();
            } else {
                manHuffman();
            }
        } else if (argc == 3){
            if (argv[1][0] == '-' && argv[1][1] == 'c'){
                fileName = argv[2];
                compressedFileName.append("out.huff");
                Compress c = Compress(fileName, compressedFileName, directory);
                c.compress();
                c.~Compress();
            } else {
                manHuffman();
            }
        } else if (argc == 2){
            fileName = argv[1];
            Compress c = Compress(fileName, compressedFileName, directory);
            c.uncompress();
            c.~Compress();
        } else {
            manHuffman();
        }


    } else {
        manHuffman();
    }

}
Ejemplo n.º 3
0
void Pk::go(int argc, char *args[], std::string& ext) 
{
	Compress *mainCompressor;
	Compress *auxiliaryCompressor = 0;

	unsigned char n = 4;
	unsigned char p = 10;
	unsigned char c = 4;
	
	findMode(args);

	if(mode == 0 && argc == 2)
	{
		n = atoi(args[1]);
		mainCompressor = new RLE_Compress(&out, n);
	}
	else if(mode == 1 && argc == 3)
	{
		p = atoi(args[1]);
		c = atoi(args[2]);
		mainCompressor = new LZSS_Compress(&out, p, c);
	}
	else if(mode == 2 && argc == 4)
	{
		n = atoi(args[1]);
		p = atoi(args[2]);
		c = atoi(args[3]);
		mainCompressor = new RLE_Compress(auxiliaryCompressor = new LZSS_Compress(&out, p, c), n);
	}
	else
	{
		cout << "Argumentos inva'lidos." << endl;
		return;
	}
	
	this->writeHeader(ext);

	mainCompressor->putHeader(out);

	if(auxiliaryCompressor)
		auxiliaryCompressor->putHeader(out);

	int b;
	while((b = in.getByte()) != EOF) 
		mainCompressor->putByte(b);

	mainCompressor->close();
	delete mainCompressor;

	if(auxiliaryCompressor)
		delete auxiliaryCompressor;

	in.close();
}
Ejemplo n.º 4
0
/*
    TODO:
    Fix the append bug
    Fix segmentation fault in FileHeaderData
    Finish and polish graphic interface
*/
int main(int argc, char *argv[]) {
    QGuiApplication app(argc,argv);
    QGuiApplication::setApplicationName("huffman");
    QCommandLineParser cliParser;
    cliParser.setApplicationDescription( QStringLiteral("huffman") );
    cliParser.addHelpOption();
    cliParser.addPositionalArgument("<file>",
                                   QCoreApplication::translate("main","Source file to compress.") );

    cliParser.addPositionalArgument("<path>",
                                   QCoreApplication::translate("main","Destination for output file.") );

    QCommandLineOption compress("c",QCoreApplication::translate("main",
                                "huffman -c <file>. Compresses <file> and saves as <file>.huff.") );

    QCommandLineOption compressOutput("o",QCoreApplication::translate("main",
                                      "huffman -c <file> -o <name>. Compresses <file> and saves as <name>."));

    QCommandLineOption decompressAtPath("d",QCoreApplication::translate("main",
                                        "huffman <file> -d <path>. Decompresses <file> and saves at <path>."));

    QCommandLineOption goGui("gui",QCoreApplication::translate("main",
                            "huffman --gui. Runs huffman in gui mode."));

    QCommandLineOption dbg("dbg",QCoreApplication::translate("main",
                            "huffman --dbg. Debug."));


    cliParser.addOption(compress);
    cliParser.addOption(compressOutput);
    cliParser.addOption(decompressAtPath);
    cliParser.addOption(goGui);
    cliParser.addOption(dbg);
    cliParser.process(app);
    bool ccompress     = cliParser.isSet(compress),
            showGui       = cliParser.isSet(goGui),
            ocompress     = cliParser.isSet(compressOutput),
            ddecompress   = cliParser.isSet(decompressAtPath);
    if ( cliParser.isSet (dbg) )
    {
        QFile file( cliParser.positionalArguments ().at(0) );
        if ( file.open (QIODevice::ReadOnly) )
        {
            FileHeaderData h;
            h.readFromFile (file);
            qDebug() << ( h.getStatus () == HeaderStatus::READY ? "Ok!" : "Not Ok." );
            file.close ();
        }
        return 0;
    }
    if ( showGui ) {// [GUI]
//        QGuiApplication guiapp(argc,argv);
        QQmlApplicationEngine engine;
        FileUtils fileUtils;
        engine.rootContext ()->setContextProperty ("FileUtils",&fileUtils);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return app.exec ();
    }
    if ( ccompress ) {
        if( ocompress ) {
            //Compress to path or file
            if ( cliParser.positionalArguments().count() == 2) {
                QString file = cliParser.positionalArguments().at(0);
                QString filename = cliParser.positionalArguments().at(1);
                Compress cfile;
                cfile.compressFile (file,filename);
            } else {
                qDebug() << "Invalid use. Check -h for help.";
            }
        }
        else {
            //            Compress file and save as filename
            QString file = cliParser.positionalArguments().at(0);
            Compress cfile;
            QString gambiarra;
            cfile.compressFile (file,gambiarra);
        }
        // c compress is not set, so we decompress
    } else {
        //if -d is set, decompress to "thing"
        if ( ddecompress ) {
            if ( cliParser.positionalArguments().count() == 2) {
                QString file = cliParser.positionalArguments().at(0);
                QString path = cliParser.positionalArguments().at(1);
                QDir test(path);
                if ( test.exists () ) {
                    Decompress srcfile;
                    srcfile.decompressFile (file,path);
                } else {
                    qDebug() << test.absolutePath () << "\n doesn't exists";
                }
            } else {
                qDebug() << "Invalid use. Check -h for help.";
            }
        // -d is not set, decompress to current path
        } else {
            if ( cliParser.positionalArguments().count() == 1 ) {
                QString file = cliParser.positionalArguments().at(0);
                Decompress srcfile;
                srcfile.decompressFile (file);
            }
            else {
                qDebug () << "Invalid use. huffman -h for help.";
            }
        }
    }
    return 0;
}
void Keep::execute(Compress& c, std::istream& input)
{
	c.addFileRaw(input, _hdr, _hdr.getFileName());
}
Ejemplo n.º 6
0
void Rename::execute(Compress& c, std::istream& input)
{
	c.addFileRaw(input, _hdr, _newZipEntryName);
}
Ejemplo n.º 7
0
/*
 * Given a URL pointing to preview data (FITS image or tab table data),
 * request the data from the server and return 0 if all is OK.  On
 * return, if there were no errors, the "ctype" argument is set to the
 * Content-type of the result to indicate the type of data. The data is
 * automatically decompressed if needed (if the content-type is
 * recognized).  The "tmpfile()" method gives the name of the file
 * containing the results on success.
 */
int AstroCatalog::getPreview(const char* url, char*& ctype)
{
    // we need to use a new file, since the old one may be still in use
    // (even after it was deleted, since it may still be open and/or mmapped)
    newTempFile();

    // open the tmp file
    std::ofstream f(tmpfile_);
    if (!f)
        return sys_error("could not open file for writing: ", tmpfile_);

    if (http_.get(url, f) != 0) {
        unlink(tmpfile_);
        return ERROR;
    }
    f.close();

    // check the Content-type of the return data to determine whether it
    // needs to be decompressed and if so, how...
    ctype = http_.content_type();
    if (!ctype)
        ctype = (char *)"";

    if (strcmp(ctype, "text/html") == 0) {
        // most likely an HTML formatted server error message
        std::ifstream is(tmpfile_);
        unlink(tmpfile_);
        return http_.html_error(is);
    }

    // for now, assume uncompressed table if the Content-type is not recognized
    char* t = ctype;
    int is_image = 0;
    if (strncmp(ctype, "image/", 6) == 0) {
        t = ctype+6;
        is_image++;
    }
    else if (strncmp(ctype, "text/", 5) == 0) {
        t = ctype+5;
    }
    else {
        // unknown content type, check if it might be a FITS file
        Mem m(tmpfile_);
        if (m.status() == 0
                && m.size() >= 2880
                && strncmp((const char*)m.ptr(), "SIMPLE", 6) == 0) {
            ctype = (char *)"image/x-fits";   // assume FITS file
            is_image++;
        } else {
            ctype = (char *)"text/x-starbase"; // assume catalog data
        }
        return 0;
    }

    // In some cases the Content-type only gives the general type and
    // we need to check the Content-Encoding also. For example "file.fits.gz"
    // might have a Content-type of image/x-fits and Content-Encoding of
    // x-gzip
    char* ce = http_.content_encoding();
    if (is_image && strcmp(t, "x-fits") == 0 && ce != NULL) {
        if (strcmp(ce, "x-gzip") == 0) {
            ctype = (char *)"image/x-gfits";
            t = ctype+6;
        }
        else if (strcmp(ce, "x-compress") == 0) {
            ctype = (char *)"image/x-cfits";
            t = ctype+6;
        }
    }

    // pure FITS or starbase table ?
    if (strcmp(t, "x-fits") == 0
            || strcmp(t, "fits" ) == 0
            || strcmp(t, "fits") == 0
            || strcmp(t, "x-starbase") == 0
            || strcmp(t, "plain") == 0
            || strcmp(t, "tab-separated-values") == 0) {
        return 0;	// not compressed, just return filename
    }

    Compress::CompressType type = Compress::NO_COMPRESS;
    if (strcmp(t, "x-hfits") == 0) {
        type = Compress::H_COMPRESS; // Hcompressed FITS file
    }
    else if (strcmp(t, "x-gfits") == 0 || strcmp(t, "x-gstarbase") == 0) {
        type = Compress::GZIP_COMPRESS; // GZIPed FITS or tab table
    }
    else if (strcmp(t, "x-cfits") == 0 || strcmp(t, "x-cstarbase") == 0) {
        type = Compress::UNIX_COMPRESS; // UNIX Compressed FITS ir tab table
    }
    else if (strcmp(t, "x-sfits") == 0) { // Compressed FITS file (Stark)
        // type = Compress::S_COMPRESS;
        unlink(tmpfile_);
        return error("x-sfits compression (Stark) not supported");
    }
    else {
        unlink(tmpfile_);
        return error("unknown preview data Content-type: ", ctype);
    }

    // do the decompression
    FILE* feedback = http_.feedback();
    if (feedback) {
        fprintf(feedback, "decompressing data...\n");
        fflush(feedback);
    }

    Compress c;
    if (c.decompress(tmpfile_, type) != 0) {
        unlink(tmpfile_);
        return ERROR;
    }

    // correct Content-type after decompression
    ctype = (char*)(is_image ? "image/x-fits" : "text/x-starbase");

    // if we got here, then we have the FITS file, so return the file name
    return 0;
}
Ejemplo n.º 8
0
void Add::execute(Compress& c, std::istream& input)
{
	c.addFile(Poco::Path(_localPath), Poco::Path(_zipPath), _cm, _cl);
}
Ejemplo n.º 9
0
// Test basic interface
int test(int argc, const char *argv[])
{
	// Bad data to compress!
	char *data = (char *)malloc(DATA_SIZE);
	for(int l = 0; l < DATA_SIZE; ++l)
	{
		data[l] = l*23;
	}
	
	// parameters about compression	
	int maxOutput = Compress_MaxSizeForCompressedData(DATA_SIZE);
	TEST_THAT(maxOutput >= DATA_SIZE);

	char *compressed = (char *)malloc(maxOutput);
	int compressedSize = 0;
	
	// Do compression, in small chunks
	{
		Compress<true> compress;
		
		int in_loc = 0;
		while(!compress.OutputHasFinished())
		{
			int ins = DATA_SIZE - in_loc;
			if(ins > CHUNK_SIZE) ins = CHUNK_SIZE;
			
			if(ins == 0)
			{
				compress.FinishInput();
			}
			else
			{
				compress.Input(data + in_loc, ins);
			}
			in_loc += ins;
			
			// Get output data
			int s = 0;
			do
			{
				TEST_THAT(compressedSize < maxOutput);
				s = compress.Output(compressed + compressedSize, maxOutput - compressedSize);
				compressedSize += s;
			} while(s > 0);
		}
	}
	
	// a reasonable test, especially given the compressability of the input data.
	TEST_THAT(compressedSize < DATA_SIZE);

	// decompression
	char *decompressed = (char*)malloc(DATA_SIZE * 2);
	int decomp_size = 0;
	{	
		Compress<false> decompress;
		
		int in_loc = 0;
		while(!decompress.OutputHasFinished())
		{
			int ins = compressedSize - in_loc;
			if(ins > DECOMP_CHUNK_SIZE) ins = DECOMP_CHUNK_SIZE;
			
			if(ins == 0)
			{
				decompress.FinishInput();
			}
			else
			{
				decompress.Input(compressed + in_loc, ins);
			}
			in_loc += ins;
			
			// Get output data
			int s = 0;
			do
			{
				TEST_THAT(decomp_size <= DATA_SIZE);
				s = decompress.Output(decompressed + decomp_size, (DATA_SIZE*2) - decomp_size);
				decomp_size += s;
			} while(s > 0);
		}
	}
	
	TEST_THAT(decomp_size == DATA_SIZE);
	TEST_THAT(::memcmp(data, decompressed, DATA_SIZE) == 0);

	::free(data);
	::free(compressed);
	::free(decompressed);
	
	return test_stream();
}
Ejemplo n.º 10
0
int main( int argc, char** argv ) {
	Compress* compr;
	unsigned long ID;
	bool dummy = true;
	unsigned char* tb;
	LZParams lzp;
	FReader rfile;
	FWriter wfile;

	int ch;
	char *title = NULL;
	char *author = NULL;
	char *bmp = NULL;
	char *raw = NULL;
	char* icon = NULL;

	fixInfo fix;
	char* infile;
	bool infileWasFXE;
	UnFXE fxe;

	//

	cout << endl << "b2fxeC v0.6a-pre2 - (c) 2002-4 Jouni 'Mr.Spiv' Korhonen" << endl << endl;


  	// Check commandline & options..

	while ((ch = getopt(argc,argv,"t:a:A:b:B:ghdfF:")) != -1) {
		switch (ch) {
		case 't':	// title
			title = optarg;
			break;
		case 'a':	// Open icon file..
			author = optarg;
			break;
		case 'A':
			algo = static_cast<FXEPalgorithm>(atoi(optarg));
			break;
		case 'B':
			raw = optarg;
			break;
		case 'b':
			bmp = optarg;
			break;
		case 'g':	// Open afx/gxb file..
			FXE = false;
			break;
		case 'd':	//
			RAW = true;
			break;
		case 'f':
			fix.mhz = 132000000; 	// 132MHz
			fix.fac = 0x24001;
			break;
		case 'F':
			if (getClockSpeed( fix, atoi(optarg) ) < 0) {
				cerr << "** Error: not support MHz.." << endl;
				exit(0);
			}
			break;
		case '?':
		case 'h':
		default:
			usage(argv);
		}
	}

	if (optind >= argc) {
		usage(argv);
		return 0;
	}

	// default name..
	
	if (title == NULL) { title = argv[optind]; }


	// Get the compressor..

	switch (algo) {
	case fxe0:
    	compr = new CompressFXE0(NBLOCKS);
    	ID = FXEP_TYPE0_ID;
		break;
	case fxe1:
		cerr << "Sorry.. No bonus!" << endl;
		exit(0);
		//cout << "compr = new CompressFXE1(NBLOCKS);" << endl;
		//compr = new CompressFXE1(NBLOCKS);
    	//ID = FXEP_TYPE1_ID;
		break;
	case fxe2:
		//cout << "compr = new CompressFXE2(NBLOCKS);" << endl;
		compr = new CompressFXE2(NBLOCKS);
    	ID = FXEP_TYPE2_ID;
		break;
	case fxe3:
		//cout << "compr = new CompressFXE2(NBLOCKS);" << endl;
		compr = new CompressFXE3(NBLOCKS);
    	ID = FXEP_TYPE3_ID;
		break;
	default:
		cerr << "** Error: unknown compression algorithm.." << endl;
		return 0;
		break;
	}

	//
	// Setup rest of LZ parameters..
	//

	CallBack cback(compressionParams[algo].windowSize);
	

	lzp.buf 			= compressionParams[algo].buffer;
	lzp.bufSize 		= compressionParams[algo].bufferSize;
	lzp.goodMatch 		= compressionParams[algo].goodMatch;
	lzp.maxMatch 		= compressionParams[algo].maxMatch;
	lzp.minMatchThres 	= compressionParams[algo].minMatchThres;
	lzp.minOffsetThres 	= compressionParams[algo].minOffsetThres;
	lzp.maxChain 		= compressionParams[algo].maxChain;
	lzp.numLazy 		= compressionParams[algo].numLazy;
	lzp.numDistBits 	= compressionParams[algo].numDistBits;
	lzp.windowSize 		= compressionParams[algo].windowSize;
	lzp.r 				= &rfile;
	lzp.c 				= &cback;

	//

	//cout << "maxmatch: " << lzp.maxMatch << endl;


	//
	// Do the size fix for incorrect files..
	// Also check for code & data section gaps
	//
	//

	int gap = -1;
	infile = argv[optind];
	infileWasFXE = false;

	if (!RAW) {
		gap = fixGXBLength(infile, infileWasFXE );

		if (gap < 0) {
			cerr << "** Error: failed to check/fix GXB length correctness.." << endl;
			return 0;
		}
	}


	// 
	// check if the "infile" is a FXE..
 	//

	if (!RAW && infileWasFXE) {
		cout << "Unpacking a FXE.." << endl;

		if (!(infile = unFXEToTemp(infile,fxe))) {
			cerr << "Unpacking the FXE failed.." << endl;
			return 0;
		}
		infileWasFXE = true;

		// Override parameters..

		title  = const_cast<char*>(fxe.getGameName());
		author = const_cast<char*>(fxe.getAuthorName());
		icon   = const_cast<char*>(fxe.getIcon());
		bmp = NULL;
		raw = NULL;
	}
	
	//
	//
	//

	compr->init(&lzp);

	// Open files..

	rfile.open(infile,IO_READ);
	wfile.open(argv[optind+1],IO_CREATE);

	if (!rfile.isOpen()) {
		delete compr;
		cerr << "** Error: failed to open file '" << infile << "'.." << endl;
		return 0;
	}
	if (!wfile.isOpen()) {
		delete compr;
		rfile.close();
		cerr << "** Error: failed to open file '" << argv[optind+1] << "'.." << endl;
		return 0;
	}
	if (infileWasFXE == true) {
		if (b2fxec_unlink(infile) < 0) {

			cout << "++ Warning: failed to delete temporary file '" << infile << "'" << endl;

		}
	}

	//
	//
	//
	// Save headers..
	//

	long origiFileSize = 0;
	long totalFileSize = 0;
	long comprFileSize = 0;
	long decomprOffset = 0;
	long n;

	rfile.seek(0,IO_SEEK_END);
	origiFileSize = rfile.tell();
	rfile.seek(0,IO_SEEK_SET);

	if (!RAW && FXE && (decomprOffset = compressionParams[algo].saveHeader(
		0,&wfile,title,author,bmp,raw,icon)) < 0) {
		wfile.close();
		rfile.close();
		delete compr;
		return 0;
	}
	if (!RAW && (totalFileSize = compressionParams[algo].saveDecompressor(&wfile)) < 0) {
		wfile.close();
		rfile.close();
		delete compr;
		return 0;
	}

	// Now we are ready to go.. first prepare the compressed
	// file header information..

	tb = buf;
	PUTL(tb,ID);
	PUTL_LE(tb,origiFileSize);
	wfile.write(buf,8);

	totalFileSize += 8;
	comprFileSize = 8;

	cout << "Crunching (0/" << origiFileSize << ")";
	cout.flush();

	//
	//
	//

	ch = 10;

	while ((n = compr->compressBlock(&lzp,dummy)) > 0) {
		unsigned short bl;
    	tb = buf;
 		GETW_LE(tb,bl);

		// Fix block size NOT to include FXEP_BLOCK_HEADER


		bl -= FXEP_BLOCK_HEADER_SIZE;
		tb = buf;
		PUTW_LE(tb,bl);

		//cerr.setf(ios::hex);
		//cerr << "Block starts: " << totalFileSize << ", " << bl <<endl;


		if (wfile.write(buf,n) != n) {
			n = -1;
			break;
		}
		comprFileSize += n;
		totalFileSize += n;

		if (--ch == 0) {
			cout << "\rCrunching (" << compr->getOriginalSize() << "/" << origiFileSize << ")";
			cout.flush();
			ch = 10;
		}
	}
	
	//
	// Finish the output file.. add EOF
	//

	if (n == 0) {
		tb = buf;
		
		// check for a proper EOF mark
		
		switch (compr->getTypeID()) {
		case FXEP_TYPE0_ID:
		case FXEP_TYPE2_ID:
		default:
			PUTW_LE(tb,FXEP_EOF);
			break;
		case FXEP_TYPE3_ID:
			PUTW_LE(tb,FXEP_EOF_NEW);
			break;
		}
		
		if (wfile.write(buf,2) != 2) {
			cerr << "** Error writing file (1).." << endl;
			rfile.close();
			wfile.close();
			delete compr;
			return 0;
		}
		comprFileSize += 2;
		totalFileSize += 2;
		
	}
	if (n < 0) {
		cerr << "** Error writing file (2).." << endl;
		rfile.close();
		wfile.close();
		delete compr;
		return 0;
	}

	// Check alignment.. must be 4
	
	if (comprFileSize & 0x03) {
		unsigned long pad = 0;
		long l = 4-(comprFileSize & 0x03);
		wfile.write(reinterpret_cast<unsigned char*>(&pad),l);
		comprFileSize += l;
		totalFileSize += l;
	}

	//
	// Fix headers..
	//

	fix.osize = origiFileSize;
	fix.csize = comprFileSize;
	fix.gap = gap;

	if (!RAW && FXE && compressionParams[algo].fixHeader(0,&wfile,totalFileSize) < 0) {
		cerr << "** Error writing file (3).." << endl;
		rfile.close();
		wfile.close();
		delete compr;
		return 0;
	}
	if (!RAW && compressionParams[algo].fixDecompressor(&wfile,decomprOffset,&fix) < 0) {
		cerr << "** Error writing file (4).." << endl;
		rfile.close();
		wfile.close();
		delete compr;
		return 0;
	}

	//
	// Calculate stuff..
	//

	double gain = 100.0 - static_cast<double>(totalFileSize)
                / static_cast<double>(origiFileSize) * 100.0;

	cout.precision(3);
	cout << "\rCrunched " << gain << "% - total " << totalFileSize << " bytes" << endl;

	//
	//
	//

	rfile.close();
	wfile.close();
	delete compr;

	//

  return 0;
}