version 1.21, 2003/11/21 11:57:03 |
version 1.21.4.2, 2005/06/05 02:22:39 |
|
|
u_int newlen; |
u_int newlen; |
void *p; |
void *p; |
|
|
if (len > 0x100000) |
if (len > BUFFER_MAX_CHUNK) |
fatal("buffer_append_space: len %u not supported", len); |
fatal("buffer_append_space: len %u not supported", len); |
|
|
/* If the buffer is empty, start using it from the beginning. */ |
/* If the buffer is empty, start using it from the beginning. */ |
|
|
* If the buffer is quite empty, but all data is at the end, move the |
* If the buffer is quite empty, but all data is at the end, move the |
* data to the beginning and retry. |
* data to the beginning and retry. |
*/ |
*/ |
if (buffer->offset > buffer->alloc / 2) { |
if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
memmove(buffer->buf, buffer->buf + buffer->offset, |
memmove(buffer->buf, buffer->buf + buffer->offset, |
buffer->end - buffer->offset); |
buffer->end - buffer->offset); |
buffer->end -= buffer->offset; |
buffer->end -= buffer->offset; |
|
|
/* Increase the size of the buffer and retry. */ |
/* Increase the size of the buffer and retry. */ |
|
|
newlen = buffer->alloc + len + 32768; |
newlen = buffer->alloc + len + 32768; |
if (newlen > 0xa00000) |
if (newlen > BUFFER_MAX_LEN) |
fatal("buffer_append_space: alloc %u not supported", |
fatal("buffer_append_space: alloc %u not supported", |
newlen); |
newlen); |
buffer->buf = xrealloc(buffer->buf, newlen); |
buffer->buf = xrealloc(buffer->buf, newlen); |
|
|
|
|
/* Gets data from the beginning of the buffer. */ |
/* Gets data from the beginning of the buffer. */ |
|
|
void |
int |
buffer_get(Buffer *buffer, void *buf, u_int len) |
buffer_get_ret(Buffer *buffer, void *buf, u_int len) |
{ |
{ |
if (len > buffer->end - buffer->offset) |
if (len > buffer->end - buffer->offset) { |
fatal("buffer_get: trying to get more bytes %d than in buffer %d", |
error("buffer_get_ret: trying to get more bytes %d than in buffer %d", |
len, buffer->end - buffer->offset); |
len, buffer->end - buffer->offset); |
|
return (-1); |
|
} |
memcpy(buf, buffer->buf + buffer->offset, len); |
memcpy(buf, buffer->buf + buffer->offset, len); |
buffer->offset += len; |
buffer->offset += len; |
|
return (0); |
} |
} |
|
|
|
void |
|
buffer_get(Buffer *buffer, void *buf, u_int len) |
|
{ |
|
if (buffer_get_ret(buffer, buf, len) == -1) |
|
fatal("buffer_get: buffer error"); |
|
} |
|
|
/* Consumes the given number of bytes from the beginning of the buffer. */ |
/* Consumes the given number of bytes from the beginning of the buffer. */ |
|
|
|
int |
|
buffer_consume_ret(Buffer *buffer, u_int bytes) |
|
{ |
|
if (bytes > buffer->end - buffer->offset) { |
|
error("buffer_consume_ret: trying to get more bytes than in buffer"); |
|
return (-1); |
|
} |
|
buffer->offset += bytes; |
|
return (0); |
|
} |
|
|
void |
void |
buffer_consume(Buffer *buffer, u_int bytes) |
buffer_consume(Buffer *buffer, u_int bytes) |
{ |
{ |
if (bytes > buffer->end - buffer->offset) |
if (buffer_consume_ret(buffer, bytes) == -1) |
fatal("buffer_consume: trying to get more bytes than in buffer"); |
fatal("buffer_consume: buffer error"); |
buffer->offset += bytes; |
|
} |
} |
|
|
/* Consumes the given number of bytes from the end of the buffer. */ |
/* Consumes the given number of bytes from the end of the buffer. */ |
|
|
|
int |
|
buffer_consume_end_ret(Buffer *buffer, u_int bytes) |
|
{ |
|
if (bytes > buffer->end - buffer->offset) |
|
return (-1); |
|
buffer->end -= bytes; |
|
return (0); |
|
} |
|
|
void |
void |
buffer_consume_end(Buffer *buffer, u_int bytes) |
buffer_consume_end(Buffer *buffer, u_int bytes) |
{ |
{ |
if (bytes > buffer->end - buffer->offset) |
if (buffer_consume_end_ret(buffer, bytes) == -1) |
fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
fatal("buffer_consume_end: trying to get more bytes than in buffer"); |
buffer->end -= bytes; |
|
} |
} |
|
|
/* Returns a pointer to the first used byte in the buffer. */ |
/* Returns a pointer to the first used byte in the buffer. */ |