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
|
/* $Xorg: lbx_zlib_io.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */
/*
* Copyright 1993 Network Computing Devices
*
* 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 of NCD. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. NCD. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* NCD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NCD.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Dale Tonogai, Network Computing Devices
*/
/* $XFree86: xc/lib/lbxutil/lbx_zlib/lbx_zlib_io.c,v 1.11 2001/01/17 19:43:36 dawes Exp $ */
#ifdef WIN32
#define _WILLWINSOCK_
#endif
#include <X11/Xos.h>
#include <X11/Xfuncs.h>
#include <errno.h>
#if !defined(WIN32) && !defined(Lynx)
#include <sys/param.h>
#endif
#include <X11/extensions/lbxbufstr.h>
#include "lbx_zlib.h"
#include <stddef.h>
/*
* The following is taken from the xtrans code, almost as is,
* it would be nice to share it...
*/
#if defined(WIN32) || defined(__sxg__)
static int
writev(int fildes, const struct iovec *iov, int iovcnt)
{
int i, len, total;
char *base;
ESET(0);
for (i = 0, total = 0; i < iovcnt; i++, iov++) {
len = iov->iov_len;
base = iov->iov_base;
while (len > 0) {
register int nbytes;
nbytes = write(fildes, base, len);
if (nbytes < 0 && total == 0) return -1;
if (nbytes <= 0) return total;
ESET(0);
len -= nbytes;
total += nbytes;
base += nbytes;
}
}
return total;
}
#endif
int
InitZlibBuffer(b, size)
ZlibBufferPtr b;
int size;
{
if ((b->bufbase = (char *)Xalloc(size)) == NULL)
return -1;
b->bufend = b->bufbase + size;
b->bufptr = b->bufbase;
b->bufcnt = 0;
return 0;
}
void
FreeZlibBuffer(b)
ZlibBufferPtr b;
{
if (b->bufbase) {
Xfree(b->bufbase);
b->bufbase = NULL;
}
}
/*
* Returns:
* 1 if desired amount of data available in input buffer
* 0 if eof
* -1 if error
*/
int
GetInputPtr(fd, inbuf, reqlen, ppkt)
int fd;
ZlibBufferPtr inbuf;
int reqlen;
unsigned char **ppkt;
{
int readbytes;
int gotbytes;
if (inbuf->bufcnt == 0)
inbuf->bufptr = inbuf->bufbase;
if (reqlen <= inbuf->bufcnt) {
*ppkt = (unsigned char *)inbuf->bufptr;
return 1;
}
if (reqlen > inbuf->bufend - inbuf->bufptr) {
memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
inbuf->bufptr = inbuf->bufbase;
}
readbytes = (inbuf->bufend - inbuf->bufptr) - inbuf->bufcnt;
gotbytes = read(fd, inbuf->bufptr + inbuf->bufcnt, readbytes);
if (gotbytes > 0) {
if (reqlen <= (inbuf->bufcnt += gotbytes)) {
*ppkt = (unsigned char *)inbuf->bufptr;
return 1;
}
}
else
return gotbytes;
errno = EWOULDBLOCK;
return -1;
}
/*
* When ZLIB is started, we may well have read some data off of the
* wire somewhere. This sticks those bytes ahead of anything we might
* read in the future
*/
int
StuffInput(inbuf, pkt, reqlen)
ZlibBufferPtr inbuf;
unsigned char *pkt;
int reqlen;
{
int readbytes;
char *last;
last = inbuf->bufptr + inbuf->bufcnt;
if (reqlen > inbuf->bufend - last)
{
memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
inbuf->bufptr = inbuf->bufbase;
last = inbuf->bufptr + inbuf->bufcnt;
}
readbytes = MIN(reqlen, inbuf->bufend - last);
memmove(last, pkt, readbytes);
inbuf->bufcnt += readbytes;
return readbytes;
}
void
FreeInput(inbuf, len)
ZlibBufferPtr inbuf;
int len;
{
inbuf->bufptr += len;
if ((inbuf->bufcnt -= len) == 0)
inbuf->bufptr = inbuf->bufbase;
}
/*
* Reserve outlen bytes in the output buffer.
*/
char *
ReserveOutBuf(outbuf, outlen)
ZlibBufferPtr outbuf;
int outlen;
{
int left;
left = (outbuf->bufend - outbuf->bufptr) - outbuf->bufcnt;
if (left < outlen)
return NULL;
else
return outbuf->bufptr + outbuf->bufcnt;
}
/*
* Commit previously reserved space as real output
*/
void
CommitOutBuf(outbuf, outlen)
ZlibBufferPtr outbuf;
int outlen;
{
outbuf->bufcnt += outlen;
}
/*
* Write out as much as possible from the output buffer.
* Returns: >= 0 - amount left in buffer
* < 0 - write error
*/
int
FlushOutBuf(fd, outbuf)
int fd;
ZlibBufferPtr outbuf;
{
int bytes;
if (outbuf->bufcnt == 0)
return 0;
bytes = write(fd, outbuf->bufptr, outbuf->bufcnt);
if (bytes > 0) {
outbuf->bufptr += bytes;
if ((outbuf->bufcnt -= bytes) == 0)
outbuf->bufptr = outbuf->bufbase;
return outbuf->bufcnt;
}
else if (bytes == 0) {
errno = EWOULDBLOCK;
bytes = -1;
}
return bytes;
}
/*
* Write out as much as possible from the iovec array (no more than
* two entries allowed).
* Returns: >= 0 - amount left in iovec[1]
* < 0 - write error
*/
int
FlushIovBuf(fd, iovbuf)
int fd;
struct iovec *iovbuf;
{
int bytes;
int niov = 2;
struct iovec *iov = iovbuf;
if (iov[0].iov_len == 0) {
++iov;
--niov;
}
bytes = writev(fd, iov, niov);
if (bytes > 0) {
int i;
int len;
for (i = 0; i < niov; i++) {
len = MIN(bytes, iov[i].iov_len);
iov[i].iov_len -= len;
/*
* An explicit cast is necessary because silly SGI changed
* iov_base from a caddr_t to a void* in IRIX 6.x, and strictly
* speaking ANSI/ISO C doesn't allow the use of a cast in an
* lvalue, i.e. such as: '((char*)(iov[i].iov_base)) += len;'
*/
iov[i].iov_base = ((char*)(iov[i].iov_base)) + len;
if ((bytes -= len) == 0)
break;
}
return iovbuf[1].iov_len;
}
else if (bytes == 0) {
errno = EWOULDBLOCK;
bytes = -1;
}
return bytes;
}
|