xserver

xserver with xephyr scale patch
git clone https://git.neptards.moe/u3shit/xserver.git
Log | Files | Refs | README | LICENSE

atom.c (5885B)


      1 /***********************************************************
      2 
      3 Copyright 1987, 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 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
     26 
     27                         All Rights Reserved
     28 
     29 Permission to use, copy, modify, and distribute this software and its
     30 documentation for any purpose and without fee is hereby granted,
     31 provided that the above copyright notice appear in all copies and that
     32 both that copyright notice and this permission notice appear in
     33 supporting documentation, and that the name of Digital not be
     34 used in advertising or publicity pertaining to distribution of the
     35 software without specific, written prior permission.
     36 
     37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
     38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
     39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
     40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
     41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     43 SOFTWARE.
     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 
     61 typedef struct _Node {
     62     struct _Node *left, *right;
     63     Atom a;
     64     unsigned int fingerPrint;
     65     const char *string;
     66 } NodeRec, *NodePtr;
     67 
     68 static Atom lastAtom = None;
     69 static NodePtr atomRoot = NULL;
     70 static unsigned long tableLength;
     71 static NodePtr *nodeTable;
     72 
     73 Atom
     74 MakeAtom(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 
    143 Bool
    144 ValidAtom(Atom atom)
    145 {
    146     return (atom != None) && (atom <= lastAtom);
    147 }
    148 
    149 const char *
    150 NameForAtom(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 
    161 void
    162 AtomError(void)
    163 {
    164     FatalError("initializing atoms");
    165 }
    166 
    167 static void
    168 FreeAtom(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 
    184 void
    185 FreeAllAtoms(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 
    196 void
    197 InitAtoms(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 }