|
Lines 18-23
RCSID("$OpenBSD: buffer.c,v 1.22 2004/10
Link Here
|
| 18 |
#include "buffer.h" |
18 |
#include "buffer.h" |
| 19 |
#include "log.h" |
19 |
#include "log.h" |
| 20 |
|
20 |
|
|
|
21 |
#define BUFFER_MAX_SIZE 0xa00000 |
| 22 |
#define BUFFER_MAX_APPEND 0x100000 |
| 23 |
#define BUFFER_ALLOC_INCREMENT 32768 |
| 24 |
|
| 21 |
/* Initializes the buffer structure. */ |
25 |
/* Initializes the buffer structure. */ |
| 22 |
|
26 |
|
| 23 |
void |
27 |
void |
|
Lines 78-84
buffer_append_space(Buffer *buffer, u_in
Link Here
|
| 78 |
u_int newlen; |
82 |
u_int newlen; |
| 79 |
void *p; |
83 |
void *p; |
| 80 |
|
84 |
|
| 81 |
if (len > 0x100000) |
85 |
if (len > BUFFER_MAX_APPEND) |
| 82 |
fatal("buffer_append_space: len %u not supported", len); |
86 |
fatal("buffer_append_space: len %u not supported", len); |
| 83 |
|
87 |
|
| 84 |
/* If the buffer is empty, start using it from the beginning. */ |
88 |
/* If the buffer is empty, start using it from the beginning. */ |
|
Lines 106-113
restart:
Link Here
|
| 106 |
} |
110 |
} |
| 107 |
/* Increase the size of the buffer and retry. */ |
111 |
/* Increase the size of the buffer and retry. */ |
| 108 |
|
112 |
|
| 109 |
newlen = buffer->alloc + len + 32768; |
113 |
newlen = buffer->alloc + len + BUFFER_ALLOC_INCREMENT; |
| 110 |
if (newlen > 0xa00000) |
114 |
if (newlen > BUFFER_MAX_SIZE) |
| 111 |
fatal("buffer_append_space: alloc %u not supported", |
115 |
fatal("buffer_append_space: alloc %u not supported", |
| 112 |
newlen); |
116 |
newlen); |
| 113 |
buffer->buf = xrealloc(buffer->buf, newlen); |
117 |
buffer->buf = xrealloc(buffer->buf, newlen); |
|
Lines 122-127
u_int
Link Here
|
| 122 |
buffer_len(Buffer *buffer) |
126 |
buffer_len(Buffer *buffer) |
| 123 |
{ |
127 |
{ |
| 124 |
return buffer->end - buffer->offset; |
128 |
return buffer->end - buffer->offset; |
|
|
129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* Returns the number of bytes that may be safely appended to the buffer in |
| 133 |
* one operation. |
| 134 |
*/ |
| 135 |
|
| 136 |
u_int |
| 137 |
buffer_max_append(Buffer *buffer) |
| 138 |
{ |
| 139 |
long remaining; |
| 140 |
|
| 141 |
if (buffer->end > BUFFER_MAX_SIZE) /* should never happen */ |
| 142 |
fatal("buffer_max_add: buffer has exceeded allowable size"); |
| 143 |
|
| 144 |
/* We need to allow for the increment buffer_append_space will add. */ |
| 145 |
remaining = BUFFER_MAX_SIZE - BUFFER_ALLOC_INCREMENT - buffer->end; |
| 146 |
|
| 147 |
if (remaining < 0) |
| 148 |
return 0; |
| 149 |
return MIN(BUFFER_MAX_APPEND, (u_int)remaining); |
| 125 |
} |
150 |
} |
| 126 |
|
151 |
|
| 127 |
/* Gets data from the beginning of the buffer. */ |
152 |
/* Gets data from the beginning of the buffer. */ |