Debian Codegraph @

nx-X11/lib/src/XlibAsync.c

raw
1 /**************************************************************************/
2 /* */
3 /* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
4 /* Copyright (c) 2008-2017 Oleksandr Shneyder <[email protected]> */
5 /* Copyright (c) 2011-2022 Mike Gabriel <[email protected]>*/
6 /* Copyright (c) 2014-2019 Mihai Moldovan <[email protected]> */
7 /* Copyright (c) 2014-2022 Ulrich Sibiller <[email protected]> */
8 /* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
9 /* */
10 /* nx-X11, NX protocol compression and NX extensions to this software */
11 /* are copyright of the aforementioned persons and companies. */
12 /* */
13 /* Redistribution and use of the present software is allowed according */
14 /* to terms specified in the file LICENSE which comes in the source */
15 /* distribution. */
16 /* */
17 /* All rights reserved. */
18 /* */
19 /* NOTE: This software has received contributions from various other */
20 /* contributors, only the core maintainers and supporters are listed as */
21 /* copyright holders. Please contact us, if you feel you should be listed */
22 /* as copyright holder, as well. */
23 /* */
24 /**************************************************************************/
25
26 /*
27
28 Copyright 1992, 1998 The Open Group
29
30 Permission to use, copy, modify, distribute, and sell this software and its
31 documentation for any purpose is hereby granted without fee, provided that
32 the above copyright notice appear in all copies and that both that
33 copyright notice and this permission notice appear in supporting
34 documentation.
35
36 The above copyright notice and this permission notice shall be included
37 in all copies or substantial portions of the Software.
38
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
43 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
44 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
45 OTHER DEALINGS IN THE SOFTWARE.
46
47 Except as contained in this notice, the name of The Open Group shall
48 not be used in advertising or otherwise to promote the sale, use or
49 other dealings in this Software without prior written authorization
50 from The Open Group.
51
52 */
53
54 #ifdef HAVE_CONFIG_H
55 #include <config.h>
56 #endif
57 #include <nx-X11/Xlibint.h>
58 #include <nx-X11/Xos.h>
59
60 /*
61 * Xlib's _XAsyncErrorState sequence number may wrap in 32bit
62 * and we cannot use 64bit as it's public API.
63 */
64 #ifdef LONG64
65 #define _XLIB_ASYNC_SEQUENCE_CMP(a,op,b) ((a == 0) || (a op b))
66 #else /* !LONG64 */
67 #define _XLIB_ASYNC_SEQUENCE_CMP(a,op,b) ((a == 0) || \
68 (((a op b) && (b - a op (UINT32_MAX >> 1))) || \
69 ((b op a) && ((UINT32_MAX >> 1) op a - b))))
70 #endif /* !LONG64 */
71
72 /*ARGSUSED*/
73 Bool
74 _XAsyncErrorHandler(
75 register Display *dpy,
76 register xReply *rep,
77 char *buf,
78 int len,
79 XPointer data)
80 {
81 register _XAsyncErrorState *state;
82
83 state = (_XAsyncErrorState *)data;
84 if (rep->generic.type == X_Error &&
85 (!state->error_code ||
86 rep->error.errorCode == state->error_code) &&
87 (!state->major_opcode ||
88 rep->error.majorCode == state->major_opcode) &&
89 (!state->minor_opcode ||
90 rep->error.minorCode == state->minor_opcode) &&
91 (_XLIB_ASYNC_SEQUENCE_CMP(state->min_sequence_number,<=,dpy->last_request_read)) &&
92 (_XLIB_ASYNC_SEQUENCE_CMP(state->max_sequence_number,>=,dpy->last_request_read))) {
93 state->last_error_received = rep->error.errorCode;
94 state->error_count++;
95 return True;
96 }
97 return False;
98 }
99
100 void _XDeqAsyncHandler(
101 Display *dpy,
102 register _XAsyncHandler *handler)
103 {
104 register _XAsyncHandler **prev;
105 register _XAsyncHandler *async;
106
107 for (prev = &dpy->async_handlers;
108 (async = *prev) && (async != handler);
109 prev = &async->next)
110 ;
111 if (async)
112 *prev = async->next;
113 }
114
115 char *
116 _XGetAsyncReply(
117 register Display *dpy,
118 register char *replbuf, /* data is read into this buffer */
119 register xReply *rep, /* value passed to calling handler */
120 char *buf, /* value passed to calling handler */
121 int len, /* value passed to calling handler */
122 int extra, /* extra words to read, ala _XReply */
123 Bool discard) /* discard after extra?, ala _XReply */
124 {
125 if (extra == 0) {
126 if (discard && (rep->generic.length << 2) > len)
127 _XEatData (dpy, (rep->generic.length << 2) - len);
128 return (char *)rep;
129 }
130
131 if (extra <= rep->generic.length) {
132 int size = SIZEOF(xReply) + (extra << 2);
133 if (size > len) {
134 memcpy(replbuf, buf, len);
135 _XRead(dpy, replbuf + len, size - len);
136 buf = replbuf;
137 len = size;
138 }
139
140 if (discard && rep->generic.length > extra &&
141 (rep->generic.length << 2) > len)
142 _XEatData (dpy, (rep->generic.length << 2) - len);
143
144 return buf;
145 }
146 /*
147 *if we get here, then extra > rep->generic.length--meaning we
148 * read a reply that's shorter than we expected. This is an
149 * error, but we still need to figure out how to handle it...
150 */
151 if ((rep->generic.length << 2) > len)
152 _XEatData (dpy, (rep->generic.length << 2) - len);
153 #ifdef NX_TRANS_SOCKET
154
155 /*
156 * The original code has provision
157 * for returning already.
158 */
159
160 #endif
161 _XIOError (dpy);
162 return (char *)rep;
163 }
164
165 void
166 _XGetAsyncData(
167 Display *dpy,
168 char *data, /* data is read into this buffer */
169 char *buf, /* value passed to calling handler */
170 int len, /* value passed to calling handler */
171 int skip, /* number of bytes already read in previous
172 _XGetAsyncReply or _XGetAsyncData calls */
173 int datalen, /* size of data buffer in bytes */
174 int discardtotal) /* min. bytes to consume (after skip) */
175 {
176 buf += skip;
177 len -= skip;
178 if (!data) {
179 if (datalen > len)
180 _XEatData(dpy, datalen - len);
181 } else if (datalen <= len) {
182 memcpy(data, buf, datalen);
183 } else {
184 memcpy(data, buf, len);
185 _XRead(dpy, data + len, datalen - len);
186 }
187 if (discardtotal > len) {
188 if (datalen > len)
189 len = datalen;
190 _XEatData(dpy, discardtotal - len);
191 }
192 }