The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bz2comprs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * bz2comprs.cpp - Bzip2Compress, a driver class that provides bzip2
4  * compression (Burrows–Wheeler with Huffman coding)
5  *
6  * $Id: bz2comprs.cpp 3754 2020-07-10 17:45:48Z scribe $
7  *
8  * Copyright 2000-2014 CrossWire Bible Society (http://www.crosswire.org)
9  * CrossWire Bible Society
10  * P. O. Box 2528
11  * Tempe, AZ 85280-2528
12  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU General Public License as published by the
15  * Free Software Foundation version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  */
23 
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <bz2comprs.h>
29 #include <bzlib.h>
30 
32 
33 /******************************************************************************
34  * Bzip2Compress Constructor - Initializes data for instance of Bzip2Compress
35  *
36  */
37 
39  level = 9;
40 }
41 
42 
43 /******************************************************************************
44  * Bzip2Compress Destructor - Cleans up instance of Bzip2Compress
45  */
46 
48 }
49 
50 
51 /******************************************************************************
52  * Bzip2Compress::Encode - This function "encodes" the input stream into the
53  * output stream.
54  * The GetChars() and SendChars() functions are
55  * used to separate this method from the actual
56  * i/o.
57  * NOTE: must set zlen for parent class to know length of
58  * compressed buffer.
59  */
60 
62  direct = 0; // set direction needed by parent [Get|Send]Chars()
63 
64  // get buffer
65  char chunk[1024];
66  char *buf = (char *)calloc(1, 1024);
67  char *chunkbuf = buf;
68  unsigned long chunklen;
69  unsigned long len = 0;
70  while((chunklen = getChars(chunk, 1023))) {
71  memcpy(chunkbuf, chunk, chunklen);
72  len += chunklen;
73  if (chunklen < 1023)
74  break;
75  else buf = (char *)realloc(buf, len + 1024);
76  chunkbuf = buf+len;
77  }
78 
79 
80  zlen = (long) (len*1.01)+600;
81  char *zbuf = new char[zlen+1];
82  if (len)
83  {
84  //printf("Doing compress\n");
85  if (BZ2_bzBuffToBuffCompress(zbuf, (unsigned int*)&zlen, buf, (unsigned int)len, level, 0, 0) != BZ_OK)
86  {
87  printf("ERROR in compression\n");
88  }
89  else {
90  sendChars(zbuf, zlen);
91  }
92  }
93  else
94  {
95  fprintf(stderr, "ERROR: no buffer to compress\n");
96  }
97  delete [] zbuf;
98  free (buf);
99 }
100 
101 
102 /******************************************************************************
103  * Bzip2Compress::Decode - This function "decodes" the input stream into the
104  * output stream.
105  * The GetChars() and SendChars() functions are
106  * used to separate this method from the actual
107  * i/o.
108  */
109 
111  direct = 1; // set direction needed by parent [Get|Send]Chars()
112 
113  // get buffer
114  char chunk[1024];
115  char *zbuf = (char *)calloc(1, 1024);
116  char *chunkbuf = zbuf;
117  int chunklen;
118  unsigned long zlen = 0;
119  while((chunklen = (int)getChars(chunk, 1023))) {
120  memcpy(chunkbuf, chunk, chunklen);
121  zlen += chunklen;
122  if (chunklen < 1023)
123  break;
124  else zbuf = (char *)realloc(zbuf, zlen + 1024);
125  chunkbuf = zbuf + zlen;
126  }
127 
128  //printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
129  if (zlen) {
130  unsigned int blen = (unsigned int)(zlen*20); // trust compression is less than 1000%
131  char *buf = new char[blen];
132  //printf("Doing decompress {%s}\n", zbuf);
133  slen = 0;
134  switch (BZ2_bzBuffToBuffDecompress(buf, &blen, zbuf, (unsigned int)zlen, 0, 0)){
135  case BZ_OK: sendChars(buf, blen); slen = blen; break;
136  case BZ_MEM_ERROR: fprintf(stderr, "ERROR: not enough memory during decompression.\n"); break;
137  case BZ_OUTBUFF_FULL: fprintf(stderr, "ERROR: not enough room in the out buffer during decompression.\n"); break;
138  case BZ_DATA_ERROR: fprintf(stderr, "ERROR: corrupt data during decompression.\n"); break;
139  default: fprintf(stderr, "ERROR: an unknown error occurred during decompression.\n"); break;
140  }
141  delete [] buf;
142  }
143  else {
144  fprintf(stderr, "ERROR: no buffer to decompress!\n");
145  }
146  //printf("Finished decoding\n");
147  free (zbuf);
148 }
149 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual void decode(void)
Definition: bz2comprs.cpp:110
unsigned long zlen
Definition: swcomprs.h:39
unsigned long slen
Definition: swcomprs.h:39
int level
Definition: swcomprs.h:40
free(preg->fastmap)
char * realloc()
virtual void encode(void)
Definition: bz2comprs.cpp:61
char * buf
Definition: swcomprs.h:38
virtual ~Bzip2Compress()
Definition: bz2comprs.cpp:47
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
char * zbuf
Definition: swcomprs.h:38
#define SWORD_NAMESPACE_END
Definition: defs.h:40
virtual unsigned long sendChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:141