REORG: buffers: split buffers into chunk,buffer,channel

Many parts of the channel definition still make use of the "buffer" word.
diff --git a/src/acl.c b/src/acl.c
index 1680ac9..d8e35b2 100644
--- a/src/acl.c
+++ b/src/acl.c
@@ -24,7 +24,7 @@
 #include <proto/acl.h>
 #include <proto/arg.h>
 #include <proto/auth.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/log.h>
 #include <proto/proxy.h>
 #include <proto/stick_table.h>
diff --git a/src/backend.c b/src/backend.c
index b777cc3..998e02c 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -19,6 +19,7 @@
 #include <ctype.h>
 #include <sys/types.h>
 
+#include <common/buffer.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/debug.h>
@@ -30,7 +31,7 @@
 #include <proto/acl.h>
 #include <proto/arg.h>
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/frontend.h>
 #include <proto/lb_chash.h>
 #include <proto/lb_fas.h>
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100644
index 0000000..764f692
--- /dev/null
+++ b/src/buffer.c
@@ -0,0 +1,143 @@
+/*
+ * Buffer management functions.
+ *
+ * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <common/config.h>
+#include <common/buffer.h>
+
+#include <types/global.h>
+
+/* This function realigns input data in a possibly wrapping buffer so that it
+ * becomes contiguous and starts at the beginning of the buffer area. The
+ * function may only be used when the buffer's output is empty.
+ */
+void buffer_slow_realign(struct buffer *buf)
+{
+	/* two possible cases :
+	 *   - the buffer is in one contiguous block, we move it in-place
+	 *   - the buffer is in two blocks, we move it via the swap_buffer
+	 */
+	if (buf->i) {
+		int block1 = buf->i;
+		int block2 = 0;
+		if (buf->p + buf->i > buf->data + buf->size) {
+			/* non-contiguous block */
+			block1 = buf->data + buf->size - buf->p;
+			block2 = buf->p + buf->i - (buf->data + buf->size);
+		}
+		if (block2)
+			memcpy(swap_buffer, buf->data, block2);
+		memmove(buf->data, buf->p, block1);
+		if (block2)
+			memcpy(buf->data + block1, swap_buffer, block2);
+	}
+
+	buf->p = buf->data;
+}
+
+
+/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
+ * destination. It does not use any intermediate buffer and does the move in
+ * place, though it will be slower than a simple memmove() on contiguous data,
+ * so it's desirable to use it only on non-contiguous buffers. No pointers are
+ * changed, the caller is responsible for that.
+ */
+void buffer_bounce_realign(struct buffer *buf)
+{
+	int advance, to_move;
+	char *from, *to;
+
+	from = bo_ptr(buf);
+	advance = buf->data + buf->size - from;
+	if (!advance)
+		return;
+
+	to_move = buffer_len(buf);
+	while (to_move) {
+		char last, save;
+
+		last = *from;
+		to = from + advance;
+		if (to >= buf->data + buf->size)
+			to -= buf->size;
+
+		while (1) {
+			save = *to;
+			*to  = last;
+			last = save;
+			to_move--;
+			if (!to_move)
+				break;
+
+			/* check if we went back home after rotating a number of bytes */
+			if (to == from)
+				break;
+
+			/* if we ended up in the empty area, let's walk to next place. The
+			 * empty area is either between buf->r and from or before from or
+			 * after buf->r.
+			 */
+			if (from > bi_end(buf)) {
+				if (to >= bi_end(buf) && to < from)
+					break;
+			} else if (from < bi_end(buf)) {
+				if (to < from || to >= bi_end(buf))
+					break;
+			}
+
+			/* we have overwritten a byte of the original set, let's move it */
+			to += advance;
+			if (to >= buf->data + buf->size)
+				to -= buf->size;
+		}
+
+		from++;
+		if (from >= buf->data + buf->size)
+			from -= buf->size;
+	}
+}
+
+
+/*
+ * Dumps part or all of a buffer.
+ */
+void buffer_dump(FILE *o, struct buffer *b, int from, int to)
+{
+	fprintf(o, "Dumping buffer %p\n", b);
+	fprintf(o, "  data=%p o=%d i=%d p=%p\n",
+		b->data, b->o, b->i, b->p);
+
+	if (!to || to > buffer_len(b))
+		to = buffer_len(b);
+
+	fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
+	for (; from < to; from++) {
+		if ((from & 15) == 0)
+			fprintf(o, "  %04x: ", from);
+		fprintf(o, "%02x ", b->data[from]);
+		if ((from & 15) == 7)
+			fprintf(o, "- ");
+		else if (((from & 15) == 15) && (from != to-1))
+			fprintf(o, "\n");
+	}
+	fprintf(o, "\n--\n");
+}
+
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ */
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 44397fc..9781783 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -26,6 +26,7 @@
 #include <netinet/tcp.h>
 
 #include <common/cfgparse.h>
+#include <common/chunk.h>
 #include <common/config.h>
 #include <common/errors.h>
 #include <common/memory.h>
@@ -40,7 +41,7 @@
 #include <proto/acl.h>
 #include <proto/auth.h>
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/checks.h>
 #include <proto/dumpstats.h>
 #include <proto/frontend.h>
diff --git a/src/buffers.c b/src/channel.c
similarity index 67%
rename from src/buffers.c
rename to src/channel.c
index db4b435..2789e89 100644
--- a/src/buffers.c
+++ b/src/channel.c
@@ -1,7 +1,7 @@
 /*
- * Buffer management functions.
+ * Channel management functions.
  *
- * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
+ * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -17,9 +17,14 @@
 
 #include <common/config.h>
 #include <common/memory.h>
-#include <proto/buffers.h>
+#include <common/buffer.h>
+#include <proto/channel.h>
 #include <types/global.h>
 
+
+/* Note: this code has not yet been completely cleaned up and still refers to
+ * the word "buffer" when "channel" is meant instead.
+ */
 struct pool_head *pool2_buffer;
 
 
@@ -385,229 +390,7 @@
 	return delta;
 }
 
-
-/* This function realigns input data in a possibly wrapping buffer so that it
- * becomes contiguous and starts at the beginning of the buffer area. The
- * function may only be used when the buffer's output is empty.
- */
-void buffer_slow_realign(struct buffer *buf)
-{
-	/* two possible cases :
-	 *   - the buffer is in one contiguous block, we move it in-place
-	 *   - the buffer is in two blocks, we move it via the swap_buffer
-	 */
-	if (buf->i) {
-		int block1 = buf->i;
-		int block2 = 0;
-		if (buf->p + buf->i > buf->data + buf->size) {
-			/* non-contiguous block */
-			block1 = buf->data + buf->size - buf->p;
-			block2 = buf->p + buf->i - (buf->data + buf->size);
-		}
-		if (block2)
-			memcpy(swap_buffer, buf->data, block2);
-		memmove(buf->data, buf->p, block1);
-		if (block2)
-			memcpy(buf->data + block1, swap_buffer, block2);
-	}
-
-	buf->p = buf->data;
-}
-
-/* Realigns a possibly non-contiguous buffer by bouncing bytes from source to
- * destination. It does not use any intermediate buffer and does the move in
- * place, though it will be slower than a simple memmove() on contiguous data,
- * so it's desirable to use it only on non-contiguous buffers. No pointers are
- * changed, the caller is responsible for that.
- */
-void buffer_bounce_realign(struct buffer *buf)
-{
-	int advance, to_move;
-	char *from, *to;
-
-	from = bo_ptr(buf);
-	advance = buf->data + buf->size - from;
-	if (!advance)
-		return;
-
-	to_move = buffer_len(buf);
-	while (to_move) {
-		char last, save;
-
-		last = *from;
-		to = from + advance;
-		if (to >= buf->data + buf->size)
-			to -= buf->size;
-
-		while (1) {
-			save = *to;
-			*to  = last;
-			last = save;
-			to_move--;
-			if (!to_move)
-				break;
-
-			/* check if we went back home after rotating a number of bytes */
-			if (to == from)
-				break;
-
-			/* if we ended up in the empty area, let's walk to next place. The
-			 * empty area is either between buf->r and from or before from or
-			 * after buf->r.
-			 */
-			if (from > bi_end(buf)) {
-				if (to >= bi_end(buf) && to < from)
-					break;
-			} else if (from < bi_end(buf)) {
-				if (to < from || to >= bi_end(buf))
-					break;
-			}
-
-			/* we have overwritten a byte of the original set, let's move it */
-			to += advance;
-			if (to >= buf->data + buf->size)
-				to -= buf->size;
-		}
-
-		from++;
-		if (from >= buf->data + buf->size)
-			from -= buf->size;
-	}
-}
-
-
-/*
- * Does an snprintf() at the end of chunk <chk>, respecting the limit of
- * at most chk->size chars. If the chk->len is over, nothing is added. Returns
- * the new chunk size.
- */
-int chunk_printf(struct chunk *chk, const char *fmt, ...)
-{
-	va_list argp;
-	int ret;
-
-	if (!chk->str || !chk->size)
-		return 0;
-
-	va_start(argp, fmt);
-	ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
-	if (ret >= chk->size - chk->len)
-		/* do not copy anything in case of truncation */
-		chk->str[chk->len] = 0;
-	else
-		chk->len += ret;
-	va_end(argp);
-	return chk->len;
-}
-
-/*
- * Encode chunk <src> into chunk <dst>, respecting the limit of at most
- * chk->size chars. Replace non-printable or special chracters with "&#%d;".
- * If the chk->len is over, nothing is added. Returns the new chunk size.
- */
-int chunk_htmlencode(struct chunk *dst, struct chunk *src) {
-
-	int i, l;
-	int olen, free;
-	char c;
-
-	olen = dst->len;
-
-	for (i = 0; i < src->len; i++) {
-		free = dst->size - dst->len;
-
-		if (!free) {
-			dst->len = olen;
-			return dst->len;
-		}
-
-		c = src->str[i];
-
-		if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
-			l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
-
-			if (free < l) {
-				dst->len = olen;
-				return dst->len;
-			}
-
-			dst->len += l;
-		} else {
-			dst->str[dst->len] = c;
-			dst->len++;
-		}
-	}
-
-	return dst->len;
-}
-
 /*
- * Encode chunk <src> into chunk <dst>, respecting the limit of at most
- * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
- * If the chk->len is over, nothing is added. Returns the new chunk size.
- */
-int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc) {
-	int i, l;
-	int olen, free;
-	char c;
-
-	olen = dst->len;
-
-	for (i = 0; i < src->len; i++) {
-		free = dst->size - dst->len;
-
-		if (!free) {
-			dst->len = olen;
-			return dst->len;
-		}
-
-		c = src->str[i];
-
-		if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
-			l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
-
-			if (free < l) {
-				dst->len = olen;
-				return dst->len;
-			}
-
-			dst->len += l;
-		} else {
-			dst->str[dst->len] = c;
-			dst->len++;
-		}
-	}
-
-	return dst->len;
-}
-
-/*
- * Dumps part or all of a buffer.
- */
-void buffer_dump(FILE *o, struct buffer *b, int from, int to)
-{
-	fprintf(o, "Dumping buffer %p\n", b);
-	fprintf(o, "  data=%p o=%d i=%d p=%p\n",
-		b->data, b->o, b->i, b->p);
-
-	if (!to || to > buffer_len(b))
-		to = buffer_len(b);
-
-	fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to);
-	for (; from < to; from++) {
-		if ((from & 15) == 0)
-			fprintf(o, "  %04x: ", from);
-		fprintf(o, "%02x ", b->data[from]);
-		if ((from & 15) == 7)
-			fprintf(o, "- ");
-		else if (((from & 15) == 15) && (from != to-1))
-			fprintf(o, "\n");
-	}
-	fprintf(o, "\n--\n");
-}
-
-
-/*
  * Local variables:
  *  c-indent-level: 8
  *  c-basic-offset: 8
diff --git a/src/checks.c b/src/checks.c
index 2cde26b..93f93df 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -26,6 +26,7 @@
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 
+#include <common/chunk.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/mini-clist.h>
@@ -36,7 +37,6 @@
 
 #include <proto/backend.h>
 #include <proto/checks.h>
-#include <proto/buffers.h>
 #include <proto/fd.h>
 #include <proto/log.h>
 #include <proto/queue.h>
diff --git a/src/chunk.c b/src/chunk.c
new file mode 100644
index 0000000..a4cbb7d
--- /dev/null
+++ b/src/chunk.c
@@ -0,0 +1,133 @@
+/*
+ * Chunk management functions.
+ *
+ * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <common/config.h>
+#include <common/chunk.h>
+
+/*
+ * Does an snprintf() at the end of chunk <chk>, respecting the limit of
+ * at most chk->size chars. If the chk->len is over, nothing is added. Returns
+ * the new chunk size.
+ */
+int chunk_printf(struct chunk *chk, const char *fmt, ...)
+{
+	va_list argp;
+	int ret;
+
+	if (!chk->str || !chk->size)
+		return 0;
+
+	va_start(argp, fmt);
+	ret = vsnprintf(chk->str + chk->len, chk->size - chk->len, fmt, argp);
+	if (ret >= chk->size - chk->len)
+		/* do not copy anything in case of truncation */
+		chk->str[chk->len] = 0;
+	else
+		chk->len += ret;
+	va_end(argp);
+	return chk->len;
+}
+
+/*
+ * Encode chunk <src> into chunk <dst>, respecting the limit of at most
+ * chk->size chars. Replace non-printable or special chracters with "&#%d;".
+ * If the chk->len is over, nothing is added. Returns the new chunk size.
+ */
+int chunk_htmlencode(struct chunk *dst, struct chunk *src)
+{
+	int i, l;
+	int olen, free;
+	char c;
+
+	olen = dst->len;
+
+	for (i = 0; i < src->len; i++) {
+		free = dst->size - dst->len;
+
+		if (!free) {
+			dst->len = olen;
+			return dst->len;
+		}
+
+		c = src->str[i];
+
+		if (!isascii(c) || !isprint((unsigned char)c) || c == '&' || c == '"' || c == '\'' || c == '<' || c == '>') {
+			l = snprintf(dst->str + dst->len, free, "&#%u;", (unsigned char)c);
+
+			if (free < l) {
+				dst->len = olen;
+				return dst->len;
+			}
+
+			dst->len += l;
+		} else {
+			dst->str[dst->len] = c;
+			dst->len++;
+		}
+	}
+
+	return dst->len;
+}
+
+/*
+ * Encode chunk <src> into chunk <dst>, respecting the limit of at most
+ * chk->size chars. Replace non-printable or char passed in qc with "<%02X>".
+ * If the chk->len is over, nothing is added. Returns the new chunk size.
+ */
+int chunk_asciiencode(struct chunk *dst, struct chunk *src, char qc)
+{
+	int i, l;
+	int olen, free;
+	char c;
+
+	olen = dst->len;
+
+	for (i = 0; i < src->len; i++) {
+		free = dst->size - dst->len;
+
+		if (!free) {
+			dst->len = olen;
+			return dst->len;
+		}
+
+		c = src->str[i];
+
+		if (!isascii(c) || !isprint((unsigned char)c) || c == '<' || c == '>' || c == qc) {
+			l = snprintf(dst->str + dst->len, free, "<%02X>", (unsigned char)c);
+
+			if (free < l) {
+				dst->len = olen;
+				return dst->len;
+			}
+
+			dst->len += l;
+		} else {
+			dst->str[dst->len] = c;
+			dst->len++;
+		}
+	}
+
+	return dst->len;
+}
+
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ */
diff --git a/src/dumpstats.c b/src/dumpstats.c
index f7252a4..f150d98 100644
--- a/src/dumpstats.c
+++ b/src/dumpstats.c
@@ -40,7 +40,7 @@
 #include <types/global.h>
 
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/checks.h>
 #include <proto/dumpstats.h>
 #include <proto/fd.h>
diff --git a/src/frontend.c b/src/frontend.c
index b07d3d1..ad4b970 100644
--- a/src/frontend.c
+++ b/src/frontend.c
@@ -22,6 +22,7 @@
 
 #include <netinet/tcp.h>
 
+#include <common/chunk.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/debug.h>
@@ -32,7 +33,7 @@
 
 #include <proto/acl.h>
 #include <proto/arg.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/fd.h>
 #include <proto/frontend.h>
 #include <proto/log.h>
diff --git a/src/haproxy.c b/src/haproxy.c
index f104740..3427dcc 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -52,6 +52,7 @@
 #include <common/appsession.h>
 #include <common/base64.h>
 #include <common/cfgparse.h>
+#include <common/chunk.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/defaults.h>
@@ -73,7 +74,7 @@
 #include <proto/auth.h>
 #include <proto/acl.h>
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/checks.h>
 #include <proto/fd.h>
 #include <proto/hdr_idx.h>
diff --git a/src/peers.c b/src/peers.c
index c293971..894502a 100644
--- a/src/peers.c
+++ b/src/peers.c
@@ -28,7 +28,7 @@
 #include <types/peers.h>
 
 #include <proto/acl.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/fd.h>
 #include <proto/log.h>
 #include <proto/hdr_idx.h>
diff --git a/src/proto_http.c b/src/proto_http.c
index de1cec6..a7e7b6e 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -27,6 +27,7 @@
 
 #include <common/appsession.h>
 #include <common/base64.h>
+#include <common/chunk.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/debug.h>
@@ -45,7 +46,7 @@
 #include <proto/arg.h>
 #include <proto/auth.h>
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/checks.h>
 #include <proto/dumpstats.h>
 #include <proto/fd.h>
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index 9ff5b66..bdab4ab 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -39,9 +39,8 @@
 
 #include <proto/acl.h>
 #include <proto/arg.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/connection.h>
-//#include <proto/frontend.h>
 #include <proto/log.h>
 #include <proto/port_range.h>
 #include <proto/protocols.h>
@@ -52,7 +51,6 @@
 #include <proto/stick_table.h>
 #include <proto/stream_interface.h>
 #include <proto/task.h>
-#include <proto/buffers.h>
 
 #ifdef CONFIG_HAP_CTTPROXY
 #include <import/ip_tproxy.h>
diff --git a/src/raw_sock.c b/src/raw_sock.c
index e13749e..80c8fb3 100644
--- a/src/raw_sock.c
+++ b/src/raw_sock.c
@@ -22,6 +22,7 @@
 
 #include <netinet/tcp.h>
 
+#include <common/buffer.h>
 #include <common/compat.h>
 #include <common/config.h>
 #include <common/debug.h>
@@ -29,7 +30,6 @@
 #include <common/ticks.h>
 #include <common/time.h>
 
-#include <proto/buffers.h>
 #include <proto/connection.h>
 #include <proto/fd.h>
 #include <proto/freq_ctr.h>
diff --git a/src/sample.c b/src/sample.c
index f14b99b..aec94fe 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -14,10 +14,10 @@
 #include <string.h>
 #include <arpa/inet.h>
 
+#include <common/chunk.h>
 #include <common/standard.h>
 
 #include <proto/arg.h>
-#include <proto/buffers.h>
 #include <proto/sample.h>
 
 /* static sample used in sample_process() when <p> is NULL */
diff --git a/src/session.c b/src/session.c
index f2330d6..db6dec4 100644
--- a/src/session.c
+++ b/src/session.c
@@ -24,7 +24,7 @@
 #include <proto/acl.h>
 #include <proto/arg.h>
 #include <proto/backend.h>
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/checks.h>
 #include <proto/connection.h>
 #include <proto/dumpstats.h>
diff --git a/src/stream_interface.c b/src/stream_interface.c
index 764bb3b..fe80b91 100644
--- a/src/stream_interface.c
+++ b/src/stream_interface.c
@@ -26,7 +26,7 @@
 #include <common/ticks.h>
 #include <common/time.h>
 
-#include <proto/buffers.h>
+#include <proto/channel.h>
 #include <proto/connection.h>
 #include <proto/fd.h>
 #include <proto/frontend.h>