summaryrefslogtreecommitdiff
path: root/src/modules/common/swcomprs.cpp
blob: 378c8b14743193aad757790317df68c603480110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/******************************************************************************
 *  swcomprs.cpp   - code for class 'SWCompress'- a driver class that provides
 *				compression utilities.
 *
 *
 * Copyright 2009 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 <swcomprs.h>

SWORD_NAMESPACE_START

/******************************************************************************
 * SWCompress Constructor - Initializes data for instance of SWCompress
 *
 */

SWCompress::SWCompress()
{
	buf = zbuf = 0;
	Init();
}


/******************************************************************************
 * SWCompress Destructor - Cleans up instance of SWCompress
 */

SWCompress::~SWCompress()
{
	if (zbuf)
		free(zbuf);

	if (buf)
		free(buf);
}


void SWCompress::Init()
{
		if (buf)
			free(buf);

		if (zbuf)
			free(zbuf);

		buf    = 0;
		zbuf   = 0;
		direct  = 0;
		zlen    = 0;
		slen    = 0;
		zpos    = 0;
		pos     = 0;
}


char *SWCompress::Buf(const char *ibuf, unsigned long *len) {
	// setting an uncompressed buffer
	if (ibuf) {
		Init();
		slen = (len) ? *len : strlen(ibuf);
		buf = (char *) calloc(slen + 1, 1);
		memcpy(buf, ibuf, slen);
	}

	// getting an uncompressed buffer
	if (!buf) {
		buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
		direct = 1;
		Decode();
//		slen = strlen(buf);
		if (len)
			*len = slen;
	}
	return buf;
}


char *SWCompress::zBuf(unsigned long *len, char *ibuf)
{
	// setting a compressed buffer
	if (ibuf) {
		Init();
		zbuf = (char *) malloc(*len);
		memcpy(zbuf, ibuf, *len);
		zlen = *len;
	}

	// getting a compressed buffer
	if (!zbuf) {
		direct = 0;
		Encode();
	}

	*len = zlen;
	return zbuf;
}


unsigned long SWCompress::GetChars(char *ibuf, unsigned long len)
{
	if (direct) {
		len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
		if (len > 0) {
			memmove(ibuf, &zbuf[zpos], len);
			zpos += len;
		}
	}
	else {
//		slen = strlen(buf);
		len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
		if (len > 0) {
			memmove(ibuf, &buf[pos], len);
			pos += len;
		}
	}
	return len;
}
	

unsigned long SWCompress::SendChars(char *ibuf, unsigned long len)
{
	if (direct) {
		if (buf) {
//			slen = strlen(buf);
			if ((pos + len) > (unsigned)slen) {
				buf = (char *) realloc(buf, pos + len + 1024);
				memset(&buf[pos], 0, len + 1024);
			}
		}
		else	buf = (char *)calloc(1, len + 1024);
		memmove(&buf[pos], ibuf, len);
		pos += len;
	}
	else {
		if (zbuf) {
			if ((zpos + len) > zlen) {
				zbuf = (char *) realloc(zbuf, zpos + len + 1024);
				zlen = zpos + len + 1024;
			}
		}
		else {
			zbuf = (char *)calloc(1, len + 1024);
			zlen = len + 1024;
		}
		memmove(&zbuf[zpos], ibuf, len);
		zpos += len;
	}
	return len;
}


/******************************************************************************
 * SWCompress::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.
 */

void SWCompress::Encode(void)
{
	cycleStream();
}


/******************************************************************************
 * SWCompress::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 SWCompress::Decode(void)
{
	cycleStream();
}


void SWCompress::cycleStream() {
	char buf[1024];
	unsigned long len, totlen = 0;

	do {
		len = GetChars(buf, 1024);
		if (len)
			totlen += SendChars(buf, len);
	} while (len == 1024);

	zlen = slen = totlen;
}

SWORD_NAMESPACE_END