server/src/util/strings.c

418 lines
9.7 KiB
C
Raw Normal View History

#ifdef _MSC_VER
#include <platform.h>
2018-12-02 20:32:04 +01:00
#undef HAVE__ITOA
#endif
#include "strings.h"
2010-08-08 10:06:34 +02:00
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
2017-12-31 21:33:31 +01:00
#ifdef HAVE_LIBBSD
#include <bsd/string.h>
#else
#include <string.h>
#endif
2018-12-02 20:32:04 +01:00
const char* str_itoa_r(int val, char *buf)
{
2018-12-02 20:32:04 +01:00
#ifdef HAVE__ITOA
return _itoa(val, buf, 10);
#else
2018-12-02 20:32:04 +01:00
snprintf(buf, 12, "%d", val);
return buf;
#endif
}
const char *str_itoa(int n)
{
2018-12-02 20:32:04 +01:00
static char buf[12];
#ifdef HAVE__ITOA
return _itoa(n, buf, 10);
#else
2018-12-02 20:32:04 +01:00
return str_itoa_r(n, buf);
#endif
}
2017-12-30 13:04:00 +01:00
size_t str_strlcpy(char *dst, const char *src, size_t len)
{
#ifdef HAVE_BSDSTRING
return strlcpy(dst, src, len);
#else
register char *d = dst;
register const char *s = src;
register size_t n = len;
2019-01-24 14:24:18 +01:00
assert(src);
assert(dst);
2017-12-30 13:04:00 +01:00
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
2018-11-27 21:30:08 +01:00
if (len != 0) {
*d = '\0'; /* NUL-terminate dst */
}
return (s - src) + strlen(s); /* count does not include NUL */
2017-12-30 13:04:00 +01:00
}
2019-01-24 14:24:18 +01:00
return (s - src - 1); /* count does not include NUL */
2017-12-30 13:04:00 +01:00
#endif
}
size_t str_strlcat(char *dst, const char *src, size_t len)
{
#ifdef HAVE_BSDSTRING
return strlcat(dst, src, len);
#else
register char *d = dst;
register const char *s = src;
register size_t n = len;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end */
while (*d != '\0' && n-- != 0)
d++;
2019-01-24 14:24:18 +01:00
dlen = d - dst;
2017-12-30 13:04:00 +01:00
n = len - dlen;
if (n == 0)
return (dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return (dlen + (s - src)); /* count does not include NUL */
#endif
}
size_t str_slprintf(char * dst, size_t size, const char * format, ...)
{
va_list args;
int result;
va_start(args, format);
result = vsnprintf(dst, size, format, args);
va_end(args);
if (result < 0 || result >= (int)size) {
dst[size - 1] = '\0';
return size;
}
return (size_t)result;
}
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var,
const char *value)
{
size_t val_len = strlen(value);
size_t var_len = strlen(var);
char *s = buffer;
while (buffer + size > s) {
char *p = strstr(tmpl, var);
size_t len;
if (p) {
2019-01-24 14:24:18 +01:00
len = p - tmpl;
}
else {
len = strlen(tmpl);
}
if (len < size) {
memmove(s, tmpl, len);
tmpl += len;
s += len;
size -= len;
if (p && val_len < size) {
tmpl += var_len;
memmove(s, value, val_len);
s += val_len;
size -= val_len;
}
}
if (!p) {
break;
}
}
*s = 0;
}
2018-05-18 02:55:33 +02:00
int str_hash(const char *s)
2010-08-08 10:06:34 +02:00
{
2018-05-18 02:55:33 +02:00
int key = 0;
2019-01-24 14:24:18 +01:00
assert(s);
while (*s) {
key = key * 37 + *s++;
}
return key & 0x7FFFFFFF;
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
}
char *str_strdup(const char *s) {
2019-01-24 14:24:18 +01:00
if (s == NULL) return NULL;
2017-12-31 21:33:31 +01:00
#ifdef HAVE_STRDUP
2019-01-24 14:24:18 +01:00
return strdup(s);
2017-12-31 21:33:31 +01:00
#elif defined(_MSC_VER)
2019-01-24 14:24:18 +01:00
return _strdup(s);
#else
2019-01-24 14:24:18 +01:00
size_t len = strlen(s);
char *dup = malloc(len+1);
memcpy(dup, s, len+1);
return dup;
#endif
}
2017-12-31 18:07:29 +01:00
void sbs_printf(struct sbstring *sbs, const char *format, ...)
{
size_t size = sbs->size - (sbs->end - sbs->begin);
if (size > 0) {
va_list argp;
va_start(argp, format);
2019-01-24 14:24:18 +01:00
int bytes = vsnprintf(sbs->end, size, format, argp);
if (bytes > 0) {
2019-01-24 14:24:18 +01:00
if ((size_t)bytes >= size) {
bytes = size - 1;
/* terminate truncated output */
2019-01-24 14:24:18 +01:00
sbs->end[bytes] = '\0';
}
2019-01-24 14:24:18 +01:00
sbs->end += bytes;
}
va_end(argp);
}
}
2017-12-31 18:07:29 +01:00
void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
{
2019-01-24 14:24:18 +01:00
assert(sbs);
assert(size > 0);
2017-12-31 18:07:29 +01:00
sbs->begin = buffer;
sbs->size = size;
sbs->end = buffer;
buffer[0] = '\0';
}
void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size)
{
size_t len = strlen(buffer);
2019-01-24 14:24:18 +01:00
assert(sbs);
assert(size > len);
sbs->begin = buffer;
sbs->size = size;
sbs->end = buffer + len;
}
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size)
{
size_t len;
2019-01-24 14:24:18 +01:00
assert(sbs);
len = sbs->size - (sbs->end - sbs->begin) - 1;
if (len < size) {
size = len;
}
memcpy(sbs->end, str, size);
sbs->end[size] = '\0';
sbs->end += size;
}
2017-12-31 18:07:29 +01:00
void sbs_strcat(struct sbstring *sbs, const char *str)
{
size_t len;
2019-01-24 14:24:18 +01:00
assert(sbs);
2017-12-31 18:07:29 +01:00
len = sbs->size - (sbs->end - sbs->begin);
2019-01-24 14:24:18 +01:00
str_strlcpy(sbs->end, str, len);
2018-11-27 21:30:08 +01:00
sbs->end += strlen(sbs->end);
assert(sbs->begin + sbs->size >= sbs->end);
2017-12-31 18:07:29 +01:00
}
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len)
{
2019-01-24 14:24:18 +01:00
if (pos > sbs->end - sbs->begin) {
/* starting past end of string, do nothing */
sbs->end = sbs->begin;
}
if (pos >= 0) {
2019-01-24 14:24:18 +01:00
size_t sz = sbs->end - (sbs->begin + pos);
if (len > sz) len = sz;
if (len - pos > 0) {
memmove(sbs->begin, sbs->begin + pos, len);
}
else {
memcpy(sbs->begin, sbs->begin + pos, len);
}
sbs->end = sbs->begin + len;
sbs->end[0] = '\0';
}
}
size_t sbs_length(const struct sbstring *sbs)
{
assert(sbs->begin + sbs->size >= sbs->end);
2019-01-24 14:24:18 +01:00
return sbs->end - sbs->begin;
}
char *str_unescape(char *str) {
char *read = str, *write = str;
while (*read) {
char * pos = strchr(read, '\\');
2019-01-24 14:24:18 +01:00
if (pos >= read) {
2019-01-22 20:37:23 +01:00
size_t len = (size_t)(pos - read);
memmove(write, read, len);
write += len;
read += (len + 1);
switch (read[0]) {
case 'r':
*write++ = '\r';
break;
case 'n':
*write++ = '\n';
break;
case 't':
*write++ = '\t';
break;
default:
*write++ = read[0];
}
*write = 0;
++read;
}
else {
size_t len = strlen(read);
memmove(write, read, len);
write[len] = 0;
break;
}
}
return str;
}
const char *str_escape_ex(const char *str, char *buffer, size_t size, const char *chars)
{
size_t slen = strlen(str);
const char *read = str;
2019-01-22 20:37:23 +01:00
unsigned char *write = (unsigned char *)buffer;
if (size < 1) {
return NULL;
}
while (slen > 0 && size > 1 && *read) {
const char *pos = strpbrk(read, chars);
size_t len = size;
2019-01-24 14:24:18 +01:00
if (pos >= read) {
len = pos - read;
}
if (len < size) {
unsigned char ch = *(const unsigned char *)pos;
if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len);
slen -= len;
write += len;
read += len;
size -= len;
}
switch (ch) {
case '\t':
if (size > 2) {
*write++ = '\\';
*write++ = 't';
size -= 2;
}
else size = 1;
break;
case '\n':
if (size > 2) {
*write++ = '\\';
*write++ = 'n';
size -= 2;
}
else size = 1;
break;
case '\r':
if (size > 2) {
*write++ = '\\';
*write++ = 'r';
size -= 2;
}
else size = 1;
break;
case '\"':
case '\'':
case '\\':
if (size > 2) {
*write++ = '\\';
*write++ = ch;
size -= 2;
}
break;
default:
if (size > 5) {
2019-01-22 20:37:23 +01:00
int n = snprintf((char *)write, size, "\\%03o", ch);
if (n > 0) {
assert(n == 5);
write += n;
size -= n;
}
else size = 1;
}
else size = 1;
}
++read;
--slen;
} else {
/* end of buffer space */
len = size - 1;
if (len > 0) {
if (len > slen) {
len = slen;
}
memmove(write, read, len);
write += len;
size -= len;
slen -= len;
break;
}
}
}
*write = '\0';
return buffer;
}
const char *str_escape(const char *str, char *buffer, size_t size) {
return str_escape_ex(str, buffer, size, "\n\t\r\'\"\\");
}