[sword-svn] r2850 - in trunk: include src/modules/common

chrislit at crosswire.org chrislit at crosswire.org
Tue Jul 2 02:57:20 MST 2013


Author: chrislit
Date: 2013-07-02 02:57:20 -0700 (Tue, 02 Jul 2013)
New Revision: 2850

Added:
   trunk/include/bz2comprs .h
   trunk/include/xzcomprs.h
   trunk/src/modules/common/bz2comprs.cpp
   trunk/src/modules/common/xzcomprs.cpp
Modified:
   trunk/include/zipcomprs.h
Log:
added skeletal code for bzip2 & xz compression support (currently just clones of the gzip compression class)

Added: trunk/include/bz2comprs .h
===================================================================
--- trunk/include/bz2comprs .h	                        (rev 0)
+++ trunk/include/bz2comprs .h	2013-07-02 09:57:20 UTC (rev 2850)
@@ -0,0 +1,45 @@
+/******************************************************************************
+ *
+ *  bz2comprs.h -	Bzip2Compress, a driver class that provides bzip2
+ *			compression (Burrows–Wheeler with Huffman coding)
+ *
+ * $Id$
+ *
+ * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
+ *	CrossWire Bible Society
+ *	P. O. Box 2528
+ *	Tempe, AZ  85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * 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.
+ *
+ */
+
+#ifndef BZ2COMPRS_H
+#define BZ2COMPRS_H
+
+#include <swcomprs.h>
+
+#include <defs.h>
+
+SWORD_NAMESPACE_START
+
+class SWDLLEXPORT Bzip2Compress : public SWCompress {
+
+protected:
+public:
+	Bzip2Compress();
+	virtual ~Bzip2Compress();
+
+	virtual void Encode(void);
+	virtual void Decode(void);
+};
+
+SWORD_NAMESPACE_END
+#endif


Property changes on: trunk/include/bz2comprs .h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: trunk/include/xzcomprs.h
===================================================================
--- trunk/include/xzcomprs.h	                        (rev 0)
+++ trunk/include/xzcomprs.h	2013-07-02 09:57:20 UTC (rev 2850)
@@ -0,0 +1,45 @@
+/******************************************************************************
+ *
+ *  xzcomprs.h -	XzCompress, a driver class that provides xz (LZMA2)
+ *			compression
+ *
+ * $Id$
+ *
+ * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org)
+ *	CrossWire Bible Society
+ *	P. O. Box 2528
+ *	Tempe, AZ  85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * 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.
+ *
+ */
+
+#ifndef XZCOMPRS_H
+#define XZCOMPRS_H
+
+#include <swcomprs.h>
+
+#include <defs.h>
+
+SWORD_NAMESPACE_START
+
+class SWDLLEXPORT XzCompress : public SWCompress {
+
+protected:
+public:
+	XzCompress();
+	virtual ~XzCompress();
+
+	virtual void Encode(void);
+	virtual void Decode(void);
+};
+
+SWORD_NAMESPACE_END
+#endif


Property changes on: trunk/include/xzcomprs.h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Modified: trunk/include/zipcomprs.h
===================================================================
--- trunk/include/zipcomprs.h	2013-06-29 18:25:57 UTC (rev 2849)
+++ trunk/include/zipcomprs.h	2013-07-02 09:57:20 UTC (rev 2850)
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- *  swcomprs.h -	definition of Class SWCompress used for data
+ *  zipcomprs.h -	definition of Class ZipCompress used for data
  *			compression
  *
  * $Id$

Added: trunk/src/modules/common/bz2comprs.cpp
===================================================================
--- trunk/src/modules/common/bz2comprs.cpp	                        (rev 0)
+++ trunk/src/modules/common/bz2comprs.cpp	2013-07-02 09:57:20 UTC (rev 2850)
@@ -0,0 +1,181 @@
+/******************************************************************************
+ *
+ *  bz2comprs.cpp -	Bzip2Compress, a driver class that provides bzip2
+ *			compression (Burrows–Wheeler with Huffman coding)
+ *				
+ * $Id$
+ *
+ * Copyright 2013 CrossWire Bible Society (http://www.crosswire.org)
+ *	CrossWire Bible Society
+ *	P. O. Box 2528
+ *	Tempe, AZ  85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * 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.
+ *
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <bzcomprs.h>
+#include <zlib.h>
+
+SWORD_NAMESPACE_START
+
+/******************************************************************************
+ * Bzip2Compress Constructor - Initializes data for instance of Bzip2Compress
+ *
+ */
+
+Bzip2Compress::Bzip2Compress() : SWCompress() {
+}
+
+
+/******************************************************************************
+ * Bzip2Compress Destructor - Cleans up instance of Bzip2Compress
+ */
+
+Bzip2Compress::~Bzip2Compress() {
+}
+
+
+/******************************************************************************
+ * Bzip2Compress::Encode - This function "encodes" the input stream into the
+ *			output stream.
+ *			The GetChars() and SendChars() functions are
+ *			used to separate this method from the actual
+ *			i/o.
+ * 		NOTE:	must set zlen for parent class to know length of
+ * 			compressed buffer.
+ */
+
+void Bzip2Compress::Encode(void)
+{
+/*
+ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
+                                 const Bytef *source, uLong sourceLen));
+     Compresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be at least 0.1% larger than
+   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
+   compressed buffer.
+     This function can be used to compress a whole file at once if the
+   input file is mmap'ed.
+     compress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer.
+*/
+	direct = 0;	// set direction needed by parent [Get|Send]Chars()
+
+	// get buffer
+	char chunk[1024];
+	char *buf = (char *)calloc(1, 1024);
+	char *chunkbuf = buf;
+	unsigned long chunklen;
+	unsigned long len = 0;
+	while((chunklen = GetChars(chunk, 1023))) {
+		memcpy(chunkbuf, chunk, chunklen);
+		len += chunklen;
+		if (chunklen < 1023)
+			break;
+		else	buf = (char *)realloc(buf, len + 1024);
+		chunkbuf = buf+len;
+	}
+
+
+	zlen = (long) (len*1.001)+15;
+	char *zbuf = new char[zlen+1];
+	if (len)
+	{
+		//printf("Doing compress\n");
+		if (compress((Bytef*)zbuf, &zlen, (const Bytef*)buf, len) != Z_OK)
+		{
+			printf("ERROR in compression\n");
+		}
+		else {
+			SendChars(zbuf, zlen);
+		}
+	}
+	else
+	{
+		fprintf(stderr, "ERROR: no buffer to compress\n");
+	}
+	delete [] zbuf;
+	free (buf);
+}
+
+
+/******************************************************************************
+ * Bzip2Compress::Decode - This function "decodes" the input stream into the
+ *			output stream.
+ *			The GetChars() and SendChars() functions are
+ *			used to separate this method from the actual
+ *			i/o.
+ */
+
+void Bzip2Compress::Decode(void)
+{
+/*
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
+                                   const Bytef *source, uLong sourceLen));
+     Decompresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be large enough to hold the
+   entire uncompressed data. (The size of the uncompressed data must have
+   been saved previously by the compressor and transmitted to the decompressor
+   by some mechanism outside the scope of this compression library.)
+   Upon exit, destLen is the actual size of the compressed buffer.
+     This function can be used to decompress a whole file at once if the
+   input file is mmap'ed.
+
+     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer, or Z_DATA_ERROR if the input data was corrupted.
+*/
+
+	// get buffer
+	char chunk[1024];
+	char *zbuf = (char *)calloc(1, 1024);
+	char *chunkbuf = zbuf;
+	int chunklen;
+	unsigned long zlen = 0;
+	while((chunklen = GetChars(chunk, 1023))) {
+		memcpy(chunkbuf, chunk, chunklen);
+		zlen += chunklen;
+		if (chunklen < 1023)
+			break;
+		else	zbuf = (char *)realloc(zbuf, zlen + 1024);
+		chunkbuf = zbuf + zlen;
+	}
+
+	//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
+	if (zlen) {
+		unsigned long blen = zlen*20;	// trust compression is less than 1000%
+		char *buf = new char[blen]; 
+		//printf("Doing decompress {%s}\n", zbuf);
+		slen = 0;
+		switch (uncompress((Bytef*)buf, &blen, (Bytef*)zbuf, zlen)){
+			case Z_OK: SendChars(buf, blen); slen = blen; break;
+			case Z_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
+			case Z_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
+			case Z_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
+			default: fprintf(stderr, "ERROR: an unknown error occured during decompression.\n"); break;
+		}
+		delete [] buf;
+	}
+	else {
+		fprintf(stderr, "ERROR: no buffer to decompress!\n");
+	}
+	//printf("Finished decoding\n");
+	free (zbuf);
+}
+
+SWORD_NAMESPACE_END


Property changes on: trunk/src/modules/common/bz2comprs.cpp
___________________________________________________________________
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Added: trunk/src/modules/common/xzcomprs.cpp
===================================================================
--- trunk/src/modules/common/xzcomprs.cpp	                        (rev 0)
+++ trunk/src/modules/common/xzcomprs.cpp	2013-07-02 09:57:20 UTC (rev 2850)
@@ -0,0 +1,181 @@
+/******************************************************************************
+ *
+ *  xzcomprs.cpp -	XzCompress, a driver class that provides xz (LZMA2)
+ *			compression
+ *				
+ * $Id$
+ *
+ * Copyright 2013 CrossWire Bible Society (http://www.crosswire.org)
+ *	CrossWire Bible Society
+ *	P. O. Box 2528
+ *	Tempe, AZ  85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * 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.
+ *
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <xzcomprs.h>
+#include <zlib.h>
+
+SWORD_NAMESPACE_START
+
+/******************************************************************************
+ * XzCompress Constructor - Initializes data for instance of XzCompress
+ *
+ */
+
+XzCompress::XzCompress() : SWCompress() {
+}
+
+
+/******************************************************************************
+ * XzCompress Destructor - Cleans up instance of XzCompress
+ */
+
+XzCompress::~XzCompress() {
+}
+
+
+/******************************************************************************
+ * XzCompress::Encode - This function "encodes" the input stream into the
+ *			output stream.
+ *			The GetChars() and SendChars() functions are
+ *			used to separate this method from the actual
+ *			i/o.
+ * 		NOTE:	must set zlen for parent class to know length of
+ * 			compressed buffer.
+ */
+
+void XzCompress::Encode(void)
+{
+/*
+ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
+                                 const Bytef *source, uLong sourceLen));
+     Compresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be at least 0.1% larger than
+   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
+   compressed buffer.
+     This function can be used to compress a whole file at once if the
+   input file is mmap'ed.
+     compress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer.
+*/
+	direct = 0;	// set direction needed by parent [Get|Send]Chars()
+
+	// get buffer
+	char chunk[1024];
+	char *buf = (char *)calloc(1, 1024);
+	char *chunkbuf = buf;
+	unsigned long chunklen;
+	unsigned long len = 0;
+	while((chunklen = GetChars(chunk, 1023))) {
+		memcpy(chunkbuf, chunk, chunklen);
+		len += chunklen;
+		if (chunklen < 1023)
+			break;
+		else	buf = (char *)realloc(buf, len + 1024);
+		chunkbuf = buf+len;
+	}
+
+
+	zlen = (long) (len*1.001)+15;
+	char *zbuf = new char[zlen+1];
+	if (len)
+	{
+		//printf("Doing compress\n");
+		if (compress((Bytef*)zbuf, &zlen, (const Bytef*)buf, len) != Z_OK)
+		{
+			printf("ERROR in compression\n");
+		}
+		else {
+			SendChars(zbuf, zlen);
+		}
+	}
+	else
+	{
+		fprintf(stderr, "ERROR: no buffer to compress\n");
+	}
+	delete [] zbuf;
+	free (buf);
+}
+
+
+/******************************************************************************
+ * XzCompress::Decode - This function "decodes" the input stream into the
+ *			output stream.
+ *			The GetChars() and SendChars() functions are
+ *			used to separate this method from the actual
+ *			i/o.
+ */
+
+void XzCompress::Decode(void)
+{
+/*
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
+                                   const Bytef *source, uLong sourceLen));
+     Decompresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be large enough to hold the
+   entire uncompressed data. (The size of the uncompressed data must have
+   been saved previously by the compressor and transmitted to the decompressor
+   by some mechanism outside the scope of this compression library.)
+   Upon exit, destLen is the actual size of the compressed buffer.
+     This function can be used to decompress a whole file at once if the
+   input file is mmap'ed.
+
+     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer, or Z_DATA_ERROR if the input data was corrupted.
+*/
+
+	// get buffer
+	char chunk[1024];
+	char *zbuf = (char *)calloc(1, 1024);
+	char *chunkbuf = zbuf;
+	int chunklen;
+	unsigned long zlen = 0;
+	while((chunklen = GetChars(chunk, 1023))) {
+		memcpy(chunkbuf, chunk, chunklen);
+		zlen += chunklen;
+		if (chunklen < 1023)
+			break;
+		else	zbuf = (char *)realloc(zbuf, zlen + 1024);
+		chunkbuf = zbuf + zlen;
+	}
+
+	//printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
+	if (zlen) {
+		unsigned long blen = zlen*20;	// trust compression is less than 1000%
+		char *buf = new char[blen]; 
+		//printf("Doing decompress {%s}\n", zbuf);
+		slen = 0;
+		switch (uncompress((Bytef*)buf, &blen, (Bytef*)zbuf, zlen)){
+			case Z_OK: SendChars(buf, blen); slen = blen; break;
+			case Z_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
+			case Z_BUF_ERROR: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
+			case Z_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
+			default: fprintf(stderr, "ERROR: an unknown error occured during decompression.\n"); break;
+		}
+		delete [] buf;
+	}
+	else {
+		fprintf(stderr, "ERROR: no buffer to decompress!\n");
+	}
+	//printf("Finished decoding\n");
+	free (zbuf);
+}
+
+SWORD_NAMESPACE_END


Property changes on: trunk/src/modules/common/xzcomprs.cpp
___________________________________________________________________
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native




More information about the sword-cvs mailing list