1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52#ifdef HAVE_CONFIG_H
53#include <config.h>
54#endif
55#include "FSlibint.h"
56
57static void
58_FS_convert_char_info(fsXCharInfo *src, FSXCharInfo *dst)
59{
60    dst->ascent = src->ascent;
61    dst->descent = src->descent;
62    dst->left = src->left;
63    dst->right = src->right;
64    dst->width = src->width;
65    dst->attributes = src->attributes;
66}
67
68int
69FSQueryXExtents8(
70    FSServer		 *svr,
71    Font		  fid,
72    Bool		  range_type,
73    const unsigned char	 *str,
74    unsigned long	  str_len,
75    FSXCharInfo		**extents)
76{
77    fsQueryXExtents8Req *req;
78    fsQueryXExtents8Reply reply;
79    FSXCharInfo *ext;
80    fsXCharInfo local_exts;
81
82    if (str_len > (FSMaxRequestBytes(svr) - SIZEOF(fsQueryXExtents8Req)))
83        return FSBadLength;
84
85    GetReq(QueryXExtents8, req);
86    req->fid = fid;
87    req->range = (BOOL) range_type;
88    req->num_ranges = (CARD32) str_len;
89    req->length += (CARD16) ((str_len + 3) >> 2);
90    _FSSend(svr, (char *) str, str_len);
91
92    /* get back the info */
93    if (!_FSReply(svr, (fsReply *) & reply,
94	       (SIZEOF(fsQueryXExtents8Reply) - SIZEOF(fsGenericReply)) >> 2,
95		  fsFalse))
96	return FSBadAlloc;
97
98#if SIZE_MAX <= UINT_MAX
99    if (reply.num_extents > SIZE_MAX / sizeof(FSXCharInfo))
100	return FSBadAlloc;
101#endif
102
103    ext = FSmallocarray(reply.num_extents, sizeof(FSXCharInfo));
104    *extents = ext;
105    if (!ext)
106	return FSBadAlloc;
107    for (CARD32 i = 0; i < reply.num_extents; i++) {
108	_FSReadPad(svr, (char *) &local_exts, SIZEOF(fsXCharInfo));
109	_FS_convert_char_info(&local_exts, &ext[i]);
110    }
111
112    SyncHandle();
113    return FSSuccess;
114}
115
116int
117FSQueryXExtents16(
118    FSServer		 *svr,
119    Font		  fid,
120    Bool		  range_type,
121    const FSChar2b	 *str,
122    unsigned long	  str_len,
123    FSXCharInfo		**extents)
124{
125    fsQueryXExtents16Req *req;
126    fsQueryXExtents16Reply reply;
127    FSXCharInfo *ext;
128    fsXCharInfo local_exts;
129
130    /* Relies on fsChar2b & fsChar2b_version1 being the same size */
131    if (str_len > ((FSMaxRequestBytes(svr) - SIZEOF(fsQueryXExtents16Req))
132                    / SIZEOF(fsChar2b)))
133        return FSBadLength;
134
135    GetReq(QueryXExtents16, req);
136    req->fid = fid;
137    req->range = (BOOL) range_type;
138    req->num_ranges = (CARD32) str_len;
139    req->length += (CARD16) (((str_len * SIZEOF(fsChar2b)) + 3) >> 2);
140    if (FSProtocolVersion(svr) == 1)
141    {
142	fsChar2b_version1 *swapped_str;
143
144	swapped_str = FSmallocarray(str_len, SIZEOF(fsChar2b_version1));
145	if (!swapped_str)
146	    return FSBadAlloc;
147	for (unsigned long i = 0; i < str_len; i++) {
148	    swapped_str[i].low = str[i].low;
149	    swapped_str[i].high = str[i].high;
150	}
151	_FSSend(svr, (char *)swapped_str, (str_len*SIZEOF(fsChar2b_version1)));
152	FSfree(swapped_str);
153    } else
154	_FSSend(svr, (char *) str, (str_len * SIZEOF(fsChar2b)));
155
156    /* get back the info */
157    if (!_FSReply(svr, (fsReply *) & reply,
158	      (SIZEOF(fsQueryXExtents16Reply) - SIZEOF(fsGenericReply)) >> 2,
159		  fsFalse))
160	return FSBadAlloc;
161
162#if SIZE_MAX <= UINT_MAX
163    if (reply.num_extents > SIZE_MAX/sizeof(FSXCharInfo))
164	return FSBadAlloc;
165#endif
166
167    ext = FSmallocarray(reply.num_extents, sizeof(FSXCharInfo));
168    *extents = ext;
169    if (!ext)
170	return FSBadAlloc;
171    for (CARD32 i = 0; i < reply.num_extents; i++) {
172	_FSReadPad(svr, (char *) &local_exts, SIZEOF(fsXCharInfo));
173	_FS_convert_char_info(&local_exts, &ext[i]);
174    }
175
176    SyncHandle();
177    return FSSuccess;
178}
179