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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
/* $Xorg: lbx_zlib.c,v 1.6 2001/02/09 02:04:05 xorgcvs Exp $ */
/*
Copyright 1995, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/
/*
* Copyright 1988, 1989, 1990, 1994 Network Computing Devices, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name Network Computing Devices, Inc. not be
* used in advertising or publicity pertaining to distribution of this
* software without specific, written prior permission.
*
* THIS SOFTWARE IS PROVIDED `AS-IS'. NETWORK COMPUTING DEVICES, INC.,
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
* LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NONINFRINGEMENT. IN NO EVENT SHALL NETWORK
* COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
* SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
* OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
* WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/* $XFree86: xc/lib/lbxutil/lbx_zlib/lbx_zlib.c,v 1.9 2001/08/27 19:01:07 dawes Exp $ */
#ifdef WIN32
#define _WILLWINSOCK_
#endif
#define _BSD_SOURCE
#include <X11/Xos.h>
#include <X11/Xfuncs.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#if !defined(WIN32) && !defined(Lynx)
#include <sys/param.h>
#endif
#include <X11/extensions/lbxbufstr.h>
#include "lbx_zlib.h"
#include <X11/extensions/lbxzlib.h>
unsigned long stream_out_compressed;
unsigned long stream_out_uncompressed;
unsigned long stream_out_plain;
unsigned long stream_in_compressed;
unsigned long stream_in_uncompressed;
unsigned long stream_in_plain;
#ifdef LBXREQSTATS
unsigned long stream_in_packet_header_bytes = 0;
extern int LbxWhoAmI;
#endif
struct ZlibInfo {
struct compress_private compress_state;
struct compress_private decompress_state;
int fd;
int compress_off;
ZlibBuffer inbuf;
ZlibBuffer outbuf;
unsigned char header[ZLIB_PACKET_HDRLEN];
struct iovec iovbuf[2];
};
static int
init_compress(struct compress_private *priv,/* local pointer to private data */
int level) /* compression level */
{
priv->cp_outputcount = 0;
priv->cp_in_count = 0; /* length of input */
priv->cp_bytes_out = 0; /* length of compressed output */
priv->cp_inputbuf = priv->cp_inputbufend = NULL;
priv->cp_packet = NULL;
priv->stream.zalloc = (alloc_func) 0;
priv->stream.zfree = (free_func) 0;
priv->stream.next_in = NULL;
priv->stream.next_out = NULL;
priv->stream.avail_in = priv->stream.avail_out = 0;
priv->z_err = deflateInit (&(priv->stream), level);
return (priv->compress_inited = (priv->z_err == Z_OK) ? 1 : 0);
}
static int
init_decompress(struct compress_private *priv)/* local pointer to private data */
{
priv->cp_outputcount = 0;
priv->cp_in_count = 0; /* length of input */
priv->cp_bytes_out = 0; /* length of compressed output */
priv->cp_inputbuf = priv->cp_inputbufend = NULL;
priv->cp_packet = NULL;
priv->stream.zalloc = (alloc_func) 0;
priv->stream.zfree = (free_func) 0;
priv->stream.next_in = NULL;
priv->stream.next_out = NULL;
priv->stream.avail_in = priv->stream.avail_out = 0;
priv->need_flush_decompress = 0;
priv->z_err = inflateInit (&(priv->stream));
#ifdef LBXREQSTATS
priv->req_length = -1;
priv->req_compbytes_read = 0;
priv->req_uncompbytes_read = 0;
priv->x_header_bytes_read = 0;
#endif
return (priv->decompress_inited = (priv->z_err == Z_OK) ? 1 : 0);
}
static void
do_compress (struct compress_private *priv,
int flush)
{
priv->stream.next_in = priv->cp_inputbuf;
priv->stream.avail_in = priv->cp_inputbufend - priv->cp_inputbuf;
priv->stream.next_out = priv->cp_outputbuf;
priv->stream.avail_out = priv->cp_outputbufend - priv->cp_outputbuf;
priv->z_err = deflate (&(priv->stream), flush);
priv->cp_inputbuf = priv->stream.next_in;
priv->cp_outputbuf = priv->stream.next_out;
}
static void
do_decompress (struct compress_private *priv)
{
priv->stream.next_in = priv->cp_inputbuf;
priv->stream.avail_in = priv->cp_inputbufend - priv->cp_inputbuf;
priv->stream.next_out = priv->cp_outputbuf;
priv->stream.avail_out = priv->cp_outputbufend - priv->cp_outputbuf;
priv->z_err = inflate (&(priv->stream), Z_PARTIAL_FLUSH);
priv->need_flush_decompress = (priv->stream.avail_out == 0);
priv->cp_inputbuf = priv->stream.next_in;
priv->cp_outputbuf = priv->stream.next_out;
}
static int
GetNewPacket(struct ZlibInfo *comp)
{
register struct compress_private *priv = &comp->decompress_state;
int len;
int result;
if (priv->cp_packet) {
/* Free up previous packet in input buffer */
FreeInput(&comp->inbuf, priv->cp_inputbufend - priv->cp_packet);
priv->cp_packet = NULL;
}
if ((result = GetInputPtr(comp->fd,
&comp->inbuf,
ZLIB_PACKET_HDRLEN,
&priv->cp_packet)) <= 0)
return result;
len = ZLIB_GET_DATALEN(priv->cp_packet);
if ((result = GetInputPtr(comp->fd,
&comp->inbuf,
len + ZLIB_PACKET_HDRLEN,
&priv->cp_packet)) <= 0) {
priv->cp_packet = NULL;
return result;
}
return len;
}
static int
NewPacketAvail(struct ZlibInfo *comp)
{
register struct compress_private *priv = &comp->decompress_state;
char *pkt;
int len;
if (priv->cp_packet) {
/* Free up previous packet in input buffer */
FreeInput(&comp->inbuf, priv->cp_inputbufend - priv->cp_packet);
priv->cp_packet = NULL;
}
if ((pkt = BYTES_AVAIL(&comp->inbuf, ZLIB_PACKET_HDRLEN))) {
len = ZLIB_GET_DATALEN(pkt);
if (BYTES_AVAIL(&comp->inbuf, len + ZLIB_PACKET_HDRLEN))
return TRUE;
}
return FALSE;
}
static int
PlainWrite(struct ZlibInfo *comp,
unsigned char *buffer,
int buflen)
{
int retval;
int lenleft = buflen;
if ((retval = ZlibFlush(comp->fd)) == 0) {
register struct iovec *iov = comp->iovbuf;
while (lenleft) {
int outlen, written;
if ((outlen = iov[1].iov_len)) {
iov[1].iov_base = (caddr_t) buffer;
}
else {
outlen = MIN(lenleft, ZLIB_MAX_DATALEN);
ZLIB_PUT_PKTHDR(comp->header, outlen, FALSE);
iov[0].iov_base = (caddr_t) comp->header;
iov[0].iov_len = ZLIB_PACKET_HDRLEN;
iov[1].iov_base = (caddr_t) buffer;
iov[1].iov_len = outlen;
stream_out_uncompressed += ZLIB_PACKET_HDRLEN;
}
if ((retval = FlushIovBuf(comp->fd, iov)) < 0)
break;
written = outlen - retval;
lenleft -= written;
buffer += written;
stream_out_plain += written;
stream_out_uncompressed += written;
if (retval)
break;
}
if (lenleft == buflen)
return retval;
else
return buflen - lenleft;
}
else if (retval > 0) {
retval = -1;
errno = EWOULDBLOCK;
}
return retval;
}
#define MAX_FDS 256
static struct ZlibInfo *per_fd[MAX_FDS];
/*
* Initialize ZLIB compressor
*/
void *
ZlibInit(int fd,
int level) /* compression level */
{
struct ZlibInfo *comp;
int ret1, ret2;
if ((comp = (struct ZlibInfo *)Xalloc(sizeof(struct ZlibInfo))) == NULL)
return NULL;
ret1 = InitZlibBuffer(&comp->inbuf, INBUFFER_SIZE);
ret2 = InitZlibBuffer(&comp->outbuf, OUTBUFFER_SIZE);
if (ret1 < 0 || ret2 < 0) {
ZlibFree(comp);
return NULL;
}
comp->compress_off = FALSE;
comp->iovbuf[1].iov_len = 0;
comp->fd = fd;
if (!init_compress(&comp->compress_state, level) ||
!init_decompress(&comp->decompress_state)) {
ZlibFree(comp);
return NULL;
}
per_fd[fd] = comp;
#ifdef LBXREQSTATS
InitLbxReqStats ();
#endif
return (void *)comp;
}
void
ZlibFree(struct ZlibInfo *comp)
{
if (!comp)
return;
per_fd[comp->fd] = 0;
FreeZlibBuffer(&comp->inbuf);
FreeZlibBuffer(&comp->outbuf);
if (comp->compress_state.compress_inited)
deflateEnd (&(comp->compress_state.stream));
else if (comp->decompress_state.decompress_inited)
inflateEnd (&(comp->compress_state.stream));
Xfree(comp);
}
int
ZlibFlush(int fd)
{
struct ZlibInfo *comp = per_fd[fd];
struct compress_private *priv = &comp->compress_state;
if (priv->cp_in_count) {
int len;
do_compress (priv, Z_PARTIAL_FLUSH);
len = priv->cp_outputbuf - (priv->cp_packet + ZLIB_PACKET_HDRLEN);
ZLIB_PUT_PKTHDR(priv->cp_packet, len, TRUE);
stream_out_compressed += (len + ZLIB_PACKET_HDRLEN);
CommitOutBuf(&comp->outbuf, len + ZLIB_PACKET_HDRLEN);
priv->cp_in_count = 0;
}
return FlushOutBuf(comp->fd, &comp->outbuf);
}
int
ZlibStuffInput(int fd,
unsigned char *buffer,
int buflen)
{
struct ZlibInfo *comp = per_fd[fd];
if (StuffInput (&comp->inbuf, buffer, buflen) != buflen)
return 0;
return 1;
}
void
ZlibCompressOn(int fd)
{
per_fd[fd]->compress_off = FALSE;
}
void
ZlibCompressOff(int fd)
{
per_fd[fd]->compress_off = TRUE;
}
int
ZlibWriteV(int fd,
struct iovec *iov,
int iovcnt)
{
int i;
int total = 0;
int this_time;
for (i = 0; i < iovcnt; i++)
{
this_time = ZlibWrite(fd, (unsigned char *)iov[i].iov_base,
iov[i].iov_len);
if (this_time > 0)
total += this_time;
if (this_time != iov[i].iov_len)
{
if (total)
return total;
return this_time;
}
}
return total;
}
int
ZlibWrite(int fd,
unsigned char *buffer,
int buflen)
{
struct ZlibInfo *comp = per_fd[fd];
struct compress_private *priv = &comp->compress_state;
int len;
int lenleft = buflen;
unsigned char *p = buffer;
if (comp->compress_off) {
return PlainWrite(comp, buffer, buflen);
}
while (lenleft) {
if (priv->cp_in_count == 0) {
priv->cp_packet = (unsigned char *) ReserveOutBuf(&comp->outbuf,
ZLIB_PACKET_HDRLEN + ZLIB_MAX_OUTLEN);
if (!priv->cp_packet) {
errno = EWOULDBLOCK;
return -1;
}
priv->cp_outputbuf = priv->cp_packet + ZLIB_PACKET_HDRLEN;
priv->cp_outputbufend = priv->cp_outputbuf + ZLIB_MAX_OUTLEN;
}
len = MIN(ZLIB_MAX_PLAIN - priv->cp_in_count, lenleft);
stream_out_plain += len;
priv->cp_inputbuf = p;
priv->cp_inputbufend = p + len;
do_compress(priv, Z_NO_FLUSH);
p += len;
lenleft -= len;
if ((priv->cp_in_count += len) == ZLIB_MAX_PLAIN) {
if (ZlibFlush(fd) < 0) {
if (lenleft == buflen)
return -1;
return buflen - lenleft;
}
}
}
return buflen;
}
int
ZlibRead(int fd,
unsigned char *buffer,
int buflen)
{
struct ZlibInfo *comp = per_fd[fd];
struct compress_private *priv = &comp->decompress_state;
unsigned char *p = buffer;
int lenleft = buflen;
int len;
int retval = -1;
/*
* First check if there is any data Zlib decompressed already but
* didn't have output buffer space to store it in.
*/
if (priv->need_flush_decompress)
{
priv->cp_outputbuf = p;
priv->cp_outputbufend = p + lenleft;
do_decompress (priv);
lenleft -= (priv->cp_outputbuf - p);
p = priv->cp_outputbuf;
}
/*
* Need to decompress some more data
*/
priv->cp_outputbuf = p;
priv->cp_outputbufend = p + lenleft;
while (priv->cp_outputbuf != priv->cp_outputbufend) {
if (priv->cp_inputbuf == priv->cp_inputbufend) {
if ((retval = GetNewPacket(comp)) <= 0)
break;
priv->cp_inputbuf = priv->cp_packet + ZLIB_PACKET_HDRLEN;
priv->cp_inputbufend = priv->cp_inputbuf + retval;
if (ZLIB_COMPRESSED(priv->cp_packet))
stream_in_compressed += (retval + ZLIB_PACKET_HDRLEN);
else
stream_in_uncompressed += (retval + ZLIB_PACKET_HDRLEN);
#ifdef LBXREQSTATS
stream_in_packet_header_bytes += ZLIB_PACKET_HDRLEN;
#endif
}
if (ZLIB_COMPRESSED(priv->cp_packet))
#ifdef LBXREQSTATS
if (LbxWhoAmI == 1) /* only support request stats for now */
do_decompress_with_stats(priv);
else
#endif
do_decompress(priv);
else {
len = MIN(priv->cp_inputbufend - priv->cp_inputbuf,
priv->cp_outputbufend - priv->cp_outputbuf);
memmove(priv->cp_outputbuf, priv->cp_inputbuf, len);
priv->cp_inputbuf += len;
priv->cp_outputbuf += len;
}
}
if ((len = priv->cp_outputbuf - buffer) == 0)
return retval;
else {
stream_in_plain += len;
return len;
}
}
int
ZlibInputAvail(int fd)
{
struct ZlibInfo *comp = per_fd[fd];
struct compress_private *priv = &comp->decompress_state;
return (
priv->need_flush_decompress ||
priv->cp_inputbuf != priv->cp_inputbufend ||
NewPacketAvail(comp) > 0);
}
|