Import OpenSSH-5.1p1.
[dragonfly.git] / crypto / openssh-3.8.1p1 / buffer.c
blob9217cb2695cf9ad68be4545b4d76ab9fced181d4
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).
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".
14 #include "includes.h"
15 RCSID("$OpenBSD: buffer.c,v 1.21 2003/11/21 11:57:03 djm Exp $");
17 #include "xmalloc.h"
18 #include "buffer.h"
19 #include "log.h"
21 /* Initializes the buffer structure. */
23 void
24 buffer_init(Buffer *buffer)
26 const u_int len = 4096;
28 buffer->alloc = 0;
29 buffer->buf = xmalloc(len);
30 buffer->alloc = len;
31 buffer->offset = 0;
32 buffer->end = 0;
35 /* Frees any memory used for the buffer. */
37 void
38 buffer_free(Buffer *buffer)
40 if (buffer->alloc > 0) {
41 memset(buffer->buf, 0, buffer->alloc);
42 buffer->alloc = 0;
43 xfree(buffer->buf);
48 * Clears any data from the buffer, making it empty. This does not actually
49 * zero the memory.
52 void
53 buffer_clear(Buffer *buffer)
55 buffer->offset = 0;
56 buffer->end = 0;
59 /* Appends data to the buffer, expanding it if necessary. */
61 void
62 buffer_append(Buffer *buffer, const void *data, u_int len)
64 void *p;
65 p = buffer_append_space(buffer, len);
66 memcpy(p, data, len);
70 * Appends space to the buffer, expanding the buffer if necessary. This does
71 * not actually copy the data into the buffer, but instead returns a pointer
72 * to the allocated region.
75 void *
76 buffer_append_space(Buffer *buffer, u_int len)
78 u_int newlen;
79 void *p;
81 if (len > 0x100000)
82 fatal("buffer_append_space: len %u not supported", len);
84 /* If the buffer is empty, start using it from the beginning. */
85 if (buffer->offset == buffer->end) {
86 buffer->offset = 0;
87 buffer->end = 0;
89 restart:
90 /* If there is enough space to store all data, store it now. */
91 if (buffer->end + len < buffer->alloc) {
92 p = buffer->buf + buffer->end;
93 buffer->end += len;
94 return p;
97 * If the buffer is quite empty, but all data is at the end, move the
98 * data to the beginning and retry.
100 if (buffer->offset > buffer->alloc / 2) {
101 memmove(buffer->buf, buffer->buf + buffer->offset,
102 buffer->end - buffer->offset);
103 buffer->end -= buffer->offset;
104 buffer->offset = 0;
105 goto restart;
107 /* Increase the size of the buffer and retry. */
109 newlen = buffer->alloc + len + 32768;
110 if (newlen > 0xa00000)
111 fatal("buffer_append_space: alloc %u not supported",
112 newlen);
113 buffer->buf = xrealloc(buffer->buf, newlen);
114 buffer->alloc = newlen;
115 goto restart;
116 /* NOTREACHED */
119 /* Returns the number of bytes of data in the buffer. */
121 u_int
122 buffer_len(Buffer *buffer)
124 return buffer->end - buffer->offset;
127 /* Gets data from the beginning of the buffer. */
129 void
130 buffer_get(Buffer *buffer, void *buf, u_int len)
132 if (len > buffer->end - buffer->offset)
133 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
134 len, buffer->end - buffer->offset);
135 memcpy(buf, buffer->buf + buffer->offset, len);
136 buffer->offset += len;
139 /* Consumes the given number of bytes from the beginning of the buffer. */
141 void
142 buffer_consume(Buffer *buffer, u_int bytes)
144 if (bytes > buffer->end - buffer->offset)
145 fatal("buffer_consume: trying to get more bytes than in buffer");
146 buffer->offset += bytes;
149 /* Consumes the given number of bytes from the end of the buffer. */
151 void
152 buffer_consume_end(Buffer *buffer, u_int bytes)
154 if (bytes > buffer->end - buffer->offset)
155 fatal("buffer_consume_end: trying to get more bytes than in buffer");
156 buffer->end -= bytes;
159 /* Returns a pointer to the first used byte in the buffer. */
161 void *
162 buffer_ptr(Buffer *buffer)
164 return buffer->buf + buffer->offset;
167 /* Dumps the contents of the buffer to stderr. */
169 void
170 buffer_dump(Buffer *buffer)
172 u_int i;
173 u_char *ucp = buffer->buf;
175 for (i = buffer->offset; i < buffer->end; i++) {
176 fprintf(stderr, "%02x", ucp[i]);
177 if ((i-buffer->offset)%16==15)
178 fprintf(stderr, "\r\n");
179 else if ((i-buffer->offset)%2==1)
180 fprintf(stderr, " ");
182 fprintf(stderr, "\r\n");