version 1.5, 2000/04/12 10:22:38 |
version 1.6, 2000/04/14 10:30:30 |
|
|
/* |
/* |
* |
* |
* buffer.c |
* buffer.c |
* |
* |
* Author: Tatu Ylonen <ylo@cs.hut.fi> |
* Author: Tatu Ylonen <ylo@cs.hut.fi> |
* |
* |
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
* All rights reserved |
* All rights reserved |
* |
* |
* Created: Sat Mar 18 04:15:33 1995 ylo |
* Created: Sat Mar 18 04:15:33 1995 ylo |
* |
* |
* Functions for manipulating fifo buffers (that can grow if needed). |
* Functions for manipulating fifo buffers (that can grow if needed). |
* |
* |
*/ |
*/ |
|
|
#include "includes.h" |
#include "includes.h" |
|
|
|
|
/* Initializes the buffer structure. */ |
/* Initializes the buffer structure. */ |
|
|
void |
void |
buffer_init(Buffer *buffer) |
buffer_init(Buffer *buffer) |
{ |
{ |
buffer->alloc = 4096; |
buffer->alloc = 4096; |
|
|
|
|
/* Frees any memory used for the buffer. */ |
/* Frees any memory used for the buffer. */ |
|
|
void |
void |
buffer_free(Buffer *buffer) |
buffer_free(Buffer *buffer) |
{ |
{ |
memset(buffer->buf, 0, buffer->alloc); |
memset(buffer->buf, 0, buffer->alloc); |
|
|
* zero the memory. |
* zero the memory. |
*/ |
*/ |
|
|
void |
void |
buffer_clear(Buffer *buffer) |
buffer_clear(Buffer *buffer) |
{ |
{ |
buffer->offset = 0; |
buffer->offset = 0; |
|
|
|
|
/* Appends data to the buffer, expanding it if necessary. */ |
/* Appends data to the buffer, expanding it if necessary. */ |
|
|
void |
void |
buffer_append(Buffer *buffer, const char *data, unsigned int len) |
buffer_append(Buffer *buffer, const char *data, unsigned int len) |
{ |
{ |
char *cp; |
char *cp; |
|
|
* to the allocated region. |
* to the allocated region. |
*/ |
*/ |
|
|
void |
void |
buffer_append_space(Buffer *buffer, char **datap, unsigned int len) |
buffer_append_space(Buffer *buffer, char **datap, unsigned int len) |
{ |
{ |
/* If the buffer is empty, start using it from the beginning. */ |
/* If the buffer is empty, start using it from the beginning. */ |
|
|
|
|
/* Returns the number of bytes of data in the buffer. */ |
/* Returns the number of bytes of data in the buffer. */ |
|
|
unsigned int |
unsigned int |
buffer_len(Buffer *buffer) |
buffer_len(Buffer *buffer) |
{ |
{ |
return buffer->end - buffer->offset; |
return buffer->end - buffer->offset; |
|
|
|
|
/* Gets data from the beginning of the buffer. */ |
/* Gets data from the beginning of the buffer. */ |
|
|
void |
void |
buffer_get(Buffer *buffer, char *buf, unsigned int len) |
buffer_get(Buffer *buffer, char *buf, unsigned int len) |
{ |
{ |
if (len > buffer->end - buffer->offset) |
if (len > buffer->end - buffer->offset) |
|
|
|
|
/* Consumes the given number of bytes from the beginning of the buffer. */ |
/* Consumes the given number of bytes from the beginning of the buffer. */ |
|
|
void |
void |
buffer_consume(Buffer *buffer, unsigned int bytes) |
buffer_consume(Buffer *buffer, unsigned int bytes) |
{ |
{ |
if (bytes > buffer->end - buffer->offset) |
if (bytes > buffer->end - buffer->offset) |
|
|
|
|
/* Consumes the given number of bytes from the end of the buffer. */ |
/* Consumes the given number of bytes from the end of the buffer. */ |
|
|
void |
void |
buffer_consume_end(Buffer *buffer, unsigned int bytes) |
buffer_consume_end(Buffer *buffer, unsigned int bytes) |
{ |
{ |
if (bytes > buffer->end - buffer->offset) |
if (bytes > buffer->end - buffer->offset) |
|
|
|
|
/* Dumps the contents of the buffer to stderr. */ |
/* Dumps the contents of the buffer to stderr. */ |
|
|
void |
void |
buffer_dump(Buffer *buffer) |
buffer_dump(Buffer *buffer) |
{ |
{ |
int i; |
int i; |