-
Notifications
You must be signed in to change notification settings - Fork 3
/
name.cpp
340 lines (284 loc) · 8.88 KB
/
name.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
** name.cpp
** Implements int-as-string mapping.
**
**---------------------------------------------------------------------------
** Copyright 2005-2007 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include "StdAfx.h"
#include "name.h"
#include "zip/zlib/zlib.h"
template <typename T, size_t N>
char ( &_ArraySizeHelper( T (&array)[N] ))[N];
#define countof( array ) (sizeof( _ArraySizeHelper( array ) ))
// MACROS ------------------------------------------------------------------
// The number of bytes to allocate to each NameBlock unless somebody is evil
// and wants a really long name. In that case, it gets its own NameBlock
// that is just large enough to hold it.
#define BLOCK_SIZE 4096
// How many entries to grow the NameArray by when it needs to grow.
#define NAME_GROW_AMOUNT 256
// TYPES -------------------------------------------------------------------
// Name text is stored in a linked list of NameBlock structures. This
// is really the header for the block, with the remainder of the block
// being populated by text for names.
struct FName::NameManager::NameBlock
{
size_t NextAlloc;
NameBlock *NextBlock;
};
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
FName::NameManager FName::NameData;
// Define the predefined names.
static const char *PredefinedNames[] =
{
#define xx(n) #n,
#include "namedef.h"
#undef xx
};
// CODE --------------------------------------------------------------------
// zlib includes some CRC32 stuff, so just use that
inline const DWORD *GetCRCTable () { return (const DWORD *)get_crc_table(); }
inline DWORD CalcCRC32 (const BYTE *buf, unsigned int len)
{
return crc32 (0, buf, len);
}
inline DWORD AddCRC32 (DWORD crc, const BYTE *buf, unsigned int len)
{
return crc32 (crc, buf, len);
}
inline DWORD CRC1 (DWORD crc, const BYTE c, const DWORD *crcTable)
{
return crcTable[(crc & 0xff) ^ c] ^ (crc >> 8);
}
unsigned int MakeKey (const char *s)
{
if (s == NULL)
{
return 0xffffffff;
}
DWORD key = 0xffffffff;
const DWORD *table = GetCRCTable ();
while (*s)
{
key = CRC1 (key, tolower (*s++), table);
}
return key ^ 0xffffffff;
}
unsigned int MakeKey (const char *s, size_t len)
{
if (len == 0 || s == NULL)
{
return 0xffffffff;
}
DWORD key = 0xffffffff;
const DWORD *table = GetCRCTable ();
while (len > 0)
{
key = CRC1 (key, tolower(*s++), table);
--len;
}
return key ^ 0xffffffff;
}
//==========================================================================
//
// FName :: NameManager :: FindName
//
// Returns the index of a name. If the name does not exist and noCreate is
// true, then it returns false. If the name does not exist and noCreate is
// false, then the name is added to the table and its new index is returned.
//
//==========================================================================
int FName::NameManager::FindName (const char *text, bool noCreate)
{
if (!Inited)
{
InitBuckets ();
}
if (text == NULL)
{
return 0;
}
unsigned int hash = MakeKey(text);
unsigned int bucket = hash % HASH_SIZE;
int scanner = Buckets[bucket];
// See if the name already exists.
while (scanner >= 0)
{
if (NameArray[scanner].Hash == hash && stricmp (NameArray[scanner].Text, text) == 0)
{
return scanner;
}
scanner = NameArray[scanner].NextHash;
}
// If we get here, then the name does not exist.
if (noCreate)
{
return 0;
}
return AddName (text, hash, bucket);
}
//==========================================================================
//
// The same as above, but the text length is also passed, for creating
// a name from a substring or for speed if the length is already known.
//
//==========================================================================
int FName::NameManager::FindName (const char *text, size_t textLen, bool noCreate)
{
if (!Inited)
{
InitBuckets ();
}
if (text == NULL)
{
return 0;
}
unsigned int hash = MakeKey(text, textLen);
unsigned int bucket = hash % HASH_SIZE;
int scanner = Buckets[bucket];
// See if the name already exists.
while (scanner >= 0)
{
if (NameArray[scanner].Hash == hash &&
strnicmp (NameArray[scanner].Text, text, textLen) == 0 &&
NameArray[scanner].Text[textLen] == '\0')
{
return scanner;
}
scanner = NameArray[scanner].NextHash;
}
// If we get here, then the name does not exist.
if (noCreate)
{
return 0;
}
return AddName (text, hash, bucket);
}
//==========================================================================
//
// FName :: NameManager :: InitBuckets
//
// Sets up the hash table and inserts all the default names into the table.
//
//==========================================================================
void FName::NameManager::InitBuckets ()
{
Inited = true;
memset (Buckets, -1, sizeof(Buckets));
// Register built-in names. 'None' must be name 0.
for (size_t i = 0; i < countof(PredefinedNames); ++i)
{
FindName (PredefinedNames[i], false);
}
}
//==========================================================================
//
// FName :: NameManager :: AddName
//
// Adds a new name to the name table.
//
//==========================================================================
int FName::NameManager::AddName (const char *text, unsigned int hash, unsigned int bucket)
{
char *textstore;
NameBlock *block = Blocks;
size_t len = strlen (text) + 1;
// Get a block large enough for the name. Only the first block in the
// list is ever considered for name storage.
if (block == NULL || block->NextAlloc + len >= BLOCK_SIZE)
{
block = AddBlock (len);
}
// Copy the string into the block.
textstore = (char *)block + block->NextAlloc;
strcpy (textstore, text);
block->NextAlloc += len;
// Add an entry for the name to the NameArray
if (NumNames >= MaxNames)
{
// If no names have been defined yet, make the first allocation
// large enough to hold all the predefined names.
MaxNames += MaxNames == 0 ? countof(PredefinedNames) + NAME_GROW_AMOUNT : NAME_GROW_AMOUNT;
NameArray = (NameEntry *)realloc (NameArray, MaxNames * sizeof(NameEntry));
}
NameArray[NumNames].Text = textstore;
NameArray[NumNames].Hash = hash;
NameArray[NumNames].NextHash = Buckets[bucket];
Buckets[bucket] = NumNames;
return NumNames++;
}
//==========================================================================
//
// FName :: NameManager :: AddBlock
//
// Creates a new NameBlock at least large enough to hold the required
// number of chars.
//
//==========================================================================
FName::NameManager::NameBlock *FName::NameManager::AddBlock (size_t len)
{
NameBlock *block;
len += sizeof(NameBlock);
if (len < BLOCK_SIZE)
{
len = BLOCK_SIZE;
}
block = (NameBlock *)malloc (len);
block->NextAlloc = sizeof(NameBlock);
block->NextBlock = Blocks;
Blocks = block;
return block;
}
//==========================================================================
//
// FName :: NameManager :: ~NameManager
//
// Release all the memory used for name bookkeeping.
//
//==========================================================================
FName::NameManager::~NameManager()
{
NameBlock *block, *next;
for (block = Blocks; block != NULL; block = next)
{
next = block->NextBlock;
free (block);
}
Blocks = NULL;
if (NameArray != NULL)
{
free (NameArray);
NameArray = NULL;
}
NumNames = MaxNames = 0;
memset (Buckets, -1, sizeof(Buckets));
}