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{
67fe4c343aSmrg    int         i;
68ba6a1819Smrg    size_t      size = 0;
691bedbe3fSmrg    FSXFontInfoHeader **fhdr = (FSXFontInfoHeader **) NULL;
701bedbe3fSmrg    FSPropInfo **pi = (FSPropInfo **) NULL;
711bedbe3fSmrg    FSPropOffset **po = (FSPropOffset **) NULL;
721bedbe3fSmrg    unsigned char **pd = (unsigned char **) NULL;
73ba6a1819Smrg    char      **flist = NULL;
74ba6a1819Smrg    fsListFontsWithXInfoReply reply;
75ba6a1819Smrg    fsListFontsWithXInfoReq *req;
76ba6a1819Smrg    fsPropInfo local_pi;
77ba6a1819Smrg    fsPropOffset local_po;
78ba6a1819Smrg    Status status;
791bedbe3fSmrg    Bool eat_data = True;
80ba6a1819Smrg
81ba6a1819Smrg    GetReq(ListFontsWithXInfo, req);
82fe4c343aSmrg    req->maxNames = (CARD32) maxNames;
83fe4c343aSmrg    req->nbytes = 0;
84fe4c343aSmrg    if (pattern != NULL) {
85fe4c343aSmrg        size_t nbytes;
86fe4c343aSmrg
87fe4c343aSmrg#ifdef HAVE_STRNLEN
88fe4c343aSmrg        nbytes = strnlen(pattern, FSMaxRequestBytes(svr));
89fe4c343aSmrg#else
90fe4c343aSmrg        nbytes = strlen(pattern);
91fe4c343aSmrg#endif
92fe4c343aSmrg
93fe4c343aSmrg        if (nbytes <= (FSMaxRequestBytes(svr) -
94fe4c343aSmrg                       SIZEOF(fsListFontsWithXInfoReq))) {
95fe4c343aSmrg            req->nbytes = (CARD16) nbytes;
96fe4c343aSmrg            req->length += (CARD16) ((nbytes + 3) >> 2);
97fe4c343aSmrg            _FSSend(svr, pattern, (long) nbytes);
98fe4c343aSmrg        }
99fe4c343aSmrg    }
100ba6a1819Smrg
101ba6a1819Smrg    for (i = 0;; i++) {
102ba6a1819Smrg	if (FSProtocolVersion(svr) > 1)
103ba6a1819Smrg	{
104ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) &reply, 0, fsFalse);
105ba6a1819Smrg	    if (status != 0  &&  reply.nameLength == 0)	/* got last reply */
106ba6a1819Smrg		break;
107ba6a1819Smrg	    if (status)
108ba6a1819Smrg		_FSRead(svr, ((char *) &reply) + SIZEOF(fsGenericReply),
109ba6a1819Smrg			SIZEOF(fsListFontsWithXInfoReply) -
110ba6a1819Smrg			SIZEOF(fsGenericReply));
111ba6a1819Smrg	} else {
112ba6a1819Smrg	    status = _FSReply(svr, (fsReply *) & reply,
113ba6a1819Smrg			      ((SIZEOF(fsListFontsWithXInfoReply) -
114ba6a1819Smrg				SIZEOF(fsGenericReply)) >> 2), fsFalse);
115ba6a1819Smrg	}
116ba6a1819Smrg	if (!status) {
1171bedbe3fSmrg	    eat_data = False;
1181bedbe3fSmrg	    goto badmem;
119ba6a1819Smrg	}
120ba6a1819Smrg	if (reply.nameLength == 0)	/* got last reply in version 1 */
121ba6a1819Smrg	    break;
122ba6a1819Smrg	if ((i + reply.nReplies) >= size) {
123ba6a1819Smrg
1241bedbe3fSmrg	    if (reply.nReplies > SIZE_MAX - i - 1)
125ba6a1819Smrg		goto badmem;
126ba6a1819Smrg	    size = i + reply.nReplies + 1;
127ba6a1819Smrg
1281bedbe3fSmrg	    if (size > SIZE_MAX / sizeof(char *))
129ba6a1819Smrg		goto badmem;
130ba6a1819Smrg
131ba6a1819Smrg	    if (fhdr) {
1321bedbe3fSmrg#define ResizeArray(var, type) { \
133da1f2d5dSmrg		    type **tmp = FSreallocarray(var, size, sizeof(type *)); \
1341bedbe3fSmrg		    if (tmp)						\
1351bedbe3fSmrg			var = tmp;					\
1361bedbe3fSmrg		    else						\
1371bedbe3fSmrg			goto badmem;					\
138ba6a1819Smrg		}
1391bedbe3fSmrg
1401bedbe3fSmrg		ResizeArray(fhdr, FSXFontInfoHeader)
1411bedbe3fSmrg		ResizeArray(flist, char)
1421bedbe3fSmrg		ResizeArray(pi, FSPropInfo)
1431bedbe3fSmrg		ResizeArray(po, FSPropOffset)
1441bedbe3fSmrg		ResizeArray(pd, unsigned char)
145ba6a1819Smrg	    } else {
1461bedbe3fSmrg#define InitArray(var, type) \
147da1f2d5dSmrg		if ((var = FSmallocarray(size, sizeof(type *))) == NULL) { \
1481bedbe3fSmrg		    goto badmem;					\
149ba6a1819Smrg		}
1501bedbe3fSmrg
1511bedbe3fSmrg		InitArray(fhdr, FSXFontInfoHeader)
1521bedbe3fSmrg		InitArray(flist, char)
1531bedbe3fSmrg		InitArray(pi, FSPropInfo)
1541bedbe3fSmrg		InitArray(po, FSPropOffset)
1551bedbe3fSmrg		InitArray(pd, unsigned char)
156ba6a1819Smrg	    }
157ba6a1819Smrg	}
1581bedbe3fSmrg	fhdr[i] = FSmalloc(sizeof(FSXFontInfoHeader));
159ba6a1819Smrg	if (!fhdr[i]) {
160ba6a1819Smrg	    goto badmem;
161ba6a1819Smrg	}
162ba6a1819Smrg	FSUnpack_XFontInfoHeader(&reply, fhdr[i], FSProtocolVersion(svr));
163ba6a1819Smrg
164ba6a1819Smrg	/* alloc space for the name */
1651bedbe3fSmrg	flist[i] = FSmalloc(reply.nameLength + 1);
1661bedbe3fSmrg	if (!flist[i])
1671bedbe3fSmrg	    goto cleanfhdr;
168ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
169ba6a1819Smrg	{
170ba6a1819Smrg	    /* get the name */
171ba6a1819Smrg	    _FSReadPad(svr, flist[i], (long) reply.nameLength);
172ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
173ba6a1819Smrg	}
174ba6a1819Smrg
1751bedbe3fSmrg	pi[i] = FSmalloc(sizeof(FSPropInfo));
1761bedbe3fSmrg	if (!pi[i])
1771bedbe3fSmrg	    goto cleanflist;
178ba6a1819Smrg	_FSReadPad(svr, (char *) &local_pi, SIZEOF(fsPropInfo));
179ba6a1819Smrg	pi[i]->num_offsets = local_pi.num_offsets;
180ba6a1819Smrg	pi[i]->data_len = local_pi.data_len;
181ba6a1819Smrg
182ba6a1819Smrg#if SIZE_MAX <= UINT_MAX
183ba6a1819Smrg	if (pi[i]->num_offsets > SIZE_MAX / sizeof(FSPropOffset))
1841bedbe3fSmrg	    goto cleanpi;
185ba6a1819Smrg#endif
186ba6a1819Smrg
187da1f2d5dSmrg	po[i] = FSmallocarray(pi[i]->num_offsets, sizeof(FSPropOffset));
1881bedbe3fSmrg	if (!po[i])
1891bedbe3fSmrg	    goto cleanpi;
1901bedbe3fSmrg	pd[i] = FSmalloc(pi[i]->data_len);
1911bedbe3fSmrg	if (!pd[i])
1921bedbe3fSmrg	    goto cleanpo;
193ba6a1819Smrg	/* get offsets */
194fe4c343aSmrg	for (unsigned int j = 0; j < pi[i]->num_offsets; j++)
195ba6a1819Smrg	{
196ba6a1819Smrg	    _FSReadPad(svr, (char *) &local_po, SIZEOF(fsPropOffset));
197ba6a1819Smrg	    po[i][j].name.position = local_po.name.position;
198ba6a1819Smrg	    po[i][j].name.length = local_po.name.length;
199ba6a1819Smrg	    po[i][j].value.position = local_po.value.position;
200ba6a1819Smrg	    po[i][j].value.length = local_po.value.length;
201ba6a1819Smrg	    po[i][j].type = local_po.type;
202ba6a1819Smrg	}
2031bedbe3fSmrg
204ba6a1819Smrg	/* get prop data */
205ba6a1819Smrg	if (FSProtocolVersion(svr) == 1)
206ba6a1819Smrg	    _FSReadPad(svr, (char *) pd[i], pi[i]->data_len);
207ba6a1819Smrg	else
208ba6a1819Smrg	    _FSRead(svr, (char *) pd[i], pi[i]->data_len);
209ba6a1819Smrg
210ba6a1819Smrg	if (FSProtocolVersion(svr) != 1)
211ba6a1819Smrg	{
212fe4c343aSmrg	    unsigned long	nbytes;
213fe4c343aSmrg
214ba6a1819Smrg	    /* get the name */
215ba6a1819Smrg	    _FSRead(svr, flist[i], (long) reply.nameLength);
216ba6a1819Smrg	    flist[i][reply.nameLength] = '\0';
217ba6a1819Smrg
218ba6a1819Smrg	    nbytes = pi[i]->data_len + reply.nameLength;
219ba6a1819Smrg	    _FSEatData(svr, (unsigned long) (((nbytes+3)&~3) - nbytes));
220ba6a1819Smrg	}
221ba6a1819Smrg	/* avoid integer overflow */
222ba6a1819Smrg	if (i > INT_MAX - 1) {
2231bedbe3fSmrg	    goto cleanpd;
224ba6a1819Smrg	}
225ba6a1819Smrg    }
226ba6a1819Smrg    *info = fhdr;
227ba6a1819Smrg    *count = i;
228ba6a1819Smrg    *pprops = pi;
229ba6a1819Smrg    *offsets = po;
230ba6a1819Smrg    *prop_data = pd;
231ba6a1819Smrg    SyncHandle();
232ba6a1819Smrg    return flist;
233ba6a1819Smrg
2341bedbe3fSmrg/* Error cleanup for when we're partway through filling in item #i in arrays */
2351bedbe3fSmrgcleanpd:
2361bedbe3fSmrg    FSfree(pd[i]);
2371bedbe3fSmrgcleanpo:
2381bedbe3fSmrg    FSfree(po[i]);
2391bedbe3fSmrgcleanpi:
2401bedbe3fSmrg    FSfree(pi[i]);
2411bedbe3fSmrgcleanflist:
2421bedbe3fSmrg    FSfree(flist[i]);
2431bedbe3fSmrgcleanfhdr:
2441bedbe3fSmrg    FSfree(fhdr[i]);
2451bedbe3fSmrg/* Error cleanup for all previously filled in items in the arrays */
246ba6a1819Smrgbadmem:
247fe4c343aSmrg    for (int j = (i - 1); j >= 0; j--) {
2481bedbe3fSmrg	FSfree(pi[j]);
2491bedbe3fSmrg	FSfree(po[j]);
2501bedbe3fSmrg	FSfree(pd[j]);
251ba6a1819Smrg	FSfree(flist[j]);
2521bedbe3fSmrg	FSfree(fhdr[j]);
253ba6a1819Smrg    }
2541bedbe3fSmrg    FSfree(flist);
2551bedbe3fSmrg    FSfree(fhdr);
2561bedbe3fSmrg    FSfree(pi);
2571bedbe3fSmrg    FSfree(po);
2581bedbe3fSmrg    FSfree(pd);
259ba6a1819Smrg
2601bedbe3fSmrg    if (eat_data) {
2611bedbe3fSmrg	do {
2621bedbe3fSmrg	    fsPropInfo  ti;
263ba6a1819Smrg
2641bedbe3fSmrg	    _FSEatData(svr, (reply.nameLength + 3) & ~3);
2651bedbe3fSmrg	    _FSReadPad(svr, (char *) &ti, SIZEOF(fsPropInfo));
2661bedbe3fSmrg	    _FSEatData(svr, (SIZEOF(fsPropOffset) * ti.num_offsets));
2671bedbe3fSmrg	    _FSEatData(svr, ti.data_len);
2681bedbe3fSmrg	} while (_FSReply(svr, (fsReply *) &reply,
2691bedbe3fSmrg			  ((SIZEOF(fsListFontsWithXInfoReply)
2701bedbe3fSmrg			    - SIZEOF(fsGenericReply)) >> 2), fsFalse)
2711bedbe3fSmrg		 && (reply.nameLength != 0));
2721bedbe3fSmrg    }
273ba6a1819Smrg    SyncHandle();
274ba6a1819Smrg    return (char **) NULL;
275ba6a1819Smrg}
276