~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/lib/lbxutil/lbx_zlib/lbx_zlib_io.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: lbx_zlib_io.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */
 
2
 
 
3
/*
 
4
 * Copyright 1993 Network Computing Devices
 
5
 *
 
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.
 
15
 *
 
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.
 
22
 *
 
23
 * Author:  Dale Tonogai, Network Computing Devices
 
24
 */
 
25
/* $XFree86: xc/lib/lbxutil/lbx_zlib/lbx_zlib_io.c,v 1.12 2001/07/25 15:04:57 dawes Exp $ */
 
26
 
 
27
#ifdef WIN32
 
28
#define _WILLWINSOCK_
 
29
#endif
 
30
#include <X11/Xos.h>
 
31
#include <X11/Xfuncs.h>
 
32
#include <errno.h>
 
33
#if !defined(WIN32) && !defined(Lynx)
 
34
#include <sys/param.h>
 
35
#endif
 
36
#include "lbxbufstr.h"
 
37
#include "lbx_zlib.h"
 
38
#include "os.h"
 
39
 
 
40
#include <stddef.h>
 
41
 
 
42
 
 
43
/*
 
44
 * The following is taken from the xtrans code, almost as is,
 
45
 * it would be nice to share it...
 
46
 */
 
47
#if defined(WIN32) || defined(__sxg__) || (defined(SCO) && !defined(SVR4) && !defined(SCO325))
 
48
static int
 
49
writev(int fildes, const struct iovec *iov, int iovcnt)
 
50
{
 
51
    int i, len, total;
 
52
    char *base;
 
53
 
 
54
    ESET(0);
 
55
    for (i = 0, total = 0;  i < iovcnt;  i++, iov++) {
 
56
        len = iov->iov_len;
 
57
        base = iov->iov_base;
 
58
        while (len > 0) {
 
59
            register int nbytes;
 
60
            nbytes = write(fildes, base, len);
 
61
            if (nbytes < 0 && total == 0)  return -1;
 
62
            if (nbytes <= 0)  return total;
 
63
            ESET(0);
 
64
            len   -= nbytes;
 
65
            total += nbytes;
 
66
            base  += nbytes;
 
67
        }
 
68
    }
 
69
    return total;
 
70
}
 
71
#endif
 
72
 
 
73
int
 
74
InitZlibBuffer(b, size)
 
75
    ZlibBufferPtr b;
 
76
    int size;
 
77
{
 
78
    if ((b->bufbase = (char *)Xalloc(size)) == NULL)
 
79
        return -1;
 
80
    b->bufend = b->bufbase + size;
 
81
    b->bufptr = b->bufbase;
 
82
    b->bufcnt = 0;
 
83
    return 0;
 
84
}
 
85
 
 
86
void
 
87
FreeZlibBuffer(b)
 
88
    ZlibBufferPtr b;
 
89
{
 
90
    if (b->bufbase) {
 
91
        Xfree(b->bufbase);
 
92
        b->bufbase = NULL;
 
93
    }
 
94
}
 
95
 
 
96
/*
 
97
 * Returns:
 
98
 *      1 if desired amount of data available in input buffer
 
99
 *      0 if eof
 
100
 *     -1 if error
 
101
 */
 
102
int
 
103
GetInputPtr(fd, inbuf, reqlen, ppkt)
 
104
    int          fd;
 
105
    ZlibBufferPtr inbuf;
 
106
    int          reqlen;
 
107
    unsigned char **ppkt;
 
108
{
 
109
    int          readbytes;
 
110
    int          gotbytes;
 
111
 
 
112
    if (inbuf->bufcnt == 0)
 
113
        inbuf->bufptr = inbuf->bufbase;
 
114
 
 
115
    if (reqlen <= inbuf->bufcnt) {
 
116
        *ppkt = (unsigned char *)inbuf->bufptr;
 
117
        return 1;
 
118
    }
 
119
 
 
120
    if (reqlen > inbuf->bufend - inbuf->bufptr) {
 
121
        memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
 
122
        inbuf->bufptr = inbuf->bufbase;
 
123
    }
 
124
    readbytes = (inbuf->bufend - inbuf->bufptr) - inbuf->bufcnt;
 
125
    gotbytes = read(fd, inbuf->bufptr + inbuf->bufcnt, readbytes);
 
126
    if (gotbytes > 0) {
 
127
        if (reqlen <= (inbuf->bufcnt += gotbytes)) {
 
128
            *ppkt = (unsigned char *)inbuf->bufptr;
 
129
            return 1;
 
130
        }
 
131
    }
 
132
    else
 
133
        return gotbytes;
 
134
 
 
135
    errno = EWOULDBLOCK;
 
136
    return -1;
 
137
}
 
138
 
 
139
/*
 
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
 
142
 * read in the future
 
143
 */
 
144
 
 
145
int
 
146
StuffInput(inbuf, pkt, reqlen)
 
147
    ZlibBufferPtr inbuf;
 
148
    unsigned char *pkt;
 
149
    int          reqlen;
 
150
{
 
151
    int          readbytes;
 
152
    char         *last;
 
153
    
 
154
    last = inbuf->bufptr + inbuf->bufcnt;
 
155
    if (reqlen > inbuf->bufend - last)
 
156
    {
 
157
        memmove(inbuf->bufbase, inbuf->bufptr, inbuf->bufcnt);
 
158
        inbuf->bufptr = inbuf->bufbase;
 
159
        last = inbuf->bufptr + inbuf->bufcnt;
 
160
    }
 
161
    readbytes = MIN(reqlen, inbuf->bufend - last);
 
162
    memmove(last, pkt, readbytes);
 
163
    inbuf->bufcnt += readbytes;
 
164
    return readbytes;
 
165
}
 
166
 
 
167
void
 
168
FreeInput(inbuf, len)
 
169
    ZlibBufferPtr inbuf;
 
170
    int          len;
 
171
{
 
172
    inbuf->bufptr += len;
 
173
    if ((inbuf->bufcnt -= len) == 0)
 
174
        inbuf->bufptr = inbuf->bufbase;
 
175
}
 
176
 
 
177
/*
 
178
 * Reserve outlen bytes in the output buffer.
 
179
 */
 
180
char *
 
181
ReserveOutBuf(outbuf, outlen)
 
182
    ZlibBufferPtr outbuf;
 
183
    int          outlen;
 
184
{
 
185
    int          left;
 
186
 
 
187
    left = (outbuf->bufend - outbuf->bufptr) - outbuf->bufcnt;
 
188
    if (left < outlen)
 
189
        return NULL;
 
190
    else
 
191
        return outbuf->bufptr + outbuf->bufcnt;
 
192
}
 
193
 
 
194
/*
 
195
 * Commit previously reserved space as real output
 
196
 */
 
197
void
 
198
CommitOutBuf(outbuf, outlen)
 
199
    ZlibBufferPtr outbuf;
 
200
    int          outlen;
 
201
{
 
202
    outbuf->bufcnt += outlen;
 
203
}
 
204
 
 
205
/*
 
206
 * Write out as much as possible from the output buffer.
 
207
 * Returns: >= 0 - amount left in buffer
 
208
 *          <  0 - write error
 
209
 */
 
210
int
 
211
FlushOutBuf(fd, outbuf)
 
212
    int          fd;
 
213
    ZlibBufferPtr outbuf;
 
214
{
 
215
    int          bytes;
 
216
 
 
217
    if (outbuf->bufcnt == 0)
 
218
        return 0;
 
219
    bytes = write(fd, outbuf->bufptr, outbuf->bufcnt);
 
220
    if (bytes > 0) {
 
221
        outbuf->bufptr += bytes;
 
222
        if ((outbuf->bufcnt -= bytes) == 0)
 
223
            outbuf->bufptr = outbuf->bufbase;
 
224
        return outbuf->bufcnt;
 
225
    }
 
226
    else if (bytes == 0) {
 
227
        errno = EWOULDBLOCK;
 
228
        bytes = -1;
 
229
    }
 
230
    return bytes;
 
231
}
 
232
 
 
233
/*
 
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]
 
237
 *          <  0 - write error
 
238
 */
 
239
int
 
240
FlushIovBuf(fd, iovbuf)
 
241
    int          fd;
 
242
    struct iovec *iovbuf;
 
243
{
 
244
    int          bytes;
 
245
    int          niov = 2;
 
246
    struct iovec *iov = iovbuf;
 
247
 
 
248
    if (iov[0].iov_len == 0) {
 
249
        ++iov;
 
250
        --niov;
 
251
    }
 
252
    bytes = writev(fd, iov, niov);
 
253
    if (bytes > 0) {
 
254
        int i;
 
255
        int len;
 
256
        for (i = 0; i < niov; i++) {
 
257
            len = MIN(bytes, iov[i].iov_len);
 
258
            iov[i].iov_len -= len;
 
259
        /* 
 
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;'
 
264
         */
 
265
            iov[i].iov_base = ((char*)(iov[i].iov_base)) + len;
 
266
            if ((bytes -= len) == 0)
 
267
                break;
 
268
        }
 
269
        return iovbuf[1].iov_len;
 
270
    }
 
271
    else if (bytes == 0) {
 
272
        errno = EWOULDBLOCK;
 
273
        bytes = -1;
 
274
    }
 
275
    return bytes;
 
276
}