server/src/util/strings.c

120 lines
3 KiB
C
Raw Normal View History

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>
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>
#include "strings.h"
#include "assert.h"
2010-08-08 10:06:34 +02:00
/* libc includes */
#include <string.h>
#include <assert.h>
#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 {
char *rs = realloc(*s, strlen(neu) + 1);
assert_alloc(rs);
*s = rs;
strcpy(*s, neu);
}
return *s;
}
2010-08-08 10:06:34 +02:00
unsigned int hashstring(const char *s)
2010-08-08 10:06:34 +02:00
{
unsigned int key = 0;
assert(s);
while (*s) {
key = key * 37 + *s++;
}
return key & 0x7FFFFFFF;
2010-08-08 10:06:34 +02:00
}
const char *escape_string(const char *str, char *buffer,
size_t len)
2010-08-08 10:06:34 +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
if (skip > len) {
skip = len;
}
memcpy(buffer, str, skip);
o = buffer + skip;
p = str + skip;
len -= skip;
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
}
return str;
2010-08-08 10:06:34 +02:00
}
unsigned int jenkins_hash(unsigned int a)
2010-08-08 10:06:34 +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
}
unsigned int wang_hash(unsigned int a)
2010-08-08 10:06:34 +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
}