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