2010-08-08 10:06:34 +02:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2014-08-24 22:54:40 +02:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2016-08-29 15:49:31 +02:00
|
|
|
#include "strings.h"
|
2016-08-29 10:20:08 +02:00
|
|
|
#include "assert.h"
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/* libc includes */
|
|
|
|
#include <string.h>
|
2014-08-24 22:54:40 +02:00
|
|
|
#include <assert.h>
|
2015-05-18 08:59:38 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
char *set_string(char **s, const char *neu)
|
|
|
|
{
|
|
|
|
if (neu == NULL) {
|
|
|
|
free(*s);
|
|
|
|
*s = NULL;
|
|
|
|
}
|
|
|
|
else if (*s == NULL) {
|
|
|
|
*s = malloc(strlen(neu) + 1);
|
|
|
|
strcpy(*s, neu);
|
|
|
|
}
|
|
|
|
else {
|
2016-08-29 10:20:08 +02:00
|
|
|
char *rs = realloc(*s, strlen(neu) + 1);
|
|
|
|
assert_alloc(rs);
|
|
|
|
*s = rs;
|
2015-05-18 08:59:38 +02:00
|
|
|
strcpy(*s, neu);
|
|
|
|
}
|
|
|
|
return *s;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
unsigned int hashstring(const char *s)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02:00
|
|
|
unsigned int key = 0;
|
2015-08-05 10:25:25 +02:00
|
|
|
assert(s);
|
2014-08-24 22:54:40 +02:00
|
|
|
while (*s) {
|
|
|
|
key = key * 37 + *s++;
|
|
|
|
}
|
|
|
|
return key & 0x7FFFFFFF;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
const char *escape_string(const char *str, char *buffer,
|
2015-05-15 20:35:36 +02:00
|
|
|
size_t len)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02:00
|
|
|
const char *start = strchr(str, '\"');
|
|
|
|
if (!start) start = strchr(str, '\\');
|
|
|
|
assert(buffer);
|
|
|
|
if (start) {
|
|
|
|
const char *p;
|
|
|
|
char *o;
|
|
|
|
size_t skip = start - str;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
2014-12-12 11:28:37 +01:00
|
|
|
if (skip > len) {
|
|
|
|
skip = len;
|
|
|
|
}
|
2014-08-24 22:54:40 +02:00
|
|
|
memcpy(buffer, str, skip);
|
|
|
|
o = buffer + skip;
|
|
|
|
p = str + skip;
|
2014-12-12 11:28:37 +01:00
|
|
|
len -= skip;
|
2014-08-24 22:54:40 +02:00
|
|
|
do {
|
|
|
|
if (*p == '\"' || *p == '\\') {
|
|
|
|
if (len < 2) {
|
|
|
|
*o = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*o++) = '\\';
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (len < 1) {
|
|
|
|
*o = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
(*o++) = (*p);
|
|
|
|
} while (*p++);
|
|
|
|
return buffer;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-08-24 22:54:40 +02:00
|
|
|
return str;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
unsigned int jenkins_hash(unsigned int a)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02: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);
|
|
|
|
return a;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:51:57 +01:00
|
|
|
unsigned int wang_hash(unsigned int a)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-08-24 22:54:40 +02:00
|
|
|
a = ~a + (a << 15); /* a = (a << 15) - a - 1; */
|
|
|
|
a = a ^ (a >> 12);
|
|
|
|
a = a + (a << 2);
|
|
|
|
a = a ^ (a >> 4);
|
|
|
|
a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */
|
|
|
|
a = a ^ (a >> 16);
|
|
|
|
return a;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|