1
/* $Xorg: lbx_zlib_io.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */
4
* Copyright 1993 Network Computing Devices
6
* Permission to use, copy, modify, distribute, and sell this software and its
7
* documentation for any purpose is hereby granted without fee, provided that
8
* the above copyright notice appear in all copies and that both that
9
* copyright notice and this permission notice appear in supporting
10
* documentation, and that the name of NCD. not be used in advertising or
11
* publicity pertaining to distribution of the software without specific,
12
* written prior permission. NCD. makes no representations about the
13
* suitability of this software for any purpose. It is provided "as is"
14
* without express or implied warranty.
16
* NCD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NCD.
18
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
* Author: Dale Tonogai, Network Computing Devices
25
/* $XFree86: xc/lib/lbxutil/lbx_zlib/lbx_zlib_io.c,v 1.12 2001/07/25 15:04:57 dawes Exp $ */
31
#include <X11/Xfuncs.h>
33
#if !defined(WIN32) && !defined(Lynx)
34
#include <sys/param.h>
36
#include "lbxbufstr.h"
44
* The following is taken from the xtrans code, almost as is,
45
* it would be nice to share it...
47
#if defined(WIN32) || defined(__sxg__) || (defined(SCO) && !defined(SVR4) && !defined(SCO325))
49
writev(int fildes, const struct iovec *iov, int iovcnt)
55
for (i = 0, total = 0; i < iovcnt; i++, iov++) {
60
nbytes = write(fildes, base, len);
61
if (nbytes < 0 && total == 0) return -1;
62
if (nbytes <= 0) return total;
74
InitZlibBuffer(b, size)
78
if ((b->bufbase = (char *)Xalloc(size)) == NULL)
80
b->bufend = b->bufbase + size;
81
b->bufptr = b->bufbase;
98
* 1 if desired amount of data available in input buffer
103
GetInputPtr(fd, inbuf, reqlen, ppkt)
107
unsigned char **ppkt;
112
if (inbuf->bufcnt == 0)
113
inbuf->bufptr = inbuf->bufbase;
115
if (reqlen <= inbuf->bufcnt) {
116
*ppkt = (unsigned char *)inbuf->bufptr;
120
if (reqlen > inbuf->bufend - inbuf->bufptr) {
121
memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
122
inbuf->bufptr = inbuf->bufbase;
124
readbytes = (inbuf->bufend - inbuf->bufptr) - inbuf->bufcnt;
125
gotbytes = read(fd, inbuf->bufptr + inbuf->bufcnt, readbytes);
127
if (reqlen <= (inbuf->bufcnt += gotbytes)) {
128
*ppkt = (unsigned char *)inbuf->bufptr;
140
* When ZLIB is started, we may well have read some data off of the
141
* wire somewhere. This sticks those bytes ahead of anything we might
146
StuffInput(inbuf, pkt, reqlen)
154
last = inbuf->bufptr + inbuf->bufcnt;
155
if (reqlen > inbuf->bufend - last)
157
memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
158
inbuf->bufptr = inbuf->bufbase;
159
last = inbuf->bufptr + inbuf->bufcnt;
161
readbytes = MIN(reqlen, inbuf->bufend - last);
162
memmove(last, pkt, readbytes);
163
inbuf->bufcnt += readbytes;
168
FreeInput(inbuf, len)
172
inbuf->bufptr += len;
173
if ((inbuf->bufcnt -= len) == 0)
174
inbuf->bufptr = inbuf->bufbase;
178
* Reserve outlen bytes in the output buffer.
181
ReserveOutBuf(outbuf, outlen)
182
ZlibBufferPtr outbuf;
187
left = (outbuf->bufend - outbuf->bufptr) - outbuf->bufcnt;
191
return outbuf->bufptr + outbuf->bufcnt;
195
* Commit previously reserved space as real output
198
CommitOutBuf(outbuf, outlen)
199
ZlibBufferPtr outbuf;
202
outbuf->bufcnt += outlen;
206
* Write out as much as possible from the output buffer.
207
* Returns: >= 0 - amount left in buffer
211
FlushOutBuf(fd, outbuf)
213
ZlibBufferPtr outbuf;
217
if (outbuf->bufcnt == 0)
219
bytes = write(fd, outbuf->bufptr, outbuf->bufcnt);
221
outbuf->bufptr += bytes;
222
if ((outbuf->bufcnt -= bytes) == 0)
223
outbuf->bufptr = outbuf->bufbase;
224
return outbuf->bufcnt;
226
else if (bytes == 0) {
234
* Write out as much as possible from the iovec array (no more than
235
* two entries allowed).
236
* Returns: >= 0 - amount left in iovec[1]
240
FlushIovBuf(fd, iovbuf)
242
struct iovec *iovbuf;
246
struct iovec *iov = iovbuf;
248
if (iov[0].iov_len == 0) {
252
bytes = writev(fd, iov, niov);
256
for (i = 0; i < niov; i++) {
257
len = MIN(bytes, iov[i].iov_len);
258
iov[i].iov_len -= len;
260
* An explicit cast is necessary because silly SGI changed
261
* iov_base from a caddr_t to a void* in IRIX 6.x, and strictly
262
* speaking ANSI/ISO C doesn't allow the use of a cast in an
263
* lvalue, i.e. such as: '((char*)(iov[i].iov_base)) += len;'
265
iov[i].iov_base = ((char*)(iov[i].iov_base)) + len;
266
if ((bytes -= len) == 0)
269
return iovbuf[1].iov_len;
271
else if (bytes == 0) {