1a96d7823Smrg/*
2a96d7823Smrg
3a96d7823Smrg  Copyright 1990, 1994, 1998  The Open Group
4a96d7823Smrg
5a96d7823Smrg  Permission to use, copy, modify, distribute, and sell this software and its
6a96d7823Smrg  documentation for any purpose is hereby granted without fee, provided that
7a96d7823Smrg  the above copyright notice appear in all copies and that both that
8a96d7823Smrg  copyright notice and this permission notice appear in supporting
9a96d7823Smrg  documentation.
10a96d7823Smrg
11a96d7823Smrg  The above copyright notice and this permission notice shall be included in
12a96d7823Smrg  all copies or substantial portions of the Software.
13a96d7823Smrg
14a96d7823Smrg  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15a96d7823Smrg  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16a96d7823Smrg  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17a96d7823Smrg  OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18a96d7823Smrg  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19a96d7823Smrg  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20a96d7823Smrg
21a96d7823Smrg  Except as contained in this notice, the name of The Open Group shall not be
22a96d7823Smrg  used in advertising or otherwise to promote the sale, use or other dealings
23a96d7823Smrg  in this Software without prior written authorization from The Open Group.
24a96d7823Smrg
25a96d7823Smrg*/
26a96d7823Smrg
27a96d7823Smrg/*
28a96d7823Smrg * Author:  Keith Packard, MIT X Consortium
29a96d7823Smrg */
30a96d7823Smrg
31a96d7823Smrg/* lame atom replacement routines for font applications */
32a96d7823Smrg
33a96d7823Smrg#ifdef HAVE_CONFIG_H
34a96d7823Smrg#include <config.h>
35a96d7823Smrg#endif
36a96d7823Smrg#include "libxfontint.h"
37c7b4381aSmrg#include "src/util/replace.h"
38a96d7823Smrg#include <X11/fonts/fontmisc.h>
39a96d7823Smrg
40a96d7823Smrgtypedef struct _AtomList {
41a96d7823Smrg    char *name;
42a96d7823Smrg    int len;
43a96d7823Smrg    int hash;
44a96d7823Smrg    Atom atom;
45a96d7823Smrg} AtomListRec, *AtomListPtr;
46a96d7823Smrg
47a96d7823Smrgstatic AtomListPtr *hashTable;
48a96d7823Smrg
496a46240fSmrgstatic unsigned hashSize, hashUsed;
506a46240fSmrgstatic unsigned hashMask;
516a46240fSmrgstatic unsigned rehash;
52a96d7823Smrg
53a96d7823Smrgstatic AtomListPtr *reverseMap;
54a96d7823Smrgstatic int reverseMapSize;
55a96d7823Smrgstatic Atom lastAtom;
56a96d7823Smrg
576a46240fSmrgstatic unsigned
586a46240fSmrgHash(const char *string, unsigned len)
59a96d7823Smrg{
606a46240fSmrg    unsigned h = 0;
61a96d7823Smrg
62a96d7823Smrg    while (len--)
63a96d7823Smrg        h = (h << 3) ^ *string++;
646a46240fSmrg
65a96d7823Smrg    return h;
66a96d7823Smrg}
67a96d7823Smrg
68a96d7823Smrgstatic int
69a96d7823SmrgResizeHashTable(void)
70a96d7823Smrg{
716a46240fSmrg    unsigned newHashSize;
726a46240fSmrg    unsigned newHashMask;
73a96d7823Smrg    AtomListPtr *newHashTable;
746a46240fSmrg    unsigned i;
756a46240fSmrg    unsigned h;
766a46240fSmrg    unsigned newRehash;
776a46240fSmrg    unsigned r;
78a96d7823Smrg
79a96d7823Smrg    if (hashSize == 0)
80a96d7823Smrg        newHashSize = 1024;
81a96d7823Smrg    else
82a96d7823Smrg        newHashSize = hashSize * 2;
83a96d7823Smrg    newHashTable = calloc(newHashSize, sizeof(AtomListPtr));
84a96d7823Smrg    if (!newHashTable) {
85a96d7823Smrg        fprintf(stderr, "ResizeHashTable(): Error: Couldn't allocate"
86a96d7823Smrg                " newHashTable (%ld)\n",
87a96d7823Smrg                newHashSize * (unsigned long) sizeof(AtomListPtr));
88a96d7823Smrg        return FALSE;
89a96d7823Smrg    }
90a96d7823Smrg    newHashMask = newHashSize - 1;
91a96d7823Smrg    newRehash = (newHashMask - 2);
92a96d7823Smrg    for (i = 0; i < hashSize; i++) {
93a96d7823Smrg        if (hashTable[i]) {
94a96d7823Smrg            h = (hashTable[i]->hash) & newHashMask;
95a96d7823Smrg            if (newHashTable[h]) {
96a96d7823Smrg                r = hashTable[i]->hash % newRehash | 1;
97a96d7823Smrg                do {
98a96d7823Smrg                    h += r;
99a96d7823Smrg                    if (h >= newHashSize)
100a96d7823Smrg                        h -= newHashSize;
101a96d7823Smrg                } while (newHashTable[h]);
102a96d7823Smrg            }
103a96d7823Smrg            newHashTable[h] = hashTable[i];
104a96d7823Smrg        }
105a96d7823Smrg    }
106a96d7823Smrg    free(hashTable);
107a96d7823Smrg    hashTable = newHashTable;
108a96d7823Smrg    hashSize = newHashSize;
109a96d7823Smrg    hashMask = newHashMask;
110a96d7823Smrg    rehash = newRehash;
111a96d7823Smrg    return TRUE;
112a96d7823Smrg}
113a96d7823Smrg
114a96d7823Smrgstatic int
115a96d7823SmrgResizeReverseMap(void)
116a96d7823Smrg{
117a96d7823Smrg    AtomListPtr *newMap;
118a96d7823Smrg    int newMapSize;
119a96d7823Smrg
120a96d7823Smrg    if (reverseMapSize == 0)
121a96d7823Smrg        newMapSize = 1000;
122a96d7823Smrg    else
123a96d7823Smrg        newMapSize = reverseMapSize * 2;
124c7b4381aSmrg    newMap = reallocarray(reverseMap, newMapSize, sizeof(AtomListPtr));
125a96d7823Smrg    if (newMap == NULL) {
126a96d7823Smrg        fprintf(stderr, "ResizeReverseMap(): Error: Couldn't reallocate"
127a96d7823Smrg                " reverseMap (%ld)\n",
128a96d7823Smrg                newMapSize * (unsigned long) sizeof(AtomListPtr));
129a96d7823Smrg        return FALSE;
130a96d7823Smrg    }
131a96d7823Smrg    reverseMap = newMap;
132a96d7823Smrg    reverseMapSize = newMapSize;
133a96d7823Smrg    return TRUE;
134a96d7823Smrg}
135a96d7823Smrg
136a96d7823Smrgstatic int
137a96d7823SmrgNameEqual(const char *a, const char *b, int l)
138a96d7823Smrg{
139a96d7823Smrg    while (l--)
140a96d7823Smrg        if (*a++ != *b++)
141a96d7823Smrg            return FALSE;
142a96d7823Smrg    return TRUE;
143a96d7823Smrg}
144a96d7823Smrg
145a96d7823SmrgAtom
146a96d7823Smrg__libxfont_internal__MakeAtom(const char *string, unsigned len, int makeit)
147a96d7823Smrg{
148a96d7823Smrg    AtomListPtr a;
1496a46240fSmrg    unsigned hash;
1506a46240fSmrg    unsigned h = 0;
1516a46240fSmrg    unsigned r;
152a96d7823Smrg
153a96d7823Smrg    hash = Hash(string, len);
154a96d7823Smrg    if (hashTable) {
155a96d7823Smrg        h = hash & hashMask;
156a96d7823Smrg        if (hashTable[h]) {
157a96d7823Smrg            if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
158a96d7823Smrg                NameEqual(hashTable[h]->name, string, len)) {
159a96d7823Smrg                return hashTable[h]->atom;
160a96d7823Smrg            }
161a96d7823Smrg            r = (hash % rehash) | 1;
162a96d7823Smrg            for (;;) {
163a96d7823Smrg                h += r;
164a96d7823Smrg                if (h >= hashSize)
165a96d7823Smrg                    h -= hashSize;
166a96d7823Smrg                if (!hashTable[h])
167a96d7823Smrg                    break;
168a96d7823Smrg                if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
169a96d7823Smrg                    NameEqual(hashTable[h]->name, string, len)) {
170a96d7823Smrg                    return hashTable[h]->atom;
171a96d7823Smrg                }
172a96d7823Smrg            }
173a96d7823Smrg        }
174a96d7823Smrg    }
175a96d7823Smrg    if (!makeit)
176a96d7823Smrg        return None;
177a96d7823Smrg    a = malloc(sizeof(AtomListRec) + len + 1);
178a96d7823Smrg    if (a == NULL) {
179a96d7823Smrg        fprintf(stderr, "MakeAtom(): Error: Couldn't allocate AtomListRec"
180a96d7823Smrg                " (%ld)\n", (unsigned long) sizeof(AtomListRec) + len + 1);
181a96d7823Smrg        return None;
182a96d7823Smrg    }
183a96d7823Smrg    a->name = (char *) (a + 1);
184a96d7823Smrg    a->len = len;
185a96d7823Smrg    strncpy(a->name, string, len);
186a96d7823Smrg    a->name[len] = '\0';
187a96d7823Smrg    a->atom = ++lastAtom;
188a96d7823Smrg    a->hash = hash;
189a96d7823Smrg    if (hashUsed >= hashSize / 2) {
190c7b4381aSmrg        if ((ResizeHashTable() == FALSE) &&
1916a46240fSmrg	    ((hashTable == NULL) || (hashUsed == hashSize))) {
1926a46240fSmrg	    free(a);
193c7b4381aSmrg	    return None;
1946a46240fSmrg        }
195a96d7823Smrg        h = hash & hashMask;
196a96d7823Smrg        if (hashTable[h]) {
197a96d7823Smrg            r = (hash % rehash) | 1;
198a96d7823Smrg            do {
199a96d7823Smrg                h += r;
200a96d7823Smrg                if (h >= hashSize)
201a96d7823Smrg                    h -= hashSize;
202a96d7823Smrg            } while (hashTable[h]);
203a96d7823Smrg        }
204a96d7823Smrg    }
205a96d7823Smrg    hashTable[h] = a;
206a96d7823Smrg    hashUsed++;
207a96d7823Smrg    if (reverseMapSize <= a->atom) {
208a96d7823Smrg        if (!ResizeReverseMap())
209a96d7823Smrg            return None;
210a96d7823Smrg    }
211a96d7823Smrg    reverseMap[a->atom] = a;
212a96d7823Smrg    return a->atom;
213a96d7823Smrg}
214a96d7823Smrg
215a96d7823Smrgint
216a96d7823Smrg__libxfont_internal__ValidAtom(Atom atom)
217a96d7823Smrg{
218a96d7823Smrg    return (atom != None) && (atom <= lastAtom);
219a96d7823Smrg}
220a96d7823Smrg
221a96d7823Smrgconst char *
222a96d7823Smrg__libxfont_internal__NameForAtom(Atom atom)
223a96d7823Smrg{
224a96d7823Smrg    if (atom != None && atom <= lastAtom)
225a96d7823Smrg        return reverseMap[atom]->name;
226a96d7823Smrg    return NULL;
227a96d7823Smrg}
228