source: trunk/third/ssh/buffer.c @ 12646

Revision 12646, 3.8 KB checked in by danw, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12645, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2
3buffer.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8                   All rights reserved
9
10Created: Sat Mar 18 04:15:33 1995 ylo
11
12Functions for manipulating fifo buffers (that can grow if needed).
13
14*/
15
16/*
17 * $Id: buffer.c,v 1.1.1.2 1999-03-08 17:43:12 danw Exp $
18 * $Log: not supported by cvs2svn $
19 * Revision 1.1.1.1  1996/02/18  21:38:11  ylo
20 *      Imported ssh-1.2.13.
21 *
22 * Revision 1.2  1995/07/13  01:18:46  ylo
23 *      Removed "Last modified" header.
24 *      Added cvs log.
25 *
26 * $Endlog$
27 */
28
29#include "includes.h"
30#include "xmalloc.h"
31#include "buffer.h"
32#include "ssh.h"
33
34/* Initializes the buffer structure. */
35
36void buffer_init(Buffer *buffer)
37{
38  buffer->alloc = 4096;
39  buffer->buf = xmalloc(buffer->alloc);
40  buffer->offset = 0;
41  buffer->end = 0;
42}
43
44/* Frees any memory used for the buffer. */
45
46void buffer_free(Buffer *buffer)
47{
48  memset(buffer->buf, 0, buffer->alloc);
49  xfree(buffer->buf);
50}
51
52/* Clears any data from the buffer, making it empty.  This does not actually
53   zero the memory. */
54
55void buffer_clear(Buffer *buffer)
56{
57  buffer->offset = 0;
58  buffer->end = 0;
59}
60
61/* Appends data to the buffer, expanding it if necessary. */
62
63void buffer_append(Buffer *buffer, const char *data, unsigned int len)
64{
65  char *cp;
66  buffer_append_space(buffer, &cp, len);
67  memcpy(cp, data, len);
68}
69
70/* Appends space to the buffer, expanding the buffer if necessary.
71   This does not actually copy the data into the buffer, but instead
72   returns a pointer to the allocated region. */
73
74void buffer_append_space(Buffer *buffer, char **datap, unsigned int len)
75{
76  /* If the buffer is empty, start using it from the beginning. */
77  if (buffer->offset == buffer->end)
78    {
79      buffer->offset = 0;
80      buffer->end = 0;
81    }
82
83 restart:
84  /* If there is enough space to store all data, store it now. */
85  if (buffer->end + len < buffer->alloc)
86    {
87      *datap = buffer->buf + buffer->end;
88      buffer->end += len;
89      return;
90    }
91
92  /* If the buffer is quite empty, but all data is at the end, move the
93     data to the beginning and retry. */
94  if (buffer->offset > buffer->alloc / 2)
95    {
96      memmove(buffer->buf, buffer->buf + buffer->offset,
97              buffer->end - buffer->offset);
98      buffer->end -= buffer->offset;
99      buffer->offset = 0;
100      goto restart;
101    }
102
103  /* Increase the size of the buffer and retry. */
104  buffer->alloc += len + 4096;
105  buffer->buf = xrealloc(buffer->buf, buffer->alloc);
106  goto restart;
107}
108
109/* Returns the number of bytes of data in the buffer. */
110
111unsigned int buffer_len(Buffer *buffer)
112{
113  return buffer->end - buffer->offset;
114}
115
116/* Gets data from the beginning of the buffer. */
117
118void buffer_get(Buffer *buffer, char *buf, unsigned int len)
119{
120  if (len > buffer->end - buffer->offset)
121    fatal("buffer_get trying to get more bytes than in buffer");
122  memcpy(buf, buffer->buf + buffer->offset, len);
123  buffer->offset += len;
124}
125
126/* Consumes the given number of bytes from the beginning of the buffer. */
127
128void buffer_consume(Buffer *buffer, unsigned int bytes)
129{
130  if (bytes > buffer->end - buffer->offset)
131    fatal("buffer_get trying to get more bytes than in buffer");
132  buffer->offset += bytes;
133
134
135/* Consumes the given number of bytes from the end of the buffer. */
136
137void buffer_consume_end(Buffer *buffer, unsigned int bytes)
138{
139  if (bytes > buffer->end - buffer->offset)
140    fatal("buffer_get trying to get more bytes than in buffer");
141  buffer->end -= bytes;
142
143
144/* Returns a pointer to the first used byte in the buffer. */
145
146char *buffer_ptr(Buffer *buffer)
147{
148  return buffer->buf + buffer->offset;
149}
150
151/* Dumps the contents of the buffer to stderr. */
152
153void buffer_dump(Buffer *buffer)
154{
155  int i;
156  unsigned char *ucp = (unsigned char *)buffer->buf;
157 
158  for (i = buffer->offset; i < buffer->end; i++)
159    fprintf(stderr, " %02x", ucp[i]);
160  fprintf(stderr, "\n");
161}
Note: See TracBrowser for help on using the repository browser.