2023-08-28 05:57:34 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright 1990, 1994, 1998 The Open Group
|
|
|
|
|
|
|
|
Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
the above copyright notice appear in all copies and that both that
|
|
|
|
copyright notice and this permission notice appear in supporting
|
|
|
|
documentation.
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name of The Open Group shall not be
|
|
|
|
used in advertising or otherwise to promote the sale, use or other dealings
|
|
|
|
in this Software without prior written authorization from The Open Group.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Author: Keith Packard, MIT X Consortium
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* lame atom replacement routines for font applications */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
#include "fontmisc.h"
|
|
|
|
|
|
|
|
typedef struct _AtomList {
|
|
|
|
char *name;
|
|
|
|
unsigned int len;
|
|
|
|
int hash;
|
|
|
|
Atom atom;
|
|
|
|
} AtomListRec, *AtomListPtr;
|
|
|
|
|
|
|
|
static AtomListPtr *hashTable;
|
|
|
|
|
2025-01-10 01:40:29 +00:00
|
|
|
static unsigned hashSize, hashUsed;
|
|
|
|
static unsigned hashMask;
|
|
|
|
static unsigned rehash;
|
2023-08-28 05:57:34 +00:00
|
|
|
|
|
|
|
static AtomListPtr *reverseMap;
|
|
|
|
static size_t reverseMapSize;
|
|
|
|
static Atom lastAtom;
|
|
|
|
|
2025-01-10 01:40:29 +00:00
|
|
|
static unsigned
|
|
|
|
Hash(const char *string, unsigned len)
|
2023-08-28 05:57:34 +00:00
|
|
|
{
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned h = 0;
|
2023-08-28 05:57:34 +00:00
|
|
|
|
|
|
|
while (len--)
|
|
|
|
h = (h << 3) ^ *string++;
|
2025-01-10 01:40:29 +00:00
|
|
|
|
2023-08-28 05:57:34 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ResizeHashTable(void)
|
|
|
|
{
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned newHashSize;
|
|
|
|
unsigned newHashMask;
|
2023-08-28 05:57:34 +00:00
|
|
|
AtomListPtr *newHashTable;
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned newRehash;
|
2023-08-28 05:57:34 +00:00
|
|
|
|
|
|
|
if (hashSize == 0)
|
|
|
|
newHashSize = 1024;
|
|
|
|
else
|
|
|
|
newHashSize = hashSize * 2;
|
|
|
|
newHashTable = calloc(newHashSize, sizeof(AtomListPtr));
|
|
|
|
if (!newHashTable) {
|
|
|
|
fprintf(stderr, "ResizeHashTable(): Error: Couldn't allocate"
|
|
|
|
" newHashTable (%ld)\n",
|
|
|
|
newHashSize * (unsigned long) sizeof(AtomListPtr));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
newHashMask = newHashSize - 1;
|
|
|
|
newRehash = (newHashMask - 2);
|
|
|
|
for (int i = 0; i < hashSize; i++) {
|
|
|
|
if (hashTable[i]) {
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned h = (hashTable[i]->hash) & newHashMask;
|
2023-08-28 05:57:34 +00:00
|
|
|
if (newHashTable[h]) {
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned r = hashTable[i]->hash % newRehash | 1;
|
2023-08-28 05:57:34 +00:00
|
|
|
do {
|
|
|
|
h += r;
|
|
|
|
if (h >= newHashSize)
|
|
|
|
h -= newHashSize;
|
|
|
|
} while (newHashTable[h]);
|
|
|
|
}
|
|
|
|
newHashTable[h] = hashTable[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(hashTable);
|
|
|
|
hashTable = newHashTable;
|
|
|
|
hashSize = newHashSize;
|
|
|
|
hashMask = newHashMask;
|
|
|
|
rehash = newRehash;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ResizeReverseMap(void)
|
|
|
|
{
|
|
|
|
AtomListPtr *newMap;
|
|
|
|
size_t newMapSize;
|
|
|
|
|
|
|
|
if (reverseMapSize == 0)
|
|
|
|
newMapSize = 1000;
|
|
|
|
else
|
|
|
|
newMapSize = reverseMapSize * 2;
|
|
|
|
newMap = realloc(reverseMap, newMapSize * sizeof(AtomListPtr));
|
|
|
|
if (newMap == NULL) {
|
|
|
|
fprintf(stderr, "ResizeReverseMap(): Error: Couldn't reallocate"
|
|
|
|
" reverseMap (%ld)\n",
|
|
|
|
newMapSize * (unsigned long) sizeof(AtomListPtr));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
reverseMap = newMap;
|
|
|
|
reverseMapSize = newMapSize;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
NameEqual(const char *a, const char *b, int l)
|
|
|
|
{
|
|
|
|
while (l--)
|
|
|
|
if (*a++ != *b++)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Atom
|
|
|
|
MakeAtom(const char *string, unsigned len, int makeit)
|
|
|
|
{
|
|
|
|
AtomListPtr a;
|
2025-01-10 01:40:29 +00:00
|
|
|
unsigned hash;
|
|
|
|
unsigned h = 0;
|
|
|
|
unsigned r;
|
2023-08-28 05:57:34 +00:00
|
|
|
|
|
|
|
hash = Hash(string, len);
|
|
|
|
if (hashTable) {
|
|
|
|
h = hash & hashMask;
|
|
|
|
if (hashTable[h]) {
|
|
|
|
if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
|
|
|
|
NameEqual(hashTable[h]->name, string, len)) {
|
|
|
|
return hashTable[h]->atom;
|
|
|
|
}
|
|
|
|
r = (hash % rehash) | 1;
|
|
|
|
for (;;) {
|
|
|
|
h += r;
|
|
|
|
if (h >= hashSize)
|
|
|
|
h -= hashSize;
|
|
|
|
if (!hashTable[h])
|
|
|
|
break;
|
|
|
|
if (hashTable[h]->hash == hash && hashTable[h]->len == len &&
|
|
|
|
NameEqual(hashTable[h]->name, string, len)) {
|
|
|
|
return hashTable[h]->atom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!makeit)
|
|
|
|
return None;
|
|
|
|
a = malloc(sizeof(AtomListRec) + len + 1);
|
|
|
|
if (a == NULL) {
|
|
|
|
fprintf(stderr, "MakeAtom(): Error: Couldn't allocate AtomListRec"
|
|
|
|
" (%ld)\n", (unsigned long) sizeof(AtomListRec) + len + 1);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
a->name = (char *) (a + 1);
|
|
|
|
a->len = len;
|
|
|
|
strncpy(a->name, string, len);
|
|
|
|
a->name[len] = '\0';
|
|
|
|
a->atom = ++lastAtom;
|
|
|
|
a->hash = hash;
|
|
|
|
if (hashUsed >= hashSize / 2) {
|
|
|
|
ResizeHashTable();
|
|
|
|
h = hash & hashMask;
|
|
|
|
if (hashTable[h]) {
|
|
|
|
r = (hash % rehash) | 1;
|
|
|
|
do {
|
|
|
|
h += r;
|
|
|
|
if (h >= hashSize)
|
|
|
|
h -= hashSize;
|
|
|
|
} while (hashTable[h]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hashTable[h] = a;
|
|
|
|
hashUsed++;
|
|
|
|
if (reverseMapSize <= a->atom) {
|
|
|
|
if (!ResizeReverseMap())
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
reverseMap[a->atom] = a;
|
|
|
|
return a->atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
NameForAtom(Atom atom)
|
|
|
|
{
|
|
|
|
if (atom != None && atom <= lastAtom)
|
|
|
|
return reverseMap[atom]->name;
|
|
|
|
return NULL;
|
|
|
|
}
|