blob: f33e6f724d12d68cf3cb9f42369042c3f0de1f84 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller5f056372000-04-16 12:31:48 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * buffer.h
Damien Miller5f056372000-04-16 12:31:48 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller5f056372000-04-16 12:31:48 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller5f056372000-04-16 12:31:48 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Sat Mar 18 04:12:25 1995 ylo
Damien Miller5f056372000-04-16 12:31:48 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Code for manipulating FIFO buffers.
Damien Miller5f056372000-04-16 12:31:48 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Miller5f056372000-04-16 12:31:48 +100016/* RCSID("$Id: buffer.h,v 1.4 2000/04/16 02:31:50 damien Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#ifndef BUFFER_H
19#define BUFFER_H
20
Damien Miller95def091999-11-25 00:26:21 +110021typedef struct {
22 char *buf; /* Buffer for data. */
23 unsigned int alloc; /* Number of bytes allocated for data. */
24 unsigned int offset; /* Offset of first byte containing data. */
25 unsigned int end; /* Offset of last byte containing data. */
26} Buffer;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027/* Initializes the buffer structure. */
Damien Miller95def091999-11-25 00:26:21 +110028void buffer_init(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
30/* Frees any memory used for the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110031void buffer_free(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
33/* Clears any data from the buffer, making it empty. This does not actually
34 zero the memory. */
Damien Miller95def091999-11-25 00:26:21 +110035void buffer_clear(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37/* Appends data to the buffer, expanding it if necessary. */
Damien Miller95def091999-11-25 00:26:21 +110038void buffer_append(Buffer * buffer, const char *data, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Miller5428f641999-11-25 11:54:57 +110040/*
41 * Appends space to the buffer, expanding the buffer if necessary. This does
42 * not actually copy the data into the buffer, but instead returns a pointer
43 * to the allocated region.
44 */
Damien Miller95def091999-11-25 00:26:21 +110045void buffer_append_space(Buffer * buffer, char **datap, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
47/* Returns the number of bytes of data in the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110048unsigned int buffer_len(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049
50/* Gets data from the beginning of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110051void buffer_get(Buffer * buffer, char *buf, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
53/* Consumes the given number of bytes from the beginning of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110054void buffer_consume(Buffer * buffer, unsigned int bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
56/* Consumes the given number of bytes from the end of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110057void buffer_consume_end(Buffer * buffer, unsigned int bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59/* Returns a pointer to the first used byte in the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110060char *buffer_ptr(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Dumps the contents of the buffer to stderr in hex. This intended for
64 * debugging purposes only.
65 */
Damien Miller95def091999-11-25 00:26:21 +110066void buffer_dump(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller95def091999-11-25 00:26:21 +110068#endif /* BUFFER_H */