FSFontInfo.c revision ba6a1819
1ba6a1819Smrg/* $Xorg: FSFontInfo.c,v 1.4 2001/02/09 02:03:25 xorgcvs Exp $ */
2ba6a1819Smrg/*
3ba6a1819Smrg * Copyright 1990 Network Computing Devices;
4ba6a1819Smrg * Portions Copyright 1987 by Digital Equipment Corporation
5ba6a1819Smrg *
6ba6a1819Smrg * Permission to use, copy, modify, distribute, and sell this software
7ba6a1819Smrg * and its documentation for any purpose is hereby granted without fee,
8ba6a1819Smrg * provided that the above copyright notice appear in all copies and
9ba6a1819Smrg * that both that copyright notice and this permission notice appear
10ba6a1819Smrg * in supporting documentation, and that the names of Network Computing
11ba6a1819Smrg * Devices or Digital not be used in advertising or publicity pertaining
12ba6a1819Smrg * to distribution of the software without specific, written prior
13ba6a1819Smrg * permission. Network Computing Devices or Digital make no representations
14ba6a1819Smrg * about the suitability of this software for any purpose.  It is provided
15ba6a1819Smrg * "as is" without express or implied warranty.
16ba6a1819Smrg *
17ba6a1819Smrg * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
18ba6a1819Smrg * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
19ba6a1819Smrg * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
20ba6a1819Smrg * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
21ba6a1819Smrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22ba6a1819Smrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23ba6a1819Smrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24ba6a1819Smrg * SOFTWARE.
25ba6a1819Smrg */
26ba6a1819Smrg/* $XFree86: xc/lib/FS/FSFontInfo.c,v 1.5tsi Exp $ */
27ba6a1819Smrg
28ba6a1819Smrg/*
29ba6a1819Smrg
30ba6a1819SmrgCopyright 1987, 1998  The Open Group
31ba6a1819Smrg
32ba6a1819SmrgPermission to use, copy, modify, distribute, and sell this software and its
33ba6a1819Smrgdocumentation for any purpose is hereby granted without fee, provided that
34ba6a1819Smrgthe above copyright notice appear in all copies and that both that
35ba6a1819Smrgcopyright notice and this permission notice appear in supporting
36ba6a1819Smrgdocumentation.
37ba6a1819Smrg
38ba6a1819SmrgThe above copyright notice and this permission notice shall be included in
39ba6a1819Smrgall copies or substantial portions of the Software.
40ba6a1819Smrg
41ba6a1819SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42ba6a1819SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43ba6a1819SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
44ba6a1819SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
45ba6a1819SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
46ba6a1819SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47ba6a1819Smrg
48ba6a1819SmrgExcept as contained in this notice, the name of The Open Group shall not be
49ba6a1819Smrgused in advertising or otherwise to promote the sale, use or other dealings
50ba6a1819Smrgin this Software without prior written authorization from The Open Group.
51ba6a1819Smrg
52ba6a1819Smrg*/
53ba6a1819Smrg#ifdef HAVE_CONFIG_H
54ba6a1819Smrg#include <config.h>
55ba6a1819Smrg#endif
56ba6a1819Smrg#include	"FSlibint.h"
57ba6a1819Smrg
58ba6a1819Smrgchar      **
59ba6a1819SmrgFSListFontsWithXInfo(
60ba6a1819Smrg    FSServer		  *svr,
61ba6a1819Smrg    char		  *pattern,
62ba6a1819Smrg    int			   maxNames,
63ba6a1819Smrg    int			  *count,
64ba6a1819Smrg    FSXFontInfoHeader	***info,
65ba6a1819Smrg    FSPropInfo		***pprops,
66ba6a1819Smrg    FSPropOffset	***offsets,
67ba6a1819Smrg    unsigned char	***prop_data)
68ba6a1819Smrg{
69ba6a1819Smrg    long        nbytes;
70ba6a1819Smrg    int         i,
71ba6a1819Smrg                j;
72ba6a1819Smrg    size_t      size = 0;
73ba6a1819Smrg    FSXFontInfoHeader **fhdr = (FSXFontInfoHeader **) 0;
74ba6a1819Smrg    FSPropInfo **pi = (FSPropInfo **) 0;
75ba6a1819Smrg    FSPropOffset **po = (FSPropOffset **) 0;
76ba6a1819Smrg    unsigned char **pd = (unsigned char **) 0;
77ba6a1819Smrg    char      **flist = NULL;
78ba6a1819Smrg    fsListFontsWithXInfoReply reply;
79ba6a1819Smrg    fsListFontsWithXInfoReq *req;
80ba6a1819Smrg    fsPropInfo local_pi;
81ba6a1819Smrg    fsPropOffset local_po;
82ba6a1819Smrg    Status status;
83ba6a1819Smrg
84ba6a1819Smrg    GetReq(ListFontsWithXInfo, req);
85ba6a1819Smrg    req->maxNames = maxNames;
86ba6a1819Smrg    nbytes = req->nbytes = pattern ? strlen(pattern) : 0;
87ba6a1819Smrg    req->length += (nbytes + 3) >> 2;
88ba6a1819Smrg    _FSSend(svr, pattern, nbytes);
89ba6a1819Smrg
90ba6a1819Smrg    for (i = 0;; i++) {
91ba6a1819Smrg	if (FSProtocolVersion(svr) > 1)
92ba6a1819Smrg	{
93ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) &reply, 0, fsFalse);
94ba6a1819Smrg	    if (status != 0  &&  reply.nameLength == 0)	/* got last reply */
95ba6a1819Smrg		break;
96ba6a1819Smrg	    if (status)
97ba6a1819Smrg		_FSRead(svr, ((char *) &reply) + SIZEOF(fsGenericReply),
98ba6a1819Smrg			SIZEOF(fsListFontsWithXInfoReply) -
99ba6a1819Smrg			SIZEOF(fsGenericReply));
100ba6a1819Smrg	} else {
101ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) & reply,
102ba6a1819Smrg			      ((SIZEOF(fsListFontsWithXInfoReply) -
103ba6a1819Smrg				SIZEOF(fsGenericReply)) >> 2), fsFalse);
104ba6a1819Smrg	}
105ba6a1819Smrg	if (!status) {
106ba6a1819Smrg	    for (j = (i - 1); j >= 0; j--) {
107ba6a1819Smrg		FSfree((char *) fhdr[j]);
108ba6a1819Smrg		FSfree((char *) pi[j]);
109ba6a1819Smrg		FSfree((char *) po[j]);
110ba6a1819Smrg		FSfree((char *) pd[j]);
111ba6a1819Smrg		FSfree(flist[j]);
112ba6a1819Smrg	    }
113ba6a1819Smrg	    if (flist)
114ba6a1819Smrg		FSfree((char *) flist);
115ba6a1819Smrg	    if (fhdr)
116ba6a1819Smrg		FSfree((char *) fhdr);
117ba6a1819Smrg	    if (pi)
118ba6a1819Smrg		FSfree((char *) pi);
119ba6a1819Smrg	    if (po)
120ba6a1819Smrg		FSfree((char *) po);
121ba6a1819Smrg	    if (pd)
122ba6a1819Smrg		FSfree((char *) pd);
123ba6a1819Smrg
124ba6a1819Smrg	    SyncHandle();
125ba6a1819Smrg	    return (char **) NULL;
126ba6a1819Smrg	}
127ba6a1819Smrg	if (reply.nameLength == 0)	/* got last reply in version 1 */
128ba6a1819Smrg	    break;
129ba6a1819Smrg	if ((i + reply.nReplies) >= size) {
130ba6a1819Smrg
131ba6a1819Smrg	    if (reply.nReplies > SIZE_MAX - i - 1)
132ba6a1819Smrg		goto badmem;
133ba6a1819Smrg	    size = i + reply.nReplies + 1;
134ba6a1819Smrg
135ba6a1819Smrg	    if (size > SIZE_MAX / sizeof(char *))
136ba6a1819Smrg		goto badmem;
137ba6a1819Smrg
138ba6a1819Smrg	    if (fhdr) {
139ba6a1819Smrg		FSXFontInfoHeader **tmp_fhdr = (FSXFontInfoHeader **)
140ba6a1819Smrg		FSrealloc((char *) fhdr,
141ba6a1819Smrg			  (unsigned) (sizeof(FSXFontInfoHeader *) * size));
142ba6a1819Smrg		char      **tmp_flist = (char **) FSrealloc((char *) flist,
143ba6a1819Smrg					 (unsigned) (sizeof(char *) * size));
144ba6a1819Smrg		FSPropInfo **tmp_pi = (FSPropInfo **)
145ba6a1819Smrg		FSrealloc((char *) pi,
146ba6a1819Smrg			  (unsigned) (sizeof(FSPropInfo *) * size));
147ba6a1819Smrg		FSPropOffset **tmp_po = (FSPropOffset **)
148ba6a1819Smrg		FSrealloc((char *) po,
149ba6a1819Smrg			  (unsigned) (sizeof(FSPropOffset *) * size));
150ba6a1819Smrg		unsigned char **tmp_pd = (unsigned char **)
151ba6a1819Smrg		FSrealloc((char *) pd,
152ba6a1819Smrg			  (unsigned) (sizeof(unsigned char *) * size));
153ba6a1819Smrg
154ba6a1819Smrg		if (!tmp_fhdr || !tmp_flist || !tmp_pi || !tmp_po || !tmp_pd) {
155ba6a1819Smrg		    for (j = (i - 1); j >= 0; j--) {
156ba6a1819Smrg			FSfree((char *) flist[j]);
157ba6a1819Smrg			FSfree((char *) fhdr[j]);
158ba6a1819Smrg			FSfree((char *) pi[j]);
159ba6a1819Smrg			FSfree((char *) po[j]);
160ba6a1819Smrg			FSfree((char *) pd[j]);
161ba6a1819Smrg		    }
162ba6a1819Smrg		    if (tmp_flist)
163ba6a1819Smrg			FSfree((char *) tmp_flist);
164ba6a1819Smrg		    else
165ba6a1819Smrg			FSfree((char *) flist);
166ba6a1819Smrg		    if (tmp_fhdr)
167ba6a1819Smrg			FSfree((char *) tmp_fhdr);
168ba6a1819Smrg		    else
169ba6a1819Smrg			FSfree((char *) fhdr);
170ba6a1819Smrg		    if (tmp_pi)
171ba6a1819Smrg			FSfree((char *) tmp_pi);
172ba6a1819Smrg		    else
173ba6a1819Smrg			FSfree((char *) pi);
174ba6a1819Smrg		    if (tmp_po)
175ba6a1819Smrg			FSfree((char *) tmp_po);
176ba6a1819Smrg		    else
177ba6a1819Smrg			FSfree((char *) po);
178ba6a1819Smrg		    if (tmp_pd)
179ba6a1819Smrg			FSfree((char *) tmp_pd);
180ba6a1819Smrg		    else
181ba6a1819Smrg			FSfree((char *) pd);
182ba6a1819Smrg		    goto clearwire;
183ba6a1819Smrg		}
184ba6a1819Smrg		fhdr = tmp_fhdr;
185ba6a1819Smrg		flist = tmp_flist;
186ba6a1819Smrg		pi = tmp_pi;
187ba6a1819Smrg		po = tmp_po;
188ba6a1819Smrg		pd = tmp_pd;
189ba6a1819Smrg	    } else {
190ba6a1819Smrg		if (!(fhdr = (FSXFontInfoHeader **)
191ba6a1819Smrg		      FSmalloc((unsigned) (sizeof(FSXFontInfoHeader *) * size))))
192ba6a1819Smrg		    goto clearwire;
193ba6a1819Smrg		if (!(flist = (char **)
194ba6a1819Smrg		      FSmalloc((unsigned) (sizeof(char *) * size)))) {
195ba6a1819Smrg		    FSfree((char *) fhdr);
196ba6a1819Smrg		    goto clearwire;
197ba6a1819Smrg		}
198ba6a1819Smrg		if (!(pi = (FSPropInfo **)
199ba6a1819Smrg		      FSmalloc((unsigned) (sizeof(FSPropInfo *) * size)))) {
200ba6a1819Smrg		    FSfree((char *) fhdr);
201ba6a1819Smrg		    FSfree((char *) flist);
202ba6a1819Smrg		    goto clearwire;
203ba6a1819Smrg		}
204ba6a1819Smrg		if (!(po = (FSPropOffset **)
205ba6a1819Smrg		      FSmalloc((unsigned) (sizeof(FSPropOffset *) * size)))) {
206ba6a1819Smrg		    FSfree((char *) fhdr);
207ba6a1819Smrg		    FSfree((char *) flist);
208ba6a1819Smrg		    FSfree((char *) pi);
209ba6a1819Smrg		    goto clearwire;
210ba6a1819Smrg		}
211ba6a1819Smrg		if (!(pd = (unsigned char **)
212ba6a1819Smrg		    FSmalloc((unsigned) (sizeof(unsigned char *) * size)))) {
213ba6a1819Smrg		    FSfree((char *) fhdr);
214ba6a1819Smrg		    FSfree((char *) flist);
215ba6a1819Smrg		    FSfree((char *) pi);
216ba6a1819Smrg		    FSfree((char *) po);
217ba6a1819Smrg		    goto clearwire;
218ba6a1819Smrg		}
219ba6a1819Smrg	    }
220ba6a1819Smrg	}
221ba6a1819Smrg	fhdr[i] = (FSXFontInfoHeader *) FSmalloc(sizeof(FSXFontInfoHeader));
222ba6a1819Smrg	if (!fhdr[i]) {
223ba6a1819Smrg	    goto badmem;
224ba6a1819Smrg	}
225ba6a1819Smrg	FSUnpack_XFontInfoHeader(&reply, fhdr[i], FSProtocolVersion(svr));
226ba6a1819Smrg
227ba6a1819Smrg	/* alloc space for the name */
228ba6a1819Smrg	flist[i] = (char *) FSmalloc((unsigned int) (reply.nameLength + 1));
229ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
230ba6a1819Smrg	{
231ba6a1819Smrg	    /* get the name */
232ba6a1819Smrg	    if (!flist[i]) {
233ba6a1819Smrg		nbytes = (reply.nameLength + 3) & ~3;
234ba6a1819Smrg		_FSEatData(svr, (unsigned long) nbytes);
235ba6a1819Smrg		goto badmem;
236ba6a1819Smrg	    }
237ba6a1819Smrg	    _FSReadPad(svr, flist[i], (long) reply.nameLength);
238ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
239ba6a1819Smrg	}
240ba6a1819Smrg
241ba6a1819Smrg	pi[i] = (FSPropInfo *) FSmalloc(sizeof(FSPropInfo));
242ba6a1819Smrg	if (!pi[i]) {
243ba6a1819Smrg	    FSfree((char *) fhdr[i]);
244ba6a1819Smrg	    goto badmem;
245ba6a1819Smrg	}
246ba6a1819Smrg	_FSReadPad(svr, (char *) &local_pi, SIZEOF(fsPropInfo));
247ba6a1819Smrg	pi[i]->num_offsets = local_pi.num_offsets;
248ba6a1819Smrg	pi[i]->data_len = local_pi.data_len;
249ba6a1819Smrg
250ba6a1819Smrg#if SIZE_MAX <= UINT_MAX
251ba6a1819Smrg	if (pi[i]->num_offsets > SIZE_MAX / sizeof(FSPropOffset))
252ba6a1819Smrg	    goto badmem;
253ba6a1819Smrg#endif
254ba6a1819Smrg
255ba6a1819Smrg	po[i] = (FSPropOffset *)
256ba6a1819Smrg	    FSmalloc(pi[i]->num_offsets * sizeof(FSPropOffset));
257ba6a1819Smrg	if (!po[i]) {
258ba6a1819Smrg	    FSfree((char *) fhdr[i]);
259ba6a1819Smrg	    FSfree((char *) pi[i]);
260ba6a1819Smrg	    goto badmem;
261ba6a1819Smrg	}
262ba6a1819Smrg	pd[i] = (unsigned char *) FSmalloc(pi[i]->data_len);
263ba6a1819Smrg	if (!pd[i]) {
264ba6a1819Smrg	    FSfree((char *) fhdr[i]);
265ba6a1819Smrg	    FSfree((char *) pi[i]);
266ba6a1819Smrg	    FSfree((char *) po[i]);
267ba6a1819Smrg	    goto badmem;
268ba6a1819Smrg	}
269ba6a1819Smrg	/* get offsets */
270ba6a1819Smrg	for (j=0; j<pi[i]->num_offsets; j++)
271ba6a1819Smrg	{
272ba6a1819Smrg	    _FSReadPad(svr, (char *) &local_po, SIZEOF(fsPropOffset));
273ba6a1819Smrg	    po[i][j].name.position = local_po.name.position;
274ba6a1819Smrg	    po[i][j].name.length = local_po.name.length;
275ba6a1819Smrg	    po[i][j].value.position = local_po.value.position;
276ba6a1819Smrg	    po[i][j].value.length = local_po.value.length;
277ba6a1819Smrg	    po[i][j].type = local_po.type;
278ba6a1819Smrg	}
279ba6a1819Smrg
280ba6a1819Smrg	/* get prop data */
281ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
282ba6a1819Smrg	    _FSReadPad(svr, (char *) pd[i], pi[i]->data_len);
283ba6a1819Smrg	else
284ba6a1819Smrg	    _FSRead(svr, (char *) pd[i], pi[i]->data_len);
285ba6a1819Smrg
286ba6a1819Smrg	if (FSProtocolVersion(svr) != 1)
287ba6a1819Smrg	{
288ba6a1819Smrg	    /* get the name */
289ba6a1819Smrg	    if (!flist[i]) {
290ba6a1819Smrg		nbytes = (reply.nameLength + 3) & ~3;
291ba6a1819Smrg		_FSEatData(svr, (unsigned long) nbytes);
292ba6a1819Smrg		goto badmem;
293ba6a1819Smrg	    }
294ba6a1819Smrg	    _FSRead(svr, flist[i], (long) reply.nameLength);
295ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
296ba6a1819Smrg
297ba6a1819Smrg	    nbytes = pi[i]->data_len + reply.nameLength;
298ba6a1819Smrg	    _FSEatData(svr, (unsigned long) (((nbytes+3)&~3) - nbytes));
299ba6a1819Smrg	}
300ba6a1819Smrg	/* avoid integer overflow */
301ba6a1819Smrg	if (i > INT_MAX - 1) {
302ba6a1819Smrg	    goto badmem;
303ba6a1819Smrg	}
304ba6a1819Smrg    }
305ba6a1819Smrg    *info = fhdr;
306ba6a1819Smrg    *count = i;
307ba6a1819Smrg    *pprops = pi;
308ba6a1819Smrg    *offsets = po;
309ba6a1819Smrg    *prop_data = pd;
310ba6a1819Smrg    SyncHandle();
311ba6a1819Smrg    return flist;
312ba6a1819Smrg
313ba6a1819Smrgbadmem:
314ba6a1819Smrg    for (j = (i - 1); j >= 0; j--) {
315ba6a1819Smrg	FSfree((char *) pi[j]);
316ba6a1819Smrg	FSfree((char *) po[j]);
317ba6a1819Smrg	FSfree((char *) pd[j]);
318ba6a1819Smrg	FSfree(flist[j]);
319ba6a1819Smrg	FSfree((char *) fhdr[j]);
320ba6a1819Smrg    }
321ba6a1819Smrg    if (flist)
322ba6a1819Smrg	FSfree((char *) flist);
323ba6a1819Smrg    if (fhdr)
324ba6a1819Smrg	FSfree((char *) fhdr);
325ba6a1819Smrg    if (pi)
326ba6a1819Smrg	FSfree((char *) pi);
327ba6a1819Smrg    if (po)
328ba6a1819Smrg	FSfree((char *) po);
329ba6a1819Smrg    if (pd)
330ba6a1819Smrg	FSfree((char *) pd);
331ba6a1819Smrg
332ba6a1819Smrg
333ba6a1819Smrgclearwire:
334ba6a1819Smrg    do {
335ba6a1819Smrg	fsPropInfo  ti;
336ba6a1819Smrg
337ba6a1819Smrg	_FSEatData(svr, (reply.nameLength + 3) & ~3);
338ba6a1819Smrg	_FSReadPad(svr, (char *) &ti, SIZEOF(fsPropInfo));
339ba6a1819Smrg	_FSEatData(svr, (SIZEOF(fsPropOffset) * ti.num_offsets));
340ba6a1819Smrg	_FSEatData(svr, ti.data_len);
341ba6a1819Smrg    } while (_FSReply(svr, (fsReply *) & reply,
342ba6a1819Smrg		      ((SIZEOF(fsListFontsWithXInfoReply)
343ba6a1819Smrg       - SIZEOF(fsGenericReply)) >> 2), fsFalse) && (reply.nameLength != 0));
344ba6a1819Smrg    SyncHandle();
345ba6a1819Smrg    return (char **) NULL;
346ba6a1819Smrg}
347