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 */
diff --git a/include/proto/action.h b/include/proto/action.h
index dcfdeac..19312db 100644
--- a/include/proto/action.h
+++ b/include/proto/action.h
@@ -44,7 +44,8 @@
 	return NULL;
 }
 
-static inline void action_build_list(struct list *keywords, struct chunk *chk)
+static inline void action_build_list(struct list *keywords,
+				     struct buffer *chk)
 {
 	struct action_kw_list *kw_list;
 	int i;
diff --git a/include/proto/channel.h b/include/proto/channel.h
index 10e3602..5a1a36e 100644
--- a/include/proto/channel.h
+++ b/include/proto/channel.h
@@ -784,7 +784,7 @@
  * Channel flag READ_PARTIAL is updated if some data can be transferred. The
  * chunk's length is updated with the number of bytes sent.
  */
-static inline int ci_putchk(struct channel *chn, struct chunk *chunk)
+static inline int ci_putchk(struct channel *chn, struct buffer *chunk)
 {
 	int ret;
 
diff --git a/include/proto/filters.h b/include/proto/filters.h
index 1be1637..d16f706 100644
--- a/include/proto/filters.h
+++ b/include/proto/filters.h
@@ -111,7 +111,7 @@
 int  flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len);
 
 void flt_http_reset(struct stream *s, struct http_msg *msg);
-void flt_http_reply(struct stream *s, short status, const struct chunk *msg);
+void flt_http_reply(struct stream *s, short status, const struct buffer *msg);
 
 int  flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
 int  flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
diff --git a/include/proto/proto_http.h b/include/proto/proto_http.h
index d3b1f43..49cb501 100644
--- a/include/proto/proto_http.h
+++ b/include/proto/proto_http.h
@@ -45,7 +45,7 @@
  */
 
 extern const int http_err_codes[HTTP_ERR_SIZE];
-extern struct chunk http_err_chunks[HTTP_ERR_SIZE];
+extern struct buffer http_err_chunks[HTTP_ERR_SIZE];
 extern const char *HTTP_302;
 extern const char *HTTP_303;
 
@@ -119,8 +119,8 @@
 struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy);
 void free_http_req_rules(struct list *r);
 void free_http_res_rules(struct list *r);
-void http_reply_and_close(struct stream *s, short status, struct chunk *msg);
-struct chunk *http_error_message(struct stream *s);
+void http_reply_and_close(struct stream *s, short status, struct buffer *msg);
+struct buffer *http_error_message(struct stream *s);
 struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
                                                const char **args, char **errmsg, int use_fmt, int dir);
 int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private);
diff --git a/include/proto/server.h b/include/proto/server.h
index 14f4926..3378c2b 100644
--- a/include/proto/server.h
+++ b/include/proto/server.h
@@ -150,7 +150,8 @@
  */
 void srv_shutdown_backup_streams(struct proxy *px, int why);
 
-void srv_append_status(struct chunk *msg, struct server *s, struct check *, int xferred, int forced);
+void srv_append_status(struct buffer *msg, struct server *s, struct check *,
+		       int xferred, int forced);
 
 void srv_set_stopped(struct server *s, const char *reason, struct check *check);
 void srv_set_running(struct server *s, const char *reason, struct check *check);
diff --git a/include/proto/spoe.h b/include/proto/spoe.h
index 299836b..b6c4ee2 100644
--- a/include/proto/spoe.h
+++ b/include/proto/spoe.h
@@ -164,7 +164,7 @@
 
 		case SMP_T_STR:
 		case SMP_T_BIN: {
-			struct chunk *chk = &smp->data.u.str;
+			struct buffer *chk = &smp->data.u.str;
 
 			/* Here, we need to know if the sample has already been
 			 * partially encoded. If yes, we only need to encode the
@@ -175,7 +175,7 @@
 				 * type (string or binary), the buffer length
 				 * (as a varint) and at least 1 byte of the
 				 * buffer. */
-				struct chunk *chk = &smp->data.u.str;
+				struct buffer *chk = &smp->data.u.str;
 
 				*p++ = (smp->data.type == SMP_T_STR)
 					? SPOE_DATA_T_STR
diff --git a/include/proto/ssl_sock.h b/include/proto/ssl_sock.h
index c6075eb..f04317a 100644
--- a/include/proto/ssl_sock.h
+++ b/include/proto/ssl_sock.h
@@ -57,15 +57,17 @@
 void ssl_sock_set_servername(struct connection *conn, const char *hostname);
 int ssl_sock_get_cert_used_sess(struct connection *conn);
 int ssl_sock_get_cert_used_conn(struct connection *conn);
-int ssl_sock_get_remote_common_name(struct connection *conn, struct chunk *out);
-int ssl_sock_get_pkey_algo(struct connection *conn, struct chunk *out);
+int ssl_sock_get_remote_common_name(struct connection *conn,
+				    struct buffer *out);
+int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out);
 unsigned int ssl_sock_get_verify_result(struct connection *conn);
 #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
-int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err);
+int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err);
 #endif
 #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
-void ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, struct chunk *tlskey);
-int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err);
+void ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
+				struct buffer *tlskey);
+int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err);
 struct tls_keys_ref *tlskeys_ref_lookup(const char *filename);
 struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id);
 #endif
diff --git a/include/proto/stats.h b/include/proto/stats.h
index ac893b8..02a3506 100644
--- a/include/proto/stats.h
+++ b/include/proto/stats.h
@@ -101,9 +101,10 @@
 extern struct applet http_stats_applet;
 
 void stats_io_handler(struct stream_interface *si);
-int stats_emit_raw_data_field(struct chunk *out, const struct field *f);
-int stats_emit_typed_data_field(struct chunk *out, const struct field *f);
-int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim);
+int stats_emit_raw_data_field(struct buffer *out, const struct field *f);
+int stats_emit_typed_data_field(struct buffer *out, const struct field *f);
+int stats_emit_field_tags(struct buffer *out, const struct field *f,
+			  char delim);
 
 #endif /* _PROTO_STATS_H */
 
diff --git a/include/proto/stream_interface.h b/include/proto/stream_interface.h
index 03756ba..7fcd4c8 100644
--- a/include/proto/stream_interface.h
+++ b/include/proto/stream_interface.h
@@ -36,7 +36,8 @@
 /* main event functions used to move data between sockets and buffers */
 int stream_int_check_timeouts(struct stream_interface *si);
 void stream_int_report_error(struct stream_interface *si);
-void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg);
+void stream_int_retnclose(struct stream_interface *si,
+			  const struct buffer *msg);
 int conn_si_send_proxy(struct connection *conn, unsigned int flag);
 void stream_sock_read0(struct stream_interface *si);
 
diff --git a/include/types/applet.h b/include/types/applet.h
index 8b7c28f..d4172bd 100644
--- a/include/types/applet.h
+++ b/include/types/applet.h
@@ -57,7 +57,7 @@
 	unsigned short state;      /* Internal appctx state */
 	unsigned int st0;          /* CLI state for stats, session state for peers */
 	unsigned int st1;          /* prompt/payload (bitwise OR of APPCTX_CLI_ST1_*) for stats, session error for peers */
-	struct chunk *chunk;       /* used to store unfinished commands */
+	struct buffer *chunk;       /* used to store unfinished commands */
 	unsigned int st2;          /* output state for stats, unused by peers  */
 	struct applet *applet;     /* applet this context refers to */
 	void *owner;               /* pointer to upper layer's entity (eg: stream interface) */
@@ -150,7 +150,7 @@
 			struct pat_ref *ref;
 			struct bref bref;	/* back-reference from the pat_ref_elt being dumped */
 			struct pattern_expr *expr;
-			struct chunk chunk;
+			struct buffer chunk;
 		} map;
 		struct {
 			struct hlua *hlua;
diff --git a/include/types/arg.h b/include/types/arg.h
index c3b09de..96974ba 100644
--- a/include/types/arg.h
+++ b/include/types/arg.h
@@ -92,7 +92,7 @@
 
 union arg_data {
 	long long int sint;
-	struct chunk str;
+	struct buffer str;
 	struct in_addr ipv4;
 	struct in6_addr ipv6;
 	struct proxy *prx; /* used for fe, be, tables */
diff --git a/include/types/connection.h b/include/types/connection.h
index 624e01e..ad406ac 100644
--- a/include/types/connection.h
+++ b/include/types/connection.h
@@ -311,7 +311,7 @@
 
 	struct conn_stream *(*attach)(struct connection *); /* Create and attach a conn_stream to an outgoing connection */
 	void (*detach)(struct conn_stream *); /* Detach a conn_stream from an outgoing connection, when the request is done */
-	void (*show_fd)(struct chunk *, struct connection *); /* append some data about connection into chunk for "show fd" */
+	void (*show_fd)(struct buffer *, struct connection *); /* append some data about connection into chunk for "show fd" */
 	unsigned int flags;                           /* some flags characterizing the mux's capabilities (MX_FL_*) */
 	char name[8];                                 /* mux layer name, zero-terminated */
 };
diff --git a/include/types/filters.h b/include/types/filters.h
index 488a97f..714cee9 100644
--- a/include/types/filters.h
+++ b/include/types/filters.h
@@ -189,7 +189,7 @@
 
 	void (*http_reset)         (struct stream *s, struct filter *f, struct http_msg *msg);
 	void (*http_reply)         (struct stream *s, struct filter *f, short status,
-				    const struct chunk *msg);
+				    const struct buffer *msg);
 
 	/*
 	 * TCP callbacks
diff --git a/include/types/global.h b/include/types/global.h
index 2ab124b..a684ea6 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -127,7 +127,7 @@
 	char *chroot;
 	char *pidfile;
 	char *node, *desc;		/* node name & description */
-	struct chunk log_tag;           /* name for syslog */
+	struct buffer log_tag;           /* name for syslog */
 	struct list logsrvs;
 	char *log_send_hostname;   /* set hostname in syslog header */
 	char *server_state_base;   /* path to a directory where server state files can be found */
@@ -207,7 +207,7 @@
 extern int  actconn;            /* # of active sessions */
 extern int  listeners;
 extern int  jobs;               /* # of active jobs (listeners, sessions, open devices) */
-extern THREAD_LOCAL struct chunk trash;
+extern THREAD_LOCAL struct buffer trash;
 extern int nb_oldpids;          /* contains the number of old pids found */
 extern const int zero;
 extern const int one;
diff --git a/include/types/proto_http.h b/include/types/proto_http.h
index 5a6cc93..9c39056 100644
--- a/include/types/proto_http.h
+++ b/include/types/proto_http.h
@@ -301,7 +301,7 @@
 struct http_auth_data {
 	enum ht_auth_m method;                /* one of HTTP_AUTH_* */
 	/* 7 bytes unused here */
-	struct chunk method_data;             /* points to the creditial part from 'Authorization:' header */
+	struct buffer method_data;             /* points to the creditial part from 'Authorization:' header */
 	char *user, *pass;                    /* extracted username & password */
 };
 
diff --git a/include/types/proxy.h b/include/types/proxy.h
index 16c13a1..ec95286 100644
--- a/include/types/proxy.h
+++ b/include/types/proxy.h
@@ -356,7 +356,7 @@
 	struct list logsrvs;
 	struct list logformat; 			/* log_format linked list */
 	struct list logformat_sd;		/* log_format linked list for the RFC5424 structured-data part */
-	struct chunk log_tag;                   /* override default syslog tag */
+	struct buffer log_tag;                   /* override default syslog tag */
 	char *header_unique_id; 		/* unique-id header */
 	struct list format_unique_id;		/* unique-id format */
 	int to_log;				/* things to be logged (LW_*) */
@@ -384,7 +384,7 @@
 	char *check_path;			/* PATH environment to use for external agent checks */
 	char *expect_str;			/* http-check expected content : string or text version of the regex */
 	struct my_regex *expect_regex;		/* http-check expected content */
-	struct chunk errmsg[HTTP_ERR_SIZE];	/* default or customized error messages for known errors */
+	struct buffer errmsg[HTTP_ERR_SIZE];	/* default or customized error messages for known errors */
 	int uuid;				/* universally unique proxy ID, used for SNMP */
 	unsigned int backlog;			/* force the frontend's listen backlog */
 	unsigned long bind_proc;		/* bitmask of processes using this proxy */
diff --git a/include/types/sample.h b/include/types/sample.h
index cf0528e..987a099 100644
--- a/include/types/sample.h
+++ b/include/types/sample.h
@@ -244,14 +244,14 @@
  */
 struct meth {
 	enum http_meth_t meth;
-	struct chunk str;
+	struct buffer str;
 };
 
 union sample_value {
 	long long int   sint;  /* used for signed 64bits integers */
 	struct in_addr  ipv4;  /* used for ipv4 addresses */
 	struct in6_addr ipv6;  /* used for ipv6 addresses */
-	struct chunk    str;   /* used for char strings or buffers */
+	struct buffer    str;   /* used for char strings or buffers */
 	struct meth     meth;  /* used for http method */
 };