FontNames.c revision bd8e7fb0
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 <limits.h>
33
34char **
35XListFonts(
36register Display *dpy,
37_Xconst char *pattern,  /* null-terminated */
38int maxNames,
39int *actualCount)	/* RETURN */
40{
41    register long nbytes;
42    register unsigned i;
43    register int length;
44    char **flist = NULL;
45    char *ch = NULL;
46    char *chstart;
47    char *chend;
48    int count = 0;
49    xListFontsReply rep;
50    register xListFontsReq *req;
51    unsigned long rlen = 0;
52
53    LockDisplay(dpy);
54    GetReq(ListFonts, req);
55    req->maxNames = maxNames;
56    nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
57    req->length += (nbytes + 3) >> 2;
58    _XSend (dpy, pattern, nbytes);
59    /* use _XSend instead of Data, since following _XReply will flush buffer */
60
61    if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) {
62	*actualCount = 0;
63	UnlockDisplay(dpy);
64	SyncHandle();
65	return (char **) NULL;
66    }
67
68    if (rep.nFonts) {
69	flist = Xmalloc (rep.nFonts * sizeof(char *));
70	if (rep.length > 0 && rep.length < (INT_MAX >> 2)) {
71	    rlen = rep.length << 2;
72	    ch = Xmalloc(rlen + 1);
73	    /* +1 to leave room for last null-terminator */
74	}
75
76	if ((! flist) || (! ch)) {
77	    Xfree(flist);
78	    Xfree(ch);
79	    _XEatDataWords(dpy, rep.length);
80	    *actualCount = 0;
81	    UnlockDisplay(dpy);
82	    SyncHandle();
83	    return (char **) NULL;
84	}
85
86	_XReadPad (dpy, ch, rlen);
87	/*
88	 * unpack into null terminated strings.
89	 */
90	chstart = ch;
91	chend = ch + rlen;
92	length = *(unsigned char *)ch;
93	*ch = 1; /* make sure it is non-zero for XFreeFontNames */
94	for (i = 0; i < rep.nFonts; i++) {
95	    if (ch + length < chend) {
96		flist[i] = ch + 1;  /* skip over length */
97		ch += length + 1;  /* find next length ... */
98		length = *(unsigned char *)ch;
99		*ch = '\0';  /* and replace with null-termination */
100		count++;
101	    } else {
102                Xfree(chstart);
103                Xfree(flist);
104                flist = NULL;
105                count = 0;
106                break;
107            }
108	}
109    }
110    *actualCount = count;
111    UnlockDisplay(dpy);
112    SyncHandle();
113    return (flist);
114}
115
116int
117XFreeFontNames(char **list)
118{
119	if (list) {
120		if (!*(list[0]-1)) { /* from ListFontsWithInfo */
121			register char **names;
122			for (names = list+1; *names; names++)
123				Xfree (*names);
124		}
125		Xfree (list[0]-1);
126		Xfree (list);
127	}
128	return 1;
129}
130