FSFontInfo.c revision 1bedbe3f
1ba6a1819Smrg/*
2ba6a1819Smrg * Copyright 1990 Network Computing Devices;
3ba6a1819Smrg * Portions Copyright 1987 by Digital Equipment Corporation
4ba6a1819Smrg *
51bedbe3fSmrg * Permission to use, copy, modify, distribute, and sell this software
61bedbe3fSmrg * and its documentation for any purpose is hereby granted without fee,
71bedbe3fSmrg * provided that the above copyright notice appear in all copies and
81bedbe3fSmrg * that both that copyright notice and this permission notice appear
91bedbe3fSmrg * in supporting documentation, and that the names of Network Computing
101bedbe3fSmrg * Devices or Digital not be used in advertising or publicity pertaining
111bedbe3fSmrg * to distribution of the software without specific, written prior
121bedbe3fSmrg * permission. Network Computing Devices or Digital make no representations
131bedbe3fSmrg * about the suitability of this software for any purpose.  It is provided
14ba6a1819Smrg * "as is" without express or implied warranty.
15ba6a1819Smrg *
16ba6a1819Smrg * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
171bedbe3fSmrg * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18ba6a1819Smrg * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
191bedbe3fSmrg * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
201bedbe3fSmrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
211bedbe3fSmrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
221bedbe3fSmrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23ba6a1819Smrg * SOFTWARE.
24ba6a1819Smrg */
25ba6a1819Smrg
26ba6a1819Smrg/*
27ba6a1819Smrg
28ba6a1819SmrgCopyright 1987, 1998  The Open Group
29ba6a1819Smrg
30ba6a1819SmrgPermission to use, copy, modify, distribute, and sell this software and its
31ba6a1819Smrgdocumentation for any purpose is hereby granted without fee, provided that
32ba6a1819Smrgthe above copyright notice appear in all copies and that both that
33ba6a1819Smrgcopyright notice and this permission notice appear in supporting
34ba6a1819Smrgdocumentation.
35ba6a1819Smrg
36ba6a1819SmrgThe above copyright notice and this permission notice shall be included in
37ba6a1819Smrgall copies or substantial portions of the Software.
38ba6a1819Smrg
39ba6a1819SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40ba6a1819SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41ba6a1819SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42ba6a1819SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43ba6a1819SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44ba6a1819SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45ba6a1819Smrg
46ba6a1819SmrgExcept as contained in this notice, the name of The Open Group shall not be
47ba6a1819Smrgused in advertising or otherwise to promote the sale, use or other dealings
48ba6a1819Smrgin this Software without prior written authorization from The Open Group.
49ba6a1819Smrg
50ba6a1819Smrg*/
51ba6a1819Smrg#ifdef HAVE_CONFIG_H
52ba6a1819Smrg#include <config.h>
53ba6a1819Smrg#endif
54ba6a1819Smrg#include	"FSlibint.h"
55ba6a1819Smrg
56ba6a1819Smrgchar      **
57ba6a1819SmrgFSListFontsWithXInfo(
58ba6a1819Smrg    FSServer		  *svr,
591bedbe3fSmrg    const char		  *pattern,
60ba6a1819Smrg    int			   maxNames,
61ba6a1819Smrg    int			  *count,
62ba6a1819Smrg    FSXFontInfoHeader	***info,
63ba6a1819Smrg    FSPropInfo		***pprops,
64ba6a1819Smrg    FSPropOffset	***offsets,
65ba6a1819Smrg    unsigned char	***prop_data)
66ba6a1819Smrg{
67ba6a1819Smrg    long        nbytes;
68ba6a1819Smrg    int         i,
69ba6a1819Smrg                j;
70ba6a1819Smrg    size_t      size = 0;
711bedbe3fSmrg    FSXFontInfoHeader **fhdr = (FSXFontInfoHeader **) NULL;
721bedbe3fSmrg    FSPropInfo **pi = (FSPropInfo **) NULL;
731bedbe3fSmrg    FSPropOffset **po = (FSPropOffset **) NULL;
741bedbe3fSmrg    unsigned char **pd = (unsigned char **) NULL;
75ba6a1819Smrg    char      **flist = NULL;
76ba6a1819Smrg    fsListFontsWithXInfoReply reply;
77ba6a1819Smrg    fsListFontsWithXInfoReq *req;
78ba6a1819Smrg    fsPropInfo local_pi;
79ba6a1819Smrg    fsPropOffset local_po;
80ba6a1819Smrg    Status status;
811bedbe3fSmrg    Bool eat_data = True;
82ba6a1819Smrg
83ba6a1819Smrg    GetReq(ListFontsWithXInfo, req);
84ba6a1819Smrg    req->maxNames = maxNames;
85ba6a1819Smrg    nbytes = req->nbytes = pattern ? strlen(pattern) : 0;
86ba6a1819Smrg    req->length += (nbytes + 3) >> 2;
87ba6a1819Smrg    _FSSend(svr, pattern, nbytes);
88ba6a1819Smrg
89ba6a1819Smrg    for (i = 0;; i++) {
90ba6a1819Smrg	if (FSProtocolVersion(svr) > 1)
91ba6a1819Smrg	{
92ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) &reply, 0, fsFalse);
93ba6a1819Smrg	    if (status != 0  &&  reply.nameLength == 0)	/* got last reply */
94ba6a1819Smrg		break;
95ba6a1819Smrg	    if (status)
96ba6a1819Smrg		_FSRead(svr, ((char *) &reply) + SIZEOF(fsGenericReply),
97ba6a1819Smrg			SIZEOF(fsListFontsWithXInfoReply) -
98ba6a1819Smrg			SIZEOF(fsGenericReply));
99ba6a1819Smrg	} else {
100ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) & reply,
101ba6a1819Smrg			      ((SIZEOF(fsListFontsWithXInfoReply) -
102ba6a1819Smrg				SIZEOF(fsGenericReply)) >> 2), fsFalse);
103ba6a1819Smrg	}
104ba6a1819Smrg	if (!status) {
1051bedbe3fSmrg	    eat_data = False;
1061bedbe3fSmrg	    goto badmem;
107ba6a1819Smrg	}
108ba6a1819Smrg	if (reply.nameLength == 0)	/* got last reply in version 1 */
109ba6a1819Smrg	    break;
110ba6a1819Smrg	if ((i + reply.nReplies) >= size) {
111ba6a1819Smrg
1121bedbe3fSmrg	    if (reply.nReplies > SIZE_MAX - i - 1)
113ba6a1819Smrg		goto badmem;
114ba6a1819Smrg	    size = i + reply.nReplies + 1;
115ba6a1819Smrg
1161bedbe3fSmrg	    if (size > SIZE_MAX / sizeof(char *))
117ba6a1819Smrg		goto badmem;
118ba6a1819Smrg
119ba6a1819Smrg	    if (fhdr) {
1201bedbe3fSmrg#define ResizeArray(var, type) { \
1211bedbe3fSmrg		    type **tmp = FSrealloc(var, sizeof(type *) * size); \
1221bedbe3fSmrg		    if (tmp)						\
1231bedbe3fSmrg			var = tmp;					\
1241bedbe3fSmrg		    else						\
1251bedbe3fSmrg			goto badmem;					\
126ba6a1819Smrg		}
1271bedbe3fSmrg
1281bedbe3fSmrg		ResizeArray(fhdr, FSXFontInfoHeader)
1291bedbe3fSmrg		ResizeArray(flist, char)
1301bedbe3fSmrg		ResizeArray(pi, FSPropInfo)
1311bedbe3fSmrg		ResizeArray(po, FSPropOffset)
1321bedbe3fSmrg		ResizeArray(pd, unsigned char)
133ba6a1819Smrg	    } else {
1341bedbe3fSmrg#define InitArray(var, type) \
1351bedbe3fSmrg		if ((var = FSmalloc(sizeof(type *) * size)) == NULL) {	\
1361bedbe3fSmrg		    goto badmem;					\
137ba6a1819Smrg		}
1381bedbe3fSmrg
1391bedbe3fSmrg		InitArray(fhdr, FSXFontInfoHeader)
1401bedbe3fSmrg		InitArray(flist, char)
1411bedbe3fSmrg		InitArray(pi, FSPropInfo)
1421bedbe3fSmrg		InitArray(po, FSPropOffset)
1431bedbe3fSmrg		InitArray(pd, unsigned char)
144ba6a1819Smrg	    }
145ba6a1819Smrg	}
1461bedbe3fSmrg	fhdr[i] = FSmalloc(sizeof(FSXFontInfoHeader));
147ba6a1819Smrg	if (!fhdr[i]) {
148ba6a1819Smrg	    goto badmem;
149ba6a1819Smrg	}
150ba6a1819Smrg	FSUnpack_XFontInfoHeader(&reply, fhdr[i], FSProtocolVersion(svr));
151ba6a1819Smrg
152ba6a1819Smrg	/* alloc space for the name */
1531bedbe3fSmrg	flist[i] = FSmalloc(reply.nameLength + 1);
1541bedbe3fSmrg	if (!flist[i])
1551bedbe3fSmrg	    goto cleanfhdr;
156ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
157ba6a1819Smrg	{
158ba6a1819Smrg	    /* get the name */
159ba6a1819Smrg	    _FSReadPad(svr, flist[i], (long) reply.nameLength);
160ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
161ba6a1819Smrg	}
162ba6a1819Smrg
1631bedbe3fSmrg	pi[i] = FSmalloc(sizeof(FSPropInfo));
1641bedbe3fSmrg	if (!pi[i])
1651bedbe3fSmrg	    goto cleanflist;
166ba6a1819Smrg	_FSReadPad(svr, (char *) &local_pi, SIZEOF(fsPropInfo));
167ba6a1819Smrg	pi[i]->num_offsets = local_pi.num_offsets;
168ba6a1819Smrg	pi[i]->data_len = local_pi.data_len;
169ba6a1819Smrg
170ba6a1819Smrg#if SIZE_MAX <= UINT_MAX
171ba6a1819Smrg	if (pi[i]->num_offsets > SIZE_MAX / sizeof(FSPropOffset))
1721bedbe3fSmrg	    goto cleanpi;
173ba6a1819Smrg#endif
174ba6a1819Smrg
1751bedbe3fSmrg	po[i] = FSmalloc(pi[i]->num_offsets * sizeof(FSPropOffset));
1761bedbe3fSmrg	if (!po[i])
1771bedbe3fSmrg	    goto cleanpi;
1781bedbe3fSmrg	pd[i] = FSmalloc(pi[i]->data_len);
1791bedbe3fSmrg	if (!pd[i])
1801bedbe3fSmrg	    goto cleanpo;
181ba6a1819Smrg	/* get offsets */
182ba6a1819Smrg	for (j=0; j<pi[i]->num_offsets; j++)
183ba6a1819Smrg	{
184ba6a1819Smrg	    _FSReadPad(svr, (char *) &local_po, SIZEOF(fsPropOffset));
185ba6a1819Smrg	    po[i][j].name.position = local_po.name.position;
186ba6a1819Smrg	    po[i][j].name.length = local_po.name.length;
187ba6a1819Smrg	    po[i][j].value.position = local_po.value.position;
188ba6a1819Smrg	    po[i][j].value.length = local_po.value.length;
189ba6a1819Smrg	    po[i][j].type = local_po.type;
190ba6a1819Smrg	}
1911bedbe3fSmrg
192ba6a1819Smrg	/* get prop data */
193ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
194ba6a1819Smrg	    _FSReadPad(svr, (char *) pd[i], pi[i]->data_len);
195ba6a1819Smrg	else
196ba6a1819Smrg	    _FSRead(svr, (char *) pd[i], pi[i]->data_len);
197ba6a1819Smrg
198ba6a1819Smrg	if (FSProtocolVersion(svr) != 1)
199ba6a1819Smrg	{
200ba6a1819Smrg	    /* get the name */
201ba6a1819Smrg	    _FSRead(svr, flist[i], (long) reply.nameLength);
202ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
203ba6a1819Smrg
204ba6a1819Smrg	    nbytes = pi[i]->data_len + reply.nameLength;
205ba6a1819Smrg	    _FSEatData(svr, (unsigned long) (((nbytes+3)&~3) - nbytes));
206ba6a1819Smrg	}
207ba6a1819Smrg	/* avoid integer overflow */
208ba6a1819Smrg	if (i > INT_MAX - 1) {
2091bedbe3fSmrg	    goto cleanpd;
210ba6a1819Smrg	}
211ba6a1819Smrg    }
212ba6a1819Smrg    *info = fhdr;
213ba6a1819Smrg    *count = i;
214ba6a1819Smrg    *pprops = pi;
215ba6a1819Smrg    *offsets = po;
216ba6a1819Smrg    *prop_data = pd;
217ba6a1819Smrg    SyncHandle();
218ba6a1819Smrg    return flist;
219ba6a1819Smrg
2201bedbe3fSmrg/* Error cleanup for when we're partway through filling in item #i in arrays */
2211bedbe3fSmrgcleanpd:
2221bedbe3fSmrg    FSfree(pd[i]);
2231bedbe3fSmrgcleanpo:
2241bedbe3fSmrg    FSfree(po[i]);
2251bedbe3fSmrgcleanpi:
2261bedbe3fSmrg    FSfree(pi[i]);
2271bedbe3fSmrgcleanflist:
2281bedbe3fSmrg    FSfree(flist[i]);
2291bedbe3fSmrgcleanfhdr:
2301bedbe3fSmrg    FSfree(fhdr[i]);
2311bedbe3fSmrg/* Error cleanup for all previously filled in items in the arrays */
232ba6a1819Smrgbadmem:
233ba6a1819Smrg    for (j = (i - 1); j >= 0; j--) {
2341bedbe3fSmrg	FSfree(pi[j]);
2351bedbe3fSmrg	FSfree(po[j]);
2361bedbe3fSmrg	FSfree(pd[j]);
237ba6a1819Smrg	FSfree(flist[j]);
2381bedbe3fSmrg	FSfree(fhdr[j]);
239ba6a1819Smrg    }
2401bedbe3fSmrg    FSfree(flist);
2411bedbe3fSmrg    FSfree(fhdr);
2421bedbe3fSmrg    FSfree(pi);
2431bedbe3fSmrg    FSfree(po);
2441bedbe3fSmrg    FSfree(pd);
245ba6a1819Smrg
2461bedbe3fSmrg    if (eat_data) {
2471bedbe3fSmrg	do {
2481bedbe3fSmrg	    fsPropInfo  ti;
249ba6a1819Smrg
2501bedbe3fSmrg	    _FSEatData(svr, (reply.nameLength + 3) & ~3);
2511bedbe3fSmrg	    _FSReadPad(svr, (char *) &ti, SIZEOF(fsPropInfo));
2521bedbe3fSmrg	    _FSEatData(svr, (SIZEOF(fsPropOffset) * ti.num_offsets));
2531bedbe3fSmrg	    _FSEatData(svr, ti.data_len);
2541bedbe3fSmrg	} while (_FSReply(svr, (fsReply *) &reply,
2551bedbe3fSmrg			  ((SIZEOF(fsListFontsWithXInfoReply)
2561bedbe3fSmrg			    - SIZEOF(fsGenericReply)) >> 2), fsFalse)
2571bedbe3fSmrg		 && (reply.nameLength != 0));
2581bedbe3fSmrg    }
259ba6a1819Smrg    SyncHandle();
260ba6a1819Smrg    return (char **) NULL;
261ba6a1819Smrg}
262