2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-03-25 07:45:05 +00:00
|
|
|
#include <time.h> /* strftime() */
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-04-14 21:10:24 +00:00
|
|
|
static const char hex_chars_lc[] = "0123456789abcdef";
|
|
|
|
static const char hex_chars_uc[] = "0123456789ABCDEF";
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
* init the buffer
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
buffer* buffer_init(void) {
|
|
|
|
buffer *b;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b = malloc(sizeof(*b));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(b);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b->ptr = NULL;
|
|
|
|
b->size = 0;
|
|
|
|
b->used = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer *buffer_init_buffer(const buffer *src) {
|
2005-08-08 14:40:47 +00:00
|
|
|
buffer *b = buffer_init();
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(b, src);
|
2005-08-08 14:40:47 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer *buffer_init_string(const char *str) {
|
|
|
|
buffer *b = buffer_init();
|
|
|
|
buffer_copy_string(b, str);
|
|
|
|
return b;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
void buffer_free(buffer *b) {
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == b) return;
|
2005-02-28 10:38:16 +00:00
|
|
|
|
|
|
|
free(b->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
2020-08-10 23:46:17 +00:00
|
|
|
void buffer_free_ptr(buffer *b) {
|
2018-10-23 00:47:42 +00:00
|
|
|
free(b->ptr);
|
|
|
|
b->ptr = NULL;
|
|
|
|
b->used = 0;
|
|
|
|
b->size = 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_move(buffer * restrict b, buffer * restrict src) {
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer tmp;
|
2018-11-23 06:19:14 +00:00
|
|
|
force_assert(NULL != b);
|
|
|
|
force_assert(NULL != src);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-11-23 06:19:14 +00:00
|
|
|
buffer_clear(b);
|
2015-02-08 12:37:10 +00:00
|
|
|
tmp = *src; *src = *b; *b = tmp;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
/* make sure buffer is at least "size" big + 1 for '\0'. keep old data */
|
|
|
|
__attribute_cold__
|
2019-12-31 03:02:34 +00:00
|
|
|
static void buffer_realloc(buffer * const b, const size_t len) {
|
2018-11-23 23:46:39 +00:00
|
|
|
#define BUFFER_PIECE_SIZE 64uL /*(must be power-of-2)*/
|
|
|
|
const size_t sz = (len + 1 + BUFFER_PIECE_SIZE-1) & ~(BUFFER_PIECE_SIZE-1);
|
|
|
|
force_assert(sz > len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
b->size = sz;
|
|
|
|
b->ptr = realloc(b->ptr, sz);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
force_assert(NULL != b->ptr);
|
2015-02-08 19:10:44 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
__attribute_cold__
|
2019-07-14 07:36:22 +00:00
|
|
|
__attribute_noinline__
|
2019-12-31 03:02:34 +00:00
|
|
|
static void buffer_alloc_replace(buffer * const b, const size_t size) {
|
2019-07-14 07:36:22 +00:00
|
|
|
force_assert(NULL != b);
|
2018-11-23 23:46:39 +00:00
|
|
|
/*(discard old data so realloc() does not copy)*/
|
|
|
|
if (NULL != b->ptr) {
|
|
|
|
free(b->ptr);
|
|
|
|
b->ptr = NULL;
|
|
|
|
}
|
|
|
|
buffer_realloc(b, size);
|
2015-02-08 12:37:10 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-12-31 03:02:34 +00:00
|
|
|
char* buffer_string_prepare_copy(buffer * const b, const size_t size) {
|
2020-07-10 23:35:26 +00:00
|
|
|
if (NULL == b->ptr || size >= b->size) buffer_alloc_replace(b, size);
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
b->used = 0;
|
2015-02-08 19:10:36 +00:00
|
|
|
return b->ptr;
|
|
|
|
}
|
|
|
|
|
2019-07-14 07:36:22 +00:00
|
|
|
__attribute_cold__
|
|
|
|
__attribute_noinline__
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2019-12-31 03:02:34 +00:00
|
|
|
static char* buffer_string_prepare_append_resize(buffer * const b, const size_t size) {
|
2015-02-08 19:10:36 +00:00
|
|
|
force_assert(NULL != b);
|
2015-02-08 19:10:39 +00:00
|
|
|
if (buffer_string_is_empty(b)) {
|
2015-02-08 19:10:36 +00:00
|
|
|
return buffer_string_prepare_copy(b, size);
|
|
|
|
} else {
|
2015-02-08 19:10:44 +00:00
|
|
|
/* not empty, b->used already includes a terminating 0 */
|
2018-08-26 16:39:37 +00:00
|
|
|
size_t req_size = b->used + size;
|
2015-02-08 19:10:39 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
/* check for overflow: unsigned overflow is defined to wrap around */
|
|
|
|
force_assert(req_size >= b->used);
|
2015-02-08 19:10:39 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
buffer_realloc(b, req_size);
|
2015-02-08 19:10:39 +00:00
|
|
|
|
|
|
|
return b->ptr + b->used - 1;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:02:34 +00:00
|
|
|
char* buffer_string_prepare_append(buffer * const b, const size_t size) {
|
2020-07-10 23:35:26 +00:00
|
|
|
return (NULL != b->ptr && size < b->size - b->used)
|
2019-07-14 07:36:22 +00:00
|
|
|
? b->ptr + b->used - (0 != b->used)
|
|
|
|
: buffer_string_prepare_append_resize(b, size);
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:08:17 +00:00
|
|
|
void buffer_string_set_length(buffer *b, uint32_t len) {
|
2015-02-08 19:10:44 +00:00
|
|
|
force_assert(NULL != b);
|
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
if (len >= b->size) buffer_realloc(b, len);
|
2015-02-08 19:10:44 +00:00
|
|
|
|
|
|
|
b->used = len + 1;
|
|
|
|
b->ptr[len] = '\0';
|
|
|
|
}
|
2015-02-08 19:10:36 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_commit(buffer *b, size_t size)
|
|
|
|
{
|
|
|
|
force_assert(NULL != b);
|
|
|
|
force_assert(b->size > 0);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (0 == b->used) b->used = 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (size > 0) {
|
|
|
|
/* check for overflow: unsigned overflow is defined to wrap around */
|
2018-11-23 23:46:39 +00:00
|
|
|
size_t sz = b->used + size;
|
|
|
|
force_assert(sz > b->used);
|
|
|
|
force_assert(sz <= b->size);
|
|
|
|
b->used = sz;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
|
|
|
|
b->ptr[b->used - 1] = '\0';
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_copy_string(buffer * restrict b, const char * restrict s) {
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_string_len(b, s, NULL != s ? strlen(s) : 0);
|
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_copy_string_len(buffer * const restrict b, const char * const restrict s, const size_t s_len) {
|
2020-07-11 02:18:26 +00:00
|
|
|
if (NULL == b->ptr || s_len >= b->size) buffer_alloc_replace(b, s_len);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2018-10-08 03:35:07 +00:00
|
|
|
b->used = s_len + 1;
|
2019-12-31 03:02:34 +00:00
|
|
|
b->ptr[s_len] = '\0';
|
|
|
|
if (0 != s_len) memcpy(b->ptr, s, s_len); /*(s might be NULL)*/
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string(buffer * restrict b, const char * restrict s) {
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_string_len(b, s, NULL != s ? strlen(s) : 0);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* append a string to the end of the buffer
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* the resulting buffer is terminated with a '\0'
|
2005-02-20 14:27:00 +00:00
|
|
|
* s is treated as a un-terminated string (a \0 is handled a normal character)
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* @param b a buffer
|
|
|
|
* @param s the string
|
|
|
|
* @param s_len size of the string (without the terminating \0)
|
|
|
|
*/
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_len(buffer * const restrict b, const char * const restrict s, const size_t s_len) {
|
2019-12-31 03:02:34 +00:00
|
|
|
char * const target_buf = buffer_string_prepare_append(b, s_len);
|
|
|
|
b->used += s_len + (0 == b->used); /*(must include '\0' for append)*/
|
|
|
|
target_buf[s_len] = '\0';
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-11-23 23:46:39 +00:00
|
|
|
/*(s might be NULL if 0 == s_len)*/
|
|
|
|
if (s_len) memcpy(target_buf, s, s_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen) {
|
2018-11-28 07:12:41 +00:00
|
|
|
size_t blen = buffer_string_length(b);
|
|
|
|
int aslash = (alen && a[0] == '/');
|
|
|
|
buffer_string_prepare_append(b, alen+2); /*(+ '/' and + '\0' if 0 == blen)*/
|
|
|
|
if (blen && b->ptr[blen-1] == '/') {
|
|
|
|
if (aslash) --b->used;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!b->used) ++b->used;
|
|
|
|
if (!aslash) b->ptr[++b->used - 2] = '/';
|
|
|
|
}
|
|
|
|
memcpy(b->ptr+b->used-1, a, alen);
|
|
|
|
b->ptr[(b->used += alen)-1] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-04-15 00:09:49 +00:00
|
|
|
void buffer_append_uint_hex_lc(buffer *b, uintmax_t value) {
|
2005-02-20 14:27:00 +00:00
|
|
|
char *buf;
|
2017-08-13 01:43:04 +00:00
|
|
|
unsigned int shift = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
{
|
2015-02-08 19:10:46 +00:00
|
|
|
uintmax_t copy = value;
|
2015-02-08 12:37:10 +00:00
|
|
|
do {
|
|
|
|
copy >>= 8;
|
2017-08-13 01:43:04 +00:00
|
|
|
shift += 8; /* counting bits */
|
2015-02-08 12:37:10 +00:00
|
|
|
} while (0 != copy);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2017-08-13 01:43:04 +00:00
|
|
|
buf = buffer_string_prepare_append(b, shift >> 2); /*nibbles (4 bits)*/
|
|
|
|
buffer_commit(b, shift >> 2); /* will fill below */
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
while (shift > 0) {
|
|
|
|
shift -= 4;
|
2018-04-15 00:09:49 +00:00
|
|
|
*(buf++) = hex_chars_lc[(value >> shift) & 0x0F];
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:02:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 12:37:10 +00:00
|
|
|
static char* utostr(char * const buf_end, uintmax_t val) {
|
|
|
|
char *cur = buf_end;
|
|
|
|
do {
|
|
|
|
int mod = val % 10;
|
|
|
|
val /= 10;
|
|
|
|
/* prepend digit mod */
|
|
|
|
*(--cur) = (char) ('0' + mod);
|
|
|
|
} while (0 != val);
|
|
|
|
return cur;
|
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2019-12-31 03:02:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 12:37:10 +00:00
|
|
|
static char* itostr(char * const buf_end, intmax_t val) {
|
2016-06-23 07:57:19 +00:00
|
|
|
/* absolute value not defined for INTMAX_MIN, but can take absolute
|
|
|
|
* value of any negative number via twos complement cast to unsigned.
|
|
|
|
* negative sign is prepended after (now unsigned) value is converted
|
|
|
|
* to string */
|
|
|
|
uintmax_t uval = val >= 0 ? (uintmax_t)val : ((uintmax_t)~val) + 1;
|
|
|
|
char *cur = utostr(buf_end, uval);
|
|
|
|
if (val < 0) *(--cur) = '-';
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return cur;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_int(buffer *b, intmax_t val) {
|
|
|
|
char buf[LI_ITOSTRING_LENGTH];
|
|
|
|
char* const buf_end = buf + sizeof(buf);
|
|
|
|
char *str;
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != b);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
str = itostr(buf_end, val);
|
|
|
|
force_assert(buf_end > str && str >= buf);
|
|
|
|
|
|
|
|
buffer_append_string_len(b, str, buf_end - str);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_strftime(buffer * const restrict b, const char * const restrict format, const struct tm * const restrict tm) {
|
2019-12-08 01:50:42 +00:00
|
|
|
size_t rv;
|
2015-02-08 19:10:44 +00:00
|
|
|
char* buf;
|
2018-11-23 23:46:39 +00:00
|
|
|
force_assert(NULL != format);
|
2015-02-08 19:10:44 +00:00
|
|
|
force_assert(NULL != tm);
|
|
|
|
|
|
|
|
buf = buffer_string_prepare_append(b, 255);
|
2019-12-08 01:50:42 +00:00
|
|
|
rv = strftime(buf, buffer_string_space(b), format, tm);
|
2015-02-08 19:10:44 +00:00
|
|
|
|
|
|
|
/* 0 (in some apis buffer_string_space(b)) signals the string may have
|
|
|
|
* been too small; but the format could also just have lead to an empty
|
|
|
|
* string
|
|
|
|
*/
|
2019-12-08 01:50:42 +00:00
|
|
|
if (0 == rv || rv >= buffer_string_space(b)) {
|
2015-02-08 19:10:44 +00:00
|
|
|
/* give it a second try with a larger string */
|
|
|
|
buf = buffer_string_prepare_append(b, 4095);
|
2019-12-08 01:50:42 +00:00
|
|
|
rv = strftime(buf, buffer_string_space(b), format, tm);
|
2015-02-08 19:10:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 01:50:42 +00:00
|
|
|
if (rv >= buffer_string_space(b)) rv = 0;
|
2015-02-08 19:10:44 +00:00
|
|
|
|
2019-12-08 01:50:42 +00:00
|
|
|
buffer_commit(b, rv);
|
2015-02-08 19:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-28 15:27:01 +00:00
|
|
|
size_t li_itostrn(char *buf, size_t buf_len, intmax_t val) {
|
2015-02-08 12:37:10 +00:00
|
|
|
char p_buf[LI_ITOSTRING_LENGTH];
|
2019-11-28 15:27:01 +00:00
|
|
|
char* const str = itostr(p_buf+sizeof(p_buf), val);
|
|
|
|
size_t len = (size_t)(p_buf+sizeof(p_buf)-str);
|
|
|
|
force_assert(len <= buf_len);
|
|
|
|
memcpy(buf, str, len);
|
|
|
|
return len;
|
2015-02-08 12:37:10 +00:00
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2019-11-28 15:27:01 +00:00
|
|
|
size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val) {
|
2015-02-08 12:37:10 +00:00
|
|
|
char p_buf[LI_ITOSTRING_LENGTH];
|
2019-11-28 15:27:01 +00:00
|
|
|
char* const str = utostr(p_buf+sizeof(p_buf), val);
|
|
|
|
size_t len = (size_t)(p_buf+sizeof(p_buf)-str);
|
|
|
|
force_assert(len <= buf_len);
|
|
|
|
memcpy(buf, str, len);
|
|
|
|
return len;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 21:17:19 +00:00
|
|
|
#define li_ntox_lc(n) ((n) <= 9 ? (n) + '0' : (n) + 'a' - 10)
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
char int2hex(char c) {
|
2018-04-14 21:17:19 +00:00
|
|
|
/*return li_ntox_lc(c & 0xF);*/
|
|
|
|
return hex_chars_lc[(c & 0x0F)];
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 21:17:19 +00:00
|
|
|
/* c (char) and n (nibble) MUST be unsigned integer types */
|
|
|
|
#define li_cton(c,n) \
|
|
|
|
(((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* converts hex char (0-9, A-Z, a-z) to decimal.
|
|
|
|
* returns 0xFF on invalid input.
|
|
|
|
*/
|
|
|
|
char hex2int(unsigned char hex) {
|
2018-04-14 21:17:19 +00:00
|
|
|
unsigned char n;
|
|
|
|
return li_cton(hex,n) ? (char)n : 0xFF;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 01:18:28 +00:00
|
|
|
|
|
|
|
int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len) {
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
unsigned int ca = ((unsigned char *)a)[i];
|
|
|
|
unsigned int cb = ((unsigned char *)b)[i];
|
|
|
|
if (ca != cb) {
|
|
|
|
ca |= 0x20;
|
|
|
|
cb |= 0x20;
|
2020-09-10 04:15:29 +00:00
|
|
|
if (ca != cb) return 0;
|
|
|
|
if (!light_islower(ca)) return 0;
|
|
|
|
if (!light_islower(cb)) return 0;
|
2019-06-05 01:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen) {
|
|
|
|
/* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
|
|
|
|
return (alen == blen && buffer_eq_icase_ssn(a, b, blen));
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen) {
|
|
|
|
/* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/
|
|
|
|
/* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
|
|
|
|
return (b->used == slen + 1 && buffer_eq_icase_ssn(b->ptr, s, slen));
|
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen) {
|
|
|
|
/* Note: b must be initialized, i.e. 0 != b->used; uninitialized is not eq*/
|
|
|
|
/* 1 = equal; 0 = not equal */ /* short string sizes expected (< INT_MAX) */
|
|
|
|
return (b->used == slen + 1 && 0 == memcmp(b->ptr, s, slen));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
|
|
|
* check if two buffer contain the same data
|
|
|
|
*/
|
|
|
|
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal(const buffer *a, const buffer *b) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != a && NULL != b);
|
|
|
|
|
2018-10-05 05:05:44 +00:00
|
|
|
/* 1 = equal; 0 = not equal */
|
|
|
|
return (a->used == b->used && 0 == memcmp(a->ptr, b->ptr, a->used));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != a && NULL != s);
|
|
|
|
force_assert(b_len + 1 > b_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-10-05 05:05:44 +00:00
|
|
|
/* 1 = equal; 0 = not equal */
|
|
|
|
return (a->used == b_len + 1 && 0 == memcmp(a->ptr, s, b_len));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 20:23:21 +00:00
|
|
|
/* buffer_is_equal_caseless_string(b, CONST_STR_LEN("value")) */
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len) {
|
2018-10-05 05:05:44 +00:00
|
|
|
force_assert(NULL != a && NULL != s);
|
|
|
|
force_assert(b_len + 1 > b_len);
|
|
|
|
/* 1 = equal; 0 = not equal */
|
2019-06-05 01:18:28 +00:00
|
|
|
return buffer_eq_icase_slen(a, s, b_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_right_len(const buffer *b1, const buffer *b2, size_t len) {
|
2015-02-08 12:37:10 +00:00
|
|
|
/* no len -> equal */
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len == 0) return 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* len > 0, but empty buffers -> not equal */
|
|
|
|
if (b1->used == 0 || b2->used == 0) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* buffers too small -> not equal */
|
2014-02-14 21:06:16 +00:00
|
|
|
if (b1->used - 1 < len || b2->used - 1 < len) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return 0 == memcmp(b1->ptr + b1->used - 1 - len, b2->ptr + b2->used - 1 - len, len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 00:01:54 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void li_tohex_lc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) {
|
2016-04-01 16:54:46 +00:00
|
|
|
force_assert(2 * s_len > s_len);
|
|
|
|
force_assert(2 * s_len < buf_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-04-15 00:01:54 +00:00
|
|
|
for (size_t i = 0; i < s_len; ++i) {
|
|
|
|
buf[2*i] = hex_chars_lc[(s[i] >> 4) & 0x0F];
|
|
|
|
buf[2*i+1] = hex_chars_lc[s[i] & 0x0F];
|
2015-02-08 19:10:44 +00:00
|
|
|
}
|
|
|
|
buf[2*s_len] = '\0';
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void li_tohex_uc(char * const restrict buf, size_t buf_len, const char * const restrict s, size_t s_len) {
|
2018-04-15 00:01:54 +00:00
|
|
|
force_assert(2 * s_len > s_len);
|
|
|
|
force_assert(2 * s_len < buf_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-04-15 00:01:54 +00:00
|
|
|
for (size_t i = 0; i < s_len; ++i) {
|
|
|
|
buf[2*i] = hex_chars_uc[(s[i] >> 4) & 0x0F];
|
|
|
|
buf[2*i+1] = hex_chars_uc[s[i] & 0x0F];
|
|
|
|
}
|
|
|
|
buf[2*s_len] = '\0';
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 06:38:30 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_substr_replace (buffer * const restrict b, const size_t offset,
|
|
|
|
const size_t len, const buffer * const restrict replace)
|
2017-04-24 06:38:30 +00:00
|
|
|
{
|
|
|
|
const size_t blen = buffer_string_length(b);
|
|
|
|
const size_t rlen = buffer_string_length(replace);
|
|
|
|
|
|
|
|
if (rlen > len) {
|
|
|
|
buffer_string_set_length(b, blen-len+rlen);
|
|
|
|
memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(b->ptr+offset, replace->ptr, rlen);
|
|
|
|
|
|
|
|
if (rlen < len) {
|
|
|
|
memmove(b->ptr+offset+rlen, b->ptr+offset+len, blen-offset-len);
|
|
|
|
buffer_string_set_length(b, blen-len+rlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_encoded_hex_lc(buffer * const restrict b, const char * const restrict s, size_t len) {
|
2018-04-14 21:10:24 +00:00
|
|
|
unsigned char * const p =
|
|
|
|
(unsigned char*) buffer_string_prepare_append(b, len*2);
|
|
|
|
buffer_commit(b, len*2); /* fill below */
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
p[(i<<1)] = hex_chars_lc[(s[i] >> 4) & 0x0F];
|
|
|
|
p[(i<<1)+1] = hex_chars_lc[(s[i]) & 0x0F];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_encoded_hex_uc(buffer * const restrict b, const char * const restrict s, size_t len) {
|
2018-04-14 21:10:24 +00:00
|
|
|
unsigned char * const p =
|
|
|
|
(unsigned char*) buffer_string_prepare_append(b, len*2);
|
|
|
|
buffer_commit(b, len*2); /* fill below */
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
p[(i<<1)] = hex_chars_uc[(s[i] >> 4) & 0x0F];
|
|
|
|
p[(i<<1)+1] = hex_chars_uc[(s[i]) & 0x0F];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-30 08:02:21 +00:00
|
|
|
/* everything except: ! ( ) * - . 0-9 A-Z _ a-z */
|
2012-08-31 14:11:39 +00:00
|
|
|
static const char encoded_chars_rel_uri_part[] = {
|
2005-11-07 13:15:51 +00:00
|
|
|
/*
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
*/
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
|
2006-10-04 13:26:23 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
|
2005-11-07 13:15:51 +00:00
|
|
|
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, /* 20 - 2F space " # $ % & ' + , / */
|
2008-08-01 17:54:04 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
|
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
|
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
|
2016-07-30 08:02:21 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
|
2005-11-07 13:15:51 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
|
|
|
|
};
|
|
|
|
|
2016-07-30 08:02:21 +00:00
|
|
|
/* everything except: ! ( ) * - . / 0-9 A-Z _ a-z */
|
2012-08-31 14:11:39 +00:00
|
|
|
static const char encoded_chars_rel_uri[] = {
|
2005-11-07 13:15:51 +00:00
|
|
|
/*
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
*/
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
|
2006-10-04 13:26:23 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
|
2008-08-01 17:54:04 +00:00
|
|
|
1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, /* 20 - 2F space " # $ % & ' + , */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 30 - 3F : ; < = > ? */
|
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F @ */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, /* 50 - 5F [ \ ] ^ */
|
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
|
2016-07-30 08:02:21 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* 70 - 7F { | } DEL */
|
2005-11-07 13:15:51 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
|
|
|
|
};
|
|
|
|
|
2012-08-31 14:11:39 +00:00
|
|
|
static const char encoded_chars_html[] = {
|
2005-11-07 13:15:51 +00:00
|
|
|
/*
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
*/
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
|
2006-10-04 13:26:23 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
|
2016-07-30 08:11:21 +00:00
|
|
|
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
|
2005-11-07 13:15:51 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
|
2016-07-30 08:11:21 +00:00
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
|
2005-11-07 13:15:51 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80 - 8F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90 - 9F */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* A0 - AF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* B0 - BF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* C0 - CF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* D0 - DF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* E0 - EF */
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* F0 - FF */
|
|
|
|
};
|
|
|
|
|
2012-08-31 14:11:39 +00:00
|
|
|
static const char encoded_chars_minimal_xml[] = {
|
2006-01-12 22:01:26 +00:00
|
|
|
/*
|
|
|
|
0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
*/
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00 - 0F control chars */
|
2006-10-04 13:26:23 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 - 1F */
|
2016-07-30 08:11:21 +00:00
|
|
|
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, /* 20 - 2F " & ' */
|
2006-01-12 22:01:26 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, /* 30 - 3F < > */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 - 4F */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50 - 5F */
|
2016-07-30 08:11:21 +00:00
|
|
|
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F ` */
|
2006-01-12 22:01:26 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70 - 7F DEL */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |