]> andersk Git - openssh.git/blob - buffer.c
- djm@cvs.openbsd.org 2006/03/25 01:13:23
[openssh.git] / buffer.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Functions for manipulating fifo buffers (that can grow if needed).
6  *
7  * 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".
12  */
13
14 #include "includes.h"
15
16 #include "xmalloc.h"
17 #include "buffer.h"
18 #include "log.h"
19
20 /* Initializes the buffer structure. */
21
22 void
23 buffer_init(Buffer *buffer)
24 {
25         const u_int len = 4096;
26
27         buffer->alloc = 0;
28         buffer->buf = xmalloc(len);
29         buffer->alloc = len;
30         buffer->offset = 0;
31         buffer->end = 0;
32 }
33
34 /* Frees any memory used for the buffer. */
35
36 void
37 buffer_free(Buffer *buffer)
38 {
39         if (buffer->alloc > 0) {
40                 memset(buffer->buf, 0, buffer->alloc);
41                 buffer->alloc = 0;
42                 xfree(buffer->buf);
43         }
44 }
45
46 /*
47  * Clears any data from the buffer, making it empty.  This does not actually
48  * zero the memory.
49  */
50
51 void
52 buffer_clear(Buffer *buffer)
53 {
54         buffer->offset = 0;
55         buffer->end = 0;
56 }
57
58 /* Appends data to the buffer, expanding it if necessary. */
59
60 void
61 buffer_append(Buffer *buffer, const void *data, u_int len)
62 {
63         void *p;
64         p = buffer_append_space(buffer, len);
65         memcpy(p, data, len);
66 }
67
68 /*
69  * Appends space to the buffer, expanding the buffer if necessary. This does
70  * not actually copy the data into the buffer, but instead returns a pointer
71  * to the allocated region.
72  */
73
74 void *
75 buffer_append_space(Buffer *buffer, u_int len)
76 {
77         u_int newlen;
78         void *p;
79
80         if (len > BUFFER_MAX_CHUNK)
81                 fatal("buffer_append_space: len %u not supported", len);
82
83         /* If the buffer is empty, start using it from the beginning. */
84         if (buffer->offset == buffer->end) {
85                 buffer->offset = 0;
86                 buffer->end = 0;
87         }
88 restart:
89         /* If there is enough space to store all data, store it now. */
90         if (buffer->end + len < buffer->alloc) {
91                 p = buffer->buf + buffer->end;
92                 buffer->end += len;
93                 return p;
94         }
95         /*
96          * If the buffer is quite empty, but all data is at the end, move the
97          * data to the beginning and retry.
98          */
99         if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) {
100                 memmove(buffer->buf, buffer->buf + buffer->offset,
101                         buffer->end - buffer->offset);
102                 buffer->end -= buffer->offset;
103                 buffer->offset = 0;
104                 goto restart;
105         }
106         /* Increase the size of the buffer and retry. */
107
108         newlen = buffer->alloc + len + 32768;
109         if (newlen > BUFFER_MAX_LEN)
110                 fatal("buffer_append_space: alloc %u not supported",
111                     newlen);
112         buffer->buf = xrealloc(buffer->buf, 1, newlen);
113         buffer->alloc = newlen;
114         goto restart;
115         /* NOTREACHED */
116 }
117
118 /* Returns the number of bytes of data in the buffer. */
119
120 u_int
121 buffer_len(Buffer *buffer)
122 {
123         return buffer->end - buffer->offset;
124 }
125
126 /* Gets data from the beginning of the buffer. */
127
128 int
129 buffer_get_ret(Buffer *buffer, void *buf, u_int len)
130 {
131         if (len > buffer->end - buffer->offset) {
132                 error("buffer_get_ret: trying to get more bytes %d than in buffer %d",
133                     len, buffer->end - buffer->offset);
134                 return (-1);
135         }
136         memcpy(buf, buffer->buf + buffer->offset, len);
137         buffer->offset += len;
138         return (0);
139 }
140
141 void
142 buffer_get(Buffer *buffer, void *buf, u_int len)
143 {
144         if (buffer_get_ret(buffer, buf, len) == -1)
145                 fatal("buffer_get: buffer error");
146 }
147
148 /* Consumes the given number of bytes from the beginning of the buffer. */
149
150 int
151 buffer_consume_ret(Buffer *buffer, u_int bytes)
152 {
153         if (bytes > buffer->end - buffer->offset) {
154                 error("buffer_consume_ret: trying to get more bytes than in buffer");
155                 return (-1);
156         }
157         buffer->offset += bytes;
158         return (0);
159 }
160
161 void
162 buffer_consume(Buffer *buffer, u_int bytes)
163 {
164         if (buffer_consume_ret(buffer, bytes) == -1)
165                 fatal("buffer_consume: buffer error");
166 }
167
168 /* Consumes the given number of bytes from the end of the buffer. */
169
170 int
171 buffer_consume_end_ret(Buffer *buffer, u_int bytes)
172 {
173         if (bytes > buffer->end - buffer->offset)
174                 return (-1);
175         buffer->end -= bytes;
176         return (0);
177 }
178
179 void
180 buffer_consume_end(Buffer *buffer, u_int bytes)
181 {
182         if (buffer_consume_end_ret(buffer, bytes) == -1)
183                 fatal("buffer_consume_end: trying to get more bytes than in buffer");
184 }
185
186 /* Returns a pointer to the first used byte in the buffer. */
187
188 void *
189 buffer_ptr(Buffer *buffer)
190 {
191         return buffer->buf + buffer->offset;
192 }
193
194 /* Dumps the contents of the buffer to stderr. */
195
196 void
197 buffer_dump(Buffer *buffer)
198 {
199         u_int i;
200         u_char *ucp = buffer->buf;
201
202         for (i = buffer->offset; i < buffer->end; i++) {
203                 fprintf(stderr, "%02x", ucp[i]);
204                 if ((i-buffer->offset)%16==15)
205                         fprintf(stderr, "\r\n");
206                 else if ((i-buffer->offset)%2==1)
207                         fprintf(stderr, " ");
208         }
209         fprintf(stderr, "\r\n");
210 }
This page took 0.688165 seconds and 5 git commands to generate.