1/***********************************************************
2
3Copyright 1987, 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
25Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
26
27                        All Rights Reserved
28
29Permission to use, copy, modify, and distribute this software and its
30documentation for any purpose and without fee is hereby granted,
31provided that the above copyright notice appear in all copies and that
32both that copyright notice and this permission notice appear in
33supporting documentation, and that the name of Digital not be
34used in advertising or publicity pertaining to distribution of the
35software without specific, written prior permission.
36
37DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43SOFTWARE.
44
45******************************************************************/
46
47#ifdef HAVE_DIX_CONFIG_H
48#include <dix-config.h>
49#endif
50
51#include <X11/X.h>
52#include <X11/Xatom.h>
53#include <stdio.h>
54#include <string.h>
55#include "misc.h"
56#include "resource.h"
57#include "dix.h"
58
59#define InitialTableSize 256
60
61typedef struct _Node {
62    struct _Node *left, *right;
63    Atom a;
64    unsigned int fingerPrint;
65    const char *string;
66} NodeRec, *NodePtr;
67
68static Atom lastAtom = None;
69static NodePtr atomRoot = NULL;
70static unsigned long tableLength;
71static NodePtr *nodeTable;
72
73Atom
74MakeAtom(const char *string, unsigned len, Bool makeit)
75{
76    NodePtr *np;
77    unsigned i;
78    int comp;
79    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 != 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, (int) 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        NodePtr nd;
103
104        nd = malloc(sizeof(NodeRec));
105        if (!nd)
106            return BAD_RESOURCE;
107        if (lastAtom < XA_LAST_PREDEFINED) {
108            nd->string = string;
109        }
110        else {
111            nd->string = strndup(string, len);
112            if (!nd->string) {
113                free(nd);
114                return BAD_RESOURCE;
115            }
116        }
117        if ((lastAtom + 1) >= tableLength) {
118            NodePtr *table;
119
120            table = reallocarray(nodeTable, tableLength, 2 * sizeof(NodePtr));
121            if (!table) {
122                if (nd->string != string) {
123                    /* nd->string has been strdup'ed */
124                    free((char *) nd->string);
125                }
126                free(nd);
127                return BAD_RESOURCE;
128            }
129            tableLength <<= 1;
130            nodeTable = table;
131        }
132        *np = nd;
133        nd->left = nd->right = NULL;
134        nd->fingerPrint = fp;
135        nd->a = ++lastAtom;
136        nodeTable[lastAtom] = nd;
137        return nd->a;
138    }
139    else
140        return None;
141}
142
143Bool
144ValidAtom(Atom atom)
145{
146    return (atom != None) && (atom <= lastAtom);
147}
148
149const char *
150NameForAtom(Atom atom)
151{
152    NodePtr node;
153
154    if (atom > lastAtom)
155        return 0;
156    if ((node = nodeTable[atom]) == NULL)
157        return 0;
158    return node->string;
159}
160
161void
162AtomError(void)
163{
164    FatalError("initializing atoms");
165}
166
167static void
168FreeAtom(NodePtr patom)
169{
170    if (patom->left)
171        FreeAtom(patom->left);
172    if (patom->right)
173        FreeAtom(patom->right);
174    if (patom->a > XA_LAST_PREDEFINED) {
175        /*
176         * All strings above XA_LAST_PREDEFINED are strdup'ed, so it's safe to
177         * cast here
178         */
179        free((char *) patom->string);
180    }
181    free(patom);
182}
183
184void
185FreeAllAtoms(void)
186{
187    if (atomRoot == NULL)
188        return;
189    FreeAtom(atomRoot);
190    atomRoot = NULL;
191    free(nodeTable);
192    nodeTable = NULL;
193    lastAtom = None;
194}
195
196void
197InitAtoms(void)
198{
199    FreeAllAtoms();
200    tableLength = InitialTableSize;
201    nodeTable = xallocarray(InitialTableSize, sizeof(NodePtr));
202    if (!nodeTable)
203        AtomError();
204    nodeTable[None] = NULL;
205    MakePredeclaredAtoms();
206    if (lastAtom != XA_LAST_PREDEFINED)
207        AtomError();
208}
209