Ejemplo n.º 1
0
int recv_comp(int s, unsigned char **data, int *size, int flags) {

unsigned char      *compressed;

int                size_comp;

/*****************************************************************************
*                                                                            *
*  Receive the compressed data preceded by its size.                         *
*                                                                            *
*****************************************************************************/

if (recv(s, (char *)&size_comp, sizeof(int), flags) != sizeof(int))
   return -1;

if ((compressed = (unsigned char *)malloc(size_comp)) == NULL)
   return -1;

if (recv(s, (char *)compressed, size_comp, flags) != size_comp) {

   free(compressed);
   return -1;

}

/*****************************************************************************
*                                                                            *
*  Uncompress the data.                                                      *
*                                                                            *
*****************************************************************************/

if ((*size = huffman_uncompress(compressed, data)) < 0)
   return -1;

/*****************************************************************************
*                                                                            *
*  Free the buffer of compressed data.                                       *
*                                                                            *
*****************************************************************************/

free(compressed);

return 0;

}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
#if defined(_WIN32) // && defined(_DEBUG)
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif


	if (argc != 4)
	{
		std::cerr << "Invalid number of arguments." << std::endl;
		std::cerr << "Usage: Huffman.exe -c/-u <infile> <outfile>" << std::endl;

		return 1;
	}

	std::ifstream in_file(argv[2], std::ios::in | std::ios::binary);
	if (!in_file)
	{
		std::cerr << "Couldn't open " << argv[2] << std::endl;
		return 2;
	}

	std::ofstream out_file(argv[3], std::ios::out | std::ios::binary | std::ios::trunc);
	if (!out_file)
	{
		std::cerr << "Couldn't open " << argv[3] << std::endl;
		return 3;
	}

	std::string arg(argv[1]);

	if (arg == "-c")
	{
		OFileBitstream stream(out_file);
		std::istreambuf_iterator<char> in_iter(in_file);

		std::cerr << "Construindo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		std::ifstream::pos_type size = in_file.tellg();
		in_file.seekg(0);
		std::cerr << "Comprimindo arquivo..." << std::endl;
		huffman_compress(tree, stream, in_iter, std::istreambuf_iterator<char>(), size);
		delete tree;
	} 
	else if (arg == "-u")
	{
		in_file.seekg(0, std::ios::end);
		unsigned long long size = in_file.tellg();
		in_file.seekg(0);

		IFileBitstream istream(in_file);
		std::cerr << "Lendo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = readNode(istream);
		size -= in_file.tellg();
		std::cerr << "Descomprimindo arquivo..." << std::endl;
		huffman_uncompress(istream, out_file, tree, size);
		delete tree;
	}
	else if (arg == "--make-tree")
	{
		std::istreambuf_iterator<char> in_iter(in_file);

		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		print_huffman_tree(tree, out_file);
		delete tree;
	}
	else if (arg == "--read-tree")
	{
		IFileBitstream istream(in_file);
		Dictionary<unsigned char>* tree = readNode(istream);
		print_huffman_tree(tree, out_file);
		delete tree;
	}

	out_file.close();
	in_file.close();


	return 0;
}
Ejemplo n.º 3
0
Archivo: ex-1.c Proyecto: whatot/ma_c
int main(int argc, char **argv)
{

	FILE *fp;

	unsigned char original[DATSIZ], *compressed, *restored;

	int csize, osize, rsize, c, i;

/*****************************************************************************
*                                                                            *
*  Read some data to compress.                                               *
*                                                                            *
*****************************************************************************/

	if ((fp = fopen("sample.txt", "r")) == NULL)
		return 1;

	i = 0;

	while ((c = getc(fp)) != EOF && i < DATSIZ) {

		original[i] = c;
		i++;

	}

	osize = i;
	fclose(fp);

/*****************************************************************************
*                                                                            *
*  Compress the data using Huffman coding.                                   *
*                                                                            *
*****************************************************************************/

	fprintf(stdout, "Compressing with Huffman coding\n");
	fprintf(stdout, "Compressing...");

	if ((csize = huffman_compress(original, &compressed, osize)) < 0) {

		fprintf(stdout, "\n");
		return 1;

	}

	fprintf(stdout, "Done\n");
	fprintf(stdout, "Uncompressing...");

	if ((rsize = huffman_uncompress(compressed, &restored)) < 0) {

		fprintf(stdout, "\n");
		free(compressed);
		return 1;

	}

	fprintf(stdout, "Done\n");
	fprintf(stdout, "osize=%d, csize=%d, rsize=%d\n", osize, csize, rsize);

	if (rsize != osize) {

		fprintf(stdout, "Data was not properly restored\n");
		free(compressed);
		free(restored);
		return 1;

	}

	else {

		for (i = 0; i < rsize; i++) {

			if (original[i] != restored[i]) {

				fprintf(stdout,
					"Data was not properly restored\n");

				if (isgraph(original[i]))
					fprintf(stdout, "original[%d]=\"%c\"\n",
						i, original[i]);
				else
					fprintf(stdout, "original[%d]=0x%02x\n",
						i, original[i]);

				if (isgraph(restored[i]))
					fprintf(stdout, "restored[%d]=\"%c\"\n",
						i, restored[i]);
				else
					fprintf(stdout, "restored[%d]=0x%02x\n",
						i, restored[i]);

				free(compressed);
				free(restored);
				return 1;

			}

		}

	}

	fprintf(stdout, "Data was restored OK\n");

	free(compressed);
	free(restored);

/*****************************************************************************
*                                                                            *
*  Compress some data using LZ77.                                            *
*                                                                            *
*****************************************************************************/

	fprintf(stdout, "Compressing with LZ77\n");
	fprintf(stdout, "Compressing...");

	if ((csize = lz77_compress(original, &compressed, osize)) < 0)
		return 1;

	fprintf(stdout, "Done\n");
	fprintf(stdout, "Uncompressing...");

	if ((rsize = lz77_uncompress(compressed, &restored)) < 0) {

		fprintf(stdout, "\n");
		free(compressed);
		return 1;

	}

	fprintf(stdout, "Done\n");
	fprintf(stdout, "osize=%d, csize=%d, rsize=%d\n", osize, csize, rsize);

	if (rsize != osize) {

		fprintf(stdout, "Data was not properly restored\n");
		free(compressed);
		free(restored);
		return 1;

	}

	else {

		for (i = 0; i < rsize; i++) {

			if (original[i] != restored[i]) {

				fprintf(stdout,
					"Data was not properly restored\n");

				if (isgraph(original[i]))
					fprintf(stdout, "original[%d]=\"%c\"\n",
						i, original[i]);
				else
					fprintf(stdout, "original[%d]=0x%02x\n",
						i, original[i]);

				if (isgraph(restored[i]))
					fprintf(stdout, "restored[%d]=\"%c\"\n",
						i, restored[i]);
				else
					fprintf(stdout, "restored[%d]=0x%02x\n",
						i, restored[i]);

				free(compressed);
				free(restored);
				return 1;

			}

		}

	}

	fprintf(stdout, "Data was restored OK\n");

	free(compressed);
	free(restored);

	return 0;

}