summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c69
1 files changed, 55 insertions, 14 deletions
diff --git a/buffer.c b/buffer.c
index 487e08105..e02e1e35c 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1,3 +1,4 @@
1/* $OpenBSD: buffer.c,v 1.31 2006/08/03 03:34:41 deraadt Exp $ */
1/* 2/*
2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -12,12 +13,21 @@
12 */ 13 */
13 14
14#include "includes.h" 15#include "includes.h"
15RCSID("$OpenBSD: buffer.c,v 1.23 2005/03/14 11:46:56 markus Exp $"); 16
17#include <sys/param.h>
18
19#include <stdio.h>
20#include <string.h>
21#include <stdarg.h>
16 22
17#include "xmalloc.h" 23#include "xmalloc.h"
18#include "buffer.h" 24#include "buffer.h"
19#include "log.h" 25#include "log.h"
20 26
27#define BUFFER_MAX_CHUNK 0x100000
28#define BUFFER_MAX_LEN 0xa00000
29#define BUFFER_ALLOCSZ 0x008000
30
21/* Initializes the buffer structure. */ 31/* Initializes the buffer structure. */
22 32
23void 33void
@@ -66,6 +76,23 @@ buffer_append(Buffer *buffer, const void *data, u_int len)
66 memcpy(p, data, len); 76 memcpy(p, data, len);
67} 77}
68 78
79static int
80buffer_compact(Buffer *buffer)
81{
82 /*
83 * If the buffer is quite empty, but all data is at the end, move the
84 * data to the beginning.
85 */
86 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
87 memmove(buffer->buf, buffer->buf + buffer->offset,
88 buffer->end - buffer->offset);
89 buffer->end -= buffer->offset;
90 buffer->offset = 0;
91 return (1);
92 }
93 return (0);
94}
95
69/* 96/*
70 * Appends space to the buffer, expanding the buffer if necessary. This does 97 * Appends space to the buffer, expanding the buffer if necessary. This does
71 * not actually copy the data into the buffer, but instead returns a pointer 98 * not actually copy the data into the buffer, but instead returns a pointer
@@ -93,29 +120,43 @@ restart:
93 buffer->end += len; 120 buffer->end += len;
94 return p; 121 return p;
95 } 122 }
96 /* 123
97 * If the buffer is quite empty, but all data is at the end, move the 124 /* Compact data back to the start of the buffer if necessary */
98 * data to the beginning and retry. 125 if (buffer_compact(buffer))
99 */
100 if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
101 memmove(buffer->buf, buffer->buf + buffer->offset,
102 buffer->end - buffer->offset);
103 buffer->end -= buffer->offset;
104 buffer->offset = 0;
105 goto restart; 126 goto restart;
106 }
107 /* Increase the size of the buffer and retry. */
108 127
109 newlen = buffer->alloc + len + 32768; 128 /* Increase the size of the buffer and retry. */
129 newlen = roundup(buffer->alloc + len, BUFFER_ALLOCSZ);
110 if (newlen > BUFFER_MAX_LEN) 130 if (newlen > BUFFER_MAX_LEN)
111 fatal("buffer_append_space: alloc %u not supported", 131 fatal("buffer_append_space: alloc %u not supported",
112 newlen); 132 newlen);
113 buffer->buf = xrealloc(buffer->buf, newlen); 133 buffer->buf = xrealloc(buffer->buf, 1, newlen);
114 buffer->alloc = newlen; 134 buffer->alloc = newlen;
115 goto restart; 135 goto restart;
116 /* NOTREACHED */ 136 /* NOTREACHED */
117} 137}
118 138
139/*
140 * Check whether an allocation of 'len' will fit in the buffer
141 * This must follow the same math as buffer_append_space
142 */
143int
144buffer_check_alloc(Buffer *buffer, u_int len)
145{
146 if (buffer->offset == buffer->end) {
147 buffer->offset = 0;
148 buffer->end = 0;
149 }
150 restart:
151 if (buffer->end + len < buffer->alloc)
152 return (1);
153 if (buffer_compact(buffer))
154 goto restart;
155 if (roundup(buffer->alloc + len, BUFFER_ALLOCSZ) <= BUFFER_MAX_LEN)
156 return (1);
157 return (0);
158}
159
119/* Returns the number of bytes of data in the buffer. */ 160/* Returns the number of bytes of data in the buffer. */
120 161
121u_int 162u_int