105b261ecSmrg/***********************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1987, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
2605b261ecSmrg
2705b261ecSmrg                        All Rights Reserved
2805b261ecSmrg
2935c4bbdfSmrgPermission to use, copy, modify, and distribute this software and its
3035c4bbdfSmrgdocumentation for any purpose and without fee is hereby granted,
3105b261ecSmrgprovided that the above copyright notice appear in all copies and that
3235c4bbdfSmrgboth that copyright notice and this permission notice appear in
3305b261ecSmrgsupporting documentation, and that the name of Digital not be
3405b261ecSmrgused in advertising or publicity pertaining to distribution of the
3535c4bbdfSmrgsoftware without specific, written prior permission.
3605b261ecSmrg
3705b261ecSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3805b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
3905b261ecSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4005b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4105b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4205b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4305b261ecSmrgSOFTWARE.
4405b261ecSmrg
4505b261ecSmrg******************************************************************/
4605b261ecSmrg
4705b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
4805b261ecSmrg#include <dix-config.h>
4905b261ecSmrg#endif
5005b261ecSmrg
5105b261ecSmrg#include <X11/X.h>
5205b261ecSmrg#include <X11/Xatom.h>
5305b261ecSmrg#include <stdio.h>
5405b261ecSmrg#include <string.h>
5505b261ecSmrg#include "misc.h"
5605b261ecSmrg#include "resource.h"
5705b261ecSmrg#include "dix.h"
5805b261ecSmrg
5935c4bbdfSmrg#define InitialTableSize 256
6005b261ecSmrg
6105b261ecSmrgtypedef struct _Node {
6235c4bbdfSmrg    struct _Node *left, *right;
6305b261ecSmrg    Atom a;
6405b261ecSmrg    unsigned int fingerPrint;
6535c4bbdfSmrg    const char *string;
6605b261ecSmrg} NodeRec, *NodePtr;
6705b261ecSmrg
6805b261ecSmrgstatic Atom lastAtom = None;
696747b715Smrgstatic NodePtr atomRoot = NULL;
7005b261ecSmrgstatic unsigned long tableLength;
7105b261ecSmrgstatic NodePtr *nodeTable;
7205b261ecSmrg
736747b715SmrgAtom
746747b715SmrgMakeAtom(const char *string, unsigned len, Bool makeit)
7505b261ecSmrg{
7635c4bbdfSmrg    NodePtr *np;
7705b261ecSmrg    unsigned i;
7805b261ecSmrg    int comp;
7905b261ecSmrg    unsigned int fp = 0;
8005b261ecSmrg
8105b261ecSmrg    np = &atomRoot;
8235c4bbdfSmrg    for (i = 0; i < (len + 1) / 2; i++) {
8335c4bbdfSmrg        fp = fp * 27 + string[i];
8435c4bbdfSmrg        fp = fp * 27 + string[len - 1 - i];
8505b261ecSmrg    }
8635c4bbdfSmrg    while (*np != NULL) {
8735c4bbdfSmrg        if (fp < (*np)->fingerPrint)
8835c4bbdfSmrg            np = &((*np)->left);
8935c4bbdfSmrg        else if (fp > (*np)->fingerPrint)
9035c4bbdfSmrg            np = &((*np)->right);
9135c4bbdfSmrg        else {                  /* now start testing the strings */
9235c4bbdfSmrg            comp = strncmp(string, (*np)->string, (int) len);
9335c4bbdfSmrg            if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
9435c4bbdfSmrg                np = &((*np)->left);
9535c4bbdfSmrg            else if (comp > 0)
9635c4bbdfSmrg                np = &((*np)->right);
9735c4bbdfSmrg            else
9835c4bbdfSmrg                return (*np)->a;
9935c4bbdfSmrg        }
10005b261ecSmrg    }
10135c4bbdfSmrg    if (makeit) {
10235c4bbdfSmrg        NodePtr nd;
10335c4bbdfSmrg
10435c4bbdfSmrg        nd = malloc(sizeof(NodeRec));
10535c4bbdfSmrg        if (!nd)
10635c4bbdfSmrg            return BAD_RESOURCE;
10735c4bbdfSmrg        if (lastAtom < XA_LAST_PREDEFINED) {
10835c4bbdfSmrg            nd->string = string;
10935c4bbdfSmrg        }
11035c4bbdfSmrg        else {
11135c4bbdfSmrg            nd->string = strndup(string, len);
11235c4bbdfSmrg            if (!nd->string) {
11335c4bbdfSmrg                free(nd);
11435c4bbdfSmrg                return BAD_RESOURCE;
11535c4bbdfSmrg            }
11635c4bbdfSmrg        }
11735c4bbdfSmrg        if ((lastAtom + 1) >= tableLength) {
11835c4bbdfSmrg            NodePtr *table;
11935c4bbdfSmrg
12035c4bbdfSmrg            table = reallocarray(nodeTable, tableLength, 2 * sizeof(NodePtr));
12135c4bbdfSmrg            if (!table) {
12235c4bbdfSmrg                if (nd->string != string) {
1236747b715Smrg                    /* nd->string has been strdup'ed */
12435c4bbdfSmrg                    free((char *) nd->string);
1256747b715Smrg                }
12635c4bbdfSmrg                free(nd);
12735c4bbdfSmrg                return BAD_RESOURCE;
12835c4bbdfSmrg            }
12935c4bbdfSmrg            tableLength <<= 1;
13035c4bbdfSmrg            nodeTable = table;
13135c4bbdfSmrg        }
13235c4bbdfSmrg        *np = nd;
13335c4bbdfSmrg        nd->left = nd->right = NULL;
13435c4bbdfSmrg        nd->fingerPrint = fp;
13535c4bbdfSmrg        nd->a = ++lastAtom;
13635c4bbdfSmrg        nodeTable[lastAtom] = nd;
13735c4bbdfSmrg        return nd->a;
13805b261ecSmrg    }
13905b261ecSmrg    else
14035c4bbdfSmrg        return None;
14105b261ecSmrg}
14205b261ecSmrg
1436747b715SmrgBool
14405b261ecSmrgValidAtom(Atom atom)
14505b261ecSmrg{
14605b261ecSmrg    return (atom != None) && (atom <= lastAtom);
14705b261ecSmrg}
14805b261ecSmrg
1496747b715Smrgconst char *
15005b261ecSmrgNameForAtom(Atom atom)
15105b261ecSmrg{
15205b261ecSmrg    NodePtr node;
15335c4bbdfSmrg
15435c4bbdfSmrg    if (atom > lastAtom)
15535c4bbdfSmrg        return 0;
15635c4bbdfSmrg    if ((node = nodeTable[atom]) == NULL)
15735c4bbdfSmrg        return 0;
15805b261ecSmrg    return node->string;
15905b261ecSmrg}
16005b261ecSmrg
16105b261ecSmrgvoid
16205b261ecSmrgAtomError(void)
16305b261ecSmrg{
16405b261ecSmrg    FatalError("initializing atoms");
16505b261ecSmrg}
16605b261ecSmrg
16735c4bbdfSmrgstatic void
16805b261ecSmrgFreeAtom(NodePtr patom)
16905b261ecSmrg{
17035c4bbdfSmrg    if (patom->left)
17135c4bbdfSmrg        FreeAtom(patom->left);
17235c4bbdfSmrg    if (patom->right)
17335c4bbdfSmrg        FreeAtom(patom->right);
1746747b715Smrg    if (patom->a > XA_LAST_PREDEFINED) {
1756747b715Smrg        /*
1766747b715Smrg         * All strings above XA_LAST_PREDEFINED are strdup'ed, so it's safe to
1776747b715Smrg         * cast here
1786747b715Smrg         */
17935c4bbdfSmrg        free((char *) patom->string);
1806747b715Smrg    }
1816747b715Smrg    free(patom);
18205b261ecSmrg}
18305b261ecSmrg
18405b261ecSmrgvoid
18505b261ecSmrgFreeAllAtoms(void)
18605b261ecSmrg{
1876747b715Smrg    if (atomRoot == NULL)
18835c4bbdfSmrg        return;
18905b261ecSmrg    FreeAtom(atomRoot);
1906747b715Smrg    atomRoot = NULL;
1916747b715Smrg    free(nodeTable);
1926747b715Smrg    nodeTable = NULL;
19305b261ecSmrg    lastAtom = None;
19405b261ecSmrg}
19505b261ecSmrg
19605b261ecSmrgvoid
19705b261ecSmrgInitAtoms(void)
19805b261ecSmrg{
19905b261ecSmrg    FreeAllAtoms();
20005b261ecSmrg    tableLength = InitialTableSize;
20135c4bbdfSmrg    nodeTable = xallocarray(InitialTableSize, sizeof(NodePtr));
20205b261ecSmrg    if (!nodeTable)
20335c4bbdfSmrg        AtomError();
2046747b715Smrg    nodeTable[None] = NULL;
20505b261ecSmrg    MakePredeclaredAtoms();
20605b261ecSmrg    if (lastAtom != XA_LAST_PREDEFINED)
20735c4bbdfSmrg        AtomError();
20805b261ecSmrg}
209