1/*
2 * font server reply swapping
3 */
4/*
5
6Copyright 1990, 1991, 1998  The Open Group
7
8Permission to use, copy, modify, distribute, and sell this software and its
9documentation for any purpose is hereby granted without fee, provided that
10the above copyright notice appear in all copies and that both that
11copyright notice and this permission notice appear in supporting
12documentation.
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of The Open Group shall not be
25used in advertising or otherwise to promote the sale, use or other dealings
26in this Software without prior written authorization from The Open Group.
27
28 * Copyright 1990, 1991 Network Computing Devices;
29 * Portions Copyright 1987 by Digital Equipment Corporation
30 *
31 * Permission to use, copy, modify, distribute, and sell this software and
32 * its documentation for any purpose is hereby granted without fee, provided
33 * that the above copyright notice appear in all copies and that both that
34 * copyright notice and this permission notice appear in supporting
35 * documentation, and that the names of Network Computing Devices, or Digital
36 * not be used in advertising or publicity pertaining to distribution
37 * of the software without specific, written prior permission.
38 *
39 * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
40 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
42 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
43 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
44 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
45 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
46 * THIS SOFTWARE.
47 */
48
49#include	"config.h"
50
51#include	<swaprep.h>
52#include	<swapreq.h>
53
54#include	<X11/fonts/FSproto.h>
55#include	"clientstr.h"
56#include	"difs.h"
57
58static void SwapConnSetupAccept(fsConnSetupAccept *pConnSetup, fsConnSetupAccept *pConnSetupT);
59
60void
61Swap32Write(
62    ClientPtr   client,
63    int         size,
64    long       *pbuf)
65{
66    int         n,
67                i;
68
69    size >>= 2;
70    for (i = 0; i < size; i++) {
71	swapl(&pbuf[i], n);
72    }
73    (void) WriteToClient(client, size << 2, (char *) pbuf);
74}
75
76void
77Swap16Write(
78    ClientPtr   client,
79    int         size,
80    short      *pbuf)
81{
82    int         n,
83                i;
84
85    size >>= 1;
86    for (i = 0; i < size; i++) {
87	swaps(&pbuf[i], n);
88    }
89    (void) WriteToClient(client, size << 1, (char *) pbuf);
90}
91
92void
93CopySwap32Write(
94    ClientPtr   client,
95    int         size,
96    long       *pbuf)
97{
98    int         bufsize = size;
99    long       *pbufT;
100    long       *from,
101               *to,
102               *fromLast,
103               *toLast;
104    long        tmpbuf[1];
105
106    while (!(pbufT = (long *) ALLOCATE_LOCAL(bufsize))) {
107	bufsize >>= 1;
108	if (bufsize == 4) {
109	    pbufT = tmpbuf;
110	    break;
111	}
112    }
113    /* convert lengths from # of bytes to # of longs */
114    size >>= 2;
115    bufsize >>= 2;
116
117    from = pbuf;
118    fromLast = from + size;
119    while (from < fromLast) {
120	int         nbytes;
121
122	to = pbufT;
123	toLast = to + min(bufsize, fromLast - from);
124	nbytes = (toLast - to) << 2;
125	while (to < toLast) {
126	    /*
127	     * can't write "cpswapl(*from++, *to++)" because cpswapl is a
128	     * macro that evaluates its args more than once
129	     */
130	    cpswapl(*from, *to);
131	    from++;
132	    to++;
133	}
134	(void) WriteToClient(client, nbytes, (char *) pbufT);
135    }
136
137    if (pbufT != tmpbuf)
138	DEALLOCATE_LOCAL((char *) pbufT);
139}
140
141void
142CopySwap16Write(
143    ClientPtr   client,
144    int         size,
145    short      *pbuf)
146{
147    int         bufsize = size;
148    short      *pbufT;
149    register short *from,
150               *to,
151               *fromLast,
152               *toLast;
153    short       tmpbuf[2];
154
155    /* Allocate as big a buffer as we can... */
156    while (!(pbufT = (short *) ALLOCATE_LOCAL(bufsize))) {
157	bufsize >>= 1;
158	if (bufsize == 4) {
159	    pbufT = tmpbuf;
160	    break;
161	}
162    }
163
164    /* convert lengths from # of bytes to # of shorts */
165    size >>= 1;
166    bufsize >>= 1;
167
168    from = pbuf;
169    fromLast = from + size;
170    while (from < fromLast) {
171	int         nbytes;
172
173	to = pbufT;
174	toLast = to + min(bufsize, fromLast - from);
175	nbytes = (toLast - to) << 1;
176	while (to < toLast) {
177	    /*
178	     * can't write "cpswaps(*from++, *to++)" because cpswaps is a
179	     * macro that evaluates its args more than once
180	     */
181	    cpswaps(*from, *to);
182	    from++;
183	    to++;
184	}
185	(void) WriteToClient(client, nbytes, (char *) pbufT);
186    }
187
188    if (pbufT != tmpbuf)
189	DEALLOCATE_LOCAL((char *) pbufT);
190}
191
192
193#undef  pRep
194#define pRep ((fsGenericReply *)data)
195
196void
197SGenericReply(
198    ClientPtr   client,
199    int         size,
200    void *	data)
201{
202    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
203    pRep->length = lswapl(pRep->length);
204    (void) WriteToClient(client, size, (char *) pRep);
205}
206
207
208#undef  pRep
209#define pRep ((fsListExtensionsReply *)data)
210
211void
212SListExtensionsReply(
213    ClientPtr   client,
214    int         size,
215    void *	data)
216{
217    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
218    pRep->length = lswapl(pRep->length);
219    (void) WriteToClient(client, size, (char *) pRep);
220}
221
222#undef  pRep
223#define pRep ((fsQueryExtensionReply *)data)
224
225void
226SQueryExtensionReply(
227    ClientPtr   client,
228    int         size,
229    void *	data)
230{
231    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
232    pRep->length = lswapl(pRep->length);
233    pRep->major_version = lswaps(pRep->major_version);
234    pRep->minor_version = lswaps(pRep->minor_version);
235    (void) WriteToClient(client, size, (char *) pRep);
236}
237
238#undef  pRep
239#define pRep ((fsListCataloguesReply *)data)
240
241void
242SListCataloguesReply(
243    ClientPtr   client,
244    int         size,
245    void *	data)
246{
247    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
248    pRep->length = lswapl(pRep->length);
249    pRep->num_replies = lswapl(pRep->num_replies);
250    pRep->num_catalogues = lswapl(pRep->num_catalogues);
251    (void) WriteToClient(client, size, (char *) pRep);
252}
253
254#undef  pRep
255#define pRep ((fsCreateACReply *)data)
256
257void
258SCreateACReply(
259    ClientPtr   client,
260    int         size,
261    void *	data)
262{
263    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
264    pRep->length = lswapl(pRep->length);
265    pRep->status = lswaps(pRep->status);
266    (void) WriteToClient(client, size, (char *) pRep);
267}
268
269#undef  pRep
270#define pRep ((fsGetEventMaskReply *)data)
271
272void
273SGetEventMaskReply(
274    ClientPtr   client,
275    int         size,
276    void *	data)
277{
278    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
279    pRep->length = lswapl(pRep->length);
280    pRep->event_mask = lswapl(pRep->event_mask);
281    (void) WriteToClient(client, size, (char *) pRep);
282}
283
284#undef  pRep
285#define pRep ((fsGetResolutionReply *)data)
286
287void
288SGetResolutionReply(
289    ClientPtr   client,
290    int         size,
291    void *	data)
292{
293    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
294    pRep->length = lswapl(pRep->length);
295    (void) WriteToClient(client, size, (char *) pRep);
296}
297
298#undef  pRep
299#define pRep ((fsListFontsReply *)data)
300
301void
302SListFontsReply(
303    ClientPtr   client,
304    int         size,
305    void *	data)
306{
307    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
308    pRep->length = lswapl(pRep->length);
309    pRep->following = lswapl(pRep->following);
310    pRep->nFonts = lswapl(pRep->nFonts);
311    (void) WriteToClient(client, size, (char *) pRep);
312}
313
314#define SwapXFontInfoHeader(reply) \
315    reply->font_header_flags = lswapl(reply->font_header_flags); \
316 \
317    reply->font_header_min_bounds_left = lswaps(reply->font_header_min_bounds_left); \
318    reply->font_header_min_bounds_right = lswaps(reply->font_header_min_bounds_right); \
319    reply->font_header_min_bounds_width = lswaps(reply->font_header_min_bounds_width); \
320    reply->font_header_min_bounds_ascent = lswaps(reply->font_header_min_bounds_ascent); \
321    reply->font_header_min_bounds_descent = lswaps(reply->font_header_min_bounds_descent); \
322    reply->font_header_min_bounds_attributes = lswaps(reply->font_header_min_bounds_attributes); \
323 \
324    reply->font_header_max_bounds_left = lswaps(reply->font_header_max_bounds_left); \
325    reply->font_header_max_bounds_right = lswaps(reply->font_header_max_bounds_right); \
326    reply->font_header_max_bounds_width = lswaps(reply->font_header_max_bounds_width); \
327    reply->font_header_max_bounds_ascent = lswaps(reply->font_header_max_bounds_ascent); \
328    reply->font_header_max_bounds_descent = lswaps(reply->font_header_max_bounds_descent); \
329    reply->font_header_max_bounds_attributes = lswaps(reply->font_header_max_bounds_attributes); \
330 \
331    reply->font_header_font_ascent = lswaps(reply->font_header_font_ascent); \
332    reply->font_header_font_descent = lswaps(reply->font_header_font_descent)
333
334#undef  pRep
335#define pRep ((fsListFontsWithXInfoReply *)data)
336
337void
338SListFontsWithXInfoReply(
339    ClientPtr   client,
340    int         size,
341    void *	data)
342{
343    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
344    pRep->length = lswapl(pRep->length);
345    if (size > SIZEOF(fsGenericReply)) { 	/* not last in series? */
346	pRep->nReplies = lswapl(pRep->nReplies);
347	SwapXFontInfoHeader(pRep);
348    }
349    (void) WriteToClient(client, size, (char *) pRep);
350}
351
352#undef  pRep
353#define pRep ((fsOpenBitmapFontReply *)data)
354
355void
356SOpenBitmapFontReply(
357    ClientPtr   client,
358    int         size,
359    void *	data)
360{
361    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
362    pRep->length = lswapl(pRep->length);
363    pRep->otherid = lswapl(pRep->otherid);
364
365    (void) WriteToClient(client, size, (char *) pRep);
366}
367
368#undef  pRep
369#define pRep ((fsQueryXInfoReply *)data)
370
371void
372SQueryXInfoReply(
373    ClientPtr   client,
374    int         size,
375    void *	data)
376{
377    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
378    pRep->length = lswapl(pRep->length);
379    SwapXFontInfoHeader(pRep);
380    (void) WriteToClient(client, size, (char *) pRep);
381}
382
383#undef  pRep
384#define pRep ((fsQueryXExtents8Reply *)data)
385
386void
387SQueryXExtentsReply(
388    ClientPtr   client,
389    int         size,
390    void *	data) /* QueryXExtents16Reply is the same */
391{
392    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
393    pRep->length = lswapl(pRep->length);
394    pRep->num_extents = lswapl(pRep->num_extents);
395    (void) WriteToClient(client, size, (char *) pRep);
396}
397
398#undef  pRep
399#define pRep ((fsQueryXBitmaps8Reply *)data)
400
401void
402SQueryXBitmapsReply(
403    ClientPtr   client,
404    int         size,
405    void *	data) /* QueryXBitmaps16Reply is the same */
406{
407    pRep->sequenceNumber = lswaps(pRep->sequenceNumber);
408    pRep->length = lswapl(pRep->length);
409    pRep->replies_hint = lswapl(pRep->replies_hint);
410    pRep->num_chars = lswapl(pRep->num_chars);
411    pRep->nbytes = lswapl(pRep->nbytes);
412    (void) WriteToClient(client, size, (char *) pRep);
413}
414
415void
416SErrorEvent(fsError *error, fsError *perror)
417{
418    *perror = *error;
419    perror->sequenceNumber = lswaps(perror->sequenceNumber);
420    perror->length = lswapl(perror->length);
421    perror->timestamp = lswapl(perror->timestamp);
422}
423
424void
425WriteSConnectionInfo(
426    ClientPtr   client,
427    unsigned long size,
428    char       *pInfo)
429{
430    char       *pInfoT,
431               *pInfoTBase;
432    fsConnSetupAccept *pConnSetup = (fsConnSetupAccept *) pInfo;
433    int         i;
434
435    pInfoT = pInfoTBase = (char *) ALLOCATE_LOCAL(size);
436    if (!pInfoT) {
437	client->noClientException = -2;
438	return;
439    }
440    SwapConnSetupAccept(pConnSetup, (fsConnSetupAccept *) pInfoT);
441    pInfoT += SIZEOF(fsConnSetup);
442    pInfo += SIZEOF(fsConnSetup);
443
444    i = (pConnSetup->vendor_len + 3) & ~3;
445    memcpy(pInfoT, pInfo, i);
446
447    (void) WriteToClient(client, (int) size, (char *) pInfoTBase);
448    DEALLOCATE_LOCAL(pInfoTBase);
449}
450
451static void
452SwapConnSetupAccept(fsConnSetupAccept *pConnSetup, fsConnSetupAccept *pConnSetupT)
453{
454    pConnSetupT->length = lswapl(pConnSetup->length);
455    pConnSetupT->max_request_len = lswaps(pConnSetup->max_request_len);
456    pConnSetupT->vendor_len = lswaps(pConnSetup->vendor_len);
457    pConnSetupT->release_number = lswapl(pConnSetup->release_number);
458}
459
460void
461WriteSConnSetup(ClientPtr client, fsConnSetup *pcsp)
462{
463    fsConnSetup cspT;
464
465    cspT.status = lswaps(pcsp->status);
466    cspT.major_version = lswaps(pcsp->major_version);
467    cspT.minor_version = lswaps(pcsp->minor_version);
468    cspT.num_alternates = pcsp->num_alternates;
469    cspT.auth_index = pcsp->auth_index;
470    cspT.alternate_len = lswaps(pcsp->alternate_len);
471    cspT.auth_len = lswaps(pcsp->auth_len);
472    (void) WriteToClient(client, SIZEOF(fsConnSetup), (char *) &cspT);
473}
474
475static void
476SwapPropOffset(char *po)
477{
478    int i, n;
479
480    for (i=0; i<4; i++)
481    {
482	swapl(po, n);
483	po += 4;
484    }
485}
486
487void
488SwapPropInfo(fsPropInfo *pi)
489{
490    int i;
491    char *po;
492
493    po = (char *) pi + SIZEOF(fsPropInfo);
494    for (i = 0; i < pi->num_offsets; i++)
495    {
496	SwapPropOffset(po);
497	po += SIZEOF(fsPropOffset);
498    }
499
500    pi->num_offsets = lswapl(pi->num_offsets);
501    pi->data_len = lswapl(pi->data_len);
502}
503
504void
505SwapExtents(fsXCharInfo *extents, int num)
506{
507    SwapShorts((short *)extents, num * (SIZEOF(fsXCharInfo) / 2));
508}
509
510void _X_COLD
511ReplySwapNotImplemented(_X_UNUSED ClientPtr client, _X_UNUSED int size,
512                        _X_UNUSED void *data)
513{
514    /* Getting here can become the next xfs exploit... so don't exit */
515    ErrorF("reply swapping not implemented for reply type %d\n",
516           ((fsReq *)(client)->requestBuffer)->reqType);
517}
518