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>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
static const char hex_chars[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_reset(buffer *b) {
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == b) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* limit don't reuse buffer larger than ... bytes */
|
|
|
|
if (b->size > BUFFER_MAX_REUSE_SIZE) {
|
|
|
|
free(b->ptr);
|
|
|
|
b->ptr = NULL;
|
|
|
|
b->size = 0;
|
2015-02-08 12:37:10 +00:00
|
|
|
} else if (b->size > 0) {
|
2008-08-01 11:24:06 +00:00
|
|
|
b->ptr[0] = '\0';
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b->used = 0;
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_move(buffer *b, buffer *src) {
|
|
|
|
buffer tmp;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (NULL == b) {
|
|
|
|
buffer_reset(src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer_reset(b);
|
|
|
|
if (NULL == src) return;
|
|
|
|
|
|
|
|
tmp = *src; *src = *b; *b = tmp;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#define BUFFER_PIECE_SIZE 64
|
2015-02-08 12:37:10 +00:00
|
|
|
static size_t buffer_align_size(size_t size) {
|
|
|
|
size_t align = BUFFER_PIECE_SIZE - (size % BUFFER_PIECE_SIZE);
|
|
|
|
/* overflow on unsinged size_t is defined to wrap around */
|
|
|
|
if (size + align < size) return size;
|
|
|
|
return size + align;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
static char* buffer_prepare_copy(buffer *b, size_t size) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != b);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* also allocate space for terminating 0 */
|
|
|
|
/* check for overflow: unsigned overflow is defined to wrap around */
|
|
|
|
force_assert(1 + size > size);
|
|
|
|
++size;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (0 == b->size || size > b->size) {
|
|
|
|
if (NULL != b->ptr) free(b->ptr);
|
|
|
|
b->ptr = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
b->size = buffer_align_size(size);
|
|
|
|
force_assert(b->size > 0);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b->ptr = malloc(b->size);
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != b->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* reset */
|
|
|
|
b->used = 0;
|
|
|
|
b->ptr[0] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return b->ptr;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
char* buffer_string_prepare_copy(buffer *b, size_t size) {
|
|
|
|
force_assert(NULL != b);
|
|
|
|
|
|
|
|
buffer_prepare_copy(b, size);
|
|
|
|
b->used = 1;
|
|
|
|
|
|
|
|
return b->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer_string_prepare_append(buffer *b, size_t size) {
|
|
|
|
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:39 +00:00
|
|
|
/* not empty, b->used already includes a terminating 0 */
|
|
|
|
size_t req_size = b->used + size;
|
|
|
|
|
|
|
|
/* check for overflow: unsigned overflow is defined to wrap around */
|
|
|
|
force_assert(req_size >= b->used);
|
|
|
|
|
|
|
|
/* only append to 0-terminated string */
|
2015-02-08 19:10:36 +00:00
|
|
|
force_assert('\0' == b->ptr[b->used - 1]);
|
2015-02-08 19:10:39 +00:00
|
|
|
|
|
|
|
if (req_size > b->size) {
|
|
|
|
char *ptr;
|
|
|
|
b->size = buffer_align_size(req_size);
|
|
|
|
|
|
|
|
ptr = realloc(b->ptr, b->size);
|
|
|
|
force_assert(NULL != ptr);
|
|
|
|
b->ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return b->ptr + b->used - 1;
|
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 */
|
|
|
|
force_assert(b->used + size > b->used);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(b->used + size <= b->size);
|
|
|
|
b->used += size;
|
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
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_copy_string(buffer *b, const char *s) {
|
|
|
|
buffer_copy_string_len(b, s, NULL != s ? strlen(s) : 0);
|
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_copy_string_len(buffer *b, const char *s, size_t s_len) {
|
|
|
|
force_assert(NULL != b);
|
|
|
|
force_assert(NULL != s || s_len == 0);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
buffer_string_prepare_copy(b, s_len);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (0 != s_len) memcpy(b->ptr, s, s_len);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_commit(b, s_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_copy_buffer(buffer *b, const buffer *src) {
|
|
|
|
if (NULL == src || 0 == src->used) {
|
|
|
|
buffer_prepare_copy(b, 0);
|
|
|
|
} else {
|
|
|
|
buffer_copy_string_len(b, src->ptr, buffer_string_length(src));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_string(buffer *b, const char *s) {
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_string_len(buffer *b, const char *s, size_t s_len) {
|
|
|
|
char *target_buf;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != b);
|
|
|
|
force_assert(NULL != s || s_len == 0);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
target_buf = buffer_string_prepare_append(b, s_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
if (0 == s_len) return; /* nothing to append */
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
memcpy(target_buf, s, s_len);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_commit(b, s_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_string_buffer(buffer *b, const buffer *src) {
|
|
|
|
if (NULL == src) {
|
|
|
|
buffer_append_string_len(b, NULL, 0);
|
|
|
|
} else {
|
|
|
|
buffer_append_string_len(b, src->ptr, buffer_string_length(src));
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_long_hex(buffer *b, unsigned long value) {
|
2005-02-20 14:27:00 +00:00
|
|
|
char *buf;
|
|
|
|
int shift = 0;
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
{
|
|
|
|
unsigned long copy = value;
|
|
|
|
do {
|
|
|
|
copy >>= 8;
|
|
|
|
shift += 2; /* counting nibbles (4 bits) */
|
|
|
|
} while (0 != copy);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
buf = buffer_string_prepare_append(b, shift);
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_commit(b, shift); /* will fill below */
|
|
|
|
|
|
|
|
shift <<= 2; /* count bits now */
|
2005-02-20 14:27:00 +00:00
|
|
|
while (shift > 0) {
|
|
|
|
shift -= 4;
|
|
|
|
*(buf++) = hex_chars[(value >> shift) & 0x0F];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static char* itostr(char * const buf_end, intmax_t val) {
|
|
|
|
char *cur = buf_end;
|
|
|
|
if (val >= 0) return utostr(buf_end, (uintmax_t) val);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* can't take absolute value, as it isn't defined for INTMAX_MIN */
|
|
|
|
do {
|
|
|
|
int mod = val % 10;
|
|
|
|
val /= 10;
|
|
|
|
/* val * 10 + mod == orig val, -10 < mod < 10 */
|
|
|
|
/* we want a negative mod */
|
|
|
|
if (mod > 0) {
|
|
|
|
mod -= 10;
|
|
|
|
val += 1;
|
|
|
|
}
|
|
|
|
/* prepend digit abs(mod) */
|
|
|
|
*(--cur) = (char) ('0' + (-mod));
|
|
|
|
} while (0 != val);
|
|
|
|
*(--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
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_copy_int(buffer *b, intmax_t val) {
|
|
|
|
force_assert(NULL != b);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
|
|
|
b->used = 0;
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(b, val);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void li_itostrn(char *buf, size_t buf_len, intmax_t val) {
|
|
|
|
char p_buf[LI_ITOSTRING_LENGTH];
|
|
|
|
char* const p_buf_end = p_buf + sizeof(p_buf);
|
|
|
|
char* str = p_buf_end - 1;
|
|
|
|
*str = '\0';
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
str = itostr(str, val);
|
|
|
|
force_assert(p_buf_end > str && str >= p_buf);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(buf_len >= (size_t) (p_buf_end - str));
|
|
|
|
memcpy(buf, str, p_buf_end - str);
|
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void li_itostr(char *buf, intmax_t val) {
|
|
|
|
li_itostrn(buf, LI_ITOSTRING_LENGTH, val);
|
|
|
|
}
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void li_utostrn(char *buf, size_t buf_len, uintmax_t val) {
|
|
|
|
char p_buf[LI_ITOSTRING_LENGTH];
|
|
|
|
char* const p_buf_end = p_buf + sizeof(p_buf);
|
|
|
|
char* str = p_buf_end - 1;
|
|
|
|
*str = '\0';
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
str = utostr(str, val);
|
|
|
|
force_assert(p_buf_end > str && str >= p_buf);
|
2005-02-28 10:38:16 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(buf_len >= (size_t) (p_buf_end - str));
|
|
|
|
memcpy(buf, str, p_buf_end - str);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void li_utostr(char *buf, uintmax_t val) {
|
|
|
|
li_utostrn(buf, LI_ITOSTRING_LENGTH, val);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char int2hex(char c) {
|
|
|
|
return hex_chars[(c & 0x0F)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* converts hex char (0-9, A-Z, a-z) to decimal.
|
|
|
|
* returns 0xFF on invalid input.
|
|
|
|
*/
|
|
|
|
char hex2int(unsigned char hex) {
|
2015-02-08 12:37:10 +00:00
|
|
|
unsigned char value = hex - '0';
|
|
|
|
if (value > 9) {
|
|
|
|
hex |= 0x20; /* to lower case */
|
|
|
|
value = hex - 'a' + 10;
|
|
|
|
if (value < 10) value = 0xff;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2015-02-08 12:37:10 +00:00
|
|
|
if (value > 15) value = 0xff;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return value;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char * buffer_search_string_len(buffer *b, const char *needle, size_t len) {
|
|
|
|
size_t i;
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != b);
|
|
|
|
force_assert(0 != len && NULL != needle); /* empty needles not allowed */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (b->used < len) return NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for(i = 0; i < b->used - len; i++) {
|
|
|
|
if (0 == memcmp(b->ptr + i, needle, len)) {
|
|
|
|
return b->ptr + i;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
int buffer_is_empty(buffer *b) {
|
|
|
|
return NULL == b || 0 == b->used;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
int buffer_string_is_empty(buffer *b) {
|
|
|
|
return 0 == buffer_string_length(b);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if two buffer contain the same data
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-08-29 11:11:35 +00:00
|
|
|
* HISTORY: this function was pretty much optimized, but didn't handled
|
|
|
|
* alignment properly.
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int buffer_is_equal(buffer *a, buffer *b) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != a && NULL != b);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (a->used != b->used) return 0;
|
|
|
|
if (a->used == 0) return 1;
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return (0 == memcmp(a->ptr, b->ptr, a->used));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_is_equal_string(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
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (a->used != b_len + 1) return 0;
|
|
|
|
if (0 != memcmp(a->ptr, s, b_len)) return 0;
|
|
|
|
if ('\0' != a->ptr[a->used-1]) return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
return 1;
|
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")) */
|
|
|
|
int buffer_is_equal_caseless_string(buffer *a, const char *s, size_t b_len) {
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert(NULL != a);
|
2013-07-31 20:23:21 +00:00
|
|
|
if (a->used != b_len + 1) return 0;
|
2015-02-08 12:37:10 +00:00
|
|
|
force_assert('\0' == a->ptr[a->used - 1]);
|
2013-07-31 20:23:21 +00:00
|
|
|
|
|
|
|
return (0 == strcasecmp(a->ptr, s));
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:09 +00:00
|
|
|
int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len) {
|
|
|
|
size_t const len = (a_len < b_len) ? a_len : b_len;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:09 +00:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
unsigned char ca = a[i], cb = b[i];
|
|
|
|
if (ca == cb) continue;
|
|
|
|
|
|
|
|
/* always lowercase for transitive results */
|
|
|
|
if (ca >= 'A' && ca <= 'Z') ca |= 32;
|
|
|
|
if (cb >= 'A' && cb <= 'Z') cb |= 32;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:09 +00:00
|
|
|
if (ca == cb) continue;
|
|
|
|
return ca - cb;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2007-08-17 21:47:24 +00:00
|
|
|
if (a_len == b_len) return 0;
|
2015-02-08 12:37:10 +00:00
|
|
|
return a_len < b_len ? -1 : 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int buffer_is_equal_right_len(buffer *b1, 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
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_copy_string_hex(buffer *b, const char *in, size_t in_len) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* overflow protection */
|
|
|
|
force_assert(in_len * 2 + 1 > in_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_prepare_copy(b, in_len * 2);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
b->used = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < in_len; i++) {
|
|
|
|
b->ptr[b->used++] = hex_chars[(in[i] >> 4) & 0x0F];
|
|
|
|
b->ptr[b->used++] = hex_chars[in[i] & 0x0F];
|
|
|
|
}
|
|
|
|
b->ptr[b->used++] = '\0';
|
|
|
|
}
|
|
|
|
|
2008-08-01 17:54:04 +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 ` */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 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 */
|
|
|
|
};
|
|
|
|
|
2008-08-01 17:54:04 +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 ` */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 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, |