1 /*
2  * font server atom manipulations
3  */
4 /*
5 Copyright 1987, 1998  The Open Group
6 
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
11 documentation.
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 
23 Except as contained in this notice, the name of The Open Group shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from The Open Group.
26  * Copyright 1990, 1991 Network Computing Devices;
27  * Portions Copyright 1987 by Digital Equipment Corporation
28  *
29  * Permission to use, copy, modify, distribute, and sell this software and its
30  * documentation for any purpose is hereby granted without fee, provided that
31  * the above copyright notice appear in all copies and that both that
32  * copyright notice and this permission notice appear in supporting
33  * documentation, and that the names of Network Computing Devices,
34  * or Digital not be used in advertising or
35  * publicity pertaining to distribution of the software without specific,
36  * written prior permission.  Network Computing Devices, or Digital
37  * make no representations about the
38  * suitability of this software for any purpose.  It is provided "as is"
39  * without express or implied warranty.
40  *
41  * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
42  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
43  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, OR DIGITAL BE
44  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
46  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
47  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48  *
49  */
50 
51 #include "config.h"
52 
53 #include "misc.h"
54 #include "fsresource.h"
55 #include "difs.h"
56 
57 #define InitialTableSize 100
58 #define	FSA_LAST_PREDEFINED	0 /* only None is predefined */
59 
60 typedef struct _Node {
61     struct _Node *left,
62                *right;
63     Atom        a;
64     unsigned int fingerPrint;
65     char       *string;
66 }           NodeRec, *NodePtr;
67 
68 static Atom lastAtom = None;
69 static NodePtr atomRoot = (NodePtr) NULL;
70 static unsigned long tableLength;
71 static NodePtr *nodeTable;
72 
73 Atom
MakeAtom(const char * string,unsigned int len,Bool makeit)74 MakeAtom(const char *string, unsigned int len, Bool makeit)
75 {
76     register NodePtr *np;
77     unsigned    i;
78     int         comp;
79     register unsigned int fp = 0;
80 
81     np = &atomRoot;
82     for (i = 0; i < (len + 1) / 2; i++) {
83 	fp = fp * 27 + string[i];
84 	fp = fp * 27 + string[len - 1 - i];
85     }
86     while (*np != (NodePtr) NULL) {
87 	if (fp < (*np)->fingerPrint)
88 	    np = &((*np)->left);
89 	else if (fp > (*np)->fingerPrint)
90 	    np = &((*np)->right);
91 	else {			/* now start testing the strings */
92 	    comp = strncmp(string, (*np)->string, len);
93 	    if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
94 		np = &((*np)->left);
95 	    else if (comp > 0)
96 		np = &((*np)->right);
97 	    else
98 		return (*np)->a;
99 	}
100     }
101     if (makeit) {
102 	register NodePtr nd;
103 
104 	nd = (NodePtr) fsalloc(sizeof(NodeRec));
105 	if (!nd)
106 	    return BAD_RESOURCE;
107 #if FSA_LAST_PREDEFINED > 0
108 	if (lastAtom < FSA_LAST_PREDEFINED) {
109 	    nd->string = string;
110 	} else
111 #endif
112 	{
113 	    nd->string = (char *) fsalloc(len + 1);
114 	    if (!nd->string) {
115 		fsfree(nd);
116 		return BAD_RESOURCE;
117 	    }
118 	    strncpy(nd->string, string, len);
119 	    nd->string[len] = 0;
120 	}
121 	if ((lastAtom + 1) >= tableLength) {
122 	    NodePtr    *table;
123 
124 	    table = (NodePtr *) fsrealloc(nodeTable,
125 					tableLength * (2 * sizeof(NodePtr)));
126 	    if (!table) {
127 		if (nd->string != string)
128 		    fsfree(nd->string);
129 		fsfree(nd);
130 		return BAD_RESOURCE;
131 	    }
132 	    tableLength <<= 1;
133 	    nodeTable = table;
134 	}
135 	*np = nd;
136 	nd->left = nd->right = (NodePtr) NULL;
137 	nd->fingerPrint = fp;
138 	nd->a = (++lastAtom);
139 	*(nodeTable + lastAtom) = nd;
140 	return nd->a;
141     } else
142 	return None;
143 }
144 
145 int
ValidAtom(Atom atom)146 ValidAtom(Atom atom)
147 {
148     return (atom != None) && (atom <= lastAtom);
149 }
150 
151 const char *
NameForAtom(Atom atom)152 NameForAtom(Atom atom)
153 {
154     NodePtr     node;
155 
156     if (atom > lastAtom)
157 	return NULL;
158     if ((node = nodeTable[atom]) == (NodePtr) NULL)
159 	return NULL;
160     return node->string;
161 }
162 
163 static void
atom_error(void)164 atom_error(void)
165 {
166     FatalError("initializing atoms\n");
167 }
168 
169 static void
free_atom(NodePtr patom)170 free_atom(NodePtr patom)
171 {
172     if (patom->left)
173 	free_atom(patom->left);
174     if (patom->right)
175 	free_atom(patom->right);
176     if (patom->a > FSA_LAST_PREDEFINED)
177 	fsfree(patom->string);
178     fsfree(patom);
179 }
180 
181 static void
free_all_atoms(void)182 free_all_atoms(void)
183 {
184     if (atomRoot == (NodePtr) NULL)
185 	return;
186     free_atom(atomRoot);
187     atomRoot = (NodePtr) NULL;
188     fsfree(nodeTable);
189     nodeTable = (NodePtr *) NULL;
190     lastAtom = None;
191 }
192 
193 void
InitAtoms(void)194 InitAtoms(void)
195 {
196     free_all_atoms();
197     tableLength = InitialTableSize;
198     nodeTable = (NodePtr *) fsalloc(InitialTableSize * sizeof(NodePtr));
199     if (!nodeTable)
200 	atom_error();
201     nodeTable[None] = (NodePtr) NULL;
202     lastAtom = FSA_LAST_PREDEFINED;
203 }
204