atom.c revision 6747b715
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
2505b261ecSmrg
2605b261ecSmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
2705b261ecSmrg
2805b261ecSmrg                        All Rights Reserved
2905b261ecSmrg
3005b261ecSmrgPermission to use, copy, modify, and distribute this software and its
3105b261ecSmrgdocumentation for any purpose and without fee is hereby granted,
3205b261ecSmrgprovided that the above copyright notice appear in all copies and that
3305b261ecSmrgboth that copyright notice and this permission notice appear in
3405b261ecSmrgsupporting documentation, and that the name of Digital not be
3505b261ecSmrgused in advertising or publicity pertaining to distribution of the
3605b261ecSmrgsoftware without specific, written prior permission.
3705b261ecSmrg
3805b261ecSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3905b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
4005b261ecSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4105b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4205b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4305b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4405b261ecSmrgSOFTWARE.
4505b261ecSmrg
4605b261ecSmrg******************************************************************/
4705b261ecSmrg
4805b261ecSmrg
4905b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
5005b261ecSmrg#include <dix-config.h>
5105b261ecSmrg#endif
5205b261ecSmrg
5305b261ecSmrg#include <X11/X.h>
5405b261ecSmrg#include <X11/Xatom.h>
5505b261ecSmrg#include <stdio.h>
5605b261ecSmrg#include <string.h>
5705b261ecSmrg#include "misc.h"
5805b261ecSmrg#include "resource.h"
5905b261ecSmrg#include "dix.h"
6005b261ecSmrg
6105b261ecSmrg#define InitialTableSize 100
6205b261ecSmrg
6305b261ecSmrgtypedef struct _Node {
6405b261ecSmrg    struct _Node   *left,   *right;
6505b261ecSmrg    Atom a;
6605b261ecSmrg    unsigned int fingerPrint;
676747b715Smrg    const char   *string;
6805b261ecSmrg} NodeRec, *NodePtr;
6905b261ecSmrg
7005b261ecSmrgstatic Atom lastAtom = None;
716747b715Smrgstatic NodePtr atomRoot = NULL;
7205b261ecSmrgstatic unsigned long tableLength;
7305b261ecSmrgstatic NodePtr *nodeTable;
7405b261ecSmrg
7505b261ecSmrgvoid FreeAtom(NodePtr patom);
7605b261ecSmrg
776747b715SmrgAtom
786747b715SmrgMakeAtom(const char *string, unsigned len, Bool makeit)
7905b261ecSmrg{
8005b261ecSmrg    NodePtr * np;
8105b261ecSmrg    unsigned i;
8205b261ecSmrg    int comp;
8305b261ecSmrg    unsigned int fp = 0;
8405b261ecSmrg
8505b261ecSmrg    np = &atomRoot;
8605b261ecSmrg    for (i = 0; i < (len+1)/2; i++)
8705b261ecSmrg    {
8805b261ecSmrg	fp = fp * 27 + string[i];
8905b261ecSmrg	fp = fp * 27 + string[len - 1 - i];
9005b261ecSmrg    }
916747b715Smrg    while (*np != NULL)
9205b261ecSmrg    {
9305b261ecSmrg	if (fp < (*np)->fingerPrint)
9405b261ecSmrg	    np = &((*np)->left);
9505b261ecSmrg	else if (fp > (*np)->fingerPrint)
9605b261ecSmrg	    np = &((*np)->right);
9705b261ecSmrg	else
9805b261ecSmrg	{			       /* now start testing the strings */
9905b261ecSmrg	    comp = strncmp(string, (*np)->string, (int)len);
10005b261ecSmrg	    if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
10105b261ecSmrg		np = &((*np)->left);
10205b261ecSmrg	    else if (comp > 0)
10305b261ecSmrg		np = &((*np)->right);
10405b261ecSmrg	    else
10505b261ecSmrg		return(*np)->a;
10605b261ecSmrg	    }
10705b261ecSmrg    }
10805b261ecSmrg    if (makeit)
10905b261ecSmrg    {
11005b261ecSmrg	NodePtr nd;
11105b261ecSmrg
1126747b715Smrg	nd = malloc(sizeof(NodeRec));
11305b261ecSmrg	if (!nd)
11405b261ecSmrg	    return BAD_RESOURCE;
11505b261ecSmrg	if (lastAtom < XA_LAST_PREDEFINED)
11605b261ecSmrg	{
11705b261ecSmrg	    nd->string = string;
11805b261ecSmrg	}
11905b261ecSmrg	else
12005b261ecSmrg	{
1216747b715Smrg	    char *newstring = malloc(len + 1);
1226747b715Smrg	    if (!newstring) {
1236747b715Smrg		free(nd);
12405b261ecSmrg		return BAD_RESOURCE;
12505b261ecSmrg	    }
1266747b715Smrg	    strncpy(newstring, string, (int)len);
1276747b715Smrg	    newstring[len] = 0;
1286747b715Smrg	    nd->string = newstring;
12905b261ecSmrg	}
13005b261ecSmrg	if ((lastAtom + 1) >= tableLength) {
13105b261ecSmrg	    NodePtr *table;
13205b261ecSmrg
1336747b715Smrg	    table = realloc(nodeTable, tableLength * (2 * sizeof(NodePtr)));
13405b261ecSmrg	    if (!table) {
1356747b715Smrg		if (nd->string != string) {
1366747b715Smrg                    /* nd->string has been strdup'ed */
1376747b715Smrg		    free((char *)nd->string);
1386747b715Smrg                }
1396747b715Smrg		free(nd);
14005b261ecSmrg		return BAD_RESOURCE;
14105b261ecSmrg	    }
14205b261ecSmrg	    tableLength <<= 1;
14305b261ecSmrg	    nodeTable = table;
14405b261ecSmrg	}
14505b261ecSmrg	*np = nd;
1466747b715Smrg	nd->left = nd->right = NULL;
14705b261ecSmrg	nd->fingerPrint = fp;
1486747b715Smrg	nd->a = ++lastAtom;
1496747b715Smrg	nodeTable[lastAtom] = nd;
15005b261ecSmrg	return nd->a;
15105b261ecSmrg    }
15205b261ecSmrg    else
15305b261ecSmrg	return None;
15405b261ecSmrg}
15505b261ecSmrg
1566747b715SmrgBool
15705b261ecSmrgValidAtom(Atom atom)
15805b261ecSmrg{
15905b261ecSmrg    return (atom != None) && (atom <= lastAtom);
16005b261ecSmrg}
16105b261ecSmrg
1626747b715Smrgconst char *
16305b261ecSmrgNameForAtom(Atom atom)
16405b261ecSmrg{
16505b261ecSmrg    NodePtr node;
16605b261ecSmrg    if (atom > lastAtom) return 0;
1676747b715Smrg    if ((node = nodeTable[atom]) == NULL) return 0;
16805b261ecSmrg    return node->string;
16905b261ecSmrg}
17005b261ecSmrg
17105b261ecSmrgvoid
17205b261ecSmrgAtomError(void)
17305b261ecSmrg{
17405b261ecSmrg    FatalError("initializing atoms");
17505b261ecSmrg}
17605b261ecSmrg
17705b261ecSmrgvoid
17805b261ecSmrgFreeAtom(NodePtr patom)
17905b261ecSmrg{
18005b261ecSmrg    if(patom->left)
18105b261ecSmrg	FreeAtom(patom->left);
18205b261ecSmrg    if(patom->right)
18305b261ecSmrg	FreeAtom(patom->right);
1846747b715Smrg    if (patom->a > XA_LAST_PREDEFINED) {
1856747b715Smrg        /*
1866747b715Smrg         * All strings above XA_LAST_PREDEFINED are strdup'ed, so it's safe to
1876747b715Smrg         * cast here
1886747b715Smrg         */
1896747b715Smrg	free((char *)patom->string);
1906747b715Smrg    }
1916747b715Smrg    free(patom);
19205b261ecSmrg}
19305b261ecSmrg
19405b261ecSmrgvoid
19505b261ecSmrgFreeAllAtoms(void)
19605b261ecSmrg{
1976747b715Smrg    if (atomRoot == NULL)
19805b261ecSmrg	return;
19905b261ecSmrg    FreeAtom(atomRoot);
2006747b715Smrg    atomRoot = NULL;
2016747b715Smrg    free(nodeTable);
2026747b715Smrg    nodeTable = NULL;
20305b261ecSmrg    lastAtom = None;
20405b261ecSmrg}
20505b261ecSmrg
20605b261ecSmrgvoid
20705b261ecSmrgInitAtoms(void)
20805b261ecSmrg{
20905b261ecSmrg    FreeAllAtoms();
21005b261ecSmrg    tableLength = InitialTableSize;
2116747b715Smrg    nodeTable = malloc(InitialTableSize * sizeof(NodePtr));
21205b261ecSmrg    if (!nodeTable)
21305b261ecSmrg	AtomError();
2146747b715Smrg    nodeTable[None] = NULL;
21505b261ecSmrg    MakePredeclaredAtoms();
21605b261ecSmrg    if (lastAtom != XA_LAST_PREDEFINED)
2176747b715Smrg	AtomError();
21805b261ecSmrg}
219