blob: 522036a41445657c6ff503e665aeeb7f66755f27 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Code for manipulating FIFO buffers.
Damien Miller5f056372000-04-16 12:31:48 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
Damien Millere4340be2000-09-16 13:29:08 +110014/* RCSID("$OpenBSD: buffer.h,v 1.6 2000/09/07 20:27:50 deraadt Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#ifndef BUFFER_H
17#define BUFFER_H
18
Damien Miller95def091999-11-25 00:26:21 +110019typedef struct {
20 char *buf; /* Buffer for data. */
21 unsigned int alloc; /* Number of bytes allocated for data. */
22 unsigned int offset; /* Offset of first byte containing data. */
23 unsigned int end; /* Offset of last byte containing data. */
24} Buffer;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025/* Initializes the buffer structure. */
Damien Miller95def091999-11-25 00:26:21 +110026void buffer_init(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
28/* Frees any memory used for the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110029void buffer_free(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030
31/* Clears any data from the buffer, making it empty. This does not actually
32 zero the memory. */
Damien Miller95def091999-11-25 00:26:21 +110033void buffer_clear(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
35/* Appends data to the buffer, expanding it if necessary. */
Damien Miller95def091999-11-25 00:26:21 +110036void buffer_append(Buffer * buffer, const char *data, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Damien Miller5428f641999-11-25 11:54:57 +110038/*
39 * Appends space to the buffer, expanding the buffer if necessary. This does
40 * not actually copy the data into the buffer, but instead returns a pointer
41 * to the allocated region.
42 */
Damien Miller95def091999-11-25 00:26:21 +110043void buffer_append_space(Buffer * buffer, char **datap, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45/* Returns the number of bytes of data in the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110046unsigned int buffer_len(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48/* Gets data from the beginning of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110049void buffer_get(Buffer * buffer, char *buf, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51/* Consumes the given number of bytes from the beginning of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110052void buffer_consume(Buffer * buffer, unsigned int bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54/* Consumes the given number of bytes from the end of the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110055void buffer_consume_end(Buffer * buffer, unsigned int bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
57/* Returns a pointer to the first used byte in the buffer. */
Damien Miller95def091999-11-25 00:26:21 +110058char *buffer_ptr(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Dumps the contents of the buffer to stderr in hex. This intended for
62 * debugging purposes only.
63 */
Damien Miller95def091999-11-25 00:26:21 +110064void buffer_dump(Buffer * buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Miller95def091999-11-25 00:26:21 +110066#endif /* BUFFER_H */