Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 187 lines (156 sloc) 4.312 kB
dbb6559 @marschap Imported Upstream version 1.92
authored
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 <sys/types.h>
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #include <config.h>
20 #include "buffer.h"
21 #include "log.h"
22 #include "xmalloc.h"
23
24 /* Initializes the buffer structure. */
25
26 void
27 buffer_init(Buffer *buffer)
28 {
29 const u_int len = 4096;
30
31 buffer->alloc = 0;
32 buffer->buf = xmalloc(len);
33 buffer->alloc = len;
34 buffer->offset = 0;
35 buffer->end = 0;
36 }
37
38 /* Frees any memory used for the buffer. */
39
40 void
41 buffer_free(Buffer *buffer)
42 {
43 if (buffer->alloc > 0) {
44 memset(buffer->buf, 0, buffer->alloc);
45 buffer->alloc = 0;
46 xfree(buffer->buf);
47 }
48 }
49
50 /*
51 * Clears any data from the buffer, making it empty. This does not actually
52 * zero the memory.
53 */
54
55 void
56 buffer_clear(Buffer *buffer)
57 {
58 buffer->offset = 0;
59 buffer->end = 0;
60 }
61
62 /* Appends data to the buffer, expanding it if necessary. */
63
64 void
65 buffer_append(Buffer *buffer, const void *data, u_int len)
66 {
67 void *p;
68 p = buffer_append_space(buffer, len);
69 memcpy(p, data, len);
70 }
71
72 /*
73 * Appends space to the buffer, expanding the buffer if necessary. This does
74 * not actually copy the data into the buffer, but instead returns a pointer
75 * to the allocated region.
76 */
77
78 void *
79 buffer_append_space(Buffer *buffer, u_int len)
80 {
81 u_int newlen;
82 void *p;
83
84 if (len > 0x100000)
85 fatal("buffer_append_space: len %u not supported", len);
86
87 /* If the buffer is empty, start using it from the beginning. */
88 if (buffer->offset == buffer->end) {
89 buffer->offset = 0;
90 buffer->end = 0;
91 }
92 restart:
93 /* If there is enough space to store all data, store it now. */
94 if (buffer->end + len < buffer->alloc) {
95 p = buffer->buf + buffer->end;
96 buffer->end += len;
97 return p;
98 }
99 /*
100 * If the buffer is quite empty, but all data is at the end, move the
101 * data to the beginning and retry.
102 */
103 if (buffer->offset > buffer->alloc / 2) {
104 memmove(buffer->buf, buffer->buf + buffer->offset,
105 buffer->end - buffer->offset);
106 buffer->end -= buffer->offset;
107 buffer->offset = 0;
108 goto restart;
109 }
110 /* Increase the size of the buffer and retry. */
111
112 newlen = buffer->alloc + len + 32768;
113 if (newlen > 0xa00000)
114 fatal("buffer_append_space: alloc %u not supported",
115 newlen);
116 buffer->buf = xrealloc(buffer->buf, newlen);
117 buffer->alloc = newlen;
118 goto restart;
119 /* NOTREACHED */
120 }
121
122 /* Returns the number of bytes of data in the buffer. */
123
124 u_int
125 buffer_len(Buffer *buffer)
126 {
127 return buffer->end - buffer->offset;
128 }
129
130 /* Gets data from the beginning of the buffer. */
131
132 void
133 buffer_get(Buffer *buffer, void *buf, u_int len)
134 {
135 if (len > buffer->end - buffer->offset)
136 fatal("buffer_get: trying to get more bytes %d than in buffer %d",
137 len, buffer->end - buffer->offset);
138 memcpy(buf, buffer->buf + buffer->offset, len);
139 buffer->offset += len;
140 }
141
142 /* Consumes the given number of bytes from the beginning of the buffer. */
143
144 void
145 buffer_consume(Buffer *buffer, u_int bytes)
146 {
147 if (bytes > buffer->end - buffer->offset)
148 fatal("buffer_consume: trying to get more bytes than in buffer");
149 buffer->offset += bytes;
150 }
151
152 /* Consumes the given number of bytes from the end of the buffer. */
153
154 void
155 buffer_consume_end(Buffer *buffer, u_int bytes)
156 {
157 if (bytes > buffer->end - buffer->offset)
158 fatal("buffer_consume_end: trying to get more bytes than in buffer");
159 buffer->end -= bytes;
160 }
161
162 /* Returns a pointer to the first used byte in the buffer. */
163
164 void *
165 buffer_ptr(Buffer *buffer)
166 {
167 return buffer->buf + buffer->offset;
168 }
169
170 /* Dumps the contents of the buffer to stderr. */
171
172 void
173 buffer_dump(Buffer *buffer)
174 {
175 int i;
176 u_char *ucp = buffer->buf;
177
178 for (i = buffer->offset; i < buffer->end; i++) {
179 fprintf(stderr, "%02x", ucp[i]);
180 if ((i-buffer->offset)%16==15)
181 fprintf(stderr, "\r\n");
182 else if ((i-buffer->offset)%2==1)
183 fprintf(stderr, " ");
184 }
185 fprintf(stderr, "\r\n");
186 }
Something went wrong with that request. Please try again.