xcmisc.c revision 6747b715
1/*
2
3Copyright 1993, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#ifdef HAVE_DIX_CONFIG_H
30#include <dix-config.h>
31#endif
32
33#include <X11/X.h>
34#include <X11/Xproto.h>
35#include "misc.h"
36#include "os.h"
37#include "dixstruct.h"
38#include "extnsionst.h"
39#include "swaprep.h"
40#include <X11/extensions/xcmiscproto.h>
41#include "modinit.h"
42
43#if HAVE_STDINT_H
44#include <stdint.h>
45#elif !defined(UINT32_MAX)
46#define UINT32_MAX 0xffffffffU
47#endif
48
49static DISPATCH_PROC(ProcXCMiscDispatch);
50static DISPATCH_PROC(ProcXCMiscGetVersion);
51static DISPATCH_PROC(ProcXCMiscGetXIDList);
52static DISPATCH_PROC(ProcXCMiscGetXIDRange);
53static DISPATCH_PROC(SProcXCMiscDispatch);
54static DISPATCH_PROC(SProcXCMiscGetVersion);
55static DISPATCH_PROC(SProcXCMiscGetXIDList);
56static DISPATCH_PROC(SProcXCMiscGetXIDRange);
57
58void XCMiscExtensionInit(INITARGS);
59
60void
61XCMiscExtensionInit(INITARGS)
62{
63    AddExtension(XCMiscExtensionName, 0, 0,
64		 ProcXCMiscDispatch, SProcXCMiscDispatch,
65		 NULL, StandardMinorOpcode);
66}
67
68static int
69ProcXCMiscGetVersion(ClientPtr client)
70{
71    xXCMiscGetVersionReply rep;
72    int n;
73
74    REQUEST_SIZE_MATCH(xXCMiscGetVersionReq);
75    rep.type = X_Reply;
76    rep.length = 0;
77    rep.sequenceNumber = client->sequence;
78    rep.majorVersion = XCMiscMajorVersion;
79    rep.minorVersion = XCMiscMinorVersion;
80    if (client->swapped) {
81    	swaps(&rep.sequenceNumber, n);
82	swaps(&rep.majorVersion, n);
83	swaps(&rep.minorVersion, n);
84    }
85    WriteToClient(client, sizeof(xXCMiscGetVersionReply), (char *)&rep);
86    return Success;
87}
88
89static int
90ProcXCMiscGetXIDRange(ClientPtr client)
91{
92    xXCMiscGetXIDRangeReply rep;
93    int n;
94    XID min_id, max_id;
95
96    REQUEST_SIZE_MATCH(xXCMiscGetXIDRangeReq);
97    GetXIDRange(client->index, FALSE, &min_id, &max_id);
98    rep.type = X_Reply;
99    rep.length = 0;
100    rep.sequenceNumber = client->sequence;
101    rep.start_id = min_id;
102    rep.count = max_id - min_id + 1;
103    if (client->swapped) {
104    	swaps(&rep.sequenceNumber, n);
105	swapl(&rep.start_id, n);
106	swapl(&rep.count, n);
107    }
108    WriteToClient(client, sizeof(xXCMiscGetXIDRangeReply), (char *)&rep);
109    return Success;
110}
111
112static int
113ProcXCMiscGetXIDList(ClientPtr client)
114{
115    REQUEST(xXCMiscGetXIDListReq);
116    xXCMiscGetXIDListReply rep;
117    int n;
118    XID *pids;
119    unsigned int count;
120
121    REQUEST_SIZE_MATCH(xXCMiscGetXIDListReq);
122
123    if (stuff->count > UINT32_MAX / sizeof(XID))
124	    return BadAlloc;
125
126    pids = (XID *)malloc(stuff->count * sizeof(XID));
127    if (!pids)
128    {
129	return BadAlloc;
130    }
131    count = GetXIDList(client, stuff->count, pids);
132    rep.type = X_Reply;
133    rep.sequenceNumber = client->sequence;
134    rep.length = count;
135    rep.count = count;
136    if (client->swapped) {
137    	swaps(&rep.sequenceNumber, n);
138	swapl(&rep.length, n);
139	swapl(&rep.count, n);
140    }
141    WriteToClient(client, sizeof(xXCMiscGetXIDListReply), (char *)&rep);
142    if (count)
143    {
144    	client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
145	WriteSwappedDataToClient(client, count * sizeof(XID), pids);
146    }
147    free(pids);
148    return Success;
149}
150
151static int
152ProcXCMiscDispatch (ClientPtr client)
153{
154    REQUEST(xReq);
155    switch (stuff->data)
156    {
157    case X_XCMiscGetVersion:
158	return ProcXCMiscGetVersion(client);
159    case X_XCMiscGetXIDRange:
160	return ProcXCMiscGetXIDRange(client);
161    case X_XCMiscGetXIDList:
162	return ProcXCMiscGetXIDList(client);
163    default:
164	return BadRequest;
165    }
166}
167
168static int
169SProcXCMiscGetVersion(ClientPtr client)
170{
171    int n;
172    REQUEST(xXCMiscGetVersionReq);
173
174    swaps(&stuff->length, n);
175    REQUEST_SIZE_MATCH(xXCMiscGetVersionReq);
176    swaps(&stuff->majorVersion, n);
177    swaps(&stuff->minorVersion, n);
178    return ProcXCMiscGetVersion(client);
179}
180
181static int
182SProcXCMiscGetXIDRange(ClientPtr client)
183{
184    int n;
185    REQUEST(xReq);
186
187    swaps(&stuff->length, n);
188    return ProcXCMiscGetXIDRange(client);
189}
190
191static int
192SProcXCMiscGetXIDList(ClientPtr client)
193{
194    int n;
195    REQUEST(xXCMiscGetXIDListReq);
196
197    swaps(&stuff->length, n);
198    swapl(&stuff->count, n);
199    return ProcXCMiscGetXIDList(client);
200}
201
202static int
203SProcXCMiscDispatch (ClientPtr client)
204{
205    REQUEST(xReq);
206    switch (stuff->data)
207    {
208    case X_XCMiscGetVersion:
209	return SProcXCMiscGetVersion(client);
210    case X_XCMiscGetXIDRange:
211	return SProcXCMiscGetXIDRange(client);
212    case X_XCMiscGetXIDList:
213	return SProcXCMiscGetXIDList(client);
214    default:
215	return BadRequest;
216    }
217}
218