The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swcomprs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swcomprs.cpp - a driver class that provides compression utilities
4  *
5  * $Id: swcomprs.cpp 3818 2020-10-19 13:41:05Z scribe $
6  *
7  * Copyright 1996-2014 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include <swcomprs.h>
27 
29 
30 /******************************************************************************
31  * SWCompress Constructor - Initializes data for instance of SWCompress
32  *
33  */
34 
36 {
37  buf = zbuf = 0;
38  level = 6;
39  init();
40 }
41 
42 
43 /******************************************************************************
44  * SWCompress Destructor - Cleans up instance of SWCompress
45  */
46 
48 {
49  if (zbuf)
50  free(zbuf);
51 
52  if (buf)
53  free(buf);
54 }
55 
56 
58 {
59  if (buf)
60  free(buf);
61 
62  if (zbuf)
63  free(zbuf);
64 
65  buf = 0;
66  zbuf = 0;
67  direct = 0;
68  zlen = 0;
69  slen = 0;
70  zpos = 0;
71  pos = 0;
72 }
73 
74 
75 void SWCompress::setUncompressedBuf(const char *ibuf, unsigned long *len) {
76  if (ibuf) {
77  init();
78  slen = (len) ? *len : strlen(ibuf);
79  buf = (char *) calloc(slen + 1, 1);
80  memcpy(buf, ibuf, slen);
81  }
82  if (!buf) {
83  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
84  direct = 1;
85  decode();
86  if (len) *len = slen;
87  }
88 }
89 
90 char *SWCompress::getUncompressedBuf(unsigned long *len) {
91  if (!buf) {
92  buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
93  direct = 1;
94  decode();
95  }
96  if (len) *len = slen;
97  return buf;
98 }
99 
100 
101 void SWCompress::setCompressedBuf(unsigned long *len, char *ibuf) {
102  if (ibuf) {
103  init();
104  zbuf = (char *) malloc(*len);
105  memcpy(zbuf, ibuf, *len);
106  zlen = *len;
107  }
108  *len = zlen;
109 }
110 
111 char *SWCompress::getCompressedBuf(unsigned long *len) {
112  if (!zbuf) {
113  direct = 0;
114  encode();
115  }
116  if (len) *len = zlen;
117  return zbuf;
118 }
119 
120 
121 unsigned long SWCompress::getChars(char *ibuf, unsigned long len) {
122  if (direct) {
123  len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
124  if (len > 0) {
125  memmove(ibuf, &zbuf[zpos], len);
126  zpos += len;
127  }
128  }
129  else {
130 // slen = strlen(buf);
131  len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
132  if (len > 0) {
133  memmove(ibuf, &buf[pos], len);
134  pos += len;
135  }
136  }
137  return len;
138 }
139 
140 
141 unsigned long SWCompress::sendChars(char *ibuf, unsigned long len) {
142  if (direct) {
143  if (buf) {
144 // slen = strlen(buf);
145  if ((pos + len) > (unsigned)slen) {
146  buf = (char *) realloc(buf, pos + len + 1024);
147  memset(&buf[pos], 0, len + 1024);
148  }
149  }
150  else buf = (char *)calloc(1, len + 1024);
151  memmove(&buf[pos], ibuf, len);
152  pos += len;
153  }
154  else {
155  if (zbuf) {
156  if ((zpos + len) > zlen) {
157  zbuf = (char *) realloc(zbuf, zpos + len + 1024);
158  zlen = zpos + len + 1024;
159  }
160  }
161  else {
162  zbuf = (char *)calloc(1, len + 1024);
163  zlen = len + 1024;
164  }
165  memmove(&zbuf[zpos], ibuf, len);
166  zpos += len;
167  }
168  return len;
169 }
170 
171 
172 /******************************************************************************
173  * SWCompress::encode - This function "encodes" the input stream into the
174  * output stream.
175  * The getChars() and sendChars() functions are
176  * used to separate this method from the actual
177  * i/o.
178  */
179 
180 void SWCompress::encode(void) {
181  cycleStream();
182 }
183 
184 
185 /******************************************************************************
186  * SWCompress::decode - This function "decodes" the input stream into the
187  * output stream.
188  * The getChars() and sendChars() functions are
189  * used to separate this method from the actual
190  * i/o.
191  */
192 
193 void SWCompress::decode(void) {
194  cycleStream();
195 }
196 
197 
199  char buf[1024];
200  unsigned long len, totlen = 0;
201 
202  do {
203  len = getChars(buf, 1024);
204  if (len)
205  totlen += sendChars(buf, len);
206  } while (len == 1024);
207 
208  zlen = slen = totlen;
209 }
210 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual void decode(void)
Definition: swcomprs.cpp:193
virtual void setCompressedBuf(unsigned long *len, char *buf=0)
Definition: swcomprs.cpp:101
unsigned long zlen
Definition: swcomprs.h:39
virtual char * getCompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:111
virtual ~SWCompress()
Definition: swcomprs.cpp:47
unsigned long slen
Definition: swcomprs.h:39
virtual char * getUncompressedBuf(unsigned long *len=0)
Definition: swcomprs.cpp:90
virtual void setUncompressedBuf(const char *buf=0, unsigned long *len=0)
Definition: swcomprs.cpp:75
int level
Definition: swcomprs.h:40
char * malloc()
virtual void encode(void)
Definition: swcomprs.cpp:180
free(preg->fastmap)
char * realloc()
char * buf
Definition: swcomprs.h:38
unsigned long zpos
Definition: swcomprs.h:39
void init()
Definition: swcomprs.cpp:57
void cycleStream()
Definition: swcomprs.cpp:198
virtual unsigned long getChars(char *buf, unsigned long len)
Definition: swcomprs.cpp:121
char direct
Definition: swcomprs.h:38
unsigned long pos
Definition: swcomprs.h:39
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