1bbe1b32bSmrg/*
2bbe1b32bSmrg * swapped requests
3bbe1b32bSmrg */
4bbe1b32bSmrg/*
5bbe1b32bSmrg
6bbe1b32bSmrgCopyright 1990, 1991, 1998  The Open Group
7bbe1b32bSmrg
8bbe1b32bSmrgPermission to use, copy, modify, distribute, and sell this software and its
9bbe1b32bSmrgdocumentation for any purpose is hereby granted without fee, provided that
10bbe1b32bSmrgthe above copyright notice appear in all copies and that both that
11bbe1b32bSmrgcopyright notice and this permission notice appear in supporting
12bbe1b32bSmrgdocumentation.
13bbe1b32bSmrg
14bbe1b32bSmrgThe above copyright notice and this permission notice shall be included in
15bbe1b32bSmrgall copies or substantial portions of the Software.
16bbe1b32bSmrg
17bbe1b32bSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bbe1b32bSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bbe1b32bSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20bbe1b32bSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21bbe1b32bSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22bbe1b32bSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23bbe1b32bSmrg
24bbe1b32bSmrgExcept as contained in this notice, the name of The Open Group shall not be
25bbe1b32bSmrgused in advertising or otherwise to promote the sale, use or other dealings
26bbe1b32bSmrgin this Software without prior written authorization from The Open Group.
27bbe1b32bSmrg
28bbe1b32bSmrg * Copyright 1990, 1991 Network Computing Devices;
29bbe1b32bSmrg * Portions Copyright 1987 by Digital Equipment Corporation
30bbe1b32bSmrg *
31bbe1b32bSmrg * Permission to use, copy, modify, distribute, and sell this software and
32bbe1b32bSmrg * its documentation for any purpose is hereby granted without fee, provided
33bbe1b32bSmrg * that the above copyright notice appear in all copies and that both that
34bbe1b32bSmrg * copyright notice and this permission notice appear in supporting
35bbe1b32bSmrg * documentation, and that the names of Network Computing Devices, or Digital
36bbe1b32bSmrg * not be used in advertising or publicity pertaining to distribution
37bbe1b32bSmrg * of the software without specific, written prior permission.
38bbe1b32bSmrg *
39bbe1b32bSmrg * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
40bbe1b32bSmrg * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
41bbe1b32bSmrg * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
42bbe1b32bSmrg * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
43bbe1b32bSmrg * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
44bbe1b32bSmrg * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
45bbe1b32bSmrg * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
46bbe1b32bSmrg * THIS SOFTWARE.
47bbe1b32bSmrg */
48ce6676dbSmrg
4934f90d55Smrg#include	"config.h"
50bbe1b32bSmrg
51bbe1b32bSmrg#include	<swapreq.h>
52bbe1b32bSmrg
53bbe1b32bSmrg#include	<X11/fonts/FSproto.h>
54bbe1b32bSmrg#include	"clientstr.h"
55bbe1b32bSmrg#include	"globals.h"
56bbe1b32bSmrg#include	"dispatch.h"
57bbe1b32bSmrg
58ce6676dbSmrg/* Size including padding to next 32-bit boundary */
59ce6676dbSmrg#define PAD_TO_32BIT(e)		(((e) + 3) & ~3)
60ce6676dbSmrg
61bbe1b32bSmrgvoid
62bbe1b32bSmrgSwapLongs(long *list, unsigned long count)
63bbe1b32bSmrg{
64bbe1b32bSmrg    int         n;
65bbe1b32bSmrg    register char *longs = (char *)list;
66bbe1b32bSmrg
67bbe1b32bSmrg    while (count >= 8) {
68bbe1b32bSmrg	swapl(longs + 0, n);
69bbe1b32bSmrg	swapl(longs + 4, n);
70bbe1b32bSmrg	swapl(longs + 8, n);
71bbe1b32bSmrg	swapl(longs + 12, n);
72bbe1b32bSmrg	swapl(longs + 16, n);
73bbe1b32bSmrg	swapl(longs + 20, n);
74bbe1b32bSmrg	swapl(longs + 24, n);
75bbe1b32bSmrg	swapl(longs + 28, n);
76bbe1b32bSmrg	longs += 32;
77bbe1b32bSmrg	count -= 8;
78bbe1b32bSmrg    }
79bbe1b32bSmrg    if (count != 0) {
80bbe1b32bSmrg	do {
81bbe1b32bSmrg	    swapl(longs, n);
82bbe1b32bSmrg	    longs += 4;
83bbe1b32bSmrg	} while (--count != 0);
84bbe1b32bSmrg    }
85bbe1b32bSmrg}
86bbe1b32bSmrg
87bbe1b32bSmrg/* Byte swap a list of shorts */
88bbe1b32bSmrg
89bbe1b32bSmrgvoid
90bbe1b32bSmrgSwapShorts(short *list, unsigned long count)
91bbe1b32bSmrg{
92bbe1b32bSmrg    register char *shorts = (char *)list;
93bbe1b32bSmrg    register int n;
94bbe1b32bSmrg
95bbe1b32bSmrg    while (count >= 16) {
96bbe1b32bSmrg	swaps(shorts + 0, n);
97bbe1b32bSmrg	swaps(shorts + 2, n);
98bbe1b32bSmrg	swaps(shorts + 4, n);
99bbe1b32bSmrg	swaps(shorts + 6, n);
100bbe1b32bSmrg	swaps(shorts + 8, n);
101bbe1b32bSmrg	swaps(shorts + 10, n);
102bbe1b32bSmrg	swaps(shorts + 12, n);
103bbe1b32bSmrg	swaps(shorts + 14, n);
104bbe1b32bSmrg	swaps(shorts + 16, n);
105bbe1b32bSmrg	swaps(shorts + 18, n);
106bbe1b32bSmrg	swaps(shorts + 20, n);
107bbe1b32bSmrg	swaps(shorts + 22, n);
108bbe1b32bSmrg	swaps(shorts + 24, n);
109bbe1b32bSmrg	swaps(shorts + 26, n);
110bbe1b32bSmrg	swaps(shorts + 28, n);
111bbe1b32bSmrg	swaps(shorts + 30, n);
112bbe1b32bSmrg	shorts += 32;
113bbe1b32bSmrg	count -= 16;
114bbe1b32bSmrg    }
115bbe1b32bSmrg    if (count != 0) {
116bbe1b32bSmrg	do {
117bbe1b32bSmrg	    swaps(shorts, n);
118bbe1b32bSmrg	    shorts += 2;
119bbe1b32bSmrg	} while (--count != 0);
120bbe1b32bSmrg    }
121bbe1b32bSmrg}
122bbe1b32bSmrg
123bbe1b32bSmrg/*
124bbe1b32bSmrg * used for all requests that have nothing but 'length' swapped
125bbe1b32bSmrg */
126bbe1b32bSmrgint
127bbe1b32bSmrgSProcSimpleRequest(ClientPtr client)
128bbe1b32bSmrg{
129bbe1b32bSmrg    REQUEST(fsReq);
130bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
131bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
132bbe1b32bSmrg}
133bbe1b32bSmrg
134bbe1b32bSmrg/*
135bbe1b32bSmrg * used for all requests that have nothing but 'length' & a resource id swapped
136bbe1b32bSmrg */
137bbe1b32bSmrgint
138bbe1b32bSmrgSProcResourceRequest(ClientPtr client)
139bbe1b32bSmrg{
140bbe1b32bSmrg    REQUEST(fsResourceReq);
141bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
142bbe1b32bSmrg    stuff->id = lswapl(stuff->id);
143bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
144bbe1b32bSmrg}
145bbe1b32bSmrg
146bbe1b32bSmrgstatic int
147bbe1b32bSmrgswap_auth(ClientPtr client, pointer data, int num, int length)
148bbe1b32bSmrg{
149bbe1b32bSmrg    unsigned char *p;
150bbe1b32bSmrg    unsigned char t;
151bbe1b32bSmrg    CARD16      namelen,
152bbe1b32bSmrg                datalen;
153bbe1b32bSmrg    int         i;
154bbe1b32bSmrg
155ce6676dbSmrg    if (num == 0) {	/* Nothing to swap */
156ce6676dbSmrg	return (FSSuccess);
157ce6676dbSmrg    }
158bbe1b32bSmrg
159ce6676dbSmrg    /* The font service protocol spec states that the lengths should always
160ce6676dbSmrg     * be in the client's native byte order, and thus need swapping for a
161ce6676dbSmrg     * byte-swapped client - but set_font_authorizations() in the X server
162ce6676dbSmrg     * sample implementation has always filled them in in big-endian format,
163ce6676dbSmrg     * and xfs was swapping when running on a byte-swapped connection, and
164ce6676dbSmrg     * then assuming the result was big-endian.
165ce6676dbSmrg     *
166ce6676dbSmrg     * It also specifies padding each string out to the next 32-bit boundary,
167ce6676dbSmrg     * but again, set_font_authorizations() in the X server sample
168ce6676dbSmrg     * implementation has always failed to do so, but byte-pads the total
169ce6676dbSmrg     * length of data instead.
170ce6676dbSmrg     *
171ce6676dbSmrg     * This code determines if the individual string lengths add up to the
172ce6676dbSmrg     * correct total length when interpreted as generated by the traditional
173ce6676dbSmrg     * sample implementation, and if so, uses them that way, otherwise it
174ce6676dbSmrg     * will swap them, to make them big-endian, before passing on to the
175ce6676dbSmrg     * unswapped code that assumes that they are big-endian.
176ce6676dbSmrg     */
177ce6676dbSmrg
178ce6676dbSmrg    /* First determine if data matches the traditional sample
179ce6676dbSmrg       implementation format */
180ce6676dbSmrg    for (i = 0, p = data; i < num; i++) {
181ce6676dbSmrg	if ((p - (unsigned char *)data) > (length - 4))
182ce6676dbSmrg	    break;
183ce6676dbSmrg
184ce6676dbSmrg	namelen = (p[0] << 8) + p[1];
185ce6676dbSmrg	datalen = (p[2] << 8) + p[3];
186ce6676dbSmrg	p += 4 + namelen + datalen;
187ce6676dbSmrg    }
188ce6676dbSmrg    if ((i == num) && (PAD_TO_32BIT(p - (unsigned char *)data) == length))
189ce6676dbSmrg	return (FSSuccess);
190ce6676dbSmrg
191ce6676dbSmrg    /* Length didn't match, so we'll try swapping & padding */
192ce6676dbSmrg    for (i = 0, p = data; i < num; i++) {
193ce6676dbSmrg	if ((p - (unsigned char *)data) > (length - 4))
194ce6676dbSmrg	    break;
195ce6676dbSmrg
196ce6676dbSmrg	namelen = (p[1] << 8) + p[0];
197bbe1b32bSmrg	t = p[0];
198bbe1b32bSmrg	p[0] = p[1];
199bbe1b32bSmrg	p[1] = t;
200bbe1b32bSmrg	p += 2;
201ce6676dbSmrg
202ce6676dbSmrg	datalen = (p[1] << 8) + p[0];
203bbe1b32bSmrg	t = p[0];
204bbe1b32bSmrg	p[0] = p[1];
205bbe1b32bSmrg	p[1] = t;
206ce6676dbSmrg	p += 2 + PAD_TO_32BIT(namelen) + PAD_TO_32BIT(datalen);
207bbe1b32bSmrg    }
208ce6676dbSmrg    if ((i == num) && ((p - (unsigned char *)data) == length))
209ce6676dbSmrg	return (FSSuccess);
210bbe1b32bSmrg
211ce6676dbSmrg    /* If length didn't match either way, we give up. */
212ce6676dbSmrg    SendErrToClient(client, FSBadLength, (pointer)&length);
213ce6676dbSmrg    return (FSBadLength);
214bbe1b32bSmrg}
215bbe1b32bSmrg
216bbe1b32bSmrgint
217bbe1b32bSmrgSProcCreateAC(ClientPtr client)
218bbe1b32bSmrg{
219bbe1b32bSmrg    int status;
220ce6676dbSmrg    int length;
221ce6676dbSmrg
222bbe1b32bSmrg    REQUEST(fsCreateACReq);
223bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
224bbe1b32bSmrg    stuff->acid = lswapl(stuff->acid);
225ce6676dbSmrg    length = (stuff->length << 2) - sizeof(fsCreateACReq);
226bbe1b32bSmrg    status = swap_auth(client, (pointer) &stuff[1],
227ce6676dbSmrg		       stuff->num_auths, length);
228bbe1b32bSmrg    if (status != FSSuccess)
229bbe1b32bSmrg	return (status);
230bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
231bbe1b32bSmrg}
232bbe1b32bSmrg
233bbe1b32bSmrgint
234bbe1b32bSmrgSProcSetResolution(ClientPtr client)
235bbe1b32bSmrg{
236bbe1b32bSmrg    REQUEST(fsSetResolutionReq);
237bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
238bbe1b32bSmrg    stuff->num_resolutions = lswaps(stuff->num_resolutions);
239bbe1b32bSmrg    if ((int)stuff->length - (&stuff[1] - &stuff[0]) !=
240bbe1b32bSmrg	stuff->num_resolutions * sizeof(fsResolution))
241bbe1b32bSmrg	return (FSBadLength);
242bbe1b32bSmrg    SwapShorts((short *) &stuff[1], stuff->num_resolutions);
243bbe1b32bSmrg
244bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
245bbe1b32bSmrg}
246bbe1b32bSmrg
247bbe1b32bSmrg
248bbe1b32bSmrgint
249bbe1b32bSmrgSProcQueryExtension(ClientPtr client)
250bbe1b32bSmrg{
251bbe1b32bSmrg    REQUEST(fsQueryExtensionReq);
252bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
253bbe1b32bSmrg    return ((*ProcVector[FS_QueryExtension]) (client));
254bbe1b32bSmrg}
255bbe1b32bSmrg
256bbe1b32bSmrgint
257bbe1b32bSmrgSProcListCatalogues(ClientPtr client)
258bbe1b32bSmrg{
259bbe1b32bSmrg    REQUEST(fsListCataloguesReq);
260bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
261bbe1b32bSmrg    stuff->maxNames = lswapl(stuff->maxNames);
262bbe1b32bSmrg    stuff->nbytes = lswaps(stuff->nbytes);
263bbe1b32bSmrg    return ((*ProcVector[FS_ListCatalogues]) (client));
264bbe1b32bSmrg}
265bbe1b32bSmrg
266bbe1b32bSmrgint
267bbe1b32bSmrgSProcListFonts(ClientPtr client)
268bbe1b32bSmrg{
269bbe1b32bSmrg    REQUEST(fsListFontsReq);
270bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
271bbe1b32bSmrg    stuff->maxNames = lswapl(stuff->maxNames);
272bbe1b32bSmrg    stuff->nbytes = lswaps(stuff->nbytes);
273bbe1b32bSmrg    return ((*ProcVector[FS_ListFonts]) (client));
274bbe1b32bSmrg}
275bbe1b32bSmrg
276bbe1b32bSmrgint
277bbe1b32bSmrgSProcListFontsWithXInfo(ClientPtr client)
278bbe1b32bSmrg{
279bbe1b32bSmrg    REQUEST(fsListFontsWithXInfoReq);
280bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
281bbe1b32bSmrg    stuff->maxNames = lswapl(stuff->maxNames);
282bbe1b32bSmrg    stuff->nbytes = lswaps(stuff->nbytes);
283bbe1b32bSmrg    return ((*ProcVector[FS_ListFontsWithXInfo]) (client));
284bbe1b32bSmrg}
285bbe1b32bSmrg
286bbe1b32bSmrgint
287bbe1b32bSmrgSProcOpenBitmapFont(ClientPtr client)
288bbe1b32bSmrg{
289bbe1b32bSmrg    REQUEST(fsOpenBitmapFontReq);
290bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
291bbe1b32bSmrg    stuff->fid = lswapl(stuff->fid);
292bbe1b32bSmrg    stuff->format_hint = lswapl(stuff->format_hint);
293bbe1b32bSmrg    stuff->format_mask = lswapl(stuff->format_mask);
294bbe1b32bSmrg    return ((*ProcVector[FS_OpenBitmapFont]) (client));
295bbe1b32bSmrg}
296bbe1b32bSmrg
297bbe1b32bSmrgint
298bbe1b32bSmrgSProcQueryXExtents(ClientPtr client)
299bbe1b32bSmrg{
300bbe1b32bSmrg    REQUEST(fsQueryXExtents8Req); /* 8 and 16 are the same here */
301bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
302bbe1b32bSmrg    stuff->fid = lswapl(stuff->fid);
303bbe1b32bSmrg    stuff->num_ranges = lswapl(stuff->num_ranges);
304bbe1b32bSmrg
305bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
306bbe1b32bSmrg}
307bbe1b32bSmrg
308bbe1b32bSmrgint
309bbe1b32bSmrgSProcQueryXBitmaps(ClientPtr client)
310bbe1b32bSmrg{
311bbe1b32bSmrg    REQUEST(fsQueryXBitmaps8Req); /* 8 and 16 are the same here */
312bbe1b32bSmrg    stuff->length = lswaps(stuff->length);
313bbe1b32bSmrg    stuff->fid = lswapl(stuff->fid);
314bbe1b32bSmrg    stuff->format = lswapl(stuff->format);
315bbe1b32bSmrg    stuff->num_ranges = lswapl(stuff->num_ranges);
316bbe1b32bSmrg
317bbe1b32bSmrg    return ((*ProcVector[stuff->reqType]) (client));
318bbe1b32bSmrg}
319bbe1b32bSmrg
320bbe1b32bSmrgint
321bbe1b32bSmrgSwapConnClientPrefix(ClientPtr client, fsConnClientPrefix *pCCP)
322bbe1b32bSmrg{
323ce6676dbSmrg#if 0
324bbe1b32bSmrg    REQUEST(fsFakeReq);
325ce6676dbSmrg#endif
326bbe1b32bSmrg
327bbe1b32bSmrg    pCCP->major_version = lswaps(pCCP->major_version);
328bbe1b32bSmrg    pCCP->minor_version = lswaps(pCCP->minor_version);
329bbe1b32bSmrg    pCCP->auth_len = lswaps(pCCP->auth_len);
330bbe1b32bSmrg    return (swap_auth(client, (pointer) &pCCP[1],
331ce6676dbSmrg		      pCCP->num_auths, pCCP->auth_len));
332bbe1b32bSmrg}
333