2005-02-20 14:27:00 +00:00
|
|
|
#ifndef _BUFFER_H_
|
|
|
|
#define _BUFFER_H_
|
2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-03-25 07:45:05 +00:00
|
|
|
struct tm; /* declaration */
|
2015-02-08 12:37:10 +00:00
|
|
|
|
|
|
|
/* generic string + binary data container; contains a terminating 0 in both
|
|
|
|
* cases
|
|
|
|
*
|
|
|
|
* used == 0 indicates a special "empty" state (unset config values); ptr
|
|
|
|
* might be NULL too then. otherwise an empty string has used == 1 (and ptr[0]
|
|
|
|
* == 0);
|
|
|
|
*
|
|
|
|
* copy/append functions will ensure used >= 1 (i.e. never leave it in the
|
|
|
|
* special empty state); only buffer_copy_buffer will copy the special empty
|
|
|
|
* state.
|
|
|
|
*/
|
2005-02-20 14:27:00 +00:00
|
|
|
typedef struct {
|
|
|
|
char *ptr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* "used" includes a terminating 0 */
|
2019-10-04 07:08:17 +00:00
|
|
|
uint32_t used;
|
2015-02-08 12:37:10 +00:00
|
|
|
/* size of allocated buffer at *ptr */
|
2019-10-04 07:08:17 +00:00
|
|
|
uint32_t size;
|
2005-02-20 14:27:00 +00:00
|
|
|
} buffer;
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* create new buffer; either empty or copy given data */
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer* buffer_init(void);
|
2019-10-09 03:31:34 +00:00
|
|
|
|
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer* buffer_init_buffer(const buffer *src); /* src can be NULL */
|
2019-10-09 03:31:34 +00:00
|
|
|
|
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer* buffer_init_string(const char *str); /* str can be NULL */
|
|
|
|
|
|
|
|
void buffer_free(buffer *b); /* b can be NULL */
|
|
|
|
/* truncates to used == 0; frees large buffers, might keep smaller ones for reuse */
|
|
|
|
void buffer_reset(buffer *b); /* b can be NULL */
|
|
|
|
|
|
|
|
/* reset b. if NULL != b && NULL != src, move src content to b. reset src. */
|
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
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
/* make sure buffer is large enough to store a string of given size
|
|
|
|
* and a terminating zero.
|
|
|
|
* sets b to an empty string, and may drop old content.
|
2015-02-08 12:37:10 +00:00
|
|
|
* @return b->ptr
|
|
|
|
*/
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 19:10:39 +00:00
|
|
|
char* buffer_string_prepare_copy(buffer *b, size_t size);
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
/* allocate buffer large enough to be able to append a string of given size
|
|
|
|
* if b was empty (used == 0) it will contain an empty string (used == 1)
|
|
|
|
* afterwards
|
|
|
|
* "used" data is preserved; if not empty buffer must contain a
|
|
|
|
* zero terminated string.
|
2015-02-08 12:37:10 +00:00
|
|
|
*/
|
2019-10-09 03:31:34 +00:00
|
|
|
__attribute_returns_nonnull__
|
2015-02-08 19:10:36 +00:00
|
|
|
char* buffer_string_prepare_append(buffer *b, size_t size);
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* use after prepare_(copy,append) when you have written data to the buffer
|
|
|
|
* to increase the buffer length by size. also sets the terminating zero.
|
|
|
|
* requires enough space is present for the terminating zero (prepare with the
|
|
|
|
* same size to be sure).
|
|
|
|
*/
|
|
|
|
void buffer_commit(buffer *b, size_t size);
|
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
/* sets string length:
|
|
|
|
* - always stores a terminating zero to terminate the "new" string
|
|
|
|
* - does not modify the string data apart from terminating zero
|
|
|
|
* - reallocates the buffer iff needed
|
|
|
|
*/
|
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
|
|
|
|
2018-11-23 05:37:38 +00:00
|
|
|
/* clear buffer
|
|
|
|
* - invalidate buffer contents
|
|
|
|
* - unsets used chars but does not modify existing ptr contents
|
|
|
|
* (b->ptr *is not* set to an empty, '\0'-terminated string "")
|
|
|
|
*/
|
|
|
|
static inline void buffer_clear(buffer *b);
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_copy_string(buffer * restrict b, const char * restrict s);
|
|
|
|
void buffer_copy_string_len(buffer * restrict b, const char * restrict s, size_t s_len);
|
|
|
|
static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src);
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string(buffer * restrict b, const char * restrict s);
|
|
|
|
void buffer_append_string_len(buffer * restrict b, const char * restrict s, size_t s_len);
|
|
|
|
static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src);
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2018-04-15 00:09:49 +00:00
|
|
|
#define buffer_append_uint_hex(b,len) buffer_append_uint_hex_lc((b),(len))
|
|
|
|
void buffer_append_uint_hex_lc(buffer *b, uintmax_t len);
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_append_int(buffer *b, intmax_t val);
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_strftime(buffer * restrict b, const char * restrict format, const struct tm * restrict tm);
|
2015-02-08 19:10:44 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */
|
|
|
|
#define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100)
|
|
|
|
|
2019-11-28 15:27:01 +00:00
|
|
|
size_t li_itostrn(char *buf, size_t buf_len, intmax_t val);
|
|
|
|
size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
/* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */
|
2018-04-15 00:01:54 +00:00
|
|
|
#define li_tohex(buf,buf_len,s,s_len) li_tohex_lc((buf),(buf_len),(s),(s_len))
|
2020-01-18 03:18:30 +00:00
|
|
|
void li_tohex_lc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
|
|
|
|
void li_tohex_uc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
|
2015-02-08 19:10:44 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* NULL buffer or empty buffer (used == 0);
|
|
|
|
* unset "string" (buffer) config options are initialized to used == 0,
|
|
|
|
* while setting an empty string leads to used == 1
|
|
|
|
*/
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
2017-04-24 06:37:10 +00:00
|
|
|
static inline int buffer_is_empty(const buffer *b);
|
2015-02-08 12:37:10 +00:00
|
|
|
/* NULL buffer, empty buffer (used == 0) or empty string (used == 1) */
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
2017-04-24 06:37:10 +00:00
|
|
|
static inline int buffer_string_is_empty(const buffer *b);
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2019-06-05 01:18:28 +00:00
|
|
|
__attribute_pure__
|
|
|
|
int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len);
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen);
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen);
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen);
|
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal(const buffer *a, const buffer *b);
|
2019-06-05 03:18:23 +00:00
|
|
|
|
|
|
|
__attribute_pure__
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_right_len(const buffer *a, const buffer *b, size_t len);
|
2019-06-05 03:18:23 +00:00
|
|
|
|
|
|
|
__attribute_pure__
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len);
|
2019-06-05 03:18:23 +00:00
|
|
|
|
|
|
|
__attribute_pure__
|
2015-02-08 19:10:49 +00:00
|
|
|
int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_substr_replace (buffer * restrict b, size_t offset, size_t len, const buffer * restrict replace);
|
2017-04-24 06:38:30 +00:00
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_encoded_hex_lc(buffer * restrict b, const char * restrict s, size_t len);
|
|
|
|
void buffer_append_string_encoded_hex_uc(buffer * restrict b, const char * restrict s, size_t len);
|
2018-04-14 21:10:24 +00:00
|
|
|
|
2005-11-07 13:15:51 +00:00
|
|
|
typedef enum {
|
|
|
|
ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */
|
|
|
|
ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */
|
2007-04-09 18:12:43 +00:00
|
|
|
ENCODING_HTML, /* & becomes & and so on */
|
2018-04-15 21:50:38 +00:00
|
|
|
ENCODING_MINIMAL_XML /* minimal encoding for xml */
|
2005-11-07 13:15:51 +00:00
|
|
|
} buffer_encoding_t;
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_encoded(buffer * restrict b, const char * restrict s, size_t s_len, buffer_encoding_t encoding);
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2015-05-28 15:47:14 +00:00
|
|
|
/* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len);
|
2015-05-28 15:47:14 +00:00
|
|
|
|
2015-02-08 19:10:39 +00:00
|
|
|
/* to upper case, replace non alpha-numerics with '_'; if is_http_header prefix with "HTTP_" unless s is "content-type" */
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_copy_string_encoded_cgi_varnames(buffer * restrict b, const char * restrict s, size_t s_len, int is_http_header);
|
2015-02-08 19:10:39 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_urldecode_path(buffer *url);
|
|
|
|
void buffer_urldecode_query(buffer *url);
|
2018-11-26 00:07:53 +00:00
|
|
|
int buffer_is_valid_UTF8(const buffer *b);
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_path_simplify(buffer *dest, buffer *src);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
void buffer_to_lower(buffer *b);
|
|
|
|
void buffer_to_upper(buffer *b);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-08-15 09:47:48 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/** deprecated */
|
|
|
|
char hex2int(unsigned char c);
|
|
|
|
char int2hex(char i);
|
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
2018-05-01 04:27:35 +00:00
|
|
|
static inline int light_isdigit(int c);
|
|
|
|
static inline int light_isdigit(int c) {
|
|
|
|
return (c >= '0' && c <= '9');
|
|
|
|
}
|
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
|
|
|
static inline int light_isxdigit(int c);
|
2018-05-01 04:27:35 +00:00
|
|
|
static inline int light_isxdigit(int c) {
|
|
|
|
return light_isdigit(c) || (c |= 32, c >= 'a' && c <= 'f');
|
|
|
|
}
|
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
|
|
|
static inline int light_isalpha(int c);
|
2018-05-01 04:27:35 +00:00
|
|
|
static inline int light_isalpha(int c) {
|
|
|
|
return (c |= 32, c >= 'a' && c <= 'z');
|
|
|
|
}
|
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
|
|
|
static inline int light_isalnum(int c);
|
2018-05-01 04:27:35 +00:00
|
|
|
static inline int light_isalnum(int c) {
|
|
|
|
return light_isdigit(c) || light_isalpha(c);
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-06-05 03:18:23 +00:00
|
|
|
__attribute_pure__
|
2019-10-04 07:08:17 +00:00
|
|
|
static inline uint32_t buffer_string_length(const buffer *b); /* buffer string length without terminating 0 */
|
2019-06-05 03:18:23 +00:00
|
|
|
|
|
|
|
__attribute_pure__
|
2019-10-04 07:08:17 +00:00
|
|
|
static inline uint32_t buffer_string_space(const buffer *b); /* maximum length of string that can be stored without reallocating */
|
2019-06-05 03:18:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static inline void buffer_append_slash(buffer *b); /* append '/' no non-empty strings not ending in '/' */
|
2020-01-18 03:18:30 +00:00
|
|
|
void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen); /* join strings with '/', if '/' not present */
|
2015-02-08 12:37:10 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#define BUFFER_APPEND_STRING_CONST(x, y) \
|
|
|
|
buffer_append_string_len(x, y, sizeof(y) - 1)
|
|
|
|
|
|
|
|
#define BUFFER_COPY_STRING_CONST(x, y) \
|
|
|
|
buffer_copy_string_len(x, y, sizeof(y) - 1)
|
|
|
|
|
2019-03-21 08:05:21 +00:00
|
|
|
#define BUFFER_INTLEN_PTR(x) (x)->used ? (int)((x)->used - 1) : 0, (x)->ptr
|
|
|
|
|
2019-10-20 22:57:18 +00:00
|
|
|
#define CONST_LEN_STR(x) (uint32_t)sizeof(x)-1, x
|
|
|
|
#define CONST_STR_LEN(x) x, (uint32_t)sizeof(x) - 1
|
2015-05-28 15:47:14 +00:00
|
|
|
#define CONST_BUF_LEN(x) ((x) ? (x)->ptr : NULL), buffer_string_length(x)
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
|
2018-09-23 21:42:28 +00:00
|
|
|
#define LI_NORETURN __attribute_noreturn__
|
2018-03-25 07:45:05 +00:00
|
|
|
|
2018-09-23 21:42:28 +00:00
|
|
|
__attribute_cold__
|
2014-02-16 13:08:20 +00:00
|
|
|
void log_failed_assert(const char *filename, unsigned int line, const char *msg) LI_NORETURN;
|
|
|
|
#define force_assert(x) do { if (!(x)) log_failed_assert(__FILE__, __LINE__, "assertion failed: " #x); } while(0)
|
|
|
|
#define SEGFAULT() log_failed_assert(__FILE__, __LINE__, "aborted");
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
/* inline implementations */
|
|
|
|
|
2017-04-24 06:37:10 +00:00
|
|
|
static inline int buffer_is_empty(const buffer *b) {
|
|
|
|
return NULL == b || 0 == b->used;
|
|
|
|
}
|
|
|
|
static inline int buffer_string_is_empty(const buffer *b) {
|
|
|
|
return NULL == b || b->used < 2;
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:08:17 +00:00
|
|
|
static inline uint32_t buffer_string_length(const buffer *b) {
|
2015-02-08 12:37:10 +00:00
|
|
|
return NULL != b && 0 != b->used ? b->used - 1 : 0;
|
|
|
|
}
|
|
|
|
|
2019-10-04 07:08:17 +00:00
|
|
|
static inline uint32_t buffer_string_space(const buffer *b) {
|
2018-11-23 05:47:08 +00:00
|
|
|
return NULL != b && b->size ? b->size - (b->used | (0 == b->used)) : 0;
|
2015-02-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src) {
|
2018-11-24 00:20:44 +00:00
|
|
|
buffer_copy_string_len(b, CONST_BUF_LEN(src));
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:18:30 +00:00
|
|
|
static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src) {
|
2018-11-09 02:49:33 +00:00
|
|
|
buffer_append_string_len(b, CONST_BUF_LEN(src));
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
static inline void buffer_append_slash(buffer *b) {
|
2019-10-04 07:08:17 +00:00
|
|
|
uint32_t len = buffer_string_length(b);
|
2015-02-08 12:37:10 +00:00
|
|
|
if (len > 0 && '/' != b->ptr[len-1]) BUFFER_APPEND_STRING_CONST(b, "/");
|
|
|
|
}
|
|
|
|
|
2018-11-23 05:37:38 +00:00
|
|
|
static inline void buffer_clear(buffer *b) {
|
|
|
|
b->used = 0;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|