summaryrefslogtreecommitdiff
path: root/modules/im/ximcp/imTransR.c
blob: e536bbc9b797105423ab55f102ee0bb066cbebef (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
/*
 * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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.
 */
/******************************************************************

              Copyright 1992, 1993, 1994 by FUJITSU LIMITED

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 FUJITSU LIMITED
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
FUJITSU LIMITED makes no representations about the suitability of
this software for any purpose.
It is provided "as is" without express or implied warranty.

FUJITSU LIMITED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL FUJITSU LIMITED 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: Hideki Hiura (hhiura@Sun.COM) Sun Microsystems, Inc.
          Takashi Fujiwara     FUJITSU LIMITED
                               fujiwara@a80.tech.yk.fujitsu.co.jp

******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "Xlibint.h"
#include "Xlcint.h"
#include "XimTrInt.h"
#include "Ximint.h"

Public TransportSW _XimTransportRec[] = {
    { "X",          _XimXConf },  /* 1st entry must be X.
					This will be a fallback */
#ifdef TCPCONN
    { "tcp",        _XimTransConf }, /* use X transport lib */
#endif /* TCPCONN */
#if defined(UNIXCONN) || defined(LOCALCONN)
    { "local",      _XimTransConf }, /* use X transport lib */
#endif /* UNIXCONN */
#ifdef DNETCONN
    { "dnet",     _XimTransConf }, /* use X transport lib */
#endif /* DNETCONN */
#ifdef STREAMSCONN
    { "streams",    _XimTransConf }, /* use X transport lib */
#endif /* STREAMSCONN */
    { (char *)NULL, (Bool (*)(Xim, char *))NULL },
};

Public Bool
_XimConnect(Xim im)
{
    return im->private.proto.connect(im);
}

Public Bool
_XimShutdown(Xim im)
{
    return im->private.proto.shutdown(im);
}

Public Bool
_XimWrite(Xim im, INT16 len, XPointer data)
{
    return im->private.proto.write(im, len, data);
}

Private int
_CheckProtocolData(
    Xim		  im,
    char	 *recv_buf)
{
    int		 data_len;

    data_len = (int)(((*((CARD16 *)recv_buf + 1)) * 4) + XIM_HEADER_SIZE);
    return data_len;
}

Private int
_XimReadData(
    Xim		 im,
    INT16	*len,
    XPointer	 buf,
    int		 buf_size)
{
    char	*hold_buf;
    char	*tmp;
    int		 data_len;
    int		 packet_size;
    int		 ret_len;
    register int i;

    if (buf_size < XIM_HEADER_SIZE) {
	*len = (INT16)XIM_HEADER_SIZE;
	return XIM_OVERFLOW;
    }

    bzero(buf, buf_size);
    packet_size = 0;
    data_len = 0;

    if ((hold_buf = im->private.proto.hold_data)) {
	data_len = im->private.proto.hold_data_len;
	if (data_len >= XIM_HEADER_SIZE) {
	    packet_size = _CheckProtocolData(im, hold_buf);
	    if (packet_size > buf_size) {
		*len = (INT16)packet_size;
		return XIM_OVERFLOW;
	    }
	    if (packet_size <= data_len) {
		memcpy(buf, hold_buf, packet_size);
		for (i = packet_size; i < data_len; i++) {
		    if (hold_buf[i])
			break;
		}
		data_len -= i;

		if (data_len) {
		    if (!(tmp = (char *)Xmalloc(data_len))) {
			return XIM_FALSE;
		    }
		    memcpy(tmp, &hold_buf[i], data_len);
		    im->private.proto.hold_data = tmp;
		    im->private.proto.hold_data_len = data_len;
		} else {
		    im->private.proto.hold_data = 0;
		    im->private.proto.hold_data_len = 0;
		}
		Xfree(hold_buf);
		*len = (INT16)packet_size;
		return XIM_TRUE;
	    }
	}
	memcpy(buf, hold_buf, data_len);
	buf_size -= data_len;
	Xfree(hold_buf);
	im->private.proto.hold_data = 0;
	im->private.proto.hold_data_len = 0;
    }

    if (!packet_size) {
	while (data_len < XIM_HEADER_SIZE) {
	    if (!(im->private.proto.read(im,
			(XPointer)&buf[data_len], buf_size, &ret_len))) {
		return XIM_FALSE;
	    }
	    data_len += ret_len;
	    buf_size -= ret_len;
	}
	packet_size = _CheckProtocolData(im, buf);
    }

    if (packet_size > buf_size) {
	if (!(tmp = (char *)Xmalloc(data_len))) {
	    return XIM_FALSE;
	}
	memcpy(tmp, buf, data_len);
	bzero(buf, data_len);
	im->private.proto.hold_data = tmp;
	im->private.proto.hold_data_len = data_len;
	*len = (INT16)packet_size;
	return XIM_OVERFLOW;
    }

    while (data_len < packet_size) {
	if (!(im->private.proto.read(im,
			(XPointer)&buf[data_len], buf_size, &ret_len))) {
	    return XIM_FALSE;
	}
	data_len += ret_len;
	buf_size -= ret_len;
    }

    for (i = packet_size; i < data_len; i++) {
	if (buf[i])
	    break;
    }
    data_len -= i;

    if (data_len) {
	if (!(tmp = (char *)Xmalloc(data_len))) {
	    return XIM_FALSE;
	}
	memcpy(tmp, &buf[i], data_len);
	bzero(&buf[i], data_len);
	im->private.proto.hold_data = tmp;
	im->private.proto.hold_data_len = data_len;
    } else {
	im->private.proto.hold_data = 0;
	im->private.proto.hold_data_len = 0;
    }
    *len = (INT16)packet_size;
    return XIM_TRUE;
}

Private Bool
_XimCallDispatcher(
    Xim		 im,
    INT16	 len,
    XPointer	 data)
{
    return im->private.proto.call_dispatcher(im, len, data);
}

Public int
_XimRead(Xim im, INT16 *len, XPointer buf, int buf_size,
	 Bool (*predicate)(Xim, INT16, XPointer, XPointer), XPointer arg)
{
    INT16	 read_len;
    int		 ret_code;

    for (;;) {
	ret_code = _XimReadData(im, &read_len, buf, buf_size);
	if(ret_code != XIM_TRUE) {
	    return ret_code;
	}
	if ((*predicate)(im, read_len, buf, arg))
	    break;
	if (_XimCallDispatcher(im, read_len, buf))
	    continue;
	_XimError(im, 0, XIM_BadProtocol, (INT16)0, (CARD16)0, (char *)NULL);
    }
    *len = read_len;
    return True;
}

Public Bool
_XimRegisterDispatcher(
    Xim		 im,
    Bool	 (*callback)(
			     Xim, INT16, XPointer, XPointer
			     ),
    XPointer	 call_data)
{
    return im->private.proto.register_dispatcher(im, callback, call_data);
}

Public void
_XimFlush(Xim im)
{
    im->private.proto.flush(im);
    return;
}

Public Bool
_XimFilterWaitEvent(Xim im)
{
    INT16	 read_len;
    CARD32	 reply32[BUFSIZE/4];
    char	*reply = (char *)reply32;
    XPointer	 preply;
    int		 buf_size;
    int		 ret_code;

    buf_size = BUFSIZE;
    ret_code = _XimReadData(im, &read_len, (XPointer)reply, buf_size);
    if(ret_code == XIM_TRUE) {
	preply = reply;
    } else if(ret_code == XIM_OVERFLOW) {
	if(read_len <= 0) {
	    preply = reply;
	} else {
	    buf_size = (int)read_len;
	    preply = (XPointer)Xmalloc(buf_size);
	    ret_code = _XimReadData(im, &read_len, preply, buf_size);
	    if(ret_code != XIM_TRUE) {
		if (preply != reply)
		    Xfree(preply);
		return False;
	    }
	}
    } else {
	return False;
    }
    if (_XimCallDispatcher(im, read_len, preply)) {
	if(reply != preply)
	    Xfree(preply);
	return True;
    }
    _XimError(im, 0, XIM_BadProtocol, (INT16)0, (CARD16)0, (char *)NULL);
    if(reply != preply)
	Xfree(preply);
    return True;
}