2010-08-08 10:06:34 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
INLINE_FUNCTION unsigned int hashstring(const char *s)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
unsigned int key = 0;
|
|
|
|
while (*s) {
|
2011-03-07 08:02:35 +01:00
|
|
|
key = key * 37 + *s++;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
return key & 0x7FFFFFFF;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
INLINE_FUNCTION const char *escape_string(const char *str, char *buffer,
|
|
|
|
unsigned int len)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *start = strchr(str, '\"');
|
2010-08-08 10:06:34 +02:00
|
|
|
if (start) {
|
|
|
|
static char s_buffer[4096]; /* STATIC_RESULT: used for return, not across calls */
|
2011-03-07 08:02:35 +01:00
|
|
|
const char *p;
|
|
|
|
char *o;
|
|
|
|
size_t skip = start - str;
|
|
|
|
|
|
|
|
if (buffer == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
buffer = s_buffer;
|
|
|
|
len = sizeof(s_buffer);
|
|
|
|
}
|
|
|
|
memcpy(buffer, str, skip);
|
|
|
|
o = buffer + skip;
|
|
|
|
p = str + skip;
|
|
|
|
do {
|
2011-03-07 08:02:35 +01:00
|
|
|
if (*p == '\"' || *p == '\\') {
|
|
|
|
if (len < 2) {
|
2010-08-08 10:06:34 +02:00
|
|
|
*o = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*o++) = '\\';
|
|
|
|
len -= 2;
|
|
|
|
} else {
|
2011-03-07 08:02:35 +01:00
|
|
|
if (len < 1) {
|
2010-08-08 10:06:34 +02:00
|
|
|
*o = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
(*o++) = (*p);
|
|
|
|
} while (*p++);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
INLINE_FUNCTION unsigned int jenkins_hash(unsigned int a)
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
a = (a + 0x7ed55d16) + (a << 12);
|
|
|
|
a = (a ^ 0xc761c23c) ^ (a >> 19);
|
|
|
|
a = (a + 0x165667b1) + (a << 5);
|
|
|
|
a = (a + 0xd3a2646c) ^ (a << 9);
|
|
|
|
a = (a + 0xfd7046c5) + (a << 3);
|
|
|
|
a = (a ^ 0xb55a4f09) ^ (a >> 16);
|
2010-08-08 10:06:34 +02:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
INLINE_FUNCTION unsigned int wang_hash(unsigned int a)
|
|
|
|
{
|
2011-03-07 17:26:50 +01:00
|
|
|
a = ~a + (a << 15); /* a = (a << 15) - a - 1; */
|
2010-08-08 10:06:34 +02:00
|
|
|
a = a ^ (a >> 12);
|
|
|
|
a = a + (a << 2);
|
|
|
|
a = a ^ (a >> 4);
|
2011-03-07 17:26:50 +01:00
|
|
|
a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */
|
2010-08-08 10:06:34 +02:00
|
|
|
a = a ^ (a >> 16);
|
|
|
|
return a;
|
|
|
|
}
|