FontNames.c revision 5c4cfbd9
1/*
2
3Copyright 1986, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "Xlibint.h"
32#include "reallocarray.h"
33#include <limits.h>
34
35char **
36XListFonts(
37register Display *dpy,
38_Xconst char *pattern,  /* null-terminated */
39int maxNames,
40int *actualCount)	/* RETURN */
41{
42    register long nbytes;
43    register unsigned i;
44    register int length;
45    char **flist = NULL;
46    char *ch = NULL;
47    char *chstart;
48    char *chend;
49    int count = 0;
50    xListFontsReply rep;
51    register xListFontsReq *req;
52    unsigned long rlen = 0;
53
54    if (strlen(pattern) >= USHRT_MAX)
55        return NULL;
56
57    LockDisplay(dpy);
58    GetReq(ListFonts, req);
59    req->maxNames = maxNames;
60    nbytes = req->nbytes = pattern ? (CARD16) strlen (pattern) : 0;
61    req->length += (nbytes + 3) >> 2;
62    _XSend (dpy, pattern, nbytes);
63    /* use _XSend instead of Data, since following _XReply will flush buffer */
64
65    if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) {
66	*actualCount = 0;
67	UnlockDisplay(dpy);
68	SyncHandle();
69	return (char **) NULL;
70    }
71
72    if (rep.nFonts) {
73	flist = Xmallocarray (rep.nFonts, sizeof(char *));
74	if (rep.length > 0 && rep.length < (INT_MAX >> 2)) {
75	    rlen = rep.length << 2;
76	    ch = Xmalloc(rlen + 1);
77	    /* +1 to leave room for last null-terminator */
78	}
79
80	if ((! flist) || (! ch)) {
81	    Xfree(flist);
82	    Xfree(ch);
83	    _XEatDataWords(dpy, rep.length);
84	    *actualCount = 0;
85	    UnlockDisplay(dpy);
86	    SyncHandle();
87	    return (char **) NULL;
88	}
89
90	_XReadPad (dpy, ch, rlen);
91	/*
92	 * unpack into null terminated strings.
93	 */
94	chstart = ch;
95	chend = ch + rlen;
96	length = *(unsigned char *)ch;
97	*ch = 1; /* make sure it is non-zero for XFreeFontNames */
98	for (i = 0; i < rep.nFonts; i++) {
99	    if (ch + length < chend) {
100		flist[i] = ch + 1;  /* skip over length */
101		ch += length + 1;  /* find next length ... */
102		length = *(unsigned char *)ch;
103		*ch = '\0';  /* and replace with null-termination */
104		count++;
105	    } else {
106                Xfree(chstart);
107                Xfree(flist);
108                flist = NULL;
109                count = 0;
110                break;
111            }
112	}
113    }
114    *actualCount = count;
115    UnlockDisplay(dpy);
116    SyncHandle();
117    return (flist);
118}
119
120int
121XFreeFontNames(char **list)
122{
123	if (list) {
124		if (!*(list[0]-1)) { /* from ListFontsWithInfo */
125			register char **names;
126			for (names = list+1; *names; names++)
127				Xfree (*names);
128		}
129		Xfree (list[0]-1);
130		Xfree (list);
131	}
132	return 1;
133}
134