1 #ifndef _BUFFER_H_ 2 #define _BUFFER_H_ 3 4 #ifdef HAVE_CONFIG_H 5 # include "config.h" 6 #endif 7 8 #include "settings.h" 9 10 #include <stdlib.h> 11 #include <sys/types.h> 12 #include <stdio.h> 13 14 typedef struct { 15 char *ptr; 16 17 size_t used; 18 size_t size; 19 } buffer; 20 21 typedef struct { 22 buffer **ptr; 23 24 size_t used; 25 size_t size; 26 } buffer_array; 27 28 typedef struct { 29 char *ptr; 30 31 size_t offset; /* input-pointer */ 32 33 size_t used; /* output-pointer */ 34 size_t size; 35 } read_buffer; 36 37 buffer_array* buffer_array_init(void); 38 void buffer_array_free(buffer_array *b); 39 void buffer_array_reset(buffer_array *b); 40 buffer *buffer_array_append_get_buffer(buffer_array *b); 41 42 buffer* buffer_init(void); 43 buffer* buffer_init_buffer(buffer *b); 44 buffer* buffer_init_string(const char *str); 45 void buffer_free(buffer *b); 46 void buffer_reset(buffer *b); 47 48 int buffer_prepare_copy(buffer *b, size_t size); 49 int buffer_prepare_append(buffer *b, size_t size); 50 51 int buffer_copy_string(buffer *b, const char *s); 52 int buffer_copy_string_len(buffer *b, const char *s, size_t s_len); 53 int buffer_copy_string_buffer(buffer *b, const buffer *src); 54 int buffer_copy_string_hex(buffer *b, const char *in, size_t in_len); 55 56 int buffer_copy_long(buffer *b, long val); 57 58 int buffer_copy_memory(buffer *b, const char *s, size_t s_len); 59 60 int buffer_append_string(buffer *b, const char *s); 61 int buffer_append_string_len(buffer *b, const char *s, size_t s_len); 62 int buffer_append_string_buffer(buffer *b, const buffer *src); 63 int buffer_append_string_lfill(buffer *b, const char *s, size_t maxlen); 64 int buffer_append_string_rfill(buffer *b, const char *s, size_t maxlen); 65 66 int buffer_append_long_hex(buffer *b, unsigned long len); 67 int buffer_append_long(buffer *b, long val); 68 69 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == SIZEOF_OFF_T) 70 #define buffer_copy_off_t(x, y) buffer_copy_long(x, y) 71 #define buffer_append_off_t(x, y) buffer_append_long(x, y) 72 #else 73 int buffer_copy_off_t(buffer *b, off_t val); 74 int buffer_append_off_t(buffer *b, off_t val); 75 #endif 76 77 int buffer_append_memory(buffer *b, const char *s, size_t s_len); 78 79 char * buffer_search_string_len(buffer *b, const char *needle, size_t len); 80 81 int buffer_is_empty(buffer *b); 82 int buffer_is_equal(buffer *a, buffer *b); 83 int buffer_is_equal_right_len(buffer *a, buffer *b, size_t len); 84 int buffer_is_equal_string(buffer *a, const char *s, size_t b_len); 85 int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len); 86 87 typedef enum { 88 ENCODING_UNSET, 89 ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */ 90 ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */ 91 ENCODING_HTML, /* & becomes & and so on */ 92 ENCODING_MINIMAL_XML, /* minimal encoding for xml */ 93 ENCODING_HEX, /* encode string as hex */ 94 ENCODING_HTTP_HEADER /* encode \n with \t\n */ 95 } buffer_encoding_t; 96 97 int buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding); 98 99 int buffer_urldecode_path(buffer *url); 100 int buffer_urldecode_query(buffer *url); 101 int buffer_path_simplify(buffer *dest, buffer *src); 102 103 int buffer_to_lower(buffer *b); 104 int buffer_to_upper(buffer *b); 105 106 /** deprecated */ 107 int LI_ltostr(char *buf, long val); 108 char hex2int(unsigned char c); 109 char int2hex(char i); 110 111 int light_isdigit(int c); 112 int light_isxdigit(int c); 113 int light_isalpha(int c); 114 int light_isalnum(int c); 115 116 #define BUFFER_APPEND_STRING_CONST(x, y) \ 117 buffer_append_string_len(x, y, sizeof(y) - 1) 118 119 #define BUFFER_COPY_STRING_CONST(x, y) \ 120 buffer_copy_string_len(x, y, sizeof(y) - 1) 121 122 #define BUFFER_APPEND_SLASH(x) \ 123 if (x->used > 1 && x->ptr[x->used - 2] != '/') { BUFFER_APPEND_STRING_CONST(x, "/"); } 124 125 #define CONST_STR_LEN(x) x, x ? sizeof(x) - 1 : 0 126 #define CONST_BUF_LEN(x) x->ptr, x->used ? x->used - 1 : 0 127 128 129 #define SEGFAULT() do { fprintf(stderr, "%s.%d: aborted\n", __FILE__, __LINE__); abort(); } while(0) 130 #ifndef UNUSED 131 #define UNUSED(x) ( (void)(x) ) 132 #endif 133 134 #endif 135