Commits

Andreas Tscharner  committed f463ba2 Merge

Merge with laptop development: bzip compression

  • Participants
  • Parent commits c6b409b, b533a7e

Comments (0)

Files changed (11)

File configure.in

 #      configure.in
 #
-#      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+#      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
 #
 #      This program is free software; you can redistribute it and/or modify
 #      it under the terms of the GNU Lesser General Public License as
 #
 # Check for libraries
 AC_CHECK_LIB(z, zlibVersion, zLib_ok=yes, zLib_ok=no)
+AC_CHECK_LIB(bz2, BZ2_bzlibVersion, bz2Lib_ok=yes, bz2Lib_ok=no)
 AC_CHECK_LIB(SDL, SDL_Init, SDL_ok=yes, SDL_ok=no)
 AC_CHECK_LIB(SDL_image, IMG_Init, SDLIMAGE_ok=yes, SDLIMAGE_ok=no)
 
-.TH lrc 5 "05 January 2012" "lrc 0.6.1"
+.TH lrc 5 "18 January 2012" "lrc 0.6.1"
 
 .SH NAME
 lrc \- Linux Resource Compiler
 
 .B zLib - zlib compression
 
+.B bzip2 - bzip2 compression
+
 .SH Encryption
 The following encryption algorithms are available:
 

File src/Factories.cxx

 
 #include "strategies/NoneCompression.hxx"
 #include "strategies/zLibCompression.hxx"
+#include "strategies/bz2LibCompression.hxx"
 #include "strategies/NoneEncryption.hxx"
 
 #ifndef NO_ENCRYPTION
 		case lrc::zLibCompression:
 			return new zLibCompression();
 			break;
+		case lrc::bz2LibCompression:
+			return new bz2LibCompression();
+			break;
 
 		default:
 			break;

File src/compiler/InFileParser.cxx

 //      InFileParser.cxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
 //
 //      This program is free software; you can redistribute it and/or modify
 //      it under the terms of the GNU Lesser General Public License as
 		return lrc::zLibCompression;
 	};
 
+	if (strcmp("bzip2", p_compStr) == 0) {
+		return lrc::bz2LibCompression;
+	};
+
 	return lrc::NoneCompression;
 }
 

File src/compiler/Makefile.am

 #      Makefile.am
 #
-#      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+#      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
 #
 #      This program is free software; you can redistribute it and/or modify
 #      it under the terms of the GNU Lesser General Public License as
               ../Factories.hxx ../Factories.cxx                                          \
               ../strategies/NoneCompression.hxx ../strategies/NoneCompression.cxx        \
               ../strategies/zLibCompression.hxx ../strategies/zLibCompression.cxx        \
+              ../strategies/bz2LibCompression.hxx ../strategies/bz2LibCompression.cxx    \
               ../strategies/NoneEncryption.hxx ../strategies/NoneEncryption.cxx          \
               ../strategies/SerpentEncryption.hxx ../strategies/SerpentEncryption.cxx    \
               ParserFactory.hxx ParserFactory.cxx                                        \
 
 #
 # Library flags for main program
-lrc_LDADD = -lz -lcryptopp -ltinyxml
+lrc_LDADD = -lz -lbz2 -lcryptopp -ltinyxml
 lrc_LDFLAGS = -L../strategies/cryptopp -Ltinyxml

File src/example/OSLogos.rif

 
 <Resources>
 	<Resource id="LINUX-LOGO" encryption="Serpent" password="MySecretPassword" compression="zLib">data/tux.png</Resource>
-	<Resource id="FREEBSD-LOGO" encryption="None" compression="zLib">data/freebsd-devil.png</Resource>
+	<Resource id="FREEBSD-LOGO" encryption="None" compression="bzip2">data/freebsd-devil.png</Resource>
 	<Resource id="WINDOWS-LOGO" encryption="None" compression="zLib">data/Windows Logo.png</Resource>
-	<Resource id="APPLE-LOGO" encryption="None" compression="zLib">data/apple.png</Resource>
+	<Resource id="APPLE-LOGO" encryption="None" compression="bzip2">data/apple.png</Resource>
 </Resources>

File src/example/logos.rc

 #
 # Resource ID		Resource File				Compression	Encryption
 APPLE-LOGO			"data/apple.png"			None		None
-FREEBSD-LOGO		"data/freebsd-devil.png"	None		Serpent     "@Password.txt"
-LINUX-LOGO			"data/tux.png"				None		Serpent     "MySecretPassword"
-WINDOWS-LOGO		"data/Windows Logo.png"		None		None
+FREEBSD-LOGO		"data/freebsd-devil.png"	zLib		Serpent     "@Password.txt"
+LINUX-LOGO			"data/tux.png"				bzip2		Serpent     "MySecretPassword"
+WINDOWS-LOGO		"data/Windows Logo.png"		bzip2		None

File src/include/CompressDecompress.hxx

 //      CompressDecompress.hxx
 //
-//      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+//      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
 //
 //      This program is free software; you can redistribute it and/or modify
 //      it under the terms of the GNU Lesser General Public License as
  * compression/decompression algorithms used in \c lrc
  *
  * \author Andreas Tscharner
- * \date 2011-08-11
+ * \date 2012-01-18
  */
 
 
 	 */
 	 enum CompressionType {
 		 NoneCompression,              //!< No compression at all
-		 zLibCompression               //!< zlib compression
+		 zLibCompression,              //!< zlib compression
+		 bz2LibCompression             //!> bzip2 compression
 	 };
 
 

File src/lib/Makefile.am

 #      Makefile.am
 #
-#      Copyright 2011 Andreas Tscharner <andy@vis.ethz.ch>
+#      Copyright 2011, 2012 Andreas Tscharner <andy@vis.ethz.ch>
 #
 #      This program is free software; you can redistribute it and/or modify
 #      it under the terms of the GNU Lesser General Public License as
                     ../Factories.hxx ../Factories.cxx                                       \
                     ../strategies/NoneCompression.hxx ../strategies/NoneCompression.cxx     \
                     ../strategies/zLibCompression.hxx ../strategies/zLibCompression.cxx     \
+                    ../strategies/bz2LibCompression.hxx ../strategies/bz2LibCompression.cxx \
                     ../strategies/NoneEncryption.hxx ../strategies/NoneEncryption.cxx       \
                     ../strategies/SerpentEncryption.hxx ../strategies/SerpentEncryption.cxx \
                     ../StatusCodes.hxx
 
 #
 # Library flags for main program
-liblrc_la_LIBADD = -lz -lcryptopp
+liblrc_la_LIBADD = -lz -lbz2 -lcryptopp
 liblrc_la_LDFLAGS = -L../strategies/cryptopp -version-info 0:0:0

File src/strategies/bz2LibCompression.cxx

+//      bz2LibCompression.cxx
+//
+//      Copyright 2012 Andreas Tscharner <andy@vis.ethz.ch>
+//
+//      This program is free software; you can redistribute it and/or modify
+//      it under the terms of the GNU Lesser General Public License as
+//      published by the Free Software Foundation; either version 3 of the
+//      License, or (at your option) any later version.
+//
+//      This program is distributed in the hope that it will be useful,
+//      but WITHOUT ANY WARRANTY; without even the implied warranty of
+//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//      GNU General Public License for more details.
+//
+//      You should have received a copy of the GNU Lesser General Public
+//      License along with this program; if not, write to the Free Software
+//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+//      MA 02110-1301, USA.
+
+
+#include <cmath>
+#include <cstring>
+#include <bzlib.h>
+#include "../StatusCodes.hxx"
+#include "bz2LibCompression.hxx"
+
+
+int bz2LibCompression::compress(const unsigned char *p_decompData, size_t p_decompSize,
+                                unsigned char **p_compData, size_t &p_compSize)
+{
+	const double GUARANTEED_SIZE_FACTOR = 1.01;
+	const int ADDITIONAL_FIX_SIZE = 600;
+
+	unsigned int maxCompressSize;
+	int retVal;
+	char *tmpBuf;
+
+
+	maxCompressSize = round(GUARANTEED_SIZE_FACTOR*(double)p_decompSize) + ADDITIONAL_FIX_SIZE;
+	tmpBuf = new char[maxCompressSize];
+
+	retVal = BZ2_bzBuffToBuffCompress(tmpBuf, &maxCompressSize,
+	                                  (char *)p_decompData, (unsigned int)p_decompSize,
+	                                  9, 0, 0);
+	switch (retVal) {
+		case BZ_CONFIG_ERROR:
+		case BZ_PARAM_ERROR:
+		case BZ_OUTBUFF_FULL:
+			delete[] tmpBuf;
+			*p_compData = nullptr;
+			return ERROR_COMPRESSION_COMPRESS;
+			break;
+
+		case BZ_MEM_ERROR:
+			delete[] tmpBuf;
+			*p_compData = nullptr;
+			return ERROR_NOT_ENOUGH_MEMORY;
+			break;
+
+		case BZ_OK:
+			p_compSize = maxCompressSize;
+			*p_compData = new unsigned char[p_compSize];
+			memcpy(*p_compData, tmpBuf, p_compSize);
+			delete[] tmpBuf;
+			return NO_ERROR;
+			break;
+	};
+
+	delete[] *p_compData;
+	*p_compData = nullptr;
+	return ERROR_COMPRESSION_COMPRESS;
+}
+
+int bz2LibCompression::decompress(const unsigned char *p_compData, size_t p_compSize,
+                                  unsigned char **p_decompData, size_t &p_decompSize)
+{
+	const int ENLARGE_FACTOR = 10;
+	unsigned int destBufSize;
+	int retVal = BZ_OUTBUFF_FULL;
+	char *tmpBuf;
+
+
+	destBufSize = p_compSize;
+	while (retVal == BZ_OUTBUFF_FULL) {
+		destBufSize = ENLARGE_FACTOR * destBufSize;
+		tmpBuf = new char[destBufSize];
+
+		retVal = BZ2_bzBuffToBuffDecompress(tmpBuf, &destBufSize,
+		                                    (char *)p_compData, (unsigned int)p_compSize,
+		                                    0, 0);
+		switch (retVal) {
+			case BZ_CONFIG_ERROR:
+			case BZ_PARAM_ERROR:
+			case BZ_DATA_ERROR:
+			case BZ_DATA_ERROR_MAGIC:
+			case BZ_UNEXPECTED_EOF:
+				delete[] tmpBuf;
+				*p_decompData = nullptr;
+				return ERROR_COMPRESSION_DECOMPRESS;
+				break;
+
+			case BZ_MEM_ERROR:
+				delete[] tmpBuf;
+				*p_decompData = nullptr;
+				return ERROR_NOT_ENOUGH_MEMORY;
+				break;
+
+			case BZ_OK:
+				*p_decompData = new unsigned char[destBufSize];
+				memcpy(*p_decompData, tmpBuf, destBufSize);
+				delete[] tmpBuf,
+				p_decompSize = destBufSize;
+				return NO_ERROR;
+				break;
+		};
+	};
+
+	delete[] tmpBuf;
+	*p_decompData = nullptr;
+	return ERROR_COMPRESSION_DECOMPRESS;
+}

File src/strategies/bz2LibCompression.hxx

+//      bz2LibCompression.hxx
+//
+//      Copyright 2012 Andreas Tscharner <andy@vis.ethz.ch>
+//
+//      This program is free software; you can redistribute it and/or modify
+//      it under the terms of the GNU Lesser General Public License as
+//      published by the Free Software Foundation; either version 3 of the
+//      License, or (at your option) any later version.
+//
+//      This program is distributed in the hope that it will be useful,
+//      but WITHOUT ANY WARRANTY; without even the implied warranty of
+//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//      GNU General Public License for more details.
+//
+//      You should have received a copy of the GNU Lesser General Public
+//      License along with this program; if not, write to the Free Software
+//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+//      MA 02110-1301, USA.
+
+
+/*! \file bz2LibCompression.hxx
+ *
+ * This file contains the declaration of the bz2Lib compression class. This
+ * class is used to compress and decompress teh resource data using the bzip2
+ * algorithm.
+ *
+ * \author Andreas Tscharner
+ * \date 2012-01-18
+ */
+
+
+#ifndef __BZ2LIBCOMPRESSION_HXX__
+#define __BZ2LIBCOMPRESSION_HXX__
+
+
+#include "../include/CompressDecompress.hxx"
+
+/*! \class bz2LibCompression
+ *
+ * \brief Compression class that uses the \em bzip2 algorithm
+ *
+ * This class uses the bzip2 algorithm for compression and decompression of
+ * the resource data.
+ */
+class bz2LibCompression : public lrc::CompressDecompress
+{
+	public:
+		/*! \brief bzip2 compression
+		 *
+		 * This method compresses the given data using the bzip2 algorithm
+		 */
+		int compress(const unsigned char *, size_t, unsigned char **, size_t &);
+		/*! \brief bzip2 decompression
+		 *
+		 * This method decompresses the given data using the bzip2 algorithm
+		 */
+		int decompress(const unsigned char *, size_t, unsigned char **, size_t &);
+};
+
+
+#endif /* __BZ2LIBCOMPRESSION_HXX__ */