summaryrefslogtreecommitdiff
path: root/xc/workInProgress/lbx/programs/lbxproxy/di/options.c
blob: 823ede6290db9f38439d13b1e11ec73617a84c09 (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
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
/* $XConsortium: options.c,v 1.4 95/05/30 19:02:09 mor Exp $ */
/*
 * Copyright 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.
 *
 * $NCDId: @(#)options.c,v 1.2 1994/09/23 18:23:50 dct Exp $
 */

#include <stdio.h>
#include "X.h"
#include "misc.h"
#include "lbxopts.h"
#include "proxyopts.h"
#ifndef NO_LZW
#include "lbxlzw.h"
#endif /* NO_LZW */

static int LbxDeltaReply();
static int LbxProxyDeltaReq();
static int LbxServerDeltaReq();
static int LbxStreamCompReq();
#ifdef notyet
static int LbxBitmapCompReq();
static int LbxPixmapCompReq();
#endif
static int LbxSquishReq();

static int LbxProxyDeltaReply();
static int LbxServerDeltaReply();
static int LbxStreamCompReply();
#ifdef notyet
static int LbxBitmapCompReply();
static int LbxPixmapCompReply();
#endif
static int LbxSquishReply();

#define OPT_INDEX_STREAM_COMP		2

/*
 * List of LBX options we recognize and may negotiate
 */
static struct _LbxOption {
    CARD8	optcode;
    Bool	negotiate;
    int		(*req)();
    int		(*reply)();
} LbxOptions[] = {
    { LBX_OPT_DELTA_PROXY,  TRUE,  LbxProxyDeltaReq,	LbxProxyDeltaReply },
    { LBX_OPT_DELTA_SERVER, TRUE,  LbxServerDeltaReq,	LbxServerDeltaReply },
    { LBX_OPT_STREAM_COMP,  TRUE,  LbxStreamCompReq,	LbxStreamCompReply },
#ifdef notyet
    { LBX_OPT_BITMAP_COMP,  TRUE,  LbxBitmapCompReq,	LbxBitmapCompReply },
    { LBX_OPT_PIXMAP_COMP,  TRUE,  LbxPixmapCompReq,	LbxPixmapCompReply },
#endif
    { LBX_OPT_MSG_COMP,     TRUE,  LbxSquishReq,	LbxSquishReply },
};

#define LBX_N_OPTS	(sizeof(LbxOptions) / sizeof(struct _LbxOption))

static int optcount;
static int optlist[LBX_N_OPTS];

LbxNegOptsRec lbxNegOpt;

void
LbxOptInit()
{
    bzero((char *)&lbxNegOpt, sizeof(LbxNegOptsRec));
    lbxNegOpt.proxyDeltaN = LBX_OPT_DELTA_NCACHE_DFLT;
    lbxNegOpt.proxyDeltaMaxLen = LBX_OPT_DELTA_MSGLEN_DFLT;
    lbxNegOpt.serverDeltaN = LBX_OPT_DELTA_NCACHE_DFLT;
    lbxNegOpt.serverDeltaMaxLen = LBX_OPT_DELTA_MSGLEN_DFLT;
    lbxNegOpt.squish = TRUE;
}

int
LbxOptBuildReq(buf)
    register char *buf;
{
    int		  i;
    char *bufstart = buf;
    char *pnopts = buf++;

    optcount = 0;

    for (i = 0; i < LBX_N_OPTS; i++) {
	int reqlen;

	if (LbxOptions[i].negotiate) {
	    reqlen = (*LbxOptions[i].req)(buf + LBX_OPT_SMALLHDR_LEN);
	    *buf++ = LbxOptions[i].optcode;
	    *buf++ = LBX_OPT_SMALLHDR_LEN + reqlen;
	    buf += reqlen;
	    optlist[optcount++] = i;
	}
    }

    *pnopts = optcount;
    return (buf - bufstart);
}

int
LbxOptParseReply(nopts, preply, replylen)
    int		  nopts;
    unsigned char *preply;
    int		  replylen;
{
    int		  i;

    for (i = 0; i < nopts; i++) {
	int j;
	int len;
	int hdrlen;
	int result;
	int optindex = *preply;

	if (optindex >= optcount)
	    return -1;
	LBX_OPT_DECODE_LEN(preply + 1, len, hdrlen);
	if (len < ++hdrlen || len > replylen) {
#ifdef OPTDEBUG
	    fprintf(stderr, "bad length, len = %d, hdrlen = %d, optlen = %d\n",
			    len, hdrlen, replylen);
#endif
	    return -1;
	}

	result = (*LbxOptions[optlist[optindex]].reply)(preply + hdrlen,
							len - hdrlen);
	if (result < 0)
	    return -1;

	preply += len;
	replylen -= len;
    }
    return 0;
}

struct deltaOpt {
    CARD8	minN;			/* min cache size */
    CARD8	maxN;			/* max cache size */
    CARD8	prefN;			/* preferred cache size */
    CARD8	minMaxMsgLen;		/* min max-message-len / 4 */
    CARD8	maxMaxMsgLen;		/* max max-message-len / 4 */
    CARD8	prefMaxMsgLen;		/* preferred max-message-len / 4 */
};

static struct deltaOpt proxyDeltaOpt = {
    0,
    0xff,
    LBX_OPT_DELTA_NCACHE_DFLT,
    LBX_OPT_DELTA_MSGLEN_MIN >> 2,
    0xff,
    LBX_OPT_DELTA_MSGLEN_DFLT >> 2
};

static struct deltaOpt serverDeltaOpt = {
    0,
    0xff,
    LBX_OPT_DELTA_NCACHE_DFLT,
    LBX_OPT_DELTA_MSGLEN_MIN >> 2,
    0xff,
    LBX_OPT_DELTA_MSGLEN_DFLT >> 2
};

void
LbxNoDelta()
{
    proxyDeltaOpt.prefN = 0;
    serverDeltaOpt.prefN = 0;
}

static int
LbxProxyDeltaReq(buf)
    unsigned char *buf;
{
    memcpy(buf, (char *)&proxyDeltaOpt, sizeof(struct deltaOpt));
    return LBX_OPT_DELTA_REQLEN;
}

static int
LbxServerDeltaReq(buf)
    unsigned char *buf;
{
    memcpy(buf, (char *)&serverDeltaOpt, sizeof(struct deltaOpt));
    return LBX_OPT_DELTA_REQLEN;
}

static int
LbxProxyDeltaReply(preply, replylen)
    unsigned char *preply;
    int		  replylen;
{
    return LbxDeltaReply(preply, 
			 replylen,
			 &lbxNegOpt.proxyDeltaN,
			 &lbxNegOpt.proxyDeltaMaxLen);
}

static int
LbxServerDeltaReply(preply, replylen)
    unsigned char *preply;
    int		  replylen;
{
    return LbxDeltaReply(preply, 
			 replylen,
			 &lbxNegOpt.serverDeltaN,
			 &lbxNegOpt.serverDeltaMaxLen);
}

static int
LbxDeltaReply(preply, replylen, pn, pmaxlen)
    unsigned char *preply;
    int		  replylen;
    short	  *pn;
    short	  *pmaxlen;
{
    if (replylen < 2)
	return -1;
    *pn = *preply++;
    *pmaxlen = *preply;
    if ((*pmaxlen <<= 2) == 0)
	*pn = 0;
    else if (*pmaxlen < 32) {
#ifdef DEBUG
	printf("bad delta max msg length %d\n", *pmaxlen);
#endif
	return -1;
     }
    return 0;
}

static int OptLzwReq();
static int OptLzwReply();

static struct _LbxStreamComp {
    int		typelen;
    char	*type;
    int		(*req)();
    int		(*reply)();
} LbxStreamComp[] = {
#ifndef NO_LZW
    { LZW_STRCOMP_OPT_LEN,  LZW_STRCOMP_OPT,  OptLzwReq,  OptLzwReply }
#endif /* NO_LZW */
};

#define LBX_N_STRCOMP	\
    (sizeof(LbxStreamComp) / sizeof(struct _LbxStreamComp))

void
LbxNoComp()
{
    LbxOptions[OPT_INDEX_STREAM_COMP].negotiate = FALSE;
}

static int
LbxStreamCompReq(buf)
    unsigned char *buf;
{
    int		  i;
    int		  reqlen;

    *buf++ = LBX_N_STRCOMP;
    reqlen = 1;

    for (i = 0; i < LBX_N_STRCOMP; i++) {
	int optdatalen;
	int typelen = LbxStreamComp[i].typelen;

	*buf++ = typelen;
	memcpy(buf, LbxStreamComp[i].type, typelen);
	buf += typelen;
	reqlen += 1 + typelen;

	optdatalen = (*LbxStreamComp[i].req)(buf + 1);
	*buf = optdatalen + 1;
	buf += optdatalen + 1;
	reqlen += optdatalen + 1;
    }

    return reqlen;
}

static int
LbxStreamCompReply(preply, replylen)
    unsigned char *preply;
    int		  replylen;
{
    int		  optindex;

    if (replylen < 1 || (optindex = *preply) >= LBX_N_STRCOMP)
	return -1;
    return (*LbxStreamComp[optindex].reply)(preply + 1, replylen - 1);
	
}

/* LZW code sizes in order of preference */
static CARD8 lzwCodeSizes[] = { 14, 12 };

static int
OptLzwReq(buf)
    unsigned char *buf;
{
    *buf++ = sizeof(lzwCodeSizes);
    memcpy(buf, lzwCodeSizes, sizeof(lzwCodeSizes));
    return (1 + sizeof(lzwCodeSizes));
}

static int
OptLzwReply(preply, replylen)
    unsigned char *preply;
    int		  replylen;
{
    int		   index;
    extern pointer LzwInit();

    if (replylen < 1 || (index = *preply) >= sizeof(lzwCodeSizes))
	return -1;
    lbxNegOpt.streamCompInit = LzwInit;
    lbxNegOpt.streamCompArg = (pointer)lzwCodeSizes[index];
    return 0;
}

static Bool lbxDoSquishing = TRUE;

void
LbxNoSquish()
{
    lbxDoSquishing = FALSE;
}

static int
LbxSquishReq(buf)
    unsigned char *buf;
{
    *buf = lbxDoSquishing;
    return 1;
}

static int
LbxSquishReply(preply, replylen)
    unsigned char *preply;
    int		  replylen;
{
    if (replylen < 1)
	return -1;
    lbxNegOpt.squish = *preply;
    return 0;
}