Home | History | Annotate | Line # | Download | only in src
      1 /*
      2 
      3 Copyright 1986, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in 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 
     35 char **
     36 XListFonts(
     37 register Display *dpy,
     38 _Xconst char *pattern,  /* null-terminated */
     39 int maxNames,
     40 int *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 (pattern != NULL && 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 
    120 int
    121 XFreeFontNames(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