MAJOR: chunks: replace struct chunk with struct buffer
Now all the code used to manipulate chunks uses a struct buffer instead.
The functions are still called "chunk*", and some of them will progressively
move to the generic buffer handling code as they are cleaned up.
diff --git a/include/common/chunk.h b/include/common/chunk.h
index b9c1d20..a127f9a 100644
--- a/include/common/chunk.h
+++ b/include/common/chunk.h
@@ -25,75 +25,73 @@
#include <stdlib.h>
#include <string.h>
+#include <common/buf.h>
#include <common/config.h>
#include <common/memory.h>
-/* describes a chunk of string */
-struct chunk {
- char *area; /* points to <size> bytes */
- size_t size; /* buffer size in bytes */
- size_t data; /* amount of data after head including wrapping */
-};
-
struct pool_head *pool_head_trash;
/* function prototypes */
-int chunk_printf(struct chunk *chk, const char *fmt, ...)
+int chunk_printf(struct buffer *chk, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
-int chunk_appendf(struct chunk *chk, const char *fmt, ...)
+int chunk_appendf(struct buffer *chk, const char *fmt, ...)
__attribute__ ((format(printf, 2, 3)));
-int chunk_htmlencode(struct chunk *dst, struct chunk *src);
-int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc);
-int chunk_strcmp(const struct chunk *chk, const char *str);
-int chunk_strcasecmp(const struct chunk *chk, const char *str);
-struct chunk *get_trash_chunk(void);
-struct chunk *alloc_trash_chunk(void);
+int chunk_htmlencode(struct buffer *dst, struct buffer *src);
+int chunk_asciiencode(struct buffer *dst, struct buffer *src, char qc);
+int chunk_strcmp(const struct buffer *chk, const char *str);
+int chunk_strcasecmp(const struct buffer *chk, const char *str);
+struct buffer *get_trash_chunk(void);
+struct buffer *alloc_trash_chunk(void);
int init_trash_buffers(int first);
void deinit_trash_buffers(void);
/*
* free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
*/
-static inline void free_trash_chunk(struct chunk *chunk)
+static inline void free_trash_chunk(struct buffer *chunk)
{
pool_free(pool_head_trash, chunk);
}
-static inline void chunk_reset(struct chunk *chk)
+static inline void chunk_reset(struct buffer *chk)
{
chk->data = 0;
}
-static inline void chunk_init(struct chunk *chk, char *str, size_t size)
+static inline void chunk_init(struct buffer *chk, char *str, size_t size)
{
- chk->area = str;
- chk->data = 0;
+ chk->area = str;
+ chk->head = 0;
+ chk->data = 0;
chk->size = size;
}
/* report 0 in case of error, 1 if OK. */
-static inline int chunk_initlen(struct chunk *chk, char *str, size_t size, int len)
+static inline int chunk_initlen(struct buffer *chk, char *str, size_t size,
+ int len)
{
if (len < 0 || (size && len > size))
return 0;
- chk->area = str;
- chk->data = len;
+ chk->area = str;
+ chk->head = 0;
+ chk->data = len;
chk->size = size;
return 1;
}
/* this is only for temporary manipulation, the chunk is read-only */
-static inline void chunk_initstr(struct chunk *chk, const char *str)
+static inline void chunk_initstr(struct buffer *chk, const char *str)
{
chk->area = (char *)str;
+ chk->head = 0;
chk->data = strlen(str);
chk->size = 0; /* mark it read-only */
}
@@ -101,7 +99,8 @@
/* copies memory area <src> into <chk> for <len> bytes. Returns 0 in
* case of failure. No trailing zero is added.
*/
-static inline int chunk_memcpy(struct chunk *chk, const char *src, size_t len)
+static inline int chunk_memcpy(struct buffer *chk, const char *src,
+ size_t len)
{
if (unlikely(len >= chk->size))
return 0;
@@ -115,7 +114,8 @@
/* appends memory area <src> after <chk> for <len> bytes. Returns 0 in
* case of failure. No trailing zero is added.
*/
-static inline int chunk_memcat(struct chunk *chk, const char *src, size_t len)
+static inline int chunk_memcat(struct buffer *chk, const char *src,
+ size_t len)
{
if (unlikely(chk->data < 0 || chk->data + len >= chk->size))
return 0;
@@ -128,7 +128,7 @@
/* copies str into <chk> followed by a trailing zero. Returns 0 in
* case of failure.
*/
-static inline int chunk_strcpy(struct chunk *chk, const char *str)
+static inline int chunk_strcpy(struct buffer *chk, const char *str)
{
size_t len;
@@ -146,7 +146,7 @@
/* appends str after <chk> followed by a trailing zero. Returns 0 in
* case of failure.
*/
-static inline int chunk_strcat(struct chunk *chk, const char *str)
+static inline int chunk_strcat(struct buffer *chk, const char *str)
{
size_t len;
@@ -163,7 +163,7 @@
/* appends <nb> characters from str after <chk>.
* Returns 0 in case of failure.
*/
-static inline int chunk_strncat(struct chunk *chk, const char *str, int nb)
+static inline int chunk_strncat(struct buffer *chk, const char *str, int nb)
{
if (unlikely(chk->data < 0 || chk->data + nb >= chk->size))
return 0;
@@ -185,7 +185,7 @@
* chunk_appendf(&trash, "%s", gethosname());
* printf("hostname=<%s>, pid=<%d>\n", name, pid);
*/
-static inline char *chunk_newstr(struct chunk *chk)
+static inline char *chunk_newstr(struct buffer *chk)
{
if (chk->data < 0 || chk->data + 1 >= chk->size)
return NULL;
@@ -194,14 +194,14 @@
return chk->area + chk->data;
}
-static inline void chunk_drop(struct chunk *chk)
+static inline void chunk_drop(struct buffer *chk)
{
chk->area = NULL;
chk->data = -1;
chk->size = 0;
}
-static inline void chunk_destroy(struct chunk *chk)
+static inline void chunk_destroy(struct buffer *chk)
{
if (!chk->size)
return;
@@ -217,13 +217,14 @@
* the destination string is returned, or NULL if the allocation fails or if
* any pointer is NULL.
*/
-static inline char *chunk_dup(struct chunk *dst, const struct chunk *src)
+static inline char *chunk_dup(struct buffer *dst, const struct buffer *src)
{
if (!dst || !src || src->data < 0 || !src->area)
return NULL;
if (dst->size)
free(dst->area);
+ dst->head = src->head;
dst->data = src->data;
dst->size = src->data;
if (dst->size < src->size || !src->size)
@@ -231,6 +232,7 @@
dst->area = (char *)malloc(dst->size);
if (!dst->area) {
+ dst->head = 0;
dst->data = 0;
dst->size = 0;
return NULL;
diff --git a/include/common/hpack-dec.h b/include/common/hpack-dec.h
index b03398a..71039d3 100644
--- a/include/common/hpack-dec.h
+++ b/include/common/hpack-dec.h
@@ -34,6 +34,7 @@
#include <common/hpack-tbl.h>
int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len,
- struct http_hdr *list, int list_size, struct chunk *tmp);
+ struct http_hdr *list, int list_size,
+ struct buffer *tmp);
#endif /* _COMMON_HPACK_DEC_H */
diff --git a/include/common/hpack-enc.h b/include/common/hpack-enc.h
index 0a44dfc..5246b83 100644
--- a/include/common/hpack-enc.h
+++ b/include/common/hpack-enc.h
@@ -33,6 +33,7 @@
#include <common/config.h>
#include <common/ist.h>
-int hpack_encode_header(struct chunk *out, const struct ist n, const struct ist v);
+int hpack_encode_header(struct buffer *out, const struct ist n,
+ const struct ist v);
#endif /* _COMMON_HPACK_ENC_H */
diff --git a/include/common/standard.h b/include/common/standard.h
index 6542759..0a956e0 100644
--- a/include/common/standard.h
+++ b/include/common/standard.h
@@ -486,7 +486,7 @@
*/
char *encode_chunk(char *start, char *stop,
const char escape, const fd_set *map,
- const struct chunk *chunk);
+ const struct buffer *chunk);
/*
* Tries to prefix characters tagged in the <map> with the <escape>
@@ -509,7 +509,7 @@
*/
char *escape_chunk(char *start, char *stop,
const char escape, const fd_set *map,
- const struct chunk *chunk);
+ const struct buffer *chunk);
/* Check a string for using it in a CSV output format. If the string contains
@@ -539,10 +539,11 @@
* This function appends the encoding to the existing output chunk. Please
* use csv_enc() instead if you want to replace the output chunk.
*/
-const char *csv_enc_append(const char *str, int quote, struct chunk *output);
+const char *csv_enc_append(const char *str, int quote, struct buffer *output);
/* same as above but the output chunk is reset first */
-static inline const char *csv_enc(const char *str, int quote, struct chunk *output)
+static inline const char *csv_enc(const char *str, int quote,
+ struct buffer *output)
{
chunk_reset(output);
return csv_enc_append(str, quote, output);
@@ -1317,9 +1318,9 @@
struct list;
int list_append_word(struct list *li, const char *str, char **err);
-int dump_text(struct chunk *out, const char *buf, int bsize);
-int dump_binary(struct chunk *out, const char *buf, int bsize);
-int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
+int dump_text(struct buffer *out, const char *buf, int bsize);
+int dump_binary(struct buffer *out, const char *buf, int bsize);
+int dump_text_line(struct buffer *out, const char *buf, int bsize, int len,
int *line, int ptr);
/* same as realloc() except that ptr is also freed upon failure */